Test code to interface AOD UPAS with IoT provider.

Dependencies:   ESP8266_AT NCP5623BMUTBG mbed ADS1115 AOD_Calculation BME280 PMS5003 SunPosition CAM_M8

Revision:
6:aeb0015d6632
Parent:
5:513617961138
--- a/main.cpp	Wed Dec 06 02:23:22 2017 +0000
+++ b/main.cpp	Wed Dec 06 03:41:54 2017 +0000
@@ -5,6 +5,19 @@
 #include "AOD_Calculation.h"
 #include "Adafruit_ADS1015.h"
 #include "BME280.h"
+#include "CAM_M8.h"
+
+/////////////////////////////////////////////
+//LED colors
+/////////////////////////////////////////////
+#define M_SET_LED_OFF()     RGB_LED.set_led(0,0,0)
+#define M_SET_LED_RED()     RGB_LED.set_led(1,0,0)
+#define M_SET_LED_GREEN()   RGB_LED.set_led(0,1,0)
+#define M_SET_LED_BLUE()    RGB_LED.set_led(0,0,1)
+#define M_SET_LED_MAGENTA() RGB_LED.set_led(1,0,1)
+#define M_SET_LED_YELLOW()  RGB_LED.set_led(1,1,0)
+#define M_SET_LED_CYAN()    RGB_LED.set_led(0,1,1)
+#define M_SET_LED_WHITE()   RGB_LED.set_led(1,1,1)
 
 /////////////////////////////////////////////
 //Define core buses and pin states.
@@ -25,7 +38,6 @@
 DigitalOut      BT_IRST(PC_8, 0);
 DigitalOut      BT_SW(PA_12, 0);
 
-
 /////////////////////////////////////////////
 //Analog to Digital Converter
 /////////////////////////////////////////////
@@ -62,8 +74,11 @@
 /////////////////////////////////////////////
 //GPS
 /////////////////////////////////////////////
-DigitalOut      gpsEN(PB_15, 0);
-DigitalIn       gpsENFault(PB_12);
+DigitalOut          gpsEN(PB_15, 0);
+CAM_M8              gps(PB_9, PB_8,(0x84));   
+uint8_t gpsBTState = 1;
+uint8_t meState = 0;
+DigitalIn           gpsENFault(PB_12); //When EN = 0 pin is HIGH, When EN = 1, LOW can be current limit, thermal limit, or UVLO.
 
 
 /////////////////////////////////////////////
@@ -101,10 +116,10 @@
 
 Timer t;
 
-//char ssid[] = "w212lab";
-//char password[] = "testarduino";
-char ssid[] = "VOLTAR";
-char password[] = "CedhCedh";
+char ssid[] = "w212lab";
+char password[] = "testarduino";
+//char ssid[] = "VOLTAR";
+//char password[] = "CedhCedh";
 
 char server[] = "api.thingspeak.com";
 char apiKey[] = "32QVSK5INPPAVIV0";
@@ -116,19 +131,34 @@
 char aod_sel_5off[1];
 char plant_sel[1];
 
+//GPS Variables
+bool    gpsReady = 0;
+uint8_t gpsquality = 0;
+uint8_t gpssatellites = 0;
+double  gpsspeed = 0.0;
+//double  gpscourse = 0.0;
+double  gpslatitude = 0.0;
+double  gpslongitude = 0.0;
+float   gpsaltitude = 0.0;
+bool ledOn = 1;
+
 //AOD Variables
 //Globals for sun calulation input
-int year = 2017;
-int month = 11;
-int day = 21;
-int hour = 12;
-int minute = 0;
-double second = 0;
+long gpsTime;
+long gpsDate;
+int year;
+int month;
+int day;
+int hour;
+int minute;
+double second;
+
 double latitude = 40.5853;
 double longitude = -105.0844;
 double altitude = 1525;
 double temperature; 
 double pressure;
+double humidity;
 
 //Globals for sun calculation output
 double zenith;
@@ -148,10 +178,10 @@
 const double lambda_680 = 0.680;
 const double lambda_870 = 0.870;
 const double CO2_ppv = 0.00036;
-const double v0_440 = 1.25;
-const double v0_520 = 1.45;
-const double v0_680 = 1.40;
-const double v0_870 = 1.35;
+const double v0_440 = 0.975524;
+const double v0_520 = 1.412519;
+const double v0_680 = 1.659305;
+const double v0_870 = 1.178819;
 const double vd = 0.001225;
 const double oz_coeff_440 = 0.0029;
 const double oz_coeff_520 = 0.0481;
