Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ESP8266-FRDM-KL25Z-THINGSPEAK by
Revision 4:1722a03793c6, committed 2018-05-22
- Comitter:
- mrbhatter
- Date:
- Tue May 22 05:02:54 2018 +0000
- Parent:
- 2:97dbf542ebee
- Commit message:
- FRDM Thingspeak data and LED Control;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 97dbf542ebee -r 1722a03793c6 main.cpp --- a/main.cpp Sun Dec 03 13:10:28 2017 +0000 +++ b/main.cpp Tue May 22 05:02:54 2018 +0000 @@ -1,125 +1,140 @@ #include "mbed.h" #include "ESP8266.h" - + Serial pc(USBTX,USBRX); - -//POT sensor -AnalogIn pot(PTB0); -AnalogIn lit(PTB1); +DigitalOut rled(LED1); //wifi UART port and baud rate -ESP8266 wifi(PTE0, PTE1, 115200); +ESP8266 wifi(PTE0, PTE1, 115200); // Class Variable Pin dexlaration for WIFI //buffers for wifi library -char resp[1000]; +char snd[255],resp[9000]; char http_cmd[300], comm[300]; - -int timeout = 8000; //timeout for wifi commands +char url_response[9000]; -//SSID and password for connection -#define SSID "Tenda_43DA50" -#define PASS "Karkhana" +int timeout = 5000; //timeout for wifi commands +//SSID and password for connection +#define SSID "Karkhana" +#define PASS "Karkhana2018" +void control_led(void); //Remote IP -#define IP "184.106.153.149" // 52.202.122.57 -//global variable -float potvalue = 0; -float litvalue = 0; +#define IP "184.106.153.149" // IP for thingspeak server. Remains same for al codes using thingspeak +//#define IP "192.168.0.25" +//waterlevelue global variable +int ledstatus=0; -//Update key for thingspeak -char* Update_Key = "P2GMSKA9NY7KV0LW"; - -//Wifi init function -void wifi_initialize(void) -{ - +//Public and private keys for phant +char* Update_Key = "QRCE7DOPVY3I49YN"; // Copy the read key for your channel and paste it here. +//char* Private_Key = "GPoWnRM60yidrB0e1pXD"; + +//Wifi init function. Sets the module to connect to wifi accesspoint with ssid and password mentioned above +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&field2=%f&field1=%f",Update_Key,litvalue,potvalue); - + + //create link + sprintf(http_cmd,"/channels/501352/fields/1.json?api_key=%s&results=2",Update_Key); // Forms the url for transmitting to thingspeak server. the format can be seen on thingspeak site as well 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)) + if (wifi.RcvReply(resp, timeout)){ + pc.printf("--------------------------Request Response -------------------------------------------\r\n"); pc.printf("%s",resp); + pc.printf("--------------------------Request Response -------------------------------------------\r\n"); + control_led(); + } else pc.printf("No response while sending URL \r\n"); + + //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server + //wifi.RcvReply(resp, timeout); + //pc.printf("%s", resp); } -int main () +void control_led(void) { - 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); + int length=strlen(resp); + pc.printf("Length of response: %d\r\n",strlen(resp)); + ledstatus=int(resp[length-13]); + pc.printf("LED Status is %c\r\n",resp[length-13]); + pc.printf("LED status is %d\r\n",ledstatus); + if(resp[length-13]==49){ + pc.printf("LED ON"); + rled=0; + } + else{ + pc.printf("LED OFF"); + rled=1; + } +} + + +int main () { + + + wifi_initialize(); // Calls function defined above to initialize the wifi module + + while (1) { + wifi_send(); // Calls function defined above to send data to thingspeak + //wait(5); } } \ No newline at end of file