Siddharth Pimprikar / Mbed 2 deprecated IAC_send

Dependencies:   DHT ESP8266 mbed

Fork of temp_hum by Siddharth Pimprikar

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 ESP8266 wifi(PTC17, PTC16, 115200);
00004 Serial pc(USBTX,USBRX);
00005 DigitalOut RED(LED1);
00006 char snd[255],rcv[1000];
00007 char http_cmd[300];
00008 float cur = 0.0f;//current value
00009 float comp = 0.0f;//compressed air flow
00010 float temp = 0.0f;//temperature
00011 float hum = 0.0f;// humidity
00012 float co = 0.0f;// CO2 
00013 float pres = 0.0f;//pressure
00014 #define IP "http://184.106.153.149/update" // thingspeak.com IP Address
00015 void wifi_send(void);
00016 
00017 int main()
00018 {
00019     pc.baud(115200);
00020     wifi.Reset();
00021     pc.printf("SET mode to AP\r\n");
00022     wifi.SetMode(1);    // set ESP mode to 1
00023     wifi.RcvReply(rcv, 1000);    //receive a response from ESP
00024     pc.printf("%s",rcv);    //Print the response onscreen
00025     pc.printf("Conneting to Wifi\r\n");
00026     wifi.Join("moto x play", "motoxplay");     // Your wifi username & Password 
00027     wifi.RcvReply(rcv, 1000);    //receive a response from ESP
00028     pc.printf("%s\n", rcv);    //Print the response onscreen
00029     wait(8);     //waits for response from ESP
00030     pc.printf("Getting IP\r\n");    //get IP addresss from the connected AP
00031     wifi.GetIP(rcv);    //receive an IP address from the AP
00032     pc.printf("%s\n", rcv);
00033     //WIFI updates the Status to Thingspeak servers//
00034     pc.printf("PLEASE STAY AWAY\r\n");
00035     pc.printf("Sending WiFi information\r\n");
00036     strcpy(snd,"AT+CIPMODE=0");//Setting WiFi into MultiChannel mode
00037     wifi.SendCMD(snd);
00038     pc.printf(snd);
00039     wifi.RcvReply(rcv, 1000);
00040     pc.printf("%s", rcv);
00041  
00042   //WIFI updates the Status to Thingspeak servers//
00043   strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode
00044   wifi.SendCMD(snd);
00045   pc.printf(snd);
00046   wifi.RcvReply(rcv, 1000);
00047   pc.printf("%s", rcv);
00048   
00049   
00050   sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80",IP); //Initiate connection with THINGSPEAK server 
00051   wifi.SendCMD(snd);
00052   //pc.printf(snd);
00053   wifi.RcvReply(rcv, 1000);
00054   pc.printf("%s", rcv);
00055     wait(2);
00056     strcpy(http_cmd,"GET http://api.thingspeak.com/update?api_key=54JPIP9GVWVX9BMV&field1=%f&field2=%f&field3=%f&field4=%f&field5=%f&field6=%f HTTP/1.0\r\n");
00057     sprintf(snd,"AT+CIPSEND=4,%d\r\n",strlen(http_cmd));    //Send Number of open connections,Characters to send 
00058     wifi.SendCMD(snd);
00059     pc.printf(snd);
00060     wait(2.0);
00061     wifi.RcvReply(rcv, 1000);
00062     pc.printf("%s\n", rcv);
00063     wait(2);
00064     while (1) 
00065     {
00066         wifi_send();
00067         RED=1;
00068         wait(10);
00069         RED=0;
00070         wait(10);
00071     }    
00072 }
00073 void wifi_send(void)
00074 {            
00075   sprintf(http_cmd,"GET http://api.thingspeak.com/update?api_key=54JPIP9GVWVX9BMV&field1=%f&field2=%f&field3=%f&field4=%f&field5=%f&field6=%f HTTP/1.0\r\n", cur,comp,temp,hum,co,pres); //Post values to thingspeak
00076   pc.printf("%s",http_cmd);
00077   wifi.SendCMD(http_cmd);
00078   wifi.RcvReply(rcv, 1000);
00079   pc.printf("%s", rcv);
00080 }