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.
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 W4VWN2SK5LC1VZ9Z //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 "Niran" 00009 #define WIFI_PASS "12345678910" 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 sprintf(snd,"GET https://api.thingspeak.com/update?key=W4VWN2SK5LC1VZ9Z&field1=%f&field2=%f\r\n",i,j); 00110 00111 int i=0; 00112 for(i=0;snd[i]!='\0';i++); 00113 i++; 00114 char cmd[255]; 00115 00116 sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send 00117 esp.SendCMD(cmd); 00118 pc.printf("S\r\n%s",cmd); 00119 while(i<=20 || rcv == ">") 00120 { 00121 esp.RcvReply(rcv, 1000); 00122 wait(100); 00123 i++; 00124 } 00125 pc.printf("R\r\n%s",rcv); 00126 00127 esp.SendCMD(snd); //Post value to thingspeak channel 00128 pc.printf("S\r\n%s",snd); 00129 00130 while(i<=20 || rcv == "OK") 00131 { 00132 esp.RcvReply(rcv, 1000); 00133 wait(100); 00134 i++; 00135 } 00136 pc.printf("R\r\n%s",rcv); 00137 00138 }
Generated on Tue Jul 19 2022 17:12:48 by
1.7.2