Sending data to thingspeak
Dependencies: DHT ESP8266 mbed
Fork of temp_hum by
main.cpp@6:fd279a17f523, 2017-04-02 (annotated)
- Committer:
- siddharthp
- Date:
- Sun Apr 02 13:38:06 2017 +0000
- Revision:
- 6:fd279a17f523
- Parent:
- 5:0d98bbc39c7b
- Child:
- 7:00c31738f232
Changes made
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 | 5:0d98bbc39c7b | 7 | float cur = 0.0f;//current value |
siddharthp | 5:0d98bbc39c7b | 8 | float comp = 0.0f;//compressed air flow |
siddharthp | 5:0d98bbc39c7b | 9 | float temp = 0.0f;//temperature |
siddharthp | 5:0d98bbc39c7b | 10 | float hum = 0.0f;// humidity |
siddharthp | 5:0d98bbc39c7b | 11 | float co = 0.0f;// CO2 |
siddharthp | 5:0d98bbc39c7b | 12 | float pres = 0.0f;//pressure |
siddharthp | 0:da7e87322045 | 13 | #define IP "184.106.153.149" // thingspeak.com IP Address |
siddharthp | 0:da7e87322045 | 14 | void wifi_send(void); |
siddharthp | 0:da7e87322045 | 15 | |
siddharthp | 0:da7e87322045 | 16 | int main() |
siddharthp | 0:da7e87322045 | 17 | { |
siddharthp | 0:da7e87322045 | 18 | pc.baud(115200); |
siddharthp | 0:da7e87322045 | 19 | pc.printf("SET mode to AP\r\n"); |
siddharthp | 0:da7e87322045 | 20 | wifi.SetMode(1); // set ESP mode to 1 |
siddharthp | 0:da7e87322045 | 21 | wifi.RcvReply(rcv, 1000); //receive a response from ESP |
siddharthp | 0:da7e87322045 | 22 | pc.printf("%s",rcv); //Print the response onscreen |
siddharthp | 0:da7e87322045 | 23 | pc.printf("Conneting to Wifi\r\n"); |
siddharthp | 5:0d98bbc39c7b | 24 | wifi.Join("SSID", "password"); // Your wifi username & Password |
siddharthp | 0:da7e87322045 | 25 | wifi.RcvReply(rcv, 1000); //receive a response from ESP |
siddharthp | 0:da7e87322045 | 26 | pc.printf("%s\n", rcv); //Print the response onscreen |
siddharthp | 0:da7e87322045 | 27 | wait(8); //waits for response from ESP |
siddharthp | 0:da7e87322045 | 28 | pc.printf("Getting IP\r\n"); //get IP addresss from the connected AP |
siddharthp | 0:da7e87322045 | 29 | wifi.GetIP(rcv); //receive an IP address from the AP |
siddharthp | 0:da7e87322045 | 30 | pc.printf("%s\n", rcv); |
siddharthp | 5:0d98bbc39c7b | 31 | //WIFI updates the Status to Thingspeak servers// |
siddharthp | 5:0d98bbc39c7b | 32 | pc.printf("PLEASE STAY AWAY\r\n"); |
siddharthp | 5:0d98bbc39c7b | 33 | pc.printf("Sending WiFi information\r\n"); |
siddharthp | 5:0d98bbc39c7b | 34 | strcpy(snd,"AT+CIPMUX=1\r\n");//Setting WiFi into MultiChannel mode |
siddharthp | 5:0d98bbc39c7b | 35 | wifi.SendCMD(snd); |
siddharthp | 5:0d98bbc39c7b | 36 | pc.printf(snd); |
siddharthp | 5:0d98bbc39c7b | 37 | wait(2.0); |
siddharthp | 5:0d98bbc39c7b | 38 | wifi.RcvReply(rcv, 1000); |
siddharthp | 5:0d98bbc39c7b | 39 | pc.printf("%s\n", rcv); |
siddharthp | 5:0d98bbc39c7b | 40 | wait(2); |
siddharthp | 5:0d98bbc39c7b | 41 | sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\r\n",IP); //Initiate connection with THINGSPEAK server |
siddharthp | 5:0d98bbc39c7b | 42 | pc.printf(snd); |
siddharthp | 5:0d98bbc39c7b | 43 | wait(3.0); |
siddharthp | 5:0d98bbc39c7b | 44 | wifi.RcvReply(rcv, 1000); |
siddharthp | 5:0d98bbc39c7b | 45 | pc.printf("%s\n", rcv); |
siddharthp | 5:0d98bbc39c7b | 46 | wait(2); |
siddharthp | 5:0d98bbc39c7b | 47 | strcpy(snd,"AT+CIPSEND=4,100\r\n"); //Send Number of open connections,Characters to send |
siddharthp | 5:0d98bbc39c7b | 48 | wifi.SendCMD(snd); |
siddharthp | 5:0d98bbc39c7b | 49 | pc.printf(snd); |
siddharthp | 5:0d98bbc39c7b | 50 | wait(2.0); |
siddharthp | 5:0d98bbc39c7b | 51 | wifi.RcvReply(rcv, 1000); |
siddharthp | 5:0d98bbc39c7b | 52 | pc.printf("%s\n", rcv); |
siddharthp | 5:0d98bbc39c7b | 53 | wait(2); |
siddharthp | 0:da7e87322045 | 54 | while (1) |
siddharthp | 0:da7e87322045 | 55 | { |
siddharthp | 0:da7e87322045 | 56 | wifi_send(); |
siddharthp | 1:402b14b56d24 | 57 | RED=1; |
siddharthp | 5:0d98bbc39c7b | 58 | wait(10); |
siddharthp | 1:402b14b56d24 | 59 | RED=0; |
siddharthp | 5:0d98bbc39c7b | 60 | wait(10); |
siddharthp | 0:da7e87322045 | 61 | } |
siddharthp | 0:da7e87322045 | 62 | } |
siddharthp | 5:0d98bbc39c7b | 63 | void wifi_send(void) |
siddharthp | 5:0d98bbc39c7b | 64 | { |
siddharthp | 6:fd279a17f523 | 65 | sprintf(snd,"GET http://api.thingspeak.com/update?api_key=54JPIP9GVWVX9BMV&field1=%f&field2=%f&field3=%f&field4=%f&field5=%f&field6=%f\r\n", cur, comp,temp,hum,co,pres); //Post values to thingspeak |
siddharthp | 0:da7e87322045 | 66 | pc.printf("%s",snd); |
siddharthp | 0:da7e87322045 | 67 | wifi.SendCMD(snd); |
siddharthp | 0:da7e87322045 | 68 | wifi.RcvReply(rcv, 1000); |
siddharthp | 0:da7e87322045 | 69 | pc.printf("%s", rcv); |
siddharthp | 0:da7e87322045 | 70 | } |