ESP8266 WiFi module test using thingspeak Sever.

Dependencies:   ESP8266 mbed

Fork of ESP8266_LocalPhant_KL25Z by Eduvance SIT2017

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ESP8266.h"               // Include header file from Author: Antonio Quevedo
00003 #include "math.h"
00004 #include <string>
00005 
00006 #define APIKEY E3AASCJVE2KQDD3V    //Put "Write key" of your channel in thingspeak.com 
00007 #define IP "184.106.153.149"       // IP Address of "api.thingspeak.com\"
00008 #define WIFI_SSID "Redmi"
00009 #define WIFI_PASS "akash12345"
00010 
00011 Serial pc(USBTX,USBRX);
00012 
00013 ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi
00014 
00015 char snd[255],rcv[1000],snd_Data[255];           //snd= string used to send command to ESP 8266 wii and  rcv = string used to receive response from ESP8266 wifi module 
00016 
00017 void esp_initialize(void); // Function used to initialize ESP8266 wifi module 
00018 void esp_send(void);       // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module 
00019 
00020 //test variable 
00021 float i=0;
00022 float j=10;
00023 
00024 int main() 
00025 {
00026     
00027     pc.baud(115200);           // Baud rate used for communicating with Tera-term on PC
00028     
00029     pc.printf("START\r\n");  // Starting point
00030     
00031     esp_initialize();
00032     
00033   
00034 
00035     while (1) 
00036     {   wait(15);
00037         pc.printf("test value is %f%f\n",i,j);
00038         esp_send(); 
00039         i++;
00040         j--;
00041         
00042     }
00043 }
00044 
00045 
00046 void esp_initialize(void)
00047 {    
00048     pc.printf("Initializing ESP\r\n"); 
00049       
00050     pc.printf("Reset ESP\r\n"); 
00051     esp.Reset();                   //RESET ESP
00052     esp.RcvReply(rcv, 400);        //receive a response from ESP
00053     //pc.printf(rcv);          //Print the response onscreen 
00054     wait(2);
00055     
00056     strcpy(snd,"AT");
00057     esp.SendCMD(snd);
00058     pc.printf(snd);
00059     //wait(2);
00060     esp.RcvReply(rcv, 400);       
00061     pc.printf(rcv);      
00062     wait(0.1);
00063     
00064     strcpy(snd,"AT+CWMODE=1");
00065     esp.SendCMD(snd);
00066     pc.printf(snd);
00067     wait(2);
00068 
00069     strcpy(snd,"AT+CWJAP=\"");
00070     strcat(snd,WIFI_SSID);
00071     strcat(snd,"\",\"");
00072     strcat(snd,WIFI_PASS);
00073     strcat(snd,"\"");
00074     
00075     esp.SendCMD(snd);
00076     pc.printf(snd);
00077     wait(5);
00078     esp.RcvReply(rcv, 400);       
00079     pc.printf("\n %s \n", rcv); 
00080     
00081     strcpy(snd,"AT+CIPMUX=0");
00082     esp.SendCMD(snd);
00083     pc.printf(snd);
00084     //wait(2);
00085     esp.RcvReply(rcv, 400);       
00086     pc.printf("\n %s \n", rcv); 
00087 
00088 }
00089 
00090 
00091 void esp_send(void)
00092 {
00093    
00094     //ESP updates the Status of Thingspeak channel//
00095     
00096     strcpy(snd,"AT+CIPSTART=");
00097     strcat(snd,"\"TCP\",\"");
00098     strcat(snd,IP);
00099     strcat(snd,"\",80");
00100     
00101     esp.SendCMD(snd); 
00102     pc.printf("S\r\n%s",snd);
00103     //wait(2);                                                    
00104     esp.RcvReply(rcv, 1000);
00105     pc.printf("R\r\n%s",rcv);
00106     wait(1);
00107     
00108     sprintf(snd,"GET https://api.thingspeak.com/update?key=E3AASCJVE2KQDD3V&field1=%f&field2=%f\r\n",i,j);
00109     
00110     int i=0;
00111     for(i=0;snd[i]!='\0';i++);
00112     i++;
00113     char cmd[255];
00114     
00115     sprintf(cmd,"AT+CIPSEND=%d",i);                                       //Send Number of open connection and Characters to send 
00116     esp.SendCMD(cmd);
00117     pc.printf("S\r\n%s",cmd);
00118     while(i<=20 || rcv == ">")
00119     {
00120         esp.RcvReply(rcv, 1000);
00121         wait(100);
00122         i++;
00123     }
00124     pc.printf("R\r\n%s",rcv);
00125     
00126     esp.SendCMD(snd);                                                      //Post value to thingspeak channel
00127     pc.printf("S\r\n%s",snd);
00128     
00129     while(i<=20 || rcv == "OK")
00130     {
00131         esp.RcvReply(rcv, 1000);
00132         wait(100);
00133         i++;
00134     }
00135     pc.printf("R\r\n%s",rcv);
00136     
00137 }