Eduvance SIT2017 / Mbed 2 deprecated ESP8266_LocalPhant_KL25Z

Dependencies:   ESP8266 mbed

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"
00003  
00004 Serial pc(USBTX,USBRX);
00005 
00006 //POT sensor 
00007 AnalogIn pot(PTB0); 
00008 
00009 //wifi UART port and baud rate
00010 ESP8266 wifi(PTE0, PTE1, 115200); 
00011 
00012 //buffers for wifi library
00013 char snd[255],resp[1000];
00014 char http_cmd[300], comm[300];
00015 
00016 int timeout = 3000; //timeout for wifi commands
00017 
00018 //SSID and password for connection
00019 #define SSID "Eduvance_WiFi"
00020 #define PASS "eduvance"
00021 
00022 //Remote IP
00023 #define IP "192.168.0.2"
00024 
00025 //ldrvalue global variable
00026 float ldrval = 0; 
00027 
00028 //Public and private keys for phant
00029 char* Public_Key = "eaWkxgdlQZszLEDKpy6OFeDeO8x";
00030 char* Private_Key = "25xqpAeBmzSml4Yjpr05Hj3jOBq";
00031  
00032 //Wifi init function
00033 void wifi_initialize(void){
00034     
00035     pc.printf("******** Resetting wifi module ********\r\n");
00036     wifi.Reset();
00037     
00038     //wait for 5 seconds for response, else display no response receiveed
00039     if (wifi.RcvReply(resp, 5000))    
00040         pc.printf("%s",resp);    
00041     else
00042         pc.printf("No response");
00043     
00044     pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
00045     wifi.SetMode(1);    // set transparent  mode
00046     if (wifi.RcvReply(resp, timeout))    //receive a response from ESP
00047         pc.printf("%s",resp);    //Print the response onscreen
00048     else
00049         pc.printf("No response while setting mode. \r\n");
00050     
00051     pc.printf("******** Joining network with SSID and PASS ********\r\n");
00052     wifi.Join(SSID, PASS);     
00053     if (wifi.RcvReply(resp, timeout))    
00054         pc.printf("%s",resp);   
00055     else
00056         pc.printf("No response while connecting to network \r\n");
00057         
00058     pc.printf("******** Getting IP and MAC of module ********\r\n");
00059     wifi.GetIP(resp);     
00060     if (wifi.RcvReply(resp, timeout))    
00061         pc.printf("%s",resp);    
00062     else
00063         pc.printf("No response while getting IP \r\n");
00064     
00065     pc.printf("******** Setting WIFI UART passthrough ********\r\n");
00066     wifi.setTransparent();          
00067     if (wifi.RcvReply(resp, timeout))    
00068         pc.printf("%s",resp);    
00069     else
00070         pc.printf("No response while setting wifi passthrough. \r\n");
00071     wait(1);    
00072     
00073     pc.printf("******** Setting single connection mode ********\r\n");
00074     wifi.SetSingle();             
00075     wifi.RcvReply(resp, timeout);
00076     if (wifi.RcvReply(resp, timeout))    
00077         pc.printf("%s",resp);    
00078     else
00079         pc.printf("No response while setting single connection \r\n");
00080     wait(1);
00081 }
00082 
00083 void wifi_send(void){
00084     
00085     pc.printf("******** Starting TCP connection on IP and port ********\r\n");
00086     wifi.startTCPConn(IP, 8080);    //cipstart
00087     wifi.RcvReply(resp, timeout);
00088     if (wifi.RcvReply(resp, timeout))    
00089         pc.printf("%s",resp);    
00090     else
00091         pc.printf("No response while starting TCP connection \r\n");
00092     wait(1);
00093     
00094     //create link 
00095     sprintf(http_cmd,"/input/%s?private_key=%s&val=%0.2f",Public_Key,Private_Key,ldrval); 
00096     pc.printf(http_cmd);
00097     
00098     pc.printf("******** Sending URL to wifi ********\r\n");
00099     wifi.sendURL(http_cmd, comm);   //cipsend and get command
00100     if (wifi.RcvReply(resp, timeout))    
00101         pc.printf("%s",resp);    
00102     else
00103         pc.printf("No response while sending URL \r\n");
00104     
00105     //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
00106     //wifi.RcvReply(resp, timeout);
00107     //pc.printf("%s", resp);
00108 }
00109 
00110 int main () {
00111     
00112     
00113     wifi_initialize();
00114     
00115     while (1) {
00116         ldrval = pot.read();
00117         pc.printf("Potentiometer ldrvalue=%.3f \r\n", ldrval);
00118         
00119         wifi_send();
00120         wait(3);
00121     }
00122 }
00123     
00124