![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Temperature Sensor DHT readings to FRDMk64f
Fork of Hexi_Blinky_Example by
main.cpp@17:690d692b29cb, 2017-04-23 (annotated)
- Committer:
- roborags
- Date:
- Sun Apr 23 05:09:47 2017 +0000
- Revision:
- 17:690d692b29cb
- Parent:
- 16:39e45e59677c
- Child:
- 18:293c2885af81
Commit with all sensors working PCB, before thread creation, ; 04/23/2017 - 01-09
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 | 17:690d692b29cb | 10 | #define WIFI_SSID "Batman2G" |
roborags | 17:690d692b29cb | 11 | #define WIFI_PASS "Alien12345" |
roborags | 15:67a7cca7ae06 | 12 | |
roborags | 15:67a7cca7ae06 | 13 | Serial FRDM_UART_Debug(USBTX,USBRX); |
roborags | 15:67a7cca7ae06 | 14 | |
roborags | 16:39e45e59677c | 15 | ESP8266 ESP_8266_UART(PTC15, PTC14, 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 | 17:690d692b29cb | 19 | DigitalOut SPI_CS_AMM(PTA2); |
roborags | 17:690d692b29cb | 20 | DigitalOut SPI_CS_VOLT(PTB9); |
roborags | 14:02ddfa711646 | 21 | |
roborags | 15:67a7cca7ae06 | 22 | I2C I2C_Bus(PTE25,PTE24); |
roborags | 15:67a7cca7ae06 | 23 | |
roborags | 16:39e45e59677c | 24 | AnalogIn AN_Thermo(PTB2); // Thermocouple Analog Input |
roborags | 15:67a7cca7ae06 | 25 | |
roborags | 17:690d692b29cb | 26 | DigitalIn DG_Motion(PTC2); // Motion module Digital Input |
roborags | 14:02ddfa711646 | 27 | |
roborags | 17:690d692b29cb | 28 | DHT DHT_Temp_Hum(PTC4,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 | 16:39e45e59677c | 70 | wait(1); |
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 | 15:67a7cca7ae06 | 81 | // Copy Motion values |
roborags | 15:67a7cca7ae06 | 82 | |
roborags | 15:67a7cca7ae06 | 83 | Motion_Out = DG_Motion; |
roborags | 15:67a7cca7ae06 | 84 | |
roborags | 15:67a7cca7ae06 | 85 | // Ammeter |
roborags | 15:67a7cca7ae06 | 86 | |
roborags | 15:67a7cca7ae06 | 87 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 88 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 89 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 90 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 91 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 92 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 93 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 94 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 95 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 96 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 97 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 98 | |
roborags | 15:67a7cca7ae06 | 99 | SPI_CS_AMM = 0; |
roborags | 15:67a7cca7ae06 | 100 | |
roborags | 15:67a7cca7ae06 | 101 | SPI_High_byte = SPI_Bus.write(0); |
roborags | 15:67a7cca7ae06 | 102 | SPI_Low_byte = SPI_Bus.write(0); |
roborags | 15:67a7cca7ae06 | 103 | |
roborags | 15:67a7cca7ae06 | 104 | SPI_CS_AMM = 1; |
roborags | 15:67a7cca7ae06 | 105 | |
roborags | 16:39e45e59677c | 106 | Temp_f_1 = (( SPI_High_byte & 0x1F ) << 7 ) | (( SPI_Low_byte >> 1 )); |
roborags | 15:67a7cca7ae06 | 107 | |
roborags | 15:67a7cca7ae06 | 108 | Temp_f_2= (float)(( Temp_f_1 * 1.00 ) / 4096.00 ); // Converting to volts |
roborags | 15:67a7cca7ae06 | 109 | |
roborags | 15:67a7cca7ae06 | 110 | Amm_Out = (float)(( Temp_f_2 - 0.50 ) * 1000.00); |
roborags | 15:67a7cca7ae06 | 111 | |
roborags | 15:67a7cca7ae06 | 112 | FRDM_UART_Debug.printf("Current value = %f mA\r\n", Amm_Out); |
roborags | 14:02ddfa711646 | 113 | wait_ms(100); |
roborags | 14:02ddfa711646 | 114 | |
roborags | 15:67a7cca7ae06 | 115 | // Voltmeter |
roborags | 15:67a7cca7ae06 | 116 | |
roborags | 15:67a7cca7ae06 | 117 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 118 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 119 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 120 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 121 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 122 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 123 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 124 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 125 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 126 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 127 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 128 | |
roborags | 15:67a7cca7ae06 | 129 | SPI_CS_VOLT = 0; |
roborags | 15:67a7cca7ae06 | 130 | |
roborags | 15:67a7cca7ae06 | 131 | SPI_High_byte = SPI_Bus.write(0); |
roborags | 15:67a7cca7ae06 | 132 | SPI_Low_byte = SPI_Bus.write(0); |
roborags | 15:67a7cca7ae06 | 133 | |
roborags | 15:67a7cca7ae06 | 134 | SPI_CS_VOLT = 1; |
roborags | 15:67a7cca7ae06 | 135 | |
roborags | 15:67a7cca7ae06 | 136 | Temp_f_1 = ((SPI_High_byte & 0x1f) << 7) | ((SPI_Low_byte >> 1)); |
roborags | 15:67a7cca7ae06 | 137 | |
roborags | 15:67a7cca7ae06 | 138 | Temp_f_2 = (float)((Temp_f_1 * 33) / 4096); // show value in volts. |
roborags | 15:67a7cca7ae06 | 139 | |
roborags | 15:67a7cca7ae06 | 140 | Volt_Out = (float)(Temp_f_2 - 16.5); |
roborags | 14:02ddfa711646 | 141 | |
roborags | 15:67a7cca7ae06 | 142 | FRDM_UART_Debug.printf("Voltage value = %f V\r\n", Volt_Out); |
roborags | 15:67a7cca7ae06 | 143 | wait_ms(100); |
roborags | 15:67a7cca7ae06 | 144 | |
roborags | 15:67a7cca7ae06 | 145 | //ambient light |
roborags | 15:67a7cca7ae06 | 146 | |
roborags | 15:67a7cca7ae06 | 147 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 148 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 149 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 150 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 151 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 152 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 153 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 154 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 155 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 156 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 157 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 158 | |
roborags | 15:67a7cca7ae06 | 159 | I2C_Cmd[0] = 0x01; //configuration register |
roborags | 15:67a7cca7ae06 | 160 | I2C_Cmd[1]= 0xCC; //configuration data |
roborags | 15:67a7cca7ae06 | 161 | I2C_Cmd[2]= 0x01; //configuration data |
roborags | 15:67a7cca7ae06 | 162 | |
roborags | 15:67a7cca7ae06 | 163 | I2C_Bus.write(Light_I2C_Addr, I2C_Cmd, 3); |
roborags | 15:67a7cca7ae06 | 164 | |
roborags | 15:67a7cca7ae06 | 165 | I2C_Cmd[0] = 0x00; // data register |
roborags | 15:67a7cca7ae06 | 166 | |
roborags | 15:67a7cca7ae06 | 167 | I2C_Bus.write(Light_I2C_Addr, I2C_Cmd, 1); |
roborags | 15:67a7cca7ae06 | 168 | |
roborags | 14:02ddfa711646 | 169 | wait_ms(100); |
roborags | 14:02ddfa711646 | 170 | |
roborags | 15:67a7cca7ae06 | 171 | I2C_Bus.read(Light_I2C_Addr, I2C_Cmd, 2); |
roborags | 14:02ddfa711646 | 172 | |
roborags | 15:67a7cca7ae06 | 173 | Temp_i_1= I2C_Cmd[0]>>4; |
roborags | 15:67a7cca7ae06 | 174 | Temp_i_2= (I2C_Cmd[0]-(Temp_i_1<<4))*256+I2C_Cmd[1]; |
roborags | 15:67a7cca7ae06 | 175 | |
roborags | 16:39e45e59677c | 176 | for(loop_count = 0,Temp_i_3 = 1 ; loop_count < Temp_i_1 ; Temp_i_3 = Temp_i_3 * 2,loop_count++); |
roborags | 15:67a7cca7ae06 | 177 | |
roborags | 15:67a7cca7ae06 | 178 | Light_Out= (Temp_i_2 * Temp_i_3) / 100; |
roborags | 15:67a7cca7ae06 | 179 | |
roborags | 15:67a7cca7ae06 | 180 | FRDM_UART_Debug.printf("Lux = %.2f\n\r", Light_Out); |
roborags | 15:67a7cca7ae06 | 181 | wait_ms(100); |
roborags | 15:67a7cca7ae06 | 182 | |
roborags | 15:67a7cca7ae06 | 183 | // Thermocouple |
roborags | 15:67a7cca7ae06 | 184 | |
roborags | 15:67a7cca7ae06 | 185 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 186 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 187 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 188 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 189 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 190 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 191 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 192 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 193 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 194 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 195 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 196 | |
roborags | 15:67a7cca7ae06 | 197 | Temp_f_1 = AN_Thermo.read_u16(); |
roborags | 15:67a7cca7ae06 | 198 | |
roborags | 15:67a7cca7ae06 | 199 | Temp_f_1 = (( Temp_f_1 / 65536 ) * 330); |
roborags | 15:67a7cca7ae06 | 200 | |
roborags | 15:67a7cca7ae06 | 201 | Thermo_Out = Temp_f_1; |
roborags | 15:67a7cca7ae06 | 202 | |
roborags | 15:67a7cca7ae06 | 203 | FRDM_UART_Debug.printf("Thermocouple volt diff = %.2f C\r\n",Thermo_Out); |
roborags | 14:02ddfa711646 | 204 | wait_ms(100); |
roborags | 14:02ddfa711646 | 205 | |
roborags | 15:67a7cca7ae06 | 206 | // Temp and Humidity |
roborags | 15:67a7cca7ae06 | 207 | |
roborags | 15:67a7cca7ae06 | 208 | SPI_High_byte = 0; |
roborags | 15:67a7cca7ae06 | 209 | SPI_Low_byte = 0; |
roborags | 15:67a7cca7ae06 | 210 | Temp_f_1 = 0.00; |
roborags | 15:67a7cca7ae06 | 211 | Temp_f_2 = 0.00; |
roborags | 15:67a7cca7ae06 | 212 | Temp_i_1 = 0; |
roborags | 15:67a7cca7ae06 | 213 | Temp_i_2 = 0; |
roborags | 15:67a7cca7ae06 | 214 | Temp_i_3 = 0; |
roborags | 15:67a7cca7ae06 | 215 | I2C_Cmd[0] = 0; |
roborags | 15:67a7cca7ae06 | 216 | I2C_Cmd[1] = 0; |
roborags | 15:67a7cca7ae06 | 217 | I2C_Cmd[2] = 0; |
roborags | 15:67a7cca7ae06 | 218 | loop_count = 0; |
roborags | 15:67a7cca7ae06 | 219 | |
roborags | 15:67a7cca7ae06 | 220 | Temp_i_1 = DHT_Temp_Hum.readData(); |
roborags | 15:67a7cca7ae06 | 221 | if (Temp_i_1 == 0) // Read success |
roborags | 15:67a7cca7ae06 | 222 | { |
roborags | 16:39e45e59677c | 223 | //wait_ms(1000); |
roborags | 15:67a7cca7ae06 | 224 | Temp_f_1 = DHT_Temp_Hum.ReadTemperature(FARENHEIT); |
roborags | 15:67a7cca7ae06 | 225 | Temp_f_2 = DHT_Temp_Hum.ReadHumidity(); |
roborags | 15:67a7cca7ae06 | 226 | } |
roborags | 15:67a7cca7ae06 | 227 | else // Read failure |
roborags | 15:67a7cca7ae06 | 228 | { |
roborags | 15:67a7cca7ae06 | 229 | Temp_f_1 = 0; |
roborags | 15:67a7cca7ae06 | 230 | Temp_f_2 = 0; |
roborags | 15:67a7cca7ae06 | 231 | } |
roborags | 15:67a7cca7ae06 | 232 | |
roborags | 15:67a7cca7ae06 | 233 | Temp_Out = Temp_f_1; |
roborags | 15:67a7cca7ae06 | 234 | Hum_Out = Temp_f_2; |
roborags | 15:67a7cca7ae06 | 235 | |
roborags | 15:67a7cca7ae06 | 236 | FRDM_UART_Debug.printf("Temperature = %4.2f F , Humidity = %4.2f \r\n",Temp_Out,Hum_Out); |
roborags | 15:67a7cca7ae06 | 237 | wait_ms(100); |
roborags | 15:67a7cca7ae06 | 238 | |
roborags | 15:67a7cca7ae06 | 239 | FRDM_UART_Debug.printf("Sending this information to thingspeak.com\r\n"); |
roborags | 15:67a7cca7ae06 | 240 | ESP_8266_TX_Data(); |
roborags | 14:02ddfa711646 | 241 | |
roborags | 16:39e45e59677c | 242 | wait(15); |
roborags | 16:39e45e59677c | 243 | |
stevep | 4:81cea7a352b0 | 244 | } |
dan | 0:7dec7e9ac085 | 245 | } |
roborags | 14:02ddfa711646 | 246 | |
roborags | 14:02ddfa711646 | 247 | |
roborags | 15:67a7cca7ae06 | 248 | void ESP_8266_Init(void) |
roborags | 14:02ddfa711646 | 249 | { |
roborags | 15:67a7cca7ae06 | 250 | FRDM_UART_Debug.printf("Initializing and Reset ESP\r\n"); |
roborags | 14:02ddfa711646 | 251 | |
roborags | 15:67a7cca7ae06 | 252 | ESP_8266_UART.Reset(); //RESET ESP |
roborags | 15:67a7cca7ae06 | 253 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400); //receive a response from ESP |
roborags | 15:67a7cca7ae06 | 254 | //FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); //Print the response onscreen |
roborags | 14:02ddfa711646 | 255 | wait(2); |
roborags | 14:02ddfa711646 | 256 | |
roborags | 15:67a7cca7ae06 | 257 | strcpy(ESP_8266_CMD_Send,"AT"); |
roborags | 15:67a7cca7ae06 | 258 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 259 | FRDM_UART_Debug.printf(ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 260 | //wait(2); |
roborags | 15:67a7cca7ae06 | 261 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400); |
roborags | 15:67a7cca7ae06 | 262 | FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 263 | wait(0.1); |
roborags | 14:02ddfa711646 | 264 | |
roborags | 15:67a7cca7ae06 | 265 | strcpy(ESP_8266_CMD_Send,"AT+CWMODE=1"); |
roborags | 15:67a7cca7ae06 | 266 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 267 | FRDM_UART_Debug.printf(ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 268 | wait(2); |
roborags | 15:67a7cca7ae06 | 269 | |
roborags | 17:690d692b29cb | 270 | |
roborags | 17:690d692b29cb | 271 | //if(!strcmp(ESP_8266_CMD_Recv,"WIFI CONNECTED")) |
roborags | 15:67a7cca7ae06 | 272 | { |
roborags | 15:67a7cca7ae06 | 273 | strcpy(ESP_8266_CMD_Send,"AT+CWJAP=\""); |
roborags | 15:67a7cca7ae06 | 274 | strcat(ESP_8266_CMD_Send,WIFI_SSID); |
roborags | 15:67a7cca7ae06 | 275 | strcat(ESP_8266_CMD_Send,"\",\""); |
roborags | 15:67a7cca7ae06 | 276 | strcat(ESP_8266_CMD_Send,WIFI_PASS); |
roborags | 15:67a7cca7ae06 | 277 | strcat(ESP_8266_CMD_Send,"\""); |
roborags | 14:02ddfa711646 | 278 | |
roborags | 15:67a7cca7ae06 | 279 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 280 | FRDM_UART_Debug.printf(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 281 | wait(5); |
roborags | 15:67a7cca7ae06 | 282 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400); |
roborags | 15:67a7cca7ae06 | 283 | FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); |
roborags | 15:67a7cca7ae06 | 284 | } |
roborags | 17:690d692b29cb | 285 | //else |
roborags | 17:690d692b29cb | 286 | // FRDM_UART_Debug.printf("Wifi was preconfigured\r\n"); |
roborags | 15:67a7cca7ae06 | 287 | |
roborags | 15:67a7cca7ae06 | 288 | strcpy(ESP_8266_CMD_Send,"AT+CIPMUX=0"); |
roborags | 15:67a7cca7ae06 | 289 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 290 | FRDM_UART_Debug.printf(ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 291 | //wait(2); |
roborags | 15:67a7cca7ae06 | 292 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400); |
roborags | 15:67a7cca7ae06 | 293 | FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 294 | |
roborags | 14:02ddfa711646 | 295 | } |
roborags | 14:02ddfa711646 | 296 | |
roborags | 14:02ddfa711646 | 297 | |
roborags | 15:67a7cca7ae06 | 298 | void ESP_8266_TX_Data(void) |
roborags | 14:02ddfa711646 | 299 | { |
roborags | 14:02ddfa711646 | 300 | |
roborags | 14:02ddfa711646 | 301 | //ESP updates the Status of Thingspeak channel// |
roborags | 14:02ddfa711646 | 302 | |
roborags | 15:67a7cca7ae06 | 303 | strcpy(ESP_8266_CMD_Send,"AT+CIPSTART="); |
roborags | 15:67a7cca7ae06 | 304 | strcat(ESP_8266_CMD_Send,"\"TCP\",\""); |
roborags | 15:67a7cca7ae06 | 305 | strcat(ESP_8266_CMD_Send,IP); |
roborags | 15:67a7cca7ae06 | 306 | strcat(ESP_8266_CMD_Send,"\",80"); |
roborags | 14:02ddfa711646 | 307 | |
roborags | 15:67a7cca7ae06 | 308 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); |
roborags | 15:67a7cca7ae06 | 309 | FRDM_UART_Debug.printf("S\r\n%s",ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 310 | //wait(2); |
roborags | 15:67a7cca7ae06 | 311 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000); |
roborags | 15:67a7cca7ae06 | 312 | FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 313 | wait(1); |
roborags | 14:02ddfa711646 | 314 | |
roborags | 15:67a7cca7ae06 | 315 | /* |
roborags | 15:67a7cca7ae06 | 316 | float Amm_Out = 0; |
roborags | 15:67a7cca7ae06 | 317 | float Volt_Out = 0; |
roborags | 15:67a7cca7ae06 | 318 | float Light_Out = 0; |
roborags | 15:67a7cca7ae06 | 319 | float Thermo_Out = 0; |
roborags | 15:67a7cca7ae06 | 320 | float Temp_Out = 0; |
roborags | 15:67a7cca7ae06 | 321 | float Hum_Out = 0; |
roborags | 15:67a7cca7ae06 | 322 | int Motion_Out = 0; |
roborags | 15:67a7cca7ae06 | 323 | */ |
roborags | 15:67a7cca7ae06 | 324 | 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 | 325 | ,Motion_Out,Finger_Out,Amm_Out,Volt_Out,Light_Out,Thermo_Out,Temp_Out,Hum_Out); |
roborags | 14:02ddfa711646 | 326 | |
roborags | 14:02ddfa711646 | 327 | int i=0; |
roborags | 15:67a7cca7ae06 | 328 | for(i=0;ESP_8266_CMD_Send[i]!='\0';i++); |
roborags | 14:02ddfa711646 | 329 | i++; |
roborags | 14:02ddfa711646 | 330 | char cmd[255]; |
roborags | 14:02ddfa711646 | 331 | |
roborags | 14:02ddfa711646 | 332 | sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send |
roborags | 15:67a7cca7ae06 | 333 | ESP_8266_UART.SendCMD(cmd); |
roborags | 15:67a7cca7ae06 | 334 | FRDM_UART_Debug.printf("S\r\n%s",cmd); |
roborags | 15:67a7cca7ae06 | 335 | while(i<=20 || ESP_8266_CMD_Recv == ">") |
roborags | 14:02ddfa711646 | 336 | { |
roborags | 15:67a7cca7ae06 | 337 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000); |
roborags | 14:02ddfa711646 | 338 | wait(100); |
roborags | 14:02ddfa711646 | 339 | i++; |
roborags | 14:02ddfa711646 | 340 | } |
roborags | 15:67a7cca7ae06 | 341 | FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 342 | |
roborags | 15:67a7cca7ae06 | 343 | ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); //Post value to thingspeak channel |
roborags | 15:67a7cca7ae06 | 344 | FRDM_UART_Debug.printf("S\r\n%s",ESP_8266_CMD_Send); |
roborags | 14:02ddfa711646 | 345 | |
roborags | 15:67a7cca7ae06 | 346 | while(i<=20 || ESP_8266_CMD_Recv == "OK") |
roborags | 14:02ddfa711646 | 347 | { |
roborags | 15:67a7cca7ae06 | 348 | ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000); |
roborags | 14:02ddfa711646 | 349 | wait(100); |
roborags | 14:02ddfa711646 | 350 | i++; |
roborags | 14:02ddfa711646 | 351 | } |
roborags | 15:67a7cca7ae06 | 352 | FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv); |
roborags | 14:02ddfa711646 | 353 | |
roborags | 14:02ddfa711646 | 354 | } |
roborags | 14:02ddfa711646 | 355 |