diff --git a/wifi-mqtt/dht-blinker.ino b/wifi-mqtt/dht-blinker.ino deleted file mode 100644 index 0dbf7fb..0000000 --- a/wifi-mqtt/dht-blinker.ino +++ /dev/null @@ -1,60 +0,0 @@ -#define BLINKER_WIFI - -#include -#include - -char auth[] = "ecdc2388f56f"; -char ssid[] = "CMCC-AAA"; -char pswd[] = "123456789"; - -BlinkerNumber HUMI("humi"); -BlinkerNumber TEMP("temp"); - -#define DHTPIN D4 - -#define DHTTYPE DHT11 // DHT 11 - -DHT dht(DHTPIN, DHTTYPE); - -float humi_read = 0, temp_read = 0; - -void heartbeat() -{ - HUMI.print(humi_read); - TEMP.print(temp_read); -} - -void setup() -{ - Serial.begin(115200); - BLINKER_DEBUG.stream(Serial); - BLINKER_DEBUG.debugAll(); - pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, LOW); - - Blinker.begin(auth, ssid, pswd); - Blinker.attachHeartbeat(heartbeat); - dht.begin(); -} - -void loop() -{ - Blinker.run(); - - float h = dht.readHumidity(); - float t = dht.readTemperature(); - - if (isnan(h) || isnan(t)) - { - BLINKER_LOG("Failed to read from DHT sensor!"); - } - else - { - BLINKER_LOG("Humidity: ", h, " %"); - BLINKER_LOG("Temperature: ", t, " *C"); - humi_read = h; - temp_read = t; - } - - Blinker.delay(2000); -} diff --git a/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino b/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino new file mode 100644 index 0000000..4b5bc69 --- /dev/null +++ b/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino @@ -0,0 +1,146 @@ +#include //调用dht11驱动库 +#include +#include +#include + + +#define DHTPIN D4 //说明数据接口为8266开发板的D4口,也可以写为#define DHTPIN 2既8266芯片的IO口2 +#define DHTTYPE DHT11//说明使用的模块是DHT11 +DHT dht(DHTPIN,DHTTYPE);//DHT11初始化 +//定义浮点类型变量保存测量值 +float Temperature; +float Humidity; +//定义WIFI信息 +const char* ssid = "CU_A9TU"; // Enter SSID here +const char* password = "452131wW"; //Enter Password here + +const char* host = "192.168.3.5:8873"; + +ESP8266WebServer server(80); + +void setup() { + pinMode(D4, INPUT); + Serial.begin(115200); + delay(100); + dht.begin(); + Serial.println("Connecting to "); + Serial.println(ssid); + //connect to your local wi-fi network + WiFi.begin(ssid, password); + //check wi-fi is connected to wi-fi network + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected..!"); + Serial.print("Got IP: "); Serial.println(WiFi.localIP()); + + server.on("/", handle_OnConnect); + server.onNotFound(handle_NotFound); + + server.begin(); + Serial.println("HTTP server started"); + +} +void loop() { + + server.handleClient(); + + delay(10000); + if(server.getServer().status()==1){ + Temperature = dht.readTemperature(); // Gets the values of the temperature + Humidity = dht.readHumidity(); // Gets the values of the humidity + Serial.print("当前湿度:");//发送字符“当前湿度:” + Serial.print(Humidity);//发送湿度值 + Serial.println("%");//发送湿度值 + + Serial.print("当前温度:");//发送字符“当前温度:” + Serial.print(Temperature);//发送温度值 + Serial.println("℃");//发送湿度值 + } + //Check the current connection status + if (WiFi.status() == WL_CONNECTED) { + Temperature = dht.readTemperature(); // Gets the values of the temperature + Humidity = dht.readHumidity(); // Gets the values of the humidity + HTTPClient http; + WiFiClient client; + + String url = "http://"+ String(host) + "/web/temperature/esp/saveEspTemperature?temperature="+Temperature+"&humidity="+Humidity; + Serial.println("url:"+url); + // http.begin(client,"http://jsonplaceholder.typicode.com/todos/1"); + http.begin(client,url); //Specify the URL + int httpCode = http.GET(); //Send the request + //Check for the returning code + if (httpCode > 0) { //Check for the returning code + String payload = http.getString(); //Get the request response payload + Serial.println(httpCode); + if (httpCode == 200) { + Serial.println("http请求 发送温湿度成功"); + Serial.println(payload); + } + } else { + Serial.println("Error on sending request"); + } + http.end(); //Close connection + }else{ + Serial.println("Error in WiFi connection"); + } +} + +void handle_OnConnect() { + Temperature = dht.readTemperature(); // Gets the values of the temperature + Humidity = dht.readHumidity(); // Gets the values of the humidity + server.send(200, "text/html", SendHTML(Temperature,Humidity)); +} + +void handle_NotFound(){ + server.send(404, "text/plain", "Not found"); +} + +String SendHTML(float Temperaturestat,float Humiditystat){ + String ptr = " \n"; + ptr +=" \n"; + ptr +="\n"; + ptr +="\n"; + ptr +="温湿计\n"; + ptr +="\n"; + ptr +="\n"; + ptr +="\n"; + + ptr +="
\n"; + + ptr +="

温湿度检测系统

\n"; + ptr +="
\n"; + ptr +="
\n"; + ptr +="
温度:
\n"; + ptr +="
"; + ptr +=Temperaturestat; + ptr +="°C
\n"; + ptr +="
\n"; + ptr +="
\n"; + ptr +="
\n"; + ptr +="
湿度:
\n"; + ptr +="
"; + ptr +=Humiditystat; + ptr +="%
\n"; + ptr +="\n"; + + + ptr +="\n"; + ptr +="\n"; + ptr +="\n"; + return ptr; + }