ssc-wifi application
Dependencies: DHT esp8266-driver
Revision 4:7161fd64b2ef, committed 2017-11-07
- Comitter:
- drrrnino7
- Date:
- Tue Nov 07 03:57:05 2017 +0000
- Parent:
- 3:97041d02648e
- Commit message:
- Add DHT Sensor logic
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed_app.json | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Nov 06 06:47:07 2017 +0000 +++ b/main.cpp Tue Nov 07 03:57:05 2017 +0000 @@ -13,9 +13,14 @@ #define TW_TCP_TEST_SERVER_IP "1.221.6.243" #define TW_TCP_TEST_SERVER_PORT 41112 +DigitalOut led(PA_5); + #include "ESP8266Interface.h" ESP8266Interface wifi(PC_4, PC_5); +#include "DHT.h" +DHT sensor(PC_2, DHT11); + const char *sec2str(nsapi_security_t sec) { switch (sec) { @@ -87,26 +92,35 @@ tr_info("Successfully created WiFi TCP Socket"); while(1) { + int err; float humid = 0.0f; float temp = 0.0f; char json_buf[100]; - //TODO: DHT humid & temp read - - tr_debug("Humidity = %f%% Temperature = %fC", humid, temp); - sprintf(json_buf, "{\r\n\t\"temp\":%f,\r\n\t\"humid\":%f\r\n}\r\n", temp, humid); - - if((ret = tcp_socket.connect(TW_TCP_TEST_SERVER_IP, TW_TCP_TEST_SERVER_PORT)) < 0) { - tr_err("Cannot connect to TCP Server"); + //Read Humid, Temp + err = sensor.readData(); + if (err == 0) { + humid = sensor.ReadHumidity(); + temp = sensor.ReadTemperature(CELCIUS); + + tr_info("Humidity = %4.2f%% Temperature = %4.2fC", humid, temp); + sprintf(json_buf, "{\r\n\t\"id\":%d,\r\n\t\"temp\":%f,\r\n\t\"humid\":%f\r\n}\r\n", TW_TCP_TEST_DEVICE_NO, temp, humid); + + if((ret = tcp_socket.connect(TW_TCP_TEST_SERVER_IP, TW_TCP_TEST_SERVER_PORT)) < 0) { + tr_err("Cannot connect to TCP Server"); + } else { + if((ret = tcp_socket.send(json_buf, strlen(json_buf))) < 0) { + tr_err("Cannot send to TCP Server"); + } else { + tr_info("Sucessfully send TCP packet via WiFi"); + } + } } else { - if((ret = tcp_socket.send(json_buf, strlen(json_buf))) < 0) { - tr_err("Cannot send to TCP Server"); - } else { - tr_info("Sucessfully send TCP packet via WiFi"); - } + tr_err("Cannot Read Humid & Temp"); } - wait(1); + led = !led; + wait(5); } } \ No newline at end of file
--- a/mbed_app.json Mon Nov 06 06:47:07 2017 +0000 +++ b/mbed_app.json Tue Nov 07 03:57:05 2017 +0000 @@ -14,7 +14,7 @@ }, "wifi-password": { "help": "WiFi Password", - "value": "\"tw123$%^\"" + "value": "\"^^tElewORk$\"" } } } \ No newline at end of file