Test code to interface AOD UPAS with IoT provider.
Dependencies: ESP8266_AT NCP5623BMUTBG mbed ADS1115 AOD_Calculation BME280 PMS5003 SunPosition CAM_M8
main.cpp@7:bd22b64acd45, 2018-04-24 (annotated)
- Committer:
- eawendtjr
- Date:
- Tue Apr 24 20:53:00 2018 +0000
- Revision:
- 7:bd22b64acd45
- Parent:
- 6:aeb0015d6632
Plantower Sensor Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eawendtjr | 0:62df2a5864d5 | 1 | #include "mbed.h" |
eawendtjr | 0:62df2a5864d5 | 2 | #include "NCP5623BMUTBG.h" |
eawendtjr | 0:62df2a5864d5 | 3 | #include "ESP8266_AT.h" |
eawendtjr | 3:f878eab2ebe0 | 4 | #include "SunPosition.h" |
eawendtjr | 3:f878eab2ebe0 | 5 | #include "AOD_Calculation.h" |
eawendtjr | 3:f878eab2ebe0 | 6 | #include "Adafruit_ADS1015.h" |
eawendtjr | 3:f878eab2ebe0 | 7 | #include "BME280.h" |
eawendtjr | 6:aeb0015d6632 | 8 | #include "CAM_M8.h" |
eawendtjr | 6:aeb0015d6632 | 9 | |
eawendtjr | 6:aeb0015d6632 | 10 | ///////////////////////////////////////////// |
eawendtjr | 6:aeb0015d6632 | 11 | //LED colors |
eawendtjr | 6:aeb0015d6632 | 12 | ///////////////////////////////////////////// |
eawendtjr | 6:aeb0015d6632 | 13 | #define M_SET_LED_OFF() RGB_LED.set_led(0,0,0) |
eawendtjr | 6:aeb0015d6632 | 14 | #define M_SET_LED_RED() RGB_LED.set_led(1,0,0) |
eawendtjr | 6:aeb0015d6632 | 15 | #define M_SET_LED_GREEN() RGB_LED.set_led(0,1,0) |
eawendtjr | 6:aeb0015d6632 | 16 | #define M_SET_LED_BLUE() RGB_LED.set_led(0,0,1) |
eawendtjr | 6:aeb0015d6632 | 17 | #define M_SET_LED_MAGENTA() RGB_LED.set_led(1,0,1) |
eawendtjr | 6:aeb0015d6632 | 18 | #define M_SET_LED_YELLOW() RGB_LED.set_led(1,1,0) |
eawendtjr | 6:aeb0015d6632 | 19 | #define M_SET_LED_CYAN() RGB_LED.set_led(0,1,1) |
eawendtjr | 6:aeb0015d6632 | 20 | #define M_SET_LED_WHITE() RGB_LED.set_led(1,1,1) |
eawendtjr | 0:62df2a5864d5 | 21 | |
eawendtjr | 0:62df2a5864d5 | 22 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 23 | //Define core buses and pin states. |
eawendtjr | 0:62df2a5864d5 | 24 | ///////////////////////////////////////////// |
eawendtjr | 2:3df874f33c42 | 25 | I2C i2c(PB_9, PB_8);//(D14, D15); SDA,SCL |
eawendtjr | 2:3df874f33c42 | 26 | Serial pc(USBTX, USBRX); |
eawendtjr | 2:3df874f33c42 | 27 | DigitalOut pumps(PA_9, 0);//(D8, 0); |
eawendtjr | 2:3df874f33c42 | 28 | DigitalOut pbKill(PC_12, 1); // Digital input pin that conncect to the LTC2950 battery charger used to shutdown the UPAS |
eawendtjr | 2:3df874f33c42 | 29 | DigitalIn nINT(PA_15); //Connected but currently unused is a digital ouput pin from LTC2950 battery charger. http://cds.linear.com/docs/en/datasheet/295012fd.pdf |
eawendtjr | 2:3df874f33c42 | 30 | NCP5623BMUTBG RGB_LED(PB_9, PB_8); //(D14, D15); |
eawendtjr | 0:62df2a5864d5 | 31 | |
eawendtjr | 0:62df2a5864d5 | 32 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 33 | //RN4677 BT/BLE Module |
eawendtjr | 0:62df2a5864d5 | 34 | ///////////////////////////////////////////// |
eawendtjr | 2:3df874f33c42 | 35 | Serial ble(PB_10, PB_11); |
eawendtjr | 2:3df874f33c42 | 36 | DigitalOut bleRTS(PB_14, 0); |
eawendtjr | 2:3df874f33c42 | 37 | DigitalOut bleCTS(PB_13, 0); |
eawendtjr | 2:3df874f33c42 | 38 | DigitalOut BT_IRST(PC_8, 0); |
eawendtjr | 2:3df874f33c42 | 39 | DigitalOut BT_SW(PA_12, 0); |
eawendtjr | 0:62df2a5864d5 | 40 | |
eawendtjr | 0:62df2a5864d5 | 41 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 42 | //Analog to Digital Converter |
eawendtjr | 0:62df2a5864d5 | 43 | ///////////////////////////////////////////// |
eawendtjr | 2:3df874f33c42 | 44 | DigitalIn ADS_ALRT(PA_10); //Connected but currently unused. (ADS1115) http://www.ti.com/lit/ds/symlink/ads1115.pdf |
eawendtjr | 0:62df2a5864d5 | 45 | |
eawendtjr | 0:62df2a5864d5 | 46 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 47 | //Battery, Charger, & Supply Monitoring |
eawendtjr | 0:62df2a5864d5 | 48 | ///////////////////////////////////////////// |
eawendtjr | 2:3df874f33c42 | 49 | DigitalIn LTCALT(PB_2); //High for normal operation. Low when a threshold is exceeded for Voltage, gas gauge, or temp |
eawendtjr | 2:3df874f33c42 | 50 | DigitalIn bcs1(PC_9); //Batt charging if High. (BQ24100)[U23] |
eawendtjr | 2:3df874f33c42 | 51 | DigitalIn bcs2(PA_8); //Charge complete if High. (BQ24100)[U23] |
eawendtjr | 2:3df874f33c42 | 52 | DigitalIn bc_npg(PB_1); //Power to the charge controller. (BQ24100)[U23] |
eawendtjr | 2:3df874f33c42 | 53 | DigitalIn SW3flt(PC_4); //When EN = 0 pin is HIGH, When EN = 1, LOW can be current limit, thermal limit, or UVLO. |
eawendtjr | 0:62df2a5864d5 | 54 | |
eawendtjr | 0:62df2a5864d5 | 55 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 56 | //Sensirion SDP3X(s) |
eawendtjr | 0:62df2a5864d5 | 57 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 58 | DigitalIn SDP3Xflt(PC_0); |
eawendtjr | 0:62df2a5864d5 | 59 | DigitalIn SDP3XAltflt(PB_7); |
eawendtjr | 0:62df2a5864d5 | 60 | |
eawendtjr | 0:62df2a5864d5 | 61 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 62 | //Accelerometer and Magnometer |
eawendtjr | 0:62df2a5864d5 | 63 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 64 | DigitalOut iNemoEnable(PA_1, 0); |
eawendtjr | 0:62df2a5864d5 | 65 | DigitalIn iNemoReady(PB_0); |
eawendtjr | 0:62df2a5864d5 | 66 | DigitalIn iNemoInt1(PC_5); |
eawendtjr | 0:62df2a5864d5 | 67 | DigitalIn iNemoInt2(PC_6); |
eawendtjr | 0:62df2a5864d5 | 68 | DigitalIn iNemoInt3(PC_7); |
eawendtjr | 0:62df2a5864d5 | 69 | |
eawendtjr | 0:62df2a5864d5 | 70 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 71 | //UV and Visible Light Sensor |
eawendtjr | 0:62df2a5864d5 | 72 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 73 | |
eawendtjr | 0:62df2a5864d5 | 74 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 75 | //GPS |
eawendtjr | 0:62df2a5864d5 | 76 | ///////////////////////////////////////////// |
eawendtjr | 6:aeb0015d6632 | 77 | DigitalOut gpsEN(PB_15, 0); |
eawendtjr | 6:aeb0015d6632 | 78 | CAM_M8 gps(PB_9, PB_8,(0x84)); |
eawendtjr | 6:aeb0015d6632 | 79 | uint8_t gpsBTState = 1; |
eawendtjr | 6:aeb0015d6632 | 80 | uint8_t meState = 0; |
eawendtjr | 6:aeb0015d6632 | 81 | DigitalIn gpsENFault(PB_12); //When EN = 0 pin is HIGH, When EN = 1, LOW can be current limit, thermal limit, or UVLO. |
eawendtjr | 0:62df2a5864d5 | 82 | |
eawendtjr | 0:62df2a5864d5 | 83 | |
eawendtjr | 0:62df2a5864d5 | 84 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 85 | //SD Card |
eawendtjr | 0:62df2a5864d5 | 86 | ///////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 87 | DigitalIn sdCD(PA_11); |
eawendtjr | 0:62df2a5864d5 | 88 | DigitalOut sdClk(PB_3,0 ); |
eawendtjr | 0:62df2a5864d5 | 89 | DigitalIn sdMISO(PB_4); |
eawendtjr | 0:62df2a5864d5 | 90 | DigitalOut sdMOSI(PB_5, 0); |
eawendtjr | 0:62df2a5864d5 | 91 | DigitalOut sdCS(PB_6, 1); |
eawendtjr | 0:62df2a5864d5 | 92 | |
eawendtjr | 0:62df2a5864d5 | 93 | DigitalIn pbIso(PA_0); |
eawendtjr | 0:62df2a5864d5 | 94 | DigitalOut hFault1(PA_7, 0); |
eawendtjr | 0:62df2a5864d5 | 95 | DigitalOut hFault2(PA_6, 0); |
eawendtjr | 0:62df2a5864d5 | 96 | DigitalOut hFault3(PA_5, 0); |
eawendtjr | 0:62df2a5864d5 | 97 | DigitalOut hFault4(PA_4, 0); |
eawendtjr | 0:62df2a5864d5 | 98 | |
eawendtjr | 0:62df2a5864d5 | 99 | DigitalOut wifiNReset(PC_1, 0); |
eawendtjr | 0:62df2a5864d5 | 100 | DigitalOut wifiEnable(PC_2, 0); |
eawendtjr | 0:62df2a5864d5 | 101 | DigitalOut qdEnable(PC_3, 0); |
eawendtjr | 0:62df2a5864d5 | 102 | DigitalIn qdFault(PC_13); |
eawendtjr | 0:62df2a5864d5 | 103 | |
eawendtjr | 3:f878eab2ebe0 | 104 | ///////////////////////////////////////////// |
eawendtjr | 3:f878eab2ebe0 | 105 | //AOD Objects |
eawendtjr | 3:f878eab2ebe0 | 106 | ///////////////////////////////////////////// |
eawendtjr | 3:f878eab2ebe0 | 107 | SunPosition sun; |
eawendtjr | 3:f878eab2ebe0 | 108 | AOD_Calculation aod_440; |
eawendtjr | 3:f878eab2ebe0 | 109 | AOD_Calculation aod_870; |
eawendtjr | 3:f878eab2ebe0 | 110 | AOD_Calculation aod_680; |
eawendtjr | 3:f878eab2ebe0 | 111 | AOD_Calculation aod_520; |
eawendtjr | 3:f878eab2ebe0 | 112 | Adafruit_ADS1115 ads_sun(&i2c, ADS1015_ADDRESS_VDD); //Adress pin connected to 3.3V |
eawendtjr | 3:f878eab2ebe0 | 113 | BME280 bme(PB_9, PB_8, 0xEC); //(D14, D15); |
eawendtjr | 3:f878eab2ebe0 | 114 | |
eawendtjr | 0:62df2a5864d5 | 115 | ESP8266_AT esp(PC_10, PC_11); |
eawendtjr | 0:62df2a5864d5 | 116 | |
eawendtjr | 1:702f144a88b2 | 117 | Timer t; |
eawendtjr | 1:702f144a88b2 | 118 | |
eawendtjr | 6:aeb0015d6632 | 119 | char ssid[] = "w212lab"; |
eawendtjr | 6:aeb0015d6632 | 120 | char password[] = "testarduino"; |
eawendtjr | 6:aeb0015d6632 | 121 | //char ssid[] = "VOLTAR"; |
eawendtjr | 6:aeb0015d6632 | 122 | //char password[] = "CedhCedh"; |
eawendtjr | 5:513617961138 | 123 | |
eawendtjr | 1:702f144a88b2 | 124 | char server[] = "api.thingspeak.com"; |
eawendtjr | 3:f878eab2ebe0 | 125 | char apiKey[] = "32QVSK5INPPAVIV0"; |
eawendtjr | 1:702f144a88b2 | 126 | char conn_type[] = "TCP"; |
eawendtjr | 1:702f144a88b2 | 127 | |
eawendtjr | 0:62df2a5864d5 | 128 | // For selecting the WiFi UART |
eawendtjr | 0:62df2a5864d5 | 129 | const int addr = 0x3F << 1; |
eawendtjr | 0:62df2a5864d5 | 130 | char aod_sel_5on[1]; |
eawendtjr | 0:62df2a5864d5 | 131 | char aod_sel_5off[1]; |
eawendtjr | 0:62df2a5864d5 | 132 | char plant_sel[1]; |
eawendtjr | 0:62df2a5864d5 | 133 | |
eawendtjr | 6:aeb0015d6632 | 134 | //GPS Variables |
eawendtjr | 6:aeb0015d6632 | 135 | bool gpsReady = 0; |
eawendtjr | 6:aeb0015d6632 | 136 | uint8_t gpsquality = 0; |
eawendtjr | 6:aeb0015d6632 | 137 | uint8_t gpssatellites = 0; |
eawendtjr | 6:aeb0015d6632 | 138 | double gpsspeed = 0.0; |
eawendtjr | 6:aeb0015d6632 | 139 | //double gpscourse = 0.0; |
eawendtjr | 6:aeb0015d6632 | 140 | double gpslatitude = 0.0; |
eawendtjr | 6:aeb0015d6632 | 141 | double gpslongitude = 0.0; |
eawendtjr | 6:aeb0015d6632 | 142 | float gpsaltitude = 0.0; |
eawendtjr | 6:aeb0015d6632 | 143 | bool ledOn = 1; |
eawendtjr | 6:aeb0015d6632 | 144 | |
eawendtjr | 3:f878eab2ebe0 | 145 | //AOD Variables |
eawendtjr | 3:f878eab2ebe0 | 146 | //Globals for sun calulation input |
eawendtjr | 6:aeb0015d6632 | 147 | long gpsTime; |
eawendtjr | 6:aeb0015d6632 | 148 | long gpsDate; |
eawendtjr | 6:aeb0015d6632 | 149 | int year; |
eawendtjr | 6:aeb0015d6632 | 150 | int month; |
eawendtjr | 6:aeb0015d6632 | 151 | int day; |
eawendtjr | 6:aeb0015d6632 | 152 | int hour; |
eawendtjr | 6:aeb0015d6632 | 153 | int minute; |
eawendtjr | 6:aeb0015d6632 | 154 | double second; |
eawendtjr | 6:aeb0015d6632 | 155 | |
eawendtjr | 3:f878eab2ebe0 | 156 | double latitude = 40.5853; |
eawendtjr | 3:f878eab2ebe0 | 157 | double longitude = -105.0844; |
eawendtjr | 3:f878eab2ebe0 | 158 | double altitude = 1525; |
eawendtjr | 3:f878eab2ebe0 | 159 | double temperature; |
eawendtjr | 3:f878eab2ebe0 | 160 | double pressure; |
eawendtjr | 6:aeb0015d6632 | 161 | double humidity; |
eawendtjr | 3:f878eab2ebe0 | 162 | |
eawendtjr | 3:f878eab2ebe0 | 163 | //Globals for sun calculation output |
eawendtjr | 3:f878eab2ebe0 | 164 | double zenith; |
eawendtjr | 3:f878eab2ebe0 | 165 | double azimuth; |
eawendtjr | 3:f878eab2ebe0 | 166 | double radius; |
eawendtjr | 3:f878eab2ebe0 | 167 | |
eawendtjr | 3:f878eab2ebe0 | 168 | //Constants |
eawendtjr | 3:f878eab2ebe0 | 169 | const double time_zone = 0; //GPS gets Greenwich time |
eawendtjr | 3:f878eab2ebe0 | 170 | const double delta_t = 68; //This parameter will be roughly constant this year |
eawendtjr | 3:f878eab2ebe0 | 171 | const double slope = 30; |
eawendtjr | 3:f878eab2ebe0 | 172 | const double azm_rotation = 10; |
eawendtjr | 3:f878eab2ebe0 | 173 | |
eawendtjr | 3:f878eab2ebe0 | 174 | //AOD CALCULATION |
eawendtjr | 3:f878eab2ebe0 | 175 | //AOD Calculation Constants |
eawendtjr | 3:f878eab2ebe0 | 176 | const double lambda_440 = 0.440; |
eawendtjr | 3:f878eab2ebe0 | 177 | const double lambda_520 = 0.520; |
eawendtjr | 3:f878eab2ebe0 | 178 | const double lambda_680 = 0.680; |
eawendtjr | 3:f878eab2ebe0 | 179 | const double lambda_870 = 0.870; |
eawendtjr | 3:f878eab2ebe0 | 180 | const double CO2_ppv = 0.00036; |
eawendtjr | 6:aeb0015d6632 | 181 | const double v0_440 = 0.975524; |
eawendtjr | 6:aeb0015d6632 | 182 | const double v0_520 = 1.412519; |
eawendtjr | 6:aeb0015d6632 | 183 | const double v0_680 = 1.659305; |
eawendtjr | 6:aeb0015d6632 | 184 | const double v0_870 = 1.178819; |
eawendtjr | 3:f878eab2ebe0 | 185 | const double vd = 0.001225; |
eawendtjr | 3:f878eab2ebe0 | 186 | const double oz_coeff_440 = 0.0029; |
eawendtjr | 3:f878eab2ebe0 | 187 | const double oz_coeff_520 = 0.0481; |
eawendtjr | 3:f878eab2ebe0 | 188 | const double oz_coeff_680 = 0.0361; |
eawendtjr | 3:f878eab2ebe0 | 189 | const double oz_coeff_870 = 0.0013; |
eawendtjr | 3:f878eab2ebe0 | 190 | |
eawendtjr | 3:f878eab2ebe0 | 191 | //Voltage from light detector |
eawendtjr | 3:f878eab2ebe0 | 192 | double v870; //Voltage read from 870nm photodiode |
eawendtjr | 3:f878eab2ebe0 | 193 | double v_raw870; //Raw analog output from 870nm photodiode |
eawendtjr | 3:f878eab2ebe0 | 194 | double v680; //Voltage read from 680nm photodiode |
eawendtjr | 3:f878eab2ebe0 | 195 | double v_raw680; //Raw analog output from 680nm photodiode |
eawendtjr | 3:f878eab2ebe0 | 196 | double v520; //Voltage read from 520nm photodiode |
eawendtjr | 3:f878eab2ebe0 | 197 | double v_raw520; //Raw analog output from 520nm photodiode |
eawendtjr | 3:f878eab2ebe0 | 198 | double v440; //Voltage read from 440nm photodiode |
eawendtjr | 3:f878eab2ebe0 | 199 | double v_raw440; //Raw analog output from 440nm photodiode |
eawendtjr | 3:f878eab2ebe0 | 200 | double AOD_870; |
eawendtjr | 3:f878eab2ebe0 | 201 | double AOD_680; |
eawendtjr | 3:f878eab2ebe0 | 202 | double AOD_520; |
eawendtjr | 3:f878eab2ebe0 | 203 | double AOD_440; |
eawendtjr | 3:f878eab2ebe0 | 204 | |
eawendtjr | 3:f878eab2ebe0 | 205 | //AOD functions |
eawendtjr | 3:f878eab2ebe0 | 206 | void getAODs(); |
eawendtjr | 3:f878eab2ebe0 | 207 | |
eawendtjr | 0:62df2a5864d5 | 208 | ////////////////////////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 209 | //Main Function |
eawendtjr | 0:62df2a5864d5 | 210 | ////////////////////////////////////////////////////////////// |
eawendtjr | 0:62df2a5864d5 | 211 | int main() |
eawendtjr | 0:62df2a5864d5 | 212 | { |
eawendtjr | 0:62df2a5864d5 | 213 | pc.baud(115200); |
eawendtjr | 1:702f144a88b2 | 214 | RGB_LED.set_led(1, 0, 1); |
eawendtjr | 6:aeb0015d6632 | 215 | wait(1); |
eawendtjr | 6:aeb0015d6632 | 216 | |
eawendtjr | 6:aeb0015d6632 | 217 | gpsEN = 1; // Enable the GPS |
eawendtjr | 6:aeb0015d6632 | 218 | wait(1); |
eawendtjr | 6:aeb0015d6632 | 219 | |
eawendtjr | 6:aeb0015d6632 | 220 | // Get the GPS time |
eawendtjr | 6:aeb0015d6632 | 221 | gps.read_gps(); |
eawendtjr | 6:aeb0015d6632 | 222 | gpsTime = (long)gps.utc; |
eawendtjr | 6:aeb0015d6632 | 223 | gpsDate = (long)gps.date; |
eawendtjr | 6:aeb0015d6632 | 224 | pc.printf("Date: %d, Time: %d\r\n", gpsTime, gpsDate); |
eawendtjr | 6:aeb0015d6632 | 225 | int gpsfixWait = 0; |
eawendtjr | 6:aeb0015d6632 | 226 | // Wait until gps time is set |
eawendtjr | 6:aeb0015d6632 | 227 | if(gpsTime==0&&gpsDate==0){ |
eawendtjr | 6:aeb0015d6632 | 228 | //gasG.resetMax(); |
eawendtjr | 6:aeb0015d6632 | 229 | gps.resetGPS(); |
eawendtjr | 6:aeb0015d6632 | 230 | wait(1); |
eawendtjr | 6:aeb0015d6632 | 231 | while(gpsquality == 0){ |
eawendtjr | 6:aeb0015d6632 | 232 | gps.read_gps(); |
eawendtjr | 6:aeb0015d6632 | 233 | gpsTime = (long)gps.utc; |
eawendtjr | 6:aeb0015d6632 | 234 | gpsDate = (long)gps.date; |
eawendtjr | 6:aeb0015d6632 | 235 | gpsquality = gps.quality; |
eawendtjr | 6:aeb0015d6632 | 236 | |
eawendtjr | 6:aeb0015d6632 | 237 | if(ledOn == 1) { |
eawendtjr | 6:aeb0015d6632 | 238 | M_SET_LED_OFF(); |
eawendtjr | 6:aeb0015d6632 | 239 | ledOn = 0; |
eawendtjr | 6:aeb0015d6632 | 240 | } |
eawendtjr | 6:aeb0015d6632 | 241 | else{ |
eawendtjr | 6:aeb0015d6632 | 242 | if(gpsquality==0){ |
eawendtjr | 6:aeb0015d6632 | 243 | M_SET_LED_MAGENTA(); |
eawendtjr | 6:aeb0015d6632 | 244 | }else{ |
eawendtjr | 6:aeb0015d6632 | 245 | M_SET_LED_YELLOW(); |
eawendtjr | 6:aeb0015d6632 | 246 | } |
eawendtjr | 6:aeb0015d6632 | 247 | ledOn = 1; |
eawendtjr | 6:aeb0015d6632 | 248 | } |
eawendtjr | 6:aeb0015d6632 | 249 | |
eawendtjr | 6:aeb0015d6632 | 250 | wait(1); |
eawendtjr | 6:aeb0015d6632 | 251 | if(gpsquality==1){ |
eawendtjr | 6:aeb0015d6632 | 252 | gpsfixWait++; |
eawendtjr | 6:aeb0015d6632 | 253 | }else{ |
eawendtjr | 6:aeb0015d6632 | 254 | gpsfixWait = 0; |
eawendtjr | 6:aeb0015d6632 | 255 | } |
eawendtjr | 6:aeb0015d6632 | 256 | } |
eawendtjr | 6:aeb0015d6632 | 257 | } |
eawendtjr | 0:62df2a5864d5 | 258 | |
eawendtjr | 0:62df2a5864d5 | 259 | aod_sel_5on[0] = 0xCB; |
eawendtjr | 0:62df2a5864d5 | 260 | aod_sel_5off[0] = 0xC9; |
eawendtjr | 0:62df2a5864d5 | 261 | plant_sel[0] = 0xCF; |
eawendtjr | 0:62df2a5864d5 | 262 | |
eawendtjr | 1:702f144a88b2 | 263 | RGB_LED.set_led(1, 1, 1); |
eawendtjr | 3:f878eab2ebe0 | 264 | i2c.write(addr, aod_sel_5on, 1); |
eawendtjr | 0:62df2a5864d5 | 265 | pc.printf("Plantower off\r\n"); |
eawendtjr | 1:702f144a88b2 | 266 | wait(1); |
eawendtjr | 0:62df2a5864d5 | 267 | |
eawendtjr | 1:702f144a88b2 | 268 | //Enable the WiFi chip |
eawendtjr | 0:62df2a5864d5 | 269 | RGB_LED.set_led(0, 1, 1); // Light LED so we know something is happening |
eawendtjr | 0:62df2a5864d5 | 270 | wifiEnable = 1; // Enable power to the WiFi while in reset, and wait a short while |
eawendtjr | 0:62df2a5864d5 | 271 | wait_ms(100); |
eawendtjr | 0:62df2a5864d5 | 272 | wifiNReset = 1; // Now de-assert the reset signal |
eawendtjr | 0:62df2a5864d5 | 273 | RGB_LED.set_led(1, 1, 0); // Color change of LED to indicate something is happening. |
eawendtjr | 6:aeb0015d6632 | 274 | wait(3); |
eawendtjr | 1:702f144a88b2 | 275 | |
eawendtjr | 1:702f144a88b2 | 276 | RGB_LED.set_led(0, 1, 0); |
eawendtjr | 1:702f144a88b2 | 277 | esp.check_esp(); |
eawendtjr | 1:702f144a88b2 | 278 | esp.version_info(); |
eawendtjr | 1:702f144a88b2 | 279 | esp.software_reset(); |
eawendtjr | 1:702f144a88b2 | 280 | |
eawendtjr | 1:702f144a88b2 | 281 | esp.command_echo_mode(ESP_ECHO_ON); |
eawendtjr | 1:702f144a88b2 | 282 | esp.set_wifi_mode(ESP_DUAL_CONFIG); |
eawendtjr | 1:702f144a88b2 | 283 | esp.enable_multiple_connections(); |
eawendtjr | 0:62df2a5864d5 | 284 | |
eawendtjr | 1:702f144a88b2 | 285 | esp.create_tcp_server(80); |
eawendtjr | 1:702f144a88b2 | 286 | |
eawendtjr | 1:702f144a88b2 | 287 | //esp.set_server_timeout(5); |
eawendtjr | 1:702f144a88b2 | 288 | |
eawendtjr | 1:702f144a88b2 | 289 | esp.list_access_points(); |
eawendtjr | 1:702f144a88b2 | 290 | esp.wifi_connect(ssid, password); |
eawendtjr | 1:702f144a88b2 | 291 | esp.check_ap(); |
eawendtjr | 1:702f144a88b2 | 292 | esp.get_ip(); |
eawendtjr | 1:702f144a88b2 | 293 | |
eawendtjr | 6:aeb0015d6632 | 294 | char someArray[160]; |
eawendtjr | 1:702f144a88b2 | 295 | |
eawendtjr | 1:702f144a88b2 | 296 | t.start(); |
eawendtjr | 0:62df2a5864d5 | 297 | while(1) |
eawendtjr | 0:62df2a5864d5 | 298 | { |
eawendtjr | 1:702f144a88b2 | 299 | RGB_LED.set_led(0, 0, 1); |
eawendtjr | 6:aeb0015d6632 | 300 | if(t.read()>15)//if 15 seconds passed |
eawendtjr | 1:702f144a88b2 | 301 | { |
eawendtjr | 1:702f144a88b2 | 302 | t.reset();//reset timer |
eawendtjr | 3:f878eab2ebe0 | 303 | |
eawendtjr | 3:f878eab2ebe0 | 304 | RGB_LED.set_led(1, 1, 1); |
eawendtjr | 3:f878eab2ebe0 | 305 | |
eawendtjr | 3:f878eab2ebe0 | 306 | //get the AOD |
eawendtjr | 3:f878eab2ebe0 | 307 | getAODs(); |
eawendtjr | 6:aeb0015d6632 | 308 | |
eawendtjr | 6:aeb0015d6632 | 309 | //send data to thingspeak |
eawendtjr | 6:aeb0015d6632 | 310 | sprintf(someArray,"GET https://api.thingspeak.com/update?api_key=32QVSK5INPPAVIV0&field1=%.2f&field2=%.2f&field3=%.2f&field4=%.2f&field5=%.2f&field6=%.2f&field7=%.2f\r\n\r\n",AOD_440,AOD_520,AOD_680,AOD_870,temperature,pressure,humidity); |
eawendtjr | 1:702f144a88b2 | 311 | |
eawendtjr | 1:702f144a88b2 | 312 | esp.delete_tcp_server(); |
eawendtjr | 1:702f144a88b2 | 313 | esp.establish_connection(0, conn_type, server, 80); |
eawendtjr | 2:3df874f33c42 | 314 | esp.send_data_tcp(0, someArray); |
eawendtjr | 2:3df874f33c42 | 315 | esp.close_connection(0); |
eawendtjr | 1:702f144a88b2 | 316 | esp.create_tcp_server(80); |
eawendtjr | 1:702f144a88b2 | 317 | pc.printf("%s\r\n", someArray); |
eawendtjr | 1:702f144a88b2 | 318 | } |
eawendtjr | 0:62df2a5864d5 | 319 | } |
eawendtjr | 0:62df2a5864d5 | 320 | } |
eawendtjr | 3:f878eab2ebe0 | 321 | |
eawendtjr | 3:f878eab2ebe0 | 322 | void getAODs() |
eawendtjr | 3:f878eab2ebe0 | 323 | { |
eawendtjr | 6:aeb0015d6632 | 324 | gps.read_gps(); |
eawendtjr | 6:aeb0015d6632 | 325 | |
eawendtjr | 6:aeb0015d6632 | 326 | gpsTime = (long)gps.utc; |
eawendtjr | 6:aeb0015d6632 | 327 | gpsDate = (long)gps.date; |
eawendtjr | 6:aeb0015d6632 | 328 | |
eawendtjr | 6:aeb0015d6632 | 329 | minute = (int)(floor((float)(gpsTime - (uint8_t)(floor((float)gpsTime/10000))*10000)/100)); // 0-59 |
eawendtjr | 6:aeb0015d6632 | 330 | second = (double)(floor(((float)gpsTime - 100*(floor((float)gpsTime/100)))));//0; //(uint8_t)(floor(((float)gpsTime - 100*(floor((float)gpsTime/100))))); // 0-59 |
eawendtjr | 6:aeb0015d6632 | 331 | hour = (int)(floor((float)gpsTime/10000)); // 0-23 |
eawendtjr | 6:aeb0015d6632 | 332 | day = (int)(floor((float)gpsDate/10000)); // 1-31 |
eawendtjr | 6:aeb0015d6632 | 333 | month = (uint8_t)(floor((float)(gpsDate - day*10000)/100)); // 0-11 |
eawendtjr | 6:aeb0015d6632 | 334 | year = 1900 + (uint8_t)(100+floor(((float)gpsDate - 100*(floor((float)gpsDate/100))))); // year since 1900 (116 = 2016)//100+16 |
eawendtjr | 6:aeb0015d6632 | 335 | |
eawendtjr | 3:f878eab2ebe0 | 336 | temperature = bme.getTemperature(); |
eawendtjr | 3:f878eab2ebe0 | 337 | pressure = bme.getPressure(temperature); |
eawendtjr | 6:aeb0015d6632 | 338 | humidity = bme.getHumidity(); |
eawendtjr | 3:f878eab2ebe0 | 339 | |
eawendtjr | 3:f878eab2ebe0 | 340 | sun.setValues(year, month, day, hour, minute, second, time_zone, delta_t, latitude, longitude, altitude, temperature, pressure, slope, azm_rotation); |
eawendtjr | 3:f878eab2ebe0 | 341 | sun.findSun(); |
eawendtjr | 3:f878eab2ebe0 | 342 | zenith = sun.getZenith(); |
eawendtjr | 3:f878eab2ebe0 | 343 | radius = sun.getRadius(); |
eawendtjr | 3:f878eab2ebe0 | 344 | |
eawendtjr | 3:f878eab2ebe0 | 345 | //Read the light detectors |
eawendtjr | 5:513617961138 | 346 | v_raw870 = (double)ads_sun.readADC_SingleEnded(A3_GAIN_TWO); //Channel A3 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV |
eawendtjr | 6:aeb0015d6632 | 347 | v870 = (v_raw870*0.0625)/(1000); //Converts to a voltage |
eawendtjr | 3:f878eab2ebe0 | 348 | |
eawendtjr | 5:513617961138 | 349 | v_raw680 = (double)ads_sun.readADC_SingleEnded(A2_GAIN_TWO); //Channel A2 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV |
eawendtjr | 6:aeb0015d6632 | 350 | v680 = (v_raw680*0.0625)/(1000); //Converts to a voltage |
eawendtjr | 3:f878eab2ebe0 | 351 | |
eawendtjr | 5:513617961138 | 352 | v_raw520 = (double)ads_sun.readADC_SingleEnded(A1_GAIN_TWO); //Channel A1 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV |
eawendtjr | 6:aeb0015d6632 | 353 | v520 = (v_raw520*0.0625)/(1000); //Converts to a voltage |
eawendtjr | 3:f878eab2ebe0 | 354 | |
eawendtjr | 5:513617961138 | 355 | v_raw440 = (double)ads_sun.readADC_SingleEnded(A0_GAIN_TWO); //Channel A1 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV |
eawendtjr | 6:aeb0015d6632 | 356 | v440 = (v_raw520*0.0625)/(1000); //Converts to a voltage |
eawendtjr | 3:f878eab2ebe0 | 357 | |
eawendtjr | 3:f878eab2ebe0 | 358 | //Calculate the AOD for all channels |
eawendtjr | 3:f878eab2ebe0 | 359 | aod_870.setAODInputs(longitude, latitude, altitude, lambda_870, CO2_ppv, pressure, month, day, oz_coeff_870, v0_870, vd, v870, radius, zenith); |
eawendtjr | 3:f878eab2ebe0 | 360 | aod_870.opticalDepth(); |
eawendtjr | 3:f878eab2ebe0 | 361 | AOD_870 = aod_870.getAOD(); |
eawendtjr | 3:f878eab2ebe0 | 362 | |
eawendtjr | 3:f878eab2ebe0 | 363 | aod_680.setAODInputs(longitude, latitude, altitude, lambda_680, CO2_ppv, pressure, month, day, oz_coeff_680, v0_680, vd, v680, radius, zenith); |
eawendtjr | 3:f878eab2ebe0 | 364 | aod_680.opticalDepth(); |
eawendtjr | 3:f878eab2ebe0 | 365 | AOD_680 = aod_680.getAOD(); |
eawendtjr | 3:f878eab2ebe0 | 366 | |
eawendtjr | 3:f878eab2ebe0 | 367 | aod_520.setAODInputs(longitude, latitude, altitude, lambda_520, CO2_ppv, pressure, month, day, oz_coeff_520, v0_520, vd, v520, radius, zenith); |
eawendtjr | 3:f878eab2ebe0 | 368 | aod_520.opticalDepth(); |
eawendtjr | 3:f878eab2ebe0 | 369 | AOD_520 = aod_520.getAOD(); |
eawendtjr | 3:f878eab2ebe0 | 370 | |
eawendtjr | 3:f878eab2ebe0 | 371 | aod_440.setAODInputs(longitude, latitude, altitude, lambda_440, CO2_ppv, pressure, month, day, oz_coeff_440, v0_440, vd, v440, radius, zenith); |
eawendtjr | 3:f878eab2ebe0 | 372 | aod_440.opticalDepth(); |
eawendtjr | 3:f878eab2ebe0 | 373 | AOD_440 = aod_440.getAOD(); |
eawendtjr | 3:f878eab2ebe0 | 374 | } |