Akash Verma
/
ESP8266-FRDM-KL25Z-THINGSPEA_Upstream
potentiometer and LDR sensor Data send to the cloud server.
Diff: main.cpp
- Revision:
- 2:97dbf542ebee
- Parent:
- 1:8ab009672555
- Child:
- 3:c3ec6ece92f6
--- a/main.cpp Sat Jun 17 09:53:27 2017 +0000 +++ b/main.cpp Sun Dec 03 13:10:28 2017 +0000 @@ -1,13 +1,14 @@ #include "mbed.h" #include "ESP8266.h" - + Serial pc(USBTX,USBRX); -//POT sensor +//POT sensor AnalogIn pot(PTB0); +AnalogIn lit(PTB1); //wifi UART port and baud rate -ESP8266 wifi(PTE0, PTE1, 115200); +ESP8266 wifi(PTE0, PTE1, 115200); //buffers for wifi library char resp[1000]; @@ -16,101 +17,108 @@ int timeout = 8000; //timeout for wifi commands //SSID and password for connection -#define SSID "Eduvance" -#define PASS "eduvance123" +#define SSID "Tenda_43DA50" +#define PASS "Karkhana" //Remote IP -#define IP "184.106.153.149" - +#define IP "184.106.153.149" // 52.202.122.57 //global variable -float potvalue = 0; +float potvalue = 0; +float litvalue = 0; //Update key for thingspeak -char* Update_Key = "O1VOWGC3WXI981H6"; - +char* Update_Key = "P2GMSKA9NY7KV0LW"; + //Wifi init function -void wifi_initialize(void){ - +void wifi_initialize(void) +{ + pc.printf("******** Resetting wifi module ********\r\n"); wifi.Reset(); - + //wait for 5 seconds for response, else display no response receiveed - if (wifi.RcvReply(resp, 5000)) - pc.printf("%s",resp); + if (wifi.RcvReply(resp, 5000)) + pc.printf("%s",resp); else pc.printf("No response"); - + + pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); wifi.SetMode(1); // set transparent mode if (wifi.RcvReply(resp, timeout)) //receive a response from ESP pc.printf("%s",resp); //Print the response onscreen else pc.printf("No response while setting mode. \r\n"); - + + pc.printf("******** Joining network with SSID and PASS ********\r\n"); - wifi.Join(SSID, PASS); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); + wifi.Join(SSID, PASS); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); else pc.printf("No response while connecting to network \r\n"); - + + pc.printf("******** Getting IP and MAC of module ********\r\n"); - wifi.GetIP(resp); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); + wifi.GetIP(resp); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); else pc.printf("No response while getting IP \r\n"); - + + pc.printf("******** Setting WIFI UART passthrough ********\r\n"); - wifi.setTransparent(); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); + wifi.setTransparent(); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); else pc.printf("No response while setting wifi passthrough. \r\n"); - wait(1); - + wait(1); + + pc.printf("******** Setting single connection mode ********\r\n"); - wifi.SetSingle(); + wifi.SetSingle(); wifi.RcvReply(resp, timeout); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); else pc.printf("No response while setting single connection \r\n"); wait(1); } -void wifi_send(void){ - + +void wifi_send(void) +{ pc.printf("******** Starting TCP connection on IP and port ********\r\n"); wifi.startTCPConn(IP,80); //cipstart wifi.RcvReply(resp, timeout); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); else pc.printf("No response while starting TCP connection \r\n"); wait(1); - - //create link - sprintf(http_cmd,"/update?api_key=%s&field1=%f",Update_Key,potvalue); + + //create link + sprintf(http_cmd,"/update?api_key=%s&field2=%f&field1=%f",Update_Key,litvalue,potvalue); + pc.printf(http_cmd); - + pc.printf("******** Sending URL to wifi ********\r\n"); wifi.sendURL(http_cmd, comm); //cipsend and get command - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); else pc.printf("No response while sending URL \r\n"); } -int main () { - - +int main () +{ wifi_initialize(); - while (1) { potvalue = 255*pot.read(); + litvalue = lit.read(); pc.printf("Current potvalue is = %.3f \r\n", potvalue); - + pc.printf("Current light value is = %.3f \r\n", litvalue); wifi_send(); wait(30); }