
DHT11 Temperature and humidity sensor using FRDM-K64f and Thingspeak
Dependencies: DHT ESP8266 mbed
main.cpp@4:aa3e25ccc05d, 2016-12-04 (annotated)
- Committer:
- siddharthp
- Date:
- Sun Dec 04 21:21:05 2016 +0000
- Revision:
- 4:aa3e25ccc05d
- Parent:
- 3:49571dd386aa
Minor changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
siddharthp | 0:da7e87322045 | 1 | #include "mbed.h" |
siddharthp | 0:da7e87322045 | 2 | #include "DHT.h" |
siddharthp | 0:da7e87322045 | 3 | #include "ESP8266.h" |
siddharthp | 0:da7e87322045 | 4 | |
siddharthp | 0:da7e87322045 | 5 | DHT sensor(D4, DHT11); |
siddharthp | 0:da7e87322045 | 6 | ESP8266 wifi(PTC17, PTC16, 115200); |
siddharthp | 0:da7e87322045 | 7 | Serial pc(USBTX,USBRX); |
siddharthp | 0:da7e87322045 | 8 | DigitalOut RED(LED1); |
siddharthp | 0:da7e87322045 | 9 | char snd[255],rcv[1000]; |
siddharthp | 0:da7e87322045 | 10 | #define IP "184.106.153.149" // thingspeak.com IP Address |
siddharthp | 0:da7e87322045 | 11 | void wifi_send(void); |
siddharthp | 0:da7e87322045 | 12 | |
siddharthp | 0:da7e87322045 | 13 | int main() |
siddharthp | 0:da7e87322045 | 14 | { |
siddharthp | 0:da7e87322045 | 15 | pc.baud(115200); |
siddharthp | 0:da7e87322045 | 16 | pc.printf("SET mode to AP\r\n"); |
siddharthp | 0:da7e87322045 | 17 | wifi.SetMode(1); // set ESP mode to 1 |
siddharthp | 0:da7e87322045 | 18 | wifi.RcvReply(rcv, 1000); //receive a response from ESP |
siddharthp | 0:da7e87322045 | 19 | pc.printf("%s",rcv); //Print the response onscreen |
siddharthp | 0:da7e87322045 | 20 | pc.printf("Conneting to Wifi\r\n"); |
siddharthp | 4:aa3e25ccc05d | 21 | wifi.Join("ssid", "password"); // Your wifi username & Password |
siddharthp | 0:da7e87322045 | 22 | wifi.RcvReply(rcv, 1000); //receive a response from ESP |
siddharthp | 0:da7e87322045 | 23 | pc.printf("%s\n", rcv); //Print the response onscreen |
siddharthp | 0:da7e87322045 | 24 | wait(8); //waits for response from ESP |
siddharthp | 0:da7e87322045 | 25 | pc.printf("Getting IP\r\n"); //get IP addresss from the connected AP |
siddharthp | 0:da7e87322045 | 26 | wifi.GetIP(rcv); //receive an IP address from the AP |
siddharthp | 0:da7e87322045 | 27 | pc.printf("%s\n", rcv); |
siddharthp | 0:da7e87322045 | 28 | while (1) |
siddharthp | 0:da7e87322045 | 29 | { |
siddharthp | 0:da7e87322045 | 30 | pc.printf("PLEASE STAY AWAY\r\n"); |
siddharthp | 0:da7e87322045 | 31 | pc.printf("Sending WiFi information\n"); |
siddharthp | 0:da7e87322045 | 32 | wifi_send(); |
siddharthp | 1:402b14b56d24 | 33 | RED=1; |
siddharthp | 0:da7e87322045 | 34 | wait(2.0f); |
siddharthp | 1:402b14b56d24 | 35 | RED=0; |
siddharthp | 0:da7e87322045 | 36 | wait(1.5f); |
siddharthp | 0:da7e87322045 | 37 | } |
siddharthp | 0:da7e87322045 | 38 | } |
siddharthp | 0:da7e87322045 | 39 | void wifi_send(void){ |
siddharthp | 0:da7e87322045 | 40 | int error = 0; |
siddharthp | 0:da7e87322045 | 41 | float h = 0.0f, c = 0.0f; |
siddharthp | 0:da7e87322045 | 42 | |
siddharthp | 0:da7e87322045 | 43 | wait(2.0f); |
siddharthp | 0:da7e87322045 | 44 | error = sensor.readData(); |
siddharthp | 0:da7e87322045 | 45 | if (0 == error) |
siddharthp | 0:da7e87322045 | 46 | { |
siddharthp | 0:da7e87322045 | 47 | c = sensor.ReadTemperature(CELCIUS); |
siddharthp | 0:da7e87322045 | 48 | h = sensor.ReadHumidity(); |
siddharthp | 0:da7e87322045 | 49 | //printf("Temperature in Celcius: %f\n", c); |
siddharthp | 0:da7e87322045 | 50 | //printf("Humidity is %f\n", h); |
siddharthp | 0:da7e87322045 | 51 | } |
siddharthp | 0:da7e87322045 | 52 | else |
siddharthp | 0:da7e87322045 | 53 | { |
siddharthp | 0:da7e87322045 | 54 | printf("Error: %d\n", error); |
siddharthp | 0:da7e87322045 | 55 | } |
siddharthp | 0:da7e87322045 | 56 | //WIFI updates the Status to Thingspeak servers// |
siddharthp | 0:da7e87322045 | 57 | strcpy(snd,"AT+CIPMUX=1\n");//Setting WiFi into MultiChannel mode |
siddharthp | 0:da7e87322045 | 58 | wifi.SendCMD(snd); |
siddharthp | 0:da7e87322045 | 59 | pc.printf(snd); |
siddharthp | 0:da7e87322045 | 60 | wait(2.0); |
siddharthp | 0:da7e87322045 | 61 | wifi.RcvReply(rcv, 1000); |
siddharthp | 0:da7e87322045 | 62 | pc.printf("%s\n", rcv); |
siddharthp | 0:da7e87322045 | 63 | wait(2); |
siddharthp | 0:da7e87322045 | 64 | sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",IP); //Initiate connection with THINGSPEAK server |
siddharthp | 0:da7e87322045 | 65 | pc.printf(snd); |
siddharthp | 0:da7e87322045 | 66 | wait(3.0); |
siddharthp | 0:da7e87322045 | 67 | wifi.RcvReply(rcv, 1000); |
siddharthp | 0:da7e87322045 | 68 | pc.printf("%s\n", rcv); |
siddharthp | 0:da7e87322045 | 69 | wait(2); |
siddharthp | 0:da7e87322045 | 70 | strcpy(snd,"AT+CIPSEND=4,47\n"); //Send Number of open connections,Characters to send |
siddharthp | 0:da7e87322045 | 71 | wifi.SendCMD(snd); |
siddharthp | 0:da7e87322045 | 72 | pc.printf(snd); |
siddharthp | 0:da7e87322045 | 73 | wait(2.0); |
siddharthp | 0:da7e87322045 | 74 | wifi.RcvReply(rcv, 1000); |
siddharthp | 0:da7e87322045 | 75 | pc.printf("%s\n", rcv); |
siddharthp | 0:da7e87322045 | 76 | wait(2); |
siddharthp | 4:aa3e25ccc05d | 77 | sprintf(snd,"GET http://api.thingspeak.com/update?api_key=**********&field1=%1.3f\n", c); //Post values to thingspeak |
siddharthp | 0:da7e87322045 | 78 | pc.printf("%s",snd); |
siddharthp | 0:da7e87322045 | 79 | wifi.SendCMD(snd); |
siddharthp | 0:da7e87322045 | 80 | wait(2); |
siddharthp | 4:aa3e25ccc05d | 81 | sprintf(snd,"GET http://api.thingspeak.com/update?api_key=***********&field2=%1.3f\n", h); //Post values to thingspeak |
siddharthp | 0:da7e87322045 | 82 | pc.printf("%s",snd); |
siddharthp | 0:da7e87322045 | 83 | wifi.SendCMD(snd); |
siddharthp | 0:da7e87322045 | 84 | wait(2); |
siddharthp | 0:da7e87322045 | 85 | wifi.RcvReply(rcv, 1000); |
siddharthp | 0:da7e87322045 | 86 | pc.printf("%s", rcv); |
siddharthp | 0:da7e87322045 | 87 | wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server |
siddharthp | 0:da7e87322045 | 88 | wifi.RcvReply(rcv, 1000); |
siddharthp | 0:da7e87322045 | 89 | pc.printf("%s", rcv); |
siddharthp | 0:da7e87322045 | 90 | } |