Rajas Chitanvis
/
ESP8266_test
thingspeak using ESP8266 wifi module
Diff: main.cpp
- Revision:
- 1:2012c31aee1b
- Parent:
- 0:8ca71ccb52db
- Child:
- 2:817794b2f733
diff -r 8ca71ccb52db -r 2012c31aee1b main.cpp --- a/main.cpp Mon Jun 06 11:45:26 2016 +0000 +++ b/main.cpp Fri Jun 24 02:22:53 2016 +0000 @@ -2,96 +2,123 @@ #include "ESP8266.h" Serial pc(USBTX,USBRX); -AnalogIn pot(PTB0); // pir senor input -ESP8266 wifi(PTE0, PTE1, 115200); // baud rate for wifi -char snd[255],rcv[1000]; -char http_cmd[300]; - -//#define IP "data.sparkfun.com" -//#define IP "184.106.153.149" -#define IP "192.168.0.39" + +//POT sensor +AnalogIn pot(PTB0); + +//wifi UART port and baud rate +ESP8266 wifi(PTE0, PTE1, 115200); + +//buffers for wifi library +char snd[255],resp[1000]; +char http_cmd[300], comm[300]; + +int timeout = 3000; //timeout for wifi commands + +//SSID and password for connection +#define SSID "Eduvance_WiFi" +#define PASS "eduvance" + +//Remote IP +#define IP "192.168.0.13" + +//ldrvalue global variable +float ldrval = 0; + +//Public and private keys for phant +char* Public_Key = "wyJD1Pza1OCk1QEKk9K9C10zdyj3"; +char* Private_Key = "OrxMl9BLlOCvPZdBv4B4H3mDOAlx"; -float val = 0; //global variable - -char* Public_Key = "Xb3ovW3grvIMo31jd8p0HmPEba3O"; -char* Private_Key = "LPZe7qZLg7t0NnWbYD2XHrmB2AMq"; +//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"); + + 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.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"); + + 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"); + +} -/************ WiFi INTIALIZATION *********/ - -void wifi_initialize(void); -void wifi_send(void); - +void wifi_send(void){ + + 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); + + 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"); + wait(1); + + //create link + sprintf(http_cmd,"/input/%s?private_key=%s&val=%0.2f",Public_Key,Private_Key,ldrval); + + 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"); + + //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server + //wifi.RcvReply(resp, timeout); + //pc.printf("%s", resp); +} + int main () { - - wifi_initialize(); - + + wifi_initialize(); + while (1) { - - val = pot.read(); - pc.printf("\r\nPotentiometer value=%.3f \r\n", val); - pc.printf("Sending WiFi information\n\r"); - wifi_send(); - wait(4); + ldrval = pot.read(); + pc.printf("Potentiometer ldrvalue=%.3f \r\n", ldrval); + + wifi_send(); + wait(3); } } -void wifi_initialize(){ - - pc.baud(9600); - pc.printf("SET mode to AP\r\n"); - wifi.SetMode(1); // set ESP mode to 1 - wifi.RcvReply(rcv, 1000); //receive a response from ESP - pc.printf("%s",rcv); //Print the response onscreen - pc.printf("Conneting to AP\r\n"); - wifi.Join("Eduvance_WiFi", "eduvance"); // Your wifi username & Password - wifi.RcvReply(rcv, 1000); //receive a response from ESP - pc.printf("%s", rcv); //Print the response onscreen - wait(8); //waits for response from ESP - pc.printf("Getting IP\r\n"); //get IP addresss from the connected AP - wifi.GetIP(rcv); //receive an IP address from the AP - pc.printf("%s", rcv); - wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding - pc.printf("Initializing WiFi\r\n"); -} -void wifi_send(void){ - - strcpy(snd,"AT+CIPMODE=0");//Setting WiFi into MultiChannel mode - wifi.SendCMD(snd); - //pc.printf(snd); - wifi.RcvReply(rcv, 3000); - pc.printf("%s", rcv); - - //WIFI updates the Status to Thingspeak servers// - strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode - wifi.SendCMD(snd); - //pc.printf(snd); - wifi.RcvReply(rcv, 3000); - pc.printf("%s", rcv); - - sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",8080",IP); //Initiate connection with server - wifi.SendCMD(snd); - //pc.printf(snd); - wifi.RcvReply(rcv, 3000); - pc.printf("%s", rcv); - - sprintf(http_cmd,"GET /input/%s?private_key=%s&val=%0.2f HTTP/1.0\n\n",Public_Key,Private_Key,val); //Post values - pc.printf("%d",strlen(http_cmd)); - strcpy(snd,"AT+CIPSEND=4,105"); //Send Number of open connections,Characters to send - wifi.SendCMD(snd); - //pc.printf(snd); - wifi.RcvReply(rcv, 3000); - pc.printf("%s", rcv); - - sprintf(snd,"GET /input/%s?private_key=%s&val=%0.2f HTTP/1.0\n\n",Public_Key,Private_Key,val); //Post values - strcpy(snd, http_cmd); - pc.printf("%s",http_cmd); - wifi.SendCMD(http_cmd); - wifi.RcvReply(rcv, 3000); - pc.printf("%s", rcv); - - //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server - //wifi.RcvReply(rcv, 3000); - //pc.printf("%s", rcv); -} \ No newline at end of file