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 "hcsr04.h" 00004 00005 #define APIKEY E21WSWLRUWTIDWNX //Put "Write key" of your channel in thingspeak.com 00006 #define IP "184.106.153.149" //IP Address of "api.thingspeak.com\" 00007 #define WIFI_SSID "some-ssid" 00008 #define WIFI_PASS "some-passwd" 00009 00010 Serial pc(USBTX,USBRX); 00011 ESP8266 esp(D1, D0, 115200); //baud rate for wifi 00012 HCSR04 usensor(A4, A5); //Trigger, Echo 00013 00014 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 00015 00016 void esp_initialize(void); //Function used to initialize ESP8266 wifi module 00017 void esp_send(char); //Function used to connect with thingspeak.com and update channel using ESP8266 wifi module 00018 00019 int ping() 00020 { 00021 usensor.start(); 00022 wait_ms(500); 00023 return(usensor.get_dist_cm()); 00024 } 00025 00026 int main() 00027 { 00028 pc.baud(115200); //Baud rate used for communicating with Tera-term on PC 00029 00030 pc.printf("START\r\n"); //Starting point 00031 esp_initialize(); 00032 00033 int distance; 00034 char cd; 00035 00036 while (1) 00037 { 00038 distance = ping(); //ping function 00039 pc.printf("\n distance = %ld cm",distance); 00040 wait_ms(10000); 00041 if(distance<=30) 00042 { 00043 cd = 'd'; 00044 esp_send(cd); 00045 } 00046 else 00047 { 00048 cd = 'a'; 00049 esp_send(cd); 00050 } 00051 } 00052 } 00053 00054 void esp_initialize(void) 00055 { 00056 pc.printf("Initializing ESP\r\n"); 00057 00058 pc.printf("Reset ESP\r\n"); 00059 esp.Reset(); //RESET ESP 00060 esp.RcvReply(rcv, 400); //receive a response from ESP 00061 //pc.printf(rcv); //Print the response onscreen 00062 wait(2); 00063 00064 strcpy(snd,"AT"); 00065 esp.SendCMD(snd); 00066 pc.printf(snd); 00067 //wait(2); 00068 esp.RcvReply(rcv, 400); 00069 pc.printf(rcv); 00070 wait(0.1); 00071 00072 strcpy(snd,"AT+CWMODE=1"); 00073 esp.SendCMD(snd); 00074 pc.printf(snd); 00075 wait(2); 00076 00077 strcpy(snd,"AT+CWJAP=\""); 00078 strcat(snd,WIFI_SSID); 00079 strcat(snd,"\",\""); 00080 strcat(snd,WIFI_PASS); 00081 strcat(snd,"\""); 00082 00083 esp.SendCMD(snd); 00084 pc.printf(snd); 00085 wait(5); 00086 esp.RcvReply(rcv, 400); 00087 pc.printf("\n %s \n", rcv); 00088 00089 strcpy(snd,"AT+CIPMUX=0"); 00090 esp.SendCMD(snd); 00091 pc.printf(snd); 00092 //wait(2); 00093 esp.RcvReply(rcv, 400); 00094 pc.printf("\n %s \n", rcv); 00095 } 00096 00097 void esp_send(char ch) 00098 { 00099 //ESP updates the Status of Thingspeak channel// 00100 strcpy(snd,"AT+CIPSTART="); 00101 strcat(snd,"\"TCP\",\""); 00102 strcat(snd,IP); 00103 strcat(snd,"\",80"); 00104 00105 esp.SendCMD(snd); 00106 pc.printf("S\r\n%s",snd); 00107 //wait(2); 00108 esp.RcvReply(rcv, 1000); 00109 pc.printf("R\r\n%s",rcv); 00110 wait(1); 00111 00112 sprintf(snd,"GET https://api.thingspeak.com/update?api_key=E21WSWLRUWTIDWNX&field1=%c\n",ch); 00113 00114 int i=0; 00115 for(i=0;snd[i]!='\0';i++); 00116 i++; 00117 char cmd[255]; 00118 00119 sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send 00120 esp.SendCMD(cmd); 00121 pc.printf("S\r\n%s",cmd); 00122 while(i<=20 || rcv == ">") 00123 { 00124 esp.RcvReply(rcv, 1000); 00125 wait(100); 00126 i++; 00127 } 00128 pc.printf("R\r\n%s",rcv); 00129 esp.SendCMD(snd); //Post value to thingspeak channel 00130 pc.printf("S\r\n%s",snd); 00131 while(i<=20 || rcv == "OK") 00132 { 00133 esp.RcvReply(rcv, 1000); 00134 wait(100); 00135 i++; 00136 } 00137 pc.printf("R\r\n%s",rcv); 00138 }
Generated on Fri Feb 9 2024 01:05:53 by