@@ -182,7 +212,49 @@
 {
     pc.baud(115200);
     RGB_LED.set_led(1, 0, 1);
-    wait(1);                             
+    wait(1); 
+    
+    gpsEN = 1; // Enable the GPS
+    wait(1);
+    
+    // Get the GPS time
+    gps.read_gps();
+    gpsTime = (long)gps.utc;
+    gpsDate = (long)gps.date;
+    pc.printf("Date: %d, Time: %d\r\n", gpsTime, gpsDate);
+    int gpsfixWait = 0;
+    // Wait until gps time is set
+    if(gpsTime==0&&gpsDate==0){
+        //gasG.resetMax();
+        gps.resetGPS();
+        wait(1);
+        while(gpsquality == 0){
+            gps.read_gps();
+            gpsTime = (long)gps.utc;
+            gpsDate = (long)gps.date;
+            gpsquality =  gps.quality;
+            
+            if(ledOn == 1) {
+                M_SET_LED_OFF();
+                ledOn = 0;
+            }
+            else{
+                if(gpsquality==0){
+                    M_SET_LED_MAGENTA();
+                }else{
+                    M_SET_LED_YELLOW();
+                }
+                ledOn = 1;
+            }
+            
+            wait(1);
+            if(gpsquality==1){
+                gpsfixWait++;
+            }else{
+                gpsfixWait = 0;
+            }
+        }
+    }                            
     
     aod_sel_5on[0] = 0xCB;
     aod_sel_5off[0] = 0xC9;
@@ -199,7 +271,7 @@
     wait_ms(100);
     wifiNReset = 1;                             // Now de-assert the reset signal
     RGB_LED.set_led(1, 1, 0);                   // Color change of LED to indicate something is happening.
-    wait(10);
+    wait(3);
     
     RGB_LED.set_led(0, 1, 0); 
     esp.check_esp();
@@ -219,13 +291,13 @@
     esp.check_ap();
     esp.get_ip();
     
-    char someArray[150];
+    char someArray[160];
     
     t.start();
     while(1)
     {
         RGB_LED.set_led(0, 0, 1); 
-        if(t.read()>30)//if 30 seconds passed
+        if(t.read()>15)//if 15 seconds passed
         {
             t.reset();//reset timer
             
@@ -233,9 +305,9 @@
             
             //get the AOD
             getAODs();
-            
-            //send data to thingspeaks
-            sprintf(someArray,"GET https://api.thingspeak.com/update?api_key=32QVSK5INPPAVIV0&field1=%.2f&field2=%.2f&field3=%.2f&field4=%.2f&field5=%.2f&field6=%.2f\r\n\r\n",AOD_440,AOD_520,AOD_680,AOD_870,temperature,pressure);
+
+            //send data to thingspeak
+            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);
             
             esp.delete_tcp_server();
             esp.establish_connection(0, conn_type, server, 80);
@@ -249,8 +321,21 @@
 
 void getAODs()
 {
+    gps.read_gps();
+   
+    gpsTime = (long)gps.utc;
+    gpsDate = (long)gps.date;
+                        
+    minute = (int)(floor((float)(gpsTime - (uint8_t)(floor((float)gpsTime/10000))*10000)/100));    // 0-59
+    second = (double)(floor(((float)gpsTime - 100*(floor((float)gpsTime/100)))));//0; //(uint8_t)(floor(((float)gpsTime - 100*(floor((float)gpsTime/100)))));    // 0-59
+    hour =  (int)(floor((float)gpsTime/10000));  // 0-23
+    day = (int)(floor((float)gpsDate/10000));   // 1-31
+    month = (uint8_t)(floor((float)(gpsDate - day*10000)/100));     // 0-11
+    year = 1900 + (uint8_t)(100+floor(((float)gpsDate - 100*(floor((float)gpsDate/100))))); // year since 1900 (116 = 2016)//100+16
+   
     temperature = bme.getTemperature();    
     pressure = bme.getPressure(temperature);
+    humidity = bme.getHumidity();
     
     sun.setValues(year, month, day, hour, minute, second, time_zone, delta_t, latitude, longitude, altitude, temperature, pressure, slope, azm_rotation);
     sun.findSun();
@@ -259,16 +344,16 @@
         
     //Read the light detectors
     v_raw870 = (double)ads_sun.readADC_SingleEnded(A3_GAIN_TWO); //Channel A3 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV
-    v870 = (v_raw870*0.125)/(1000); //Converts to a voltage
+    v870 = (v_raw870*0.0625)/(1000); //Converts to a voltage
         
     v_raw680 = (double)ads_sun.readADC_SingleEnded(A2_GAIN_TWO); //Channel A2 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV
-    v680 = (v_raw680*0.125)/(1000); //Converts to a voltage
+    v680 = (v_raw680*0.0625)/(1000); //Converts to a voltage
         
     v_raw520 = (double)ads_sun.readADC_SingleEnded(A1_GAIN_TWO); //Channel A1 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV
-    v520 = (v_raw520*0.125)/(1000); //Converts to a voltage
+    v520 = (v_raw520*0.0625)/(1000); //Converts to a voltage
     
     v_raw440 = (double)ads_sun.readADC_SingleEnded(A0_GAIN_TWO); //Channel A1 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV
-    v440 = (v_raw520*0.125)/(1000); //Converts to a voltage
+    v440 = (v_raw520*0.0625)/(1000); //Converts to a voltage
     
     //Calculate the AOD for all channels
     aod_870.setAODInputs(longitude, latitude, altitude, lambda_870, CO2_ppv, pressure, month, day, oz_coeff_870, v0_870, vd, v870, radius, zenith);