Temperature Sensor DHT readings to FRDMk64f
Fork of Hexi_Blinky_Example by
main.cpp@15:67a7cca7ae06, 2017-03-31 (annotated)
- Committer:
- roborags
- Date:
- Fri Mar 31 15:03:37 2017 +0000
- Revision:
- 15:67a7cca7ae06
- Parent:
- 14:02ddfa711646
- Child:
- 16:39e45e59677c
Initial commit : all sensors sending data to cloud .
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roborags | 14:02ddfa711646 | 1 | |
dan | 0:7dec7e9ac085 | 2 | #include "mbed.h" |
roborags | 14:02ddfa711646 | 3 | #include "ESP8266.h" // Include header file from Author: Antonio Quevedo |
roborags | 14:02ddfa711646 | 4 | #include "math.h" |
roborags | 15:67a7cca7ae06 | 5 | #include "DHT.h" |
roborags | 14:02ddfa711646 | 6 | #include <string> |
roborags | 14:02ddfa711646 | 7 | |
roborags | 15:67a7cca7ae06 | 8 | #define APIKEY JJAOBK32WOINKT00 //Put "Write key" of your channel in thingspeak.com |
roborags | 14:02ddfa711646 | 9 | #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" |
roborags | 15:67a7cca7ae06 | 10 | #define WIFI_SSID "Batman2G" |
roborags | 15:67a7cca7ae06 | 11 | #define WIFI_PASS "Alien12345" |
roborags | 15:67a7cca7ae06 | 12 | |
roborags | 15:67a7cca7ae06 | 13 | Serial FRDM_UART_Debug(USBTX,USBRX); |
roborags | 15:67a7cca7ae06 | 14 | |
roborags | 15:67a7cca7ae06 | 15 | ESP8266 ESP_8266_UART(PTC17, PTC16, 115200); // UART for ESP8266 Wifi module |
roborags | 15:67a7cca7ae06 | 16 | // Options are TX-RX - PTB11 - PTB10 , PTC17 - PTC16 , PTC15 - PTC14 |
roborags | 14:02ddfa711646 | 17 | |
roborags | 15:67a7cca7ae06 | 18 | SPI SPI_Bus(PTD2,PTD3,PTD1); // (MOSI MISO CLK)setup SPI interface |
roborags | 15:67a7cca7ae06 | 19 | DigitalOut SPI_CS_AMM(PTC3); |
roborags | 15:67a7cca7ae06 | 20 | DigitalOut SPI_CS_VOLT(PTC2); |
roborags | 14:02ddfa711646 | 21 | |
roborags | 15:67a7cca7ae06 | 22 | I2C I2C_Bus(PTE25,PTE24); |
roborags | 15:67a7cca7ae06 | 23 | |
roborags | 15:67a7cca7ae06 | 24 | AnalogIn AN_Thermo(PTB3); // Thermocouple Analog Input |
roborags | 15:67a7cca7ae06 | 25 | |
roborags | 15:67a7cca7ae06 | 26 | DigitalIn DG_Motion(PTB2); // Motion module Digital Input |
roborags | 14:02ddfa711646 | 27 | |
roborags | 15:67a7cca7ae06 | 28 | DHT DHT_Temp_Hum(PTC11,DHT22); //DHT Sensor |
roborags | 14:02ddfa711646 | 29 | |
roborags | 15:67a7cca7ae06 | 30 | const int Light_I2C_Addr = 0x88; |
roborags | 15:67a7cca7ae06 | 31 | |
roborags | 15:67a7cca7ae06 | 32 | char ESP_8266_CMD_Send[255],ESP_8266_CMD_Recv[1000]; //ESP_8266_CMD_Send = string used to send command to ESP8266 & ESP_8266_CMD_Recv = string used to receive response from ESP8266 |
roborags | 14:02ddfa711646 | 33 | |
roborags | 15:67a7cca7ae06 | 34 | float Amm_Out = 0; |
roborags | 15:67a7cca7ae06 | 35 | float Volt_Out = 0; |
roborags | 15:67a7cca7ae06 | 36 | float Light_Out = 0; |
roborags | 15:67a7cca7ae06 | 37 | float Thermo_Out = 0; |
roborags | 15:67a7cca7ae06 | 38 | float Temp_Out = 0; |
roborags | 15:67a7cca7ae06 | 39 | float Hum_Out = 0; |
roborags | 15:67a7cca7ae06 | 40 | int Motion_Out = 0; |
roborags | 15:67a7cca7ae06 | 41 | int Finger_Out = 0; |
roborags | 15:67a7cca7ae06 | 42 | float Pres_Out = 0; |
dan | 0:7dec7e9ac085 | 43 | |
roborags | 14:02ddfa711646 | 44 | |
roborags | 15:67a7cca7ae06 | 45 | void ESP_8266_Init(void); // Function used to initialize ESP8266 wifi module |
roborags | 15:67a7cca7ae06 | 46 | void ESP_8266_TX_Data(void); // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module |
roborags | 14:02ddfa711646 | 47 | |
roborags | 14:02ddfa711646 | 48 | int main() |
roborags | 14:02ddfa711646 | 49 | { |
roborags | 15:67a7cca7ae06 | 50 | int SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 51 | int SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 52 | float Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 53 | float Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 54 | int Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 55 | int Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 56 | int Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 57 | char I2C_Cmd[3]; |
roborags | 15:67a7cca7ae06 | 58 | int loop_count = 0; |
roborags | 14:02ddfa711646 | 59 | |
roborags | 15:67a7cca7ae06 | 60 | FRDM_UART_Debug.baud(115200); // Baud rate used for communicating with Tera-term on PC |
roborags | 15:67a7cca7ae06 | 61 | |
roborags | 15:67a7cca7ae06 | 62 | ESP_8266_Init(); |
roborags | 14:02ddfa711646 | 63 | |
roborags | 15:67a7cca7ae06 | 64 | SPI_Bus.format(8,0); |
roborags | 15:67a7cca7ae06 | 65 | SPI_Bus.frequency(1000000); |
roborags | 15:67a7cca7ae06 | 66 | |
roborags | 15:67a7cca7ae06 | 67 | I2C_Bus.frequency(100000); // set required i2c frequency |
roborags | 15:67a7cca7ae06 | 68 | |
roborags | 15:67a7cca7ae06 | 69 | FRDM_UART_Debug.printf("Start sampling data\r\n"); // Starting point |
roborags | 15:67a7cca7ae06 | 70 | |
roborags | 14:02ddfa711646 | 71 | while (1) |
roborags | 14:02ddfa711646 | 72 | { |
roborags | 15:67a7cca7ae06 | 73 | Amm_Out = 0; |
roborags | 15:67a7cca7ae06 | 74 | Volt_Out = 0; |
roborags | 15:67a7cca7ae06 | 75 | Light_Out = 0; |
roborags | 15:67a7cca7ae06 | 76 | Thermo_Out = 0; |
roborags | 15:67a7cca7ae06 | 77 | Temp_Out = 0; |
roborags | 15:67a7cca7ae06 | 78 | Hum_Out = 0; |
roborags | 15:67a7cca7ae06 | 79 | Motion_Out = 0; |
roborags | 15:67a7cca7ae06 | 80 | |
roborags | 14:02ddfa711646 | 81 | wait(15); |
roborags | 15:67a7cca7ae06 | 82 | |
roborags | 15:67a7cca7ae06 | 83 | // Copy Motion values |
roborags | 15:67a7cca7ae06 | 84 | |
roborags | 15:67a7cca7ae06 | 85 | Motion_Out = DG_Motion; |
roborags | 15:67a7cca7ae06 | 86 | |
roborags | 15:67a7cca7ae06 | 87 | // Ammeter |
roborags | 15:67a7cca7ae06 | 88 | |
roborags | 15:67a7cca7ae06 | 89 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 90 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 91 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 92 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 93 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 94 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 95 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 96 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 97 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 98 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 99 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 100 | |
roborags | 15:67a7cca7ae06 | 101 | SPI_CS_AMM = 0; |
roborags | 15:67a7cca7ae06 | 102 | |
roborags | 15:67a7cca7ae06 | 103 | SPI_High_byte = SPI_Bus.write(0); |
roborags | 15:67a7cca7ae06 | 104 | SPI_Low_byte = SPI_Bus.write(0); |
roborags | 15:67a7cca7ae06 | 105 | |
roborags | 15:67a7cca7ae06 | 106 | SPI_CS_AMM = 1; |
roborags | 15:67a7cca7ae06 | 107 | |
roborags | 15:67a7cca7ae06 | 108 | Temp_f_1 = (( SPI_High_byte & 0x1f ) << 7 ) | (( SPI_Low_byte >> 1 )); |
roborags | 15:67a7cca7ae06 | 109 | |
roborags | 15:67a7cca7ae06 | 110 | Temp_f_2= (float)(( Temp_f_1 * 1.00 ) / 4096.00 ); // Converting to volts |
roborags | 15:67a7cca7ae06 | 111 | |
roborags | 15:67a7cca7ae06 | 112 | Amm_Out = (float)(( Temp_f_2 - 0.50 ) * 1000.00); |
roborags | 15:67a7cca7ae06 | 113 | |
roborags | 15:67a7cca7ae06 | 114 | FRDM_UART_Debug.printf("Current value = %f mA\r\n", Amm_Out); |
roborags | 14:02ddfa711646 | 115 | wait_ms(100); |
roborags | 14:02ddfa711646 | 116 | |
roborags | 15:67a7cca7ae06 | 117 | // Voltmeter |
roborags | 15:67a7cca7ae06 | 118 | |
roborags | 15:67a7cca7ae06 | 119 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 120 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 121 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 122 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 123 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 124 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 125 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 126 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 127 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 128 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 129 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 130 | |
roborags | 15:67a7cca7ae06 | 131 | SPI_CS_VOLT = 0; |
roborags | 15:67a7cca7ae06 | 132 | |
roborags | 15:67a7cca7ae06 | 133 | SPI_High_byte = SPI_Bus.write(0); |
roborags | 15:67a7cca7ae06 | 134 | SPI_Low_byte = SPI_Bus.write(0); |
roborags | 15:67a7cca7ae06 | 135 | |
roborags | 15:67a7cca7ae06 | 136 | SPI_CS_VOLT = 1; |
roborags | 15:67a7cca7ae06 | 137 | |
roborags | 15:67a7cca7ae06 | 138 | Temp_f_1 = ((SPI_High_byte & 0x1f) << 7) | ((SPI_Low_byte >> 1)); |
roborags | 15:67a7cca7ae06 | 139 | |
roborags | 15:67a7cca7ae06 | 140 | Temp_f_2 = (float)((Temp_f_1 * 33) / 4096); // show value in volts. |
roborags | 15:67a7cca7ae06 | 141 | |
roborags | 15:67a7cca7ae06 | 142 | Volt_Out = (float)(Temp_f_2 - 16.5); |
roborags | 14:02ddfa711646 | 143 | |
roborags | 15:67a7cca7ae06 | 144 | FRDM_UART_Debug.printf("Voltage value = %f V\r\n", Volt_Out); |
roborags | 15:67a7cca7ae06 | 145 | wait_ms(100); |
roborags | 15:67a7cca7ae06 | 146 | |
roborags | 15:67a7cca7ae06 | 147 | //ambient light |
roborags | 15:67a7cca7ae06 | 148 | |
roborags | 15:67a7cca7ae06 | 149 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 150 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 151 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 152 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 153 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 154 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 155 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 156 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 157 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 158 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 159 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 160 | |
roborags | 15:67a7cca7ae06 | 161 | I2C_Cmd[0] = 0x01; //configuration register |
roborags | 15:67a7cca7ae06 | 162 | I2C_Cmd[1]= 0xCC; //configuration data |
roborags | 15:67a7cca7ae06 | 163 | I2C_Cmd[2]= 0x01; //configuration data |
roborags | 15:67a7cca7ae06 | 164 | |
roborags | 15:67a7cca7ae06 | 165 | I2C_Bus.write(Light_I2C_Addr, I2C_Cmd, 3); |
roborags | 15:67a7cca7ae06 | 166 | |
roborags | 15:67a7cca7ae06 | 167 | I2C_Cmd[0] = 0x00; // data register |
roborags | 15:67a7cca7ae06 | 168 | |
roborags | 15:67a7cca7ae06 | 169 | I2C_Bus.write(Light_I2C_Addr, I2C_Cmd, 1); |
roborags | 15:67a7cca7ae06 | 170 | |
roborags | 14:02ddfa711646 | 171 | wait_ms(100); |
roborags | 14:02ddfa711646 | 172 | |
roborags | 15:67a7cca7ae06 | 173 | I2C_Bus.read(Light_I2C_Addr, I2C_Cmd, 2); |
roborags | 14:02ddfa711646 | 174 | |
roborags | 15:67a7cca7ae06 | 175 | Temp_i_1= I2C_Cmd[0]>>4; |
roborags | 15:67a7cca7ae06 | 176 | Temp_i_2= (I2C_Cmd[0]-(Temp_i_1<<4))*256+I2C_Cmd[1]; |
roborags | 15:67a7cca7ae06 | 177 | |
roborags | 15:67a7cca7ae06 | 178 | for(loop_count = 0,Temp_i_3 = 1 ; loop_count < Temp_i_1 ; Temp_i_3*=2,loop_count++); |
roborags | 15:67a7cca7ae06 | 179 | |
roborags | 15:67a7cca7ae06 | 180 | Light_Out= (Temp_i_2 * Temp_i_3) / 100; |
roborags | 15:67a7cca7ae06 | 181 | |
roborags | 15:67a7cca7ae06 | 182 | FRDM_UART_Debug.printf("Lux = %.2f\n\r", Light_Out); |
roborags | 15:67a7cca7ae06 | 183 | wait_ms(100); |
roborags | 15:67a7cca7ae06 | 184 | |
roborags | 15:67a7cca7ae06 | 185 | // Thermocouple |
roborags | 15:67a7cca7ae06 | 186 | |
roborags | 15:67a7cca7ae06 | 187 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 188 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 189 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 190 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 191 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 192 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 193 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 194 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 195 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 196 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 197 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 198 | |
roborags | 15:67a7cca7ae06 | 199 | Temp_f_1 = AN_Thermo.read_u16(); |
roborags | 15:67a7cca7ae06 | 200 | |
roborags | 15:67a7cca7ae06 | 201 | Temp_f_1 = (( Temp_f_1 / 65536 ) * 330); |
roborags | 15:67a7cca7ae06 | 202 | |
roborags | 15:67a7cca7ae06 | 203 | Thermo_Out = Temp_f_1; |
roborags | 15:67a7cca7ae06 | 204 | |
roborags | 15:67a7cca7ae06 | 205 | FRDM_UART_Debug.printf("Thermocouple volt diff = %.2f C\r\n",Thermo_Out); |
roborags | 14:02ddfa711646 | 206 | wait_ms(100); |
roborags | 14:02ddfa711646 | 207 | |
roborags | 15:67a7cca7ae06 | 208 | // Temp and Humidity |
roborags | 15:67a7cca7ae06 | 209 | |
roborags | 15:67a7cca7ae06 | 210 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 211 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 212 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 213 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 214 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 215 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 216 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 217 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 218 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 219 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 220 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 221 | |
roborags | 15:67a7cca7ae06 | 222 | Temp_i_1 = DHT_Temp_Hum.readData(); |
roborags | 15:67a7cca7ae06 | 223 | if (Temp_i_1 == 0) // Read success |
roborags | 15:67a7cca7ae06 | 224 | { |
roborags | 15:67a7cca7ae06 | 225 | Temp_f_1 = DHT_Temp_Hum.ReadTemperature(FARENHEIT); |
roborags | 15:67a7cca7ae06 | 226 | Temp_f_2 = DHT_Temp_Hum.ReadHumidity(); |
roborags | 15:67a7cca7ae06 | 227 | } |
roborags | 15:67a7cca7ae06 | 228 | else // Read failure |
roborags | 15:67a7cca7ae06 | 229 | { |
roborags | 15:67a7cca7ae06 | 230 | Temp_f_1 = 0; |
roborags | 15:67a7cca7ae06 | 231 | Temp_f_2 = 0; |
roborags | 15:67a7cca7ae06 | 232 | } |
roborags | 15:67a7cca7ae06 | 233 | |
roborags | 15:67a7cca7ae06 | 234 | Temp_Out = Temp_f_1; |
roborags | 15:67a7cca7ae06 | 235 | Hum_Out = Temp_f_2; |
roborags | 15:67a7cca7ae06 | 236 | |
roborags | 15:67a7cca7ae06 | 237 | FRDM_UART_Debug.printf("Temperature = %4.2f F , Humidity = %4.2f \r\n",Temp_Out,Hum_Out); |
roborags | 15:67a7cca7ae06 | 238 | wait_ms(100); |
roborags | 15:67a7cca7ae06 | 239 | |
roborags | 15:67a7cca7ae06 | 240 | FRDM_UART_Debug.printf("Sending this information to thingspeak.com\r\n"); |
roborags | 15:67a7cca7ae06 | 241 | ESP_8266_TX_Data(); |
roborags | 14:02ddfa711646 | 242 | |
stevep | 4:81cea7a352b0 | 243 | } |
dan | 0:7dec7e9ac085 | 244 | } |
roborags | 14:02ddfa711646 | 245 | |
roborags | 14:02ddfa711646 | 246 | |
roborags | 15:67a7cca7ae06 | 247 | void ESP_8266_Init(void) |
roborags | 14:02ddfa711646 | 248 | { |
roborags | 15:67a7cca7ae06 | 249 | FRDM_UART_Debug.printf("Initializing and Reset ESP\r\n"); |
roborags | 14:02ddfa711646 | 250 | |
roborags | 15:67a7cca7ae06 | 251 | ESP_8266_UART.Reset(); //RESET ESP |
roborags | 15:67a7cca7ae06 | 252 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400); //receive a response from ESP |
roborags | 15:67a7cca7ae06 | 253 | //FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); //Print the response onscreen |
roborags | 14:02ddfa711646 | 254 | wait(2); |
roborags | 14:02ddfa711646 | 255 | |
roborags | 15:67a7cca7ae06 | 256 | strcpy(ESP_8266_CMD_Send,"AT"); |
roborags | 15:67a7cca7ae06 | 257 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 258 | FRDM_UART_Debug.printf(ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 259 | //wait(2); |
roborags | 15:67a7cca7ae06 | 260 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400); |
roborags | 15:67a7cca7ae06 | 261 | FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 262 | wait(0.1); |
roborags | 14:02ddfa711646 | 263 | |
roborags | 15:67a7cca7ae06 | 264 | strcpy(ESP_8266_CMD_Send,"AT+CWMODE=1"); |
roborags | 15:67a7cca7ae06 | 265 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 266 | FRDM_UART_Debug.printf(ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 267 | wait(2); |
roborags | 15:67a7cca7ae06 | 268 | |
roborags | 15:67a7cca7ae06 | 269 | if(!strcmp(ESP_8266_CMD_Recv,"WIFI CONNECTED")) |
roborags | 15:67a7cca7ae06 | 270 | { |
roborags | 15:67a7cca7ae06 | 271 | strcpy(ESP_8266_CMD_Send,"AT+CWJAP=\""); |
roborags | 15:67a7cca7ae06 | 272 | strcat(ESP_8266_CMD_Send,WIFI_SSID); |
roborags | 15:67a7cca7ae06 | 273 | strcat(ESP_8266_CMD_Send,"\",\""); |
roborags | 15:67a7cca7ae06 | 274 | strcat(ESP_8266_CMD_Send,WIFI_PASS); |
roborags | 15:67a7cca7ae06 | 275 | strcat(ESP_8266_CMD_Send,"\""); |
roborags | 14:02ddfa711646 | 276 | |
roborags | 15:67a7cca7ae06 | 277 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 278 | FRDM_UART_Debug.printf(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 279 | wait(5); |
roborags | 15:67a7cca7ae06 | 280 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400); |
roborags | 15:67a7cca7ae06 | 281 | FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); |
roborags | 15:67a7cca7ae06 | 282 | } |
roborags | 15:67a7cca7ae06 | 283 | else |
roborags | 15:67a7cca7ae06 | 284 | FRDM_UART_Debug.printf("Wifi was preconfigured\r\n"); |
roborags | 15:67a7cca7ae06 | 285 | |
roborags | 15:67a7cca7ae06 | 286 | strcpy(ESP_8266_CMD_Send,"AT+CIPMUX=0"); |
roborags | 15:67a7cca7ae06 | 287 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 288 | FRDM_UART_Debug.printf(ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 289 | //wait(2); |
roborags | 15:67a7cca7ae06 | 290 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400); |
roborags | 15:67a7cca7ae06 | 291 | FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 292 | |
roborags | 14:02ddfa711646 | 293 | } |
roborags | 14:02ddfa711646 | 294 | |
roborags | 14:02ddfa711646 | 295 | |
roborags | 15:67a7cca7ae06 | 296 | void ESP_8266_TX_Data(void) |
roborags | 14:02ddfa711646 | 297 | { |
roborags | 14:02ddfa711646 | 298 | |
roborags | 14:02ddfa711646 | 299 | //ESP updates the Status of Thingspeak channel// |
roborags | 14:02ddfa711646 | 300 | |
roborags | 15:67a7cca7ae06 | 301 | strcpy(ESP_8266_CMD_Send,"AT+CIPSTART="); |
roborags | 15:67a7cca7ae06 | 302 | strcat(ESP_8266_CMD_Send,"\"TCP\",\""); |
roborags | 15:67a7cca7ae06 | 303 | strcat(ESP_8266_CMD_Send,IP); |
roborags | 15:67a7cca7ae06 | 304 | strcat(ESP_8266_CMD_Send,"\",80"); |
roborags | 14:02ddfa711646 | 305 | |
roborags | 15:67a7cca7ae06 | 306 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 307 | FRDM_UART_Debug.printf("S\r\n%s",ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 308 | //wait(2); |
roborags | 15:67a7cca7ae06 | 309 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000); |
roborags | 15:67a7cca7ae06 | 310 | FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 311 | wait(1); |
roborags | 14:02ddfa711646 | 312 | |
roborags | 15:67a7cca7ae06 | 313 | /* |
roborags | 15:67a7cca7ae06 | 314 | float Amm_Out = 0; |
roborags | 15:67a7cca7ae06 | 315 | float Volt_Out = 0; |
roborags | 15:67a7cca7ae06 | 316 | float Light_Out = 0; |
roborags | 15:67a7cca7ae06 | 317 | float Thermo_Out = 0; |
roborags | 15:67a7cca7ae06 | 318 | float Temp_Out = 0; |
roborags | 15:67a7cca7ae06 | 319 | float Hum_Out = 0; |
roborags | 15:67a7cca7ae06 | 320 | int Motion_Out = 0; |
roborags | 15:67a7cca7ae06 | 321 | */ |
roborags | 15:67a7cca7ae06 | 322 | sprintf(ESP_8266_CMD_Send,"GET https://api.thingspeak.com/update?key=JJAOBK32WOINKT00&field1=%d&field2=%d&field3=%f&field4=%f&field5=%f&field6=%f&field7=%f&field8=%f\r\n" |
roborags | 15:67a7cca7ae06 | 323 | ,Motion_Out,Finger_Out,Amm_Out,Volt_Out,Light_Out,Thermo_Out,Temp_Out,Hum_Out); |
roborags | 14:02ddfa711646 | 324 | |
roborags | 14:02ddfa711646 | 325 | int i=0; |
roborags | 15:67a7cca7ae06 | 326 | for(i=0;ESP_8266_CMD_Send[i]!='\0';i++); |
roborags | 14:02ddfa711646 | 327 | i++; |
roborags | 14:02ddfa711646 | 328 | char cmd[255]; |
roborags | 14:02ddfa711646 | 329 | |
roborags | 14:02ddfa711646 | 330 | sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send |
roborags | 15:67a7cca7ae06 | 331 | ESP_8266_UART.SendCMD(cmd); |
roborags | 15:67a7cca7ae06 | 332 | FRDM_UART_Debug.printf("S\r\n%s",cmd); |
roborags | 15:67a7cca7ae06 | 333 | while(i<=20 || ESP_8266_CMD_Recv == ">") |
roborags | 14:02ddfa711646 | 334 | { |
roborags | 15:67a7cca7ae06 | 335 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000); |
roborags | 14:02ddfa711646 | 336 | wait(100); |
roborags | 14:02ddfa711646 | 337 | i++; |
roborags | 14:02ddfa711646 | 338 | } |
roborags | 15:67a7cca7ae06 | 339 | FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 340 | |
roborags | 15:67a7cca7ae06 | 341 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); //Post value to thingspeak channel |
roborags | 15:67a7cca7ae06 | 342 | FRDM_UART_Debug.printf("S\r\n%s",ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 343 | |
roborags | 15:67a7cca7ae06 | 344 | while(i<=20 || ESP_8266_CMD_Recv == "OK") |
roborags | 14:02ddfa711646 | 345 | { |
roborags | 15:67a7cca7ae06 | 346 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000); |
roborags | 14:02ddfa711646 | 347 | wait(100); |
roborags | 14:02ddfa711646 | 348 | i++; |
roborags | 14:02ddfa711646 | 349 | } |
roborags | 15:67a7cca7ae06 | 350 | FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 351 | |
roborags | 14:02ddfa711646 | 352 | } |
roborags | 14:02ddfa711646 | 353 |