Test code to interface AOD UPAS with IoT provider.

Dependencies:   ESP8266_AT NCP5623BMUTBG mbed ADS1115 AOD_Calculation BME280 PMS5003 SunPosition CAM_M8

Committer:
eawendtjr
Date:
Wed Nov 22 01:36:27 2017 +0000
Revision:
1:702f144a88b2
Parent:
0:62df2a5864d5
Child:
2:3df874f33c42
Connecting to thingspeak API using TCP connection.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:62df2a5864d5 4
eawendtjr 0:62df2a5864d5 5 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 6 //Define core buses and pin states.
eawendtjr 0:62df2a5864d5 7 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 8 I2C i2c(PB_9, PB_8);//(D14, D15); SDA,SCL
eawendtjr 0:62df2a5864d5 9 Serial pc(USBTX, USBRX);
eawendtjr 0:62df2a5864d5 10 DigitalOut pumps(PA_9, 0);//(D8, 0);
eawendtjr 0:62df2a5864d5 11 DigitalOut pbKill(PC_12, 1); // Digital input pin that conncect to the LTC2950 battery charger used to shutdown the UPAS
eawendtjr 0:62df2a5864d5 12 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 0:62df2a5864d5 13 NCP5623BMUTBG RGB_LED(PB_9, PB_8); //(D14, D15);
eawendtjr 0:62df2a5864d5 14
eawendtjr 0:62df2a5864d5 15 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 16 //RN4677 BT/BLE Module
eawendtjr 0:62df2a5864d5 17 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 18 Serial ble(PB_10, PB_11);
eawendtjr 0:62df2a5864d5 19 DigitalOut bleRTS(PB_14, 0);
eawendtjr 0:62df2a5864d5 20 DigitalOut bleCTS(PB_13, 0);
eawendtjr 0:62df2a5864d5 21 DigitalOut BT_IRST(PC_8, 0);
eawendtjr 0:62df2a5864d5 22 DigitalOut BT_SW(PA_12, 0);
eawendtjr 0:62df2a5864d5 23
eawendtjr 0:62df2a5864d5 24
eawendtjr 0:62df2a5864d5 25 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 26 //Analog to Digital Converter
eawendtjr 0:62df2a5864d5 27 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 28 DigitalIn ADS_ALRT(PA_10); //Connected but currently unused. (ADS1115) http://www.ti.com/lit/ds/symlink/ads1115.pdf
eawendtjr 0:62df2a5864d5 29
eawendtjr 0:62df2a5864d5 30 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 31 //Battery, Charger, & Supply Monitoring
eawendtjr 0:62df2a5864d5 32 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 33 DigitalIn LTCALT(PB_2); //High for normal operation. Low when a threshold is exceeded for Voltage, gas gauge, or temp
eawendtjr 0:62df2a5864d5 34 DigitalIn bcs1(PC_9); //Batt charging if High. (BQ24100)[U23]
eawendtjr 0:62df2a5864d5 35 DigitalIn bcs2(PA_8); //Charge complete if High. (BQ24100)[U23]
eawendtjr 0:62df2a5864d5 36 DigitalIn bc_npg(PB_1); //Power to the charge controller. (BQ24100)[U23]
eawendtjr 0:62df2a5864d5 37 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 38
eawendtjr 0:62df2a5864d5 39 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 40 //Sensirion SDP3X(s)
eawendtjr 0:62df2a5864d5 41 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 42 DigitalIn SDP3Xflt(PC_0);
eawendtjr 0:62df2a5864d5 43 DigitalIn SDP3XAltflt(PB_7);
eawendtjr 0:62df2a5864d5 44
eawendtjr 0:62df2a5864d5 45 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 46 //Accelerometer and Magnometer
eawendtjr 0:62df2a5864d5 47 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 48 DigitalOut iNemoEnable(PA_1, 0);
eawendtjr 0:62df2a5864d5 49 DigitalIn iNemoReady(PB_0);
eawendtjr 0:62df2a5864d5 50 DigitalIn iNemoInt1(PC_5);
eawendtjr 0:62df2a5864d5 51 DigitalIn iNemoInt2(PC_6);
eawendtjr 0:62df2a5864d5 52 DigitalIn iNemoInt3(PC_7);
eawendtjr 0:62df2a5864d5 53
eawendtjr 0:62df2a5864d5 54 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 55 //UV and Visible Light Sensor
eawendtjr 0:62df2a5864d5 56 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 57
eawendtjr 0:62df2a5864d5 58 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 59 //GPS
eawendtjr 0:62df2a5864d5 60 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 61 DigitalOut gpsEN(PB_15, 0);
eawendtjr 0:62df2a5864d5 62 DigitalIn gpsENFault(PB_12);
eawendtjr 0:62df2a5864d5 63
eawendtjr 0:62df2a5864d5 64
eawendtjr 0:62df2a5864d5 65 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 66 //SD Card
eawendtjr 0:62df2a5864d5 67 /////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 68 DigitalIn sdCD(PA_11);
eawendtjr 0:62df2a5864d5 69 DigitalOut sdClk(PB_3,0 );
eawendtjr 0:62df2a5864d5 70 DigitalIn sdMISO(PB_4);
eawendtjr 0:62df2a5864d5 71 DigitalOut sdMOSI(PB_5, 0);
eawendtjr 0:62df2a5864d5 72 DigitalOut sdCS(PB_6, 1);
eawendtjr 0:62df2a5864d5 73
eawendtjr 0:62df2a5864d5 74 DigitalIn pbIso(PA_0);
eawendtjr 0:62df2a5864d5 75 DigitalOut hFault1(PA_7, 0);
eawendtjr 0:62df2a5864d5 76 DigitalOut hFault2(PA_6, 0);
eawendtjr 0:62df2a5864d5 77 DigitalOut hFault3(PA_5, 0);
eawendtjr 0:62df2a5864d5 78 DigitalOut hFault4(PA_4, 0);
eawendtjr 0:62df2a5864d5 79
eawendtjr 0:62df2a5864d5 80 DigitalOut wifiNReset(PC_1, 0);
eawendtjr 0:62df2a5864d5 81 DigitalOut wifiEnable(PC_2, 0);
eawendtjr 0:62df2a5864d5 82 DigitalOut qdEnable(PC_3, 0);
eawendtjr 0:62df2a5864d5 83 DigitalIn qdFault(PC_13);
eawendtjr 0:62df2a5864d5 84
eawendtjr 0:62df2a5864d5 85 ESP8266_AT esp(PC_10, PC_11);
eawendtjr 0:62df2a5864d5 86
eawendtjr 1:702f144a88b2 87 Timer t;
eawendtjr 1:702f144a88b2 88
eawendtjr 1:702f144a88b2 89 char ssid[] = "VOLTAR";
eawendtjr 1:702f144a88b2 90 char password[] = "CedhCedh";
eawendtjr 1:702f144a88b2 91 char server[] = "api.thingspeak.com";
eawendtjr 1:702f144a88b2 92 char apiKey[] = "SU35AOKAZ30E0CAX";
eawendtjr 1:702f144a88b2 93 char conn_type[] = "TCP";
eawendtjr 1:702f144a88b2 94
eawendtjr 0:62df2a5864d5 95 // For selecting the WiFi UART
eawendtjr 0:62df2a5864d5 96 const int addr = 0x3F << 1;
eawendtjr 0:62df2a5864d5 97 char aod_sel_5on[1];
eawendtjr 0:62df2a5864d5 98 char aod_sel_5off[1];
eawendtjr 0:62df2a5864d5 99 char plant_sel[1];
eawendtjr 0:62df2a5864d5 100
eawendtjr 0:62df2a5864d5 101 //////////////////////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 102 //Main Function
eawendtjr 0:62df2a5864d5 103 //////////////////////////////////////////////////////////////
eawendtjr 0:62df2a5864d5 104 int main()
eawendtjr 0:62df2a5864d5 105 {
eawendtjr 0:62df2a5864d5 106 pc.baud(115200);
eawendtjr 1:702f144a88b2 107 RGB_LED.set_led(1, 0, 1);
eawendtjr 1:702f144a88b2 108 wait(1);
eawendtjr 0:62df2a5864d5 109
eawendtjr 0:62df2a5864d5 110 aod_sel_5on[0] = 0xCB;
eawendtjr 0:62df2a5864d5 111 aod_sel_5off[0] = 0xC9;
eawendtjr 0:62df2a5864d5 112 plant_sel[0] = 0xCF;
eawendtjr 0:62df2a5864d5 113
eawendtjr 1:702f144a88b2 114 RGB_LED.set_led(1, 1, 1);
eawendtjr 0:62df2a5864d5 115 i2c.write(addr, aod_sel_5off, 1);
eawendtjr 0:62df2a5864d5 116 pc.printf("Plantower off\r\n");
eawendtjr 1:702f144a88b2 117 wait(1);
eawendtjr 0:62df2a5864d5 118
eawendtjr 1:702f144a88b2 119 //Enable the WiFi chip
eawendtjr 0:62df2a5864d5 120 RGB_LED.set_led(0, 1, 1); // Light LED so we know something is happening
eawendtjr 0:62df2a5864d5 121 wifiEnable = 1; // Enable power to the WiFi while in reset, and wait a short while
eawendtjr 0:62df2a5864d5 122 wait_ms(100);
eawendtjr 0:62df2a5864d5 123 wifiNReset = 1; // Now de-assert the reset signal
eawendtjr 0:62df2a5864d5 124 RGB_LED.set_led(1, 1, 0); // Color change of LED to indicate something is happening.
eawendtjr 1:702f144a88b2 125 wait(10);
eawendtjr 1:702f144a88b2 126
eawendtjr 1:702f144a88b2 127 RGB_LED.set_led(0, 1, 0);
eawendtjr 1:702f144a88b2 128 esp.check_esp();
eawendtjr 1:702f144a88b2 129 esp.version_info();
eawendtjr 1:702f144a88b2 130 esp.software_reset();
eawendtjr 1:702f144a88b2 131
eawendtjr 1:702f144a88b2 132 esp.command_echo_mode(ESP_ECHO_ON);
eawendtjr 1:702f144a88b2 133 esp.set_wifi_mode(ESP_DUAL_CONFIG);
eawendtjr 1:702f144a88b2 134 esp.enable_multiple_connections();
eawendtjr 0:62df2a5864d5 135
eawendtjr 1:702f144a88b2 136 esp.create_tcp_server(80);
eawendtjr 1:702f144a88b2 137
eawendtjr 1:702f144a88b2 138 //esp.set_server_timeout(5);
eawendtjr 1:702f144a88b2 139
eawendtjr 1:702f144a88b2 140 esp.list_access_points();
eawendtjr 1:702f144a88b2 141 esp.wifi_connect(ssid, password);
eawendtjr 1:702f144a88b2 142 esp.check_ap();
eawendtjr 1:702f144a88b2 143 esp.get_ip();
eawendtjr 1:702f144a88b2 144
eawendtjr 1:702f144a88b2 145 char someArray[150];
eawendtjr 1:702f144a88b2 146
eawendtjr 1:702f144a88b2 147 t.start();
eawendtjr 0:62df2a5864d5 148 while(1)
eawendtjr 0:62df2a5864d5 149 {
eawendtjr 1:702f144a88b2 150 RGB_LED.set_led(0, 0, 1);
eawendtjr 1:702f144a88b2 151 if(t.read()>30)//if 30 seconds passed
eawendtjr 1:702f144a88b2 152 {
eawendtjr 1:702f144a88b2 153 t.reset();//reset timer
eawendtjr 1:702f144a88b2 154 //now send temperature value to thingspeak
eawendtjr 1:702f144a88b2 155 //use your thingspeak channel's Write api key
eawendtjr 1:702f144a88b2 156 sprintf(someArray,"GET https://api.thingspeak.com/update?api_key=SU35AOKAZ30E0CAX&field1=%d\r\n\r\n",rand() % 10 + 1);
eawendtjr 1:702f144a88b2 157
eawendtjr 1:702f144a88b2 158 esp.delete_tcp_server();
eawendtjr 1:702f144a88b2 159 esp.establish_connection(0, conn_type, server, 80);
eawendtjr 1:702f144a88b2 160 esp.create_tcp_server(80);
eawendtjr 1:702f144a88b2 161 esp.close_connection(0);
eawendtjr 1:702f144a88b2 162 pc.printf("%s\r\n", someArray);
eawendtjr 1:702f144a88b2 163 }
eawendtjr 0:62df2a5864d5 164 }
eawendtjr 0:62df2a5864d5 165 }