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.
Dependencies: mbed HBridge MQ7 Ton SimpleScheduler Pir_sensor
Thingspeak.cpp
00001 #include "mbed.h" 00002 #include "Thingspeak.h" 00003 #include "ESP8266.h" 00004 #include <string> 00005 00006 /*============================================================================*/ 00007 /* (a) MACRO and Constants declaration section */ 00008 /*============================================================================*/ 00009 #define APIKEY NS64DJVP5YJKXUW2 //Put "Write key" of your channel in thingspeak.com 00010 #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" 00011 #define WIFI_SSID "Z" 00012 #define WIFI_PASS "12345678" 00013 00014 /*============================================================================*/ 00015 /* (b) Variable declaration section */ 00016 /*============================================================================*/ 00017 Serial pc(USBTX,USBRX); 00018 ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi 00019 00020 /** snd= string used to send command to ESP 8266 wifi 00021 * and rcv = string used to receive response from ESP8266 wifi module 00022 **/ 00023 char snd[255],rcv[1000],snd_Data[255],ip[100],connection[100]; 00024 00025 /*============================================================================*/ 00026 /* (c) Function definition section */ 00027 /*============================================================================*/ 00028 00029 /*! 00030 * Function used to initialize ESP8266 wifi module 00031 * ============================================================================= 00032 */ 00033 void Esp8266_Init ( void ) 00034 { 00035 /* Baud rate used for communicating with Tera-term on PC */ 00036 pc.baud(115200); 00037 pc.printf("\n\n\rInitializing ESP\n\n\r"); 00038 //RESET ESP 00039 pc.printf("\n\n\rReset ESP\n\n\r"); 00040 esp.Reset(); 00041 /* Wait for 2 seconds */ 00042 wait(2); 00043 pc.printf("\n\n\rSending AT command\n\n\r"); 00044 strcpy(snd,"AT"); 00045 esp.SendCMD(snd); 00046 wait(0.1); 00047 pc.printf("\n\n\rSetting mode\n\n\r"); 00048 esp.SetMode(1); 00049 wait(2); 00050 pc.printf("\n\n\rConnecting to wifi network\n\n\r"); 00051 esp.Join(WIFI_SSID, WIFI_PASS); 00052 wait(5); 00053 // Set single connection 00054 esp.SetSingle(); 00055 pc.printf("\n\n\rGetting IP\n\n\r"); 00056 esp.GetIP(&ip[0]); 00057 pc.printf(ip); 00058 //receive a response from ESP 00059 pc.printf("\n\n\rReading connection status\n\n\r"); 00060 esp.GetConnStatus(&connection[0]); 00061 pc.printf(connection); 00062 } 00063 00064 00065 /*! 00066 * Function used to connect with thingspeak.com and update channel using 00067 * ESP8266 wifi module 00068 * ============================================================================= 00069 */ 00070 void Send_to_Thingspeak ( int field1, int field2, int field3 ) 00071 { 00072 /* ESP updates the Status of Thingspeak channel */ 00073 esp.startTCPConn(IP,80); 00074 wait(1); 00075 sprintf(snd,"GET https://api.thingspeak.com/update?api_key=NS64DJVP5YJKXUW2&field1=+%d+&field2=+%d+&field3=+%d+\r\n",field1, field2, field3); 00076 00077 int i=0; 00078 for(i=0;snd[i]!='\0';i++); 00079 i++; 00080 char cmd[255]; 00081 //Send Number of open connection and Characters to send 00082 sprintf(cmd,"AT+CIPSEND=%d",i); 00083 esp.SendCMD(cmd); 00084 pc.printf("S\r\n%s",cmd); 00085 while(i<=20 || rcv == ">") 00086 { 00087 esp.RcvReply(rcv, 1000); 00088 wait(100); 00089 i++; 00090 } 00091 //Post value to thingspeak channel 00092 esp.SendCMD(snd); 00093 00094 while(i<=20 || rcv == "OK") 00095 { 00096 esp.RcvReply(rcv, 1000); 00097 wait(100); 00098 i++; 00099 } 00100 } 00101 00102 /*! 00103 * Function used to Read door status from Thingspeak channel 00104 * 00105 * ============================================================================= 00106 */ 00107 int Read_from_Thingspeak ( void ) 00108 { 00109 char *prevPointer = NULL; 00110 char *currPointer = NULL; 00111 int doorCommand = 0xFF; 00112 00113 /*Read door status from Thingspeak channel*/ 00114 esp.startTCPConn(IP,80); 00115 wait(1); 00116 sprintf(snd,"GET https://api.thingspeak.com/channels/1309755/fields/1.json?api_key=F2HR03LUWEXO3QO0&results=2\r\n"); 00117 00118 int i=0; 00119 for(i=0;snd[i]!='\0';i++); 00120 i++; 00121 char cmd[255]; 00122 //Send Number of open connection and Characters to send 00123 sprintf(cmd,"AT+CIPSEND=%d",i); 00124 esp.SendCMD(cmd); 00125 pc.printf("S\r\n%s",cmd); 00126 while(i<=20 || rcv == ">") 00127 { 00128 esp.RcvReply(rcv, 1000); 00129 wait(100); 00130 i++; 00131 } 00132 pc.printf("R\r\n%s",rcv); 00133 //Post value to thingspeak channel 00134 esp.SendCMD(snd); 00135 pc.printf("S\r\n%s",snd); 00136 00137 while(i<=20 || rcv == "OK") 00138 { 00139 esp.RcvReply(rcv, 1000); 00140 wait(100); 00141 i++; 00142 } 00143 esp.RcvReply(rcv, 1000); 00144 if(strstr(rcv,"field1") != NULL) 00145 { 00146 prevPointer = strstr(rcv,"field1"); 00147 currPointer = strstr(rcv,"field1"); 00148 if(strstr((currPointer + 6),"field1") != NULL) 00149 { 00150 currPointer = strstr((currPointer + 6),"field1"); 00151 doorCommand = *(currPointer+9); 00152 } 00153 else 00154 { 00155 doorCommand = *(prevPointer+9); 00156 } 00157 //printf("word found\n\r"); 00158 } 00159 pc.printf("R\r\n%s",rcv); 00160 00161 return ( doorCommand - 48 ); 00162 }
Generated on Fri Jul 29 2022 09:34:43 by
1.7.2