ESP8266 WiFi module test using thingspeak Sever.

Dependencies:   ESP8266 mbed

Fork of ESP8266_LocalPhant_KL25Z by Eduvance SIT2017

Files at this revision

API Documentation at this revision

Comitter:
agaikwad
Date:
Sat May 06 21:17:22 2017 +0000
Parent:
2:817794b2f733
Commit message:
ESP 8266 WiFi Module test

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 817794b2f733 -r 164296bb4985 main.cpp
--- a/main.cpp	Sun Jun 26 10:19:24 2016 +0000
+++ b/main.cpp	Sat May 06 21:17:22 2017 +0000
@@ -1,124 +1,137 @@
 #include "mbed.h"
-#include "ESP8266.h"
- 
+#include "ESP8266.h"               // Include header file from Author: Antonio Quevedo
+#include "math.h"
+#include <string>
+
+#define APIKEY E3AASCJVE2KQDD3V    //Put "Write key" of your channel in thingspeak.com 
+#define IP "184.106.153.149"       // IP Address of "api.thingspeak.com\"
+#define WIFI_SSID "Redmi"
+#define WIFI_PASS "akash12345"
+
 Serial pc(USBTX,USBRX);
 
-//POT sensor 
-AnalogIn pot(PTB0); 
-
-//wifi UART port and baud rate
-ESP8266 wifi(PTE0, PTE1, 115200); 
+ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi
 
-//buffers for wifi library
-char snd[255],resp[1000];
-char http_cmd[300], comm[300];
-
-int timeout = 3000; //timeout for wifi commands
+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 
 
-//SSID and password for connection
-#define SSID "Eduvance_WiFi"
-#define PASS "eduvance"
-
-//Remote IP
-#define IP "192.168.0.2"
+void esp_initialize(void); // Function used to initialize ESP8266 wifi module 
+void esp_send(void);       // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module 
 
-//ldrvalue global variable
-float ldrval = 0; 
+//test variable 
+float i=0;
+float j=10;
 
-//Public and private keys for phant
-char* Public_Key = "eaWkxgdlQZszLEDKpy6OFeDeO8x";
-char* Private_Key = "25xqpAeBmzSml4Yjpr05Hj3jOBq";
- 
-//Wifi init function
-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);    
-    else
-        pc.printf("No response");
+int main() 
+{
     
-    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.baud(115200);           // Baud rate used for communicating with Tera-term on PC
+    
+    pc.printf("START\r\n");  // Starting point
+    
+    esp_initialize();
     
-    pc.printf("******** Joining network with SSID and PASS ********\r\n");
-    wifi.Join(SSID, PASS);     
-    if (wifi.RcvReply(resp, timeout))    
-        pc.printf("%s",resp);   
-    else
-        pc.printf("No response while connecting to network \r\n");
+  
+
+    while (1) 
+    {   wait(15);
+        pc.printf("test value is %f%f\n",i,j);
+        esp_send(); 
+        i++;
+        j--;
         
-    pc.printf("******** Getting IP and MAC of module ********\r\n");
-    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);    
-    else
-        pc.printf("No response while setting wifi passthrough. \r\n");
-    wait(1);    
-    
-    pc.printf("******** Setting single connection mode ********\r\n");
-    wifi.SetSingle();             
-    wifi.RcvReply(resp, timeout);
-    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 esp_initialize(void)
+{    
+    pc.printf("Initializing ESP\r\n"); 
+      
+    pc.printf("Reset ESP\r\n"); 
+    esp.Reset();                   //RESET ESP
+    esp.RcvReply(rcv, 400);        //receive a response from ESP
+    //pc.printf(rcv);          //Print the response onscreen 
+    wait(2);
+    
+    strcpy(snd,"AT");
+    esp.SendCMD(snd);
+    pc.printf(snd);
+    //wait(2);
+    esp.RcvReply(rcv, 400);       
+    pc.printf(rcv);      
+    wait(0.1);
+    
+    strcpy(snd,"AT+CWMODE=1");
+    esp.SendCMD(snd);
+    pc.printf(snd);
+    wait(2);
+
+    strcpy(snd,"AT+CWJAP=\"");
+    strcat(snd,WIFI_SSID);
+    strcat(snd,"\",\"");
+    strcat(snd,WIFI_PASS);
+    strcat(snd,"\"");
     
-    pc.printf("******** Starting TCP connection on IP and port ********\r\n");
-    wifi.startTCPConn(IP, 8080);    //cipstart
-    wifi.RcvReply(resp, timeout);
-    if (wifi.RcvReply(resp, timeout))    
-        pc.printf("%s",resp);    
-    else
-        pc.printf("No response while starting TCP connection \r\n");
+    esp.SendCMD(snd);
+    pc.printf(snd);
+    wait(5);
+    esp.RcvReply(rcv, 400);       
+    pc.printf("\n %s \n", rcv); 
+    
+    strcpy(snd,"AT+CIPMUX=0");
+    esp.SendCMD(snd);
+    pc.printf(snd);
+    //wait(2);
+    esp.RcvReply(rcv, 400);       
+    pc.printf("\n %s \n", rcv); 
+
+}
+
+
+void esp_send(void)
+{
+   
+    //ESP updates the Status of Thingspeak channel//
+    
+    strcpy(snd,"AT+CIPSTART=");
+    strcat(snd,"\"TCP\",\"");
+    strcat(snd,IP);
+    strcat(snd,"\",80");
+    
+    esp.SendCMD(snd); 
+    pc.printf("S\r\n%s",snd);
+    //wait(2);                                                    
+    esp.RcvReply(rcv, 1000);
+    pc.printf("R\r\n%s",rcv);
     wait(1);
     
-    //create link 
-    sprintf(http_cmd,"/input/%s?private_key=%s&val=%0.2f",Public_Key,Private_Key,ldrval); 
-    pc.printf(http_cmd);
+    sprintf(snd,"GET https://api.thingspeak.com/update?key=E3AASCJVE2KQDD3V&field1=%f&field2=%f\r\n",i,j);
+    
+    int i=0;
+    for(i=0;snd[i]!='\0';i++);
+    i++;
+    char cmd[255];
     
-    pc.printf("******** Sending URL to wifi ********\r\n");
-    wifi.sendURL(http_cmd, comm);   //cipsend and get command
-    if (wifi.RcvReply(resp, timeout))    
-        pc.printf("%s",resp);    
-    else
-        pc.printf("No response while sending URL \r\n");
+    sprintf(cmd,"AT+CIPSEND=%d",i);                                       //Send Number of open connection and Characters to send 
+    esp.SendCMD(cmd);
+    pc.printf("S\r\n%s",cmd);
+    while(i<=20 || rcv == ">")
+    {
+        esp.RcvReply(rcv, 1000);
+        wait(100);
+        i++;
+    }
+    pc.printf("R\r\n%s",rcv);
     
-    //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
-    //wifi.RcvReply(resp, timeout);
-    //pc.printf("%s", resp);
+    esp.SendCMD(snd);                                                      //Post value to thingspeak channel
+    pc.printf("S\r\n%s",snd);
+    
+    while(i<=20 || rcv == "OK")
+    {
+        esp.RcvReply(rcv, 1000);
+        wait(100);
+        i++;
+    }
+    pc.printf("R\r\n%s",rcv);
+    
 }
-
-int main () {
-    
-    
-    wifi_initialize();
-    
-    while (1) {
-        ldrval = pot.read();
-        pc.printf("Potentiometer ldrvalue=%.3f \r\n", ldrval);
-        
-        wifi_send();
-        wait(3);
-    }
-}
-    
-