diff --git a/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino b/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino index 4b5bc69..91059fc 100644 --- a/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino +++ b/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino @@ -2,6 +2,7 @@ #include #include #include +#include #define DHTPIN D4 //说明数据接口为8266开发板的D4口,也可以写为#define DHTPIN 2既8266芯片的IO口2 @@ -14,7 +15,20 @@ float Humidity; const char* ssid = "CU_A9TU"; // Enter SSID here const char* password = "452131wW"; //Enter Password here -const char* host = "192.168.3.5:8873"; +const char* host = "192.168.3.26:8873"; + + +// MQTT代理 +const char *mqtt_broker = "47.242.184.139"; +const char *topic = "/device/esp8266/001"; +const char *mqtt_username = "admin"; +const char *mqtt_password = "publish452131wW452131wW$"; +const int mqtt_port = 1883; + + +WiFiClient espClient; +PubSubClient client(espClient); + ESP8266WebServer server(80); @@ -29,8 +43,8 @@ void setup() { WiFi.begin(ssid, password); //check wi-fi is connected to wi-fi network while (WiFi.status() != WL_CONNECTED) { - delay(1000); - Serial.print("."); + delay(1000); + Serial.print("."); } Serial.println(""); Serial.println("WiFi connected..!"); @@ -42,15 +56,36 @@ void setup() { server.begin(); Serial.println("HTTP server started"); + //mqtt init + //连接到mqtt代理 + client.setServer(mqtt_broker, mqtt_port); + //client.setCallback(callback); + while (!client.connected()) { + String client_id = "esp8266-client-"; + client_id += String(WiFi.macAddress()); + Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str()); + if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) { + Serial.println("Public emqx mqtt broker connected"); + } else { + Serial.print("failed with state "); + Serial.print(client.state()); + delay(2000); + } + } + // 发布和订阅 + //client.publish(topic, "hello emqx"); + client.subscribe(topic); } void loop() { - + //http server.handleClient(); + //mqtt + client.loop(); delay(10000); + Temperature = dht.readTemperature(); // Gets the values of the temperature + Humidity = dht.readHumidity(); // Gets the values of the humidity 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("%");//发送湿度值 @@ -60,16 +95,14 @@ void loop() { Serial.println("℃");//发送湿度值 } //Check the current connection status + Serial.println("------------http start-----------"); 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; - + WiFiClient wifiClient; 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 + http.begin(wifiClient,url); //Specify the URL int httpCode = http.GET(); //Send the request //Check for the returning code if (httpCode > 0) { //Check for the returning code @@ -77,6 +110,7 @@ void loop() { Serial.println(httpCode); if (httpCode == 200) { Serial.println("http请求 发送温湿度成功"); + Serial.print("http响应结果:"); Serial.println(payload); } } else { @@ -86,6 +120,28 @@ void loop() { }else{ Serial.println("Error in WiFi connection"); } + Serial.println("------------http end-----------"); + //mqtt发布消息 + Serial.println("------------mqtt publish start-----------"); + if (WiFi.status() == WL_CONNECTED) { + if(client.connected()){ + String sendStr = String(Temperature)+" "+String(Humidity); + Serial.println(sendStr); + client.publish(topic,sendStr.c_str()); + }else{ + Serial.println(client.state()); + String client_id = "esp8266-client-"; + client_id += String(WiFi.macAddress()); + Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str()); + if(client.connect(client_id.c_str(), mqtt_username, mqtt_password)) { + Serial.println("Public emqx mqtt broker connected"); + }else{ + Serial.print("failed with state "); + Serial.print(client.state()); + } + } + } + Serial.println("------------mqtt publish end-----------"); } void handle_OnConnect() { @@ -119,28 +175,37 @@ String SendHTML(float Temperaturestat,float Humiditystat){ 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; - } + 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; +} + +void callback(char *topic, byte *payload, unsigned int length) { + Serial.println("-----------callback------------"); + Serial.print("Message arrived in topic:"); + Serial.println(topic); + Serial.print("Message:"); + for (int i = 0; i < length; i++) { + Serial.print((char) payload[i]); + } + Serial.println(); + Serial.println("-----------callback------------"); +} +