Test code to interface AOD UPAS with IoT provider.

Dependencies:   ESP8266_AT NCP5623BMUTBG mbed ADS1115 AOD_Calculation BME280 PMS5003 SunPosition CAM_M8

Revision:
3:f878eab2ebe0
Parent:
2:3df874f33c42
Child:
4:8a667120ee2b
--- a/main.cpp	Wed Nov 22 04:47:21 2017 +0000
+++ b/main.cpp	Sun Nov 26 01:43:18 2017 +0000
@@ -1,6 +1,10 @@
 #include "mbed.h"
 #include "NCP5623BMUTBG.h"
 #include "ESP8266_AT.h"
+#include "SunPosition.h"
+#include "AOD_Calculation.h"
+#include "Adafruit_ADS1015.h"
+#include "BME280.h"
 
 /////////////////////////////////////////////
 //Define core buses and pin states.
@@ -82,6 +86,17 @@
 DigitalOut      qdEnable(PC_3, 0);
 DigitalIn       qdFault(PC_13);
 
+/////////////////////////////////////////////
+//AOD Objects
+/////////////////////////////////////////////
+SunPosition         sun;
+AOD_Calculation     aod_440;
+AOD_Calculation     aod_870;
+AOD_Calculation     aod_680;
+AOD_Calculation     aod_520;
+Adafruit_ADS1115    ads_sun(&i2c, ADS1015_ADDRESS_VDD); //Adress pin connected to 3.3V
+BME280              bme(PB_9, PB_8, 0xEC);  //(D14, D15);
+
 ESP8266_AT      esp(PC_10, PC_11);
 
 Timer t;
@@ -89,7 +104,7 @@
 char ssid[] = "VOLTAR";
 char password[] = "CedhCedh";
 char server[] = "api.thingspeak.com";
-char apiKey[] = "SU35AOKAZ30E0CAX";
+char apiKey[] = "32QVSK5INPPAVIV0";
 char conn_type[] = "TCP";
 
 // For selecting the WiFi UART
@@ -98,6 +113,65 @@
 char aod_sel_5off[1];
 char plant_sel[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;
+double latitude = 40.5853;
+double longitude = -105.0844;
+double altitude = 1525;
+double temperature; 
+double pressure;
+
+//Globals for sun calculation output
+double zenith;
+double azimuth;
+double radius;
+
+//Constants
+const double time_zone = 0; //GPS gets Greenwich time
+const double delta_t = 68;  //This parameter will be roughly constant this year
+const double slope = 30;
+const double azm_rotation = 10;
+
+//AOD CALCULATION
+//AOD Calculation Constants
+const double lambda_440 = 0.440;
+const double lambda_520 = 0.520;
+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 vd = 0.001225;
+const double oz_coeff_440 = 0.0029;
+const double oz_coeff_520 = 0.0481;
+const double oz_coeff_680 = 0.0361;
+const double oz_coeff_870 = 0.0013;
+
+//Voltage from light detector
+double v870;     //Voltage read from 870nm photodiode
+double v_raw870; //Raw analog output from 870nm photodiode
+double v680;     //Voltage read from 680nm photodiode
+double v_raw680; //Raw analog output from 680nm photodiode
+double v520;     //Voltage read from 520nm photodiode
+double v_raw520; //Raw analog output from 520nm photodiode
+double v440;     //Voltage read from 440nm photodiode
+double v_raw440; //Raw analog output from 440nm photodiode
+double AOD_870;
+double AOD_680;
+double AOD_520;
+double AOD_440;
+
+//AOD functions
+void getAODs();
+
 //////////////////////////////////////////////////////////////
 //Main Function
 //////////////////////////////////////////////////////////////
@@ -112,7 +186,7 @@
     plant_sel[0] = 0xCF;
          
     RGB_LED.set_led(1, 1, 1); 
-    i2c.write(addr, aod_sel_5off, 1);
+    i2c.write(addr, aod_sel_5on, 1);
     pc.printf("Plantower off\r\n");
     wait(1);
     
@@ -151,9 +225,14 @@
         if(t.read()>30)//if 30 seconds passed
         {
             t.reset();//reset timer
-            //now send temperature value to thingspeak
-            //use your thingspeak channel's Write api key
-            sprintf(someArray,"GET https://api.thingspeak.com/update?api_key=SU35AOKAZ30E0CAX&field1=%d\r\n\r\n",rand() % 10 + 1);
+            
+            RGB_LED.set_led(1, 1, 1); 
+            
+            //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\r\n\r\n",AOD_440,AOD_520,AOD_680,AOD_870);
             
             esp.delete_tcp_server();
             esp.establish_connection(0, conn_type, server, 80);
@@ -164,3 +243,44 @@
         }
     }
 }
+
+void getAODs()
+{
+    temperature = bme.getTemperature();    
+    pressure = bme.getPressure(temperature);
+    
+    sun.setValues(year, month, day, hour, minute, second, time_zone, delta_t, latitude, longitude, altitude, temperature, pressure, slope, azm_rotation);
+    sun.findSun();
+    zenith = sun.getZenith();
+    radius = sun.getRadius();
+        
+    //Read the light detectors
+    v_raw870 = (double)ads_sun.readADC_SingleEnded(A3_GAIN_ONE); //Channel A3 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV
+    v870 = (v_raw870*0.125)/(1000); //Converts to a voltage
+        
+    v_raw680 = (double)ads_sun.readADC_SingleEnded(A2_GAIN_ONE); //Channel A2 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV
+    v680 = (v_raw680*0.125)/(1000); //Converts to a voltage
+        
+    v_raw520 = (double)ads_sun.readADC_SingleEnded(A1_GAIN_ONE); //Channel A1 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV
+    v520 = (v_raw520*0.125)/(1000); //Converts to a voltage
+    
+    v_raw440 = (double)ads_sun.readADC_SingleEnded(A0_GAIN_ONE); //Channel A1 | 1x gain | +/-4.096V | 1 bit = 2mV | 0.125mV
+    v440 = (v_raw520*0.125)/(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);
+    aod_870.opticalDepth();
+    AOD_870 = aod_870.getAOD();
+        
+    aod_680.setAODInputs(longitude, latitude, altitude, lambda_680, CO2_ppv, pressure, month, day, oz_coeff_680, v0_680, vd, v680, radius, zenith);
+    aod_680.opticalDepth();
+    AOD_680 = aod_680.getAOD();
+        
+    aod_520.setAODInputs(longitude, latitude, altitude, lambda_520, CO2_ppv, pressure, month, day, oz_coeff_520, v0_520, vd, v520, radius, zenith);
+    aod_520.opticalDepth();
+    AOD_520 = aod_520.getAOD();
+    
+    aod_440.setAODInputs(longitude, latitude, altitude, lambda_440, CO2_ppv, pressure, month, day, oz_coeff_440, v0_440, vd, v440, radius, zenith);
+    aod_440.opticalDepth();
+    AOD_440 = aod_440.getAOD();
+}