Sending data to thingspeak
Dependencies: DHT ESP8266 mbed
Fork of temp_hum by
main.cpp@9:32443e001090, 2017-04-03 (annotated)
- Committer:
- siddharthp
- Date:
- Mon Apr 03 17:06:13 2017 +0000
- Revision:
- 9:32443e001090
- Parent:
- 8:f14bdc09a319
Minor changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
siddharthp | 0:da7e87322045 | 1 | #include "mbed.h" |
siddharthp | 5:0d98bbc39c7b | 2 | #include "ESP8266.h" |
siddharthp | 0:da7e87322045 | 3 | ESP8266 wifi(PTC17, PTC16, 115200); |
siddharthp | 0:da7e87322045 | 4 | Serial pc(USBTX,USBRX); |
siddharthp | 0:da7e87322045 | 5 | DigitalOut RED(LED1); |
siddharthp | 0:da7e87322045 | 6 | char snd[255],rcv[1000]; |
siddharthp | 9:32443e001090 | 7 | char http_cmd[300]; |
siddharthp | 5:0d98bbc39c7b | 8 | float cur = 0.0f;//current value |
siddharthp | 5:0d98bbc39c7b | 9 | float comp = 0.0f;//compressed air flow |
siddharthp | 5:0d98bbc39c7b | 10 | float temp = 0.0f;//temperature |
siddharthp | 5:0d98bbc39c7b | 11 | float hum = 0.0f;// humidity |
siddharthp | 5:0d98bbc39c7b | 12 | float co = 0.0f;// CO2 |
siddharthp | 5:0d98bbc39c7b | 13 | float pres = 0.0f;//pressure |
siddharthp | 9:32443e001090 | 14 | #define IP "http://184.106.153.149/update" // thingspeak.com IP Address |
siddharthp | 0:da7e87322045 | 15 | void wifi_send(void); |
siddharthp | 0:da7e87322045 | 16 | |
siddharthp | 0:da7e87322045 | 17 | int main() |
siddharthp | 0:da7e87322045 | 18 | { |
siddharthp | 9:32443e001090 | 19 | pc.baud(115200); |
siddharthp | 9:32443e001090 | 20 | wifi.Reset(); |
siddharthp | 0:da7e87322045 | 21 | pc.printf("SET mode to AP\r\n"); |
siddharthp | 0:da7e87322045 | 22 | wifi.SetMode(1); // set ESP mode to 1 |
siddharthp | 0:da7e87322045 | 23 | wifi.RcvReply(rcv, 1000); //receive a response from ESP |
siddharthp | 0:da7e87322045 | 24 | pc.printf("%s",rcv); //Print the response onscreen |
siddharthp | 0:da7e87322045 | 25 | pc.printf("Conneting to Wifi\r\n"); |
siddharthp | 9:32443e001090 | 26 | wifi.Join("moto x play", "motoxplay"); // Your wifi username & Password |
siddharthp | 0:da7e87322045 | 27 | wifi.RcvReply(rcv, 1000); //receive a response from ESP |
siddharthp | 0:da7e87322045 | 28 | pc.printf("%s\n", rcv); //Print the response onscreen |
siddharthp | 0:da7e87322045 | 29 | wait(8); //waits for response from ESP |
siddharthp | 0:da7e87322045 | 30 | pc.printf("Getting IP\r\n"); //get IP addresss from the connected AP |
siddharthp | 0:da7e87322045 | 31 | wifi.GetIP(rcv); //receive an IP address from the AP |
siddharthp | 0:da7e87322045 | 32 | pc.printf("%s\n", rcv); |
siddharthp | 5:0d98bbc39c7b | 33 | //WIFI updates the Status to Thingspeak servers// |
siddharthp | 5:0d98bbc39c7b | 34 | pc.printf("PLEASE STAY AWAY\r\n"); |
siddharthp | 5:0d98bbc39c7b | 35 | pc.printf("Sending WiFi information\r\n"); |
siddharthp | 9:32443e001090 | 36 | strcpy(snd,"AT+CIPMODE=0");//Setting WiFi into MultiChannel mode |
siddharthp | 5:0d98bbc39c7b | 37 | wifi.SendCMD(snd); |
siddharthp | 5:0d98bbc39c7b | 38 | pc.printf(snd); |
siddharthp | 5:0d98bbc39c7b | 39 | wifi.RcvReply(rcv, 1000); |
siddharthp | 9:32443e001090 | 40 | pc.printf("%s", rcv); |
siddharthp | 9:32443e001090 | 41 | |
siddharthp | 9:32443e001090 | 42 | //WIFI updates the Status to Thingspeak servers// |
siddharthp | 9:32443e001090 | 43 | strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode |
siddharthp | 9:32443e001090 | 44 | wifi.SendCMD(snd); |
siddharthp | 9:32443e001090 | 45 | pc.printf(snd); |
siddharthp | 9:32443e001090 | 46 | wifi.RcvReply(rcv, 1000); |
siddharthp | 9:32443e001090 | 47 | pc.printf("%s", rcv); |
siddharthp | 9:32443e001090 | 48 | |
siddharthp | 9:32443e001090 | 49 | |
siddharthp | 9:32443e001090 | 50 | sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80",IP); //Initiate connection with THINGSPEAK server |
siddharthp | 9:32443e001090 | 51 | wifi.SendCMD(snd); |
siddharthp | 9:32443e001090 | 52 | //pc.printf(snd); |
siddharthp | 9:32443e001090 | 53 | wifi.RcvReply(rcv, 1000); |
siddharthp | 9:32443e001090 | 54 | pc.printf("%s", rcv); |
siddharthp | 5:0d98bbc39c7b | 55 | wait(2); |
siddharthp | 9:32443e001090 | 56 | 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"); |
siddharthp | 9:32443e001090 | 57 | sprintf(snd,"AT+CIPSEND=4,%d\r\n",strlen(http_cmd)); //Send Number of open connections,Characters to send |
siddharthp | 5:0d98bbc39c7b | 58 | wifi.SendCMD(snd); |
siddharthp | 5:0d98bbc39c7b | 59 | pc.printf(snd); |
siddharthp | 5:0d98bbc39c7b | 60 | wait(2.0); |
siddharthp | 5:0d98bbc39c7b | 61 | wifi.RcvReply(rcv, 1000); |
siddharthp | 5:0d98bbc39c7b | 62 | pc.printf("%s\n", rcv); |
siddharthp | 5:0d98bbc39c7b | 63 | wait(2); |
siddharthp | 0:da7e87322045 | 64 | while (1) |
siddharthp | 0:da7e87322045 | 65 | { |
siddharthp | 0:da7e87322045 | 66 | wifi_send(); |
siddharthp | 1:402b14b56d24 | 67 | RED=1; |
siddharthp | 5:0d98bbc39c7b | 68 | wait(10); |
siddharthp | 1:402b14b56d24 | 69 | RED=0; |
siddharthp | 5:0d98bbc39c7b | 70 | wait(10); |
siddharthp | 0:da7e87322045 | 71 | } |
siddharthp | 0:da7e87322045 | 72 | } |
siddharthp | 5:0d98bbc39c7b | 73 | void wifi_send(void) |
siddharthp | 5:0d98bbc39c7b | 74 | { |
siddharthp | 9:32443e001090 | 75 | 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 |
siddharthp | 9:32443e001090 | 76 | pc.printf("%s",http_cmd); |
siddharthp | 9:32443e001090 | 77 | wifi.SendCMD(http_cmd); |
siddharthp | 0:da7e87322045 | 78 | wifi.RcvReply(rcv, 1000); |
siddharthp | 0:da7e87322045 | 79 | pc.printf("%s", rcv); |
siddharthp | 0:da7e87322045 | 80 | } |