mqtt finish

main
修改密码漏洞修复完成 4 months ago
parent e8697ebcd9
commit bccf1a1b23

@ -2,6 +2,7 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
#define DHTPIN D4 //说明数据接口为8266开发板的D4口也可以写为#define DHTPIN 2既8266芯片的IO口2 #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* ssid = "CU_A9TU"; // Enter SSID here
const char* password = "452131wW"; //Enter Password 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); ESP8266WebServer server(80);
@ -29,8 +43,8 @@ void setup() {
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
//check wi-fi is connected to wi-fi network //check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(1000); delay(1000);
Serial.print("."); Serial.print(".");
} }
Serial.println(""); Serial.println("");
Serial.println("WiFi connected..!"); Serial.println("WiFi connected..!");
@ -42,15 +56,36 @@ void setup() {
server.begin(); server.begin();
Serial.println("HTTP server started"); 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() { void loop() {
//http
server.handleClient(); server.handleClient();
//mqtt
client.loop();
delay(10000); 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){ 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("当前湿度:");//发送字符“当前湿度:”
Serial.print(Humidity);//发送湿度值 Serial.print(Humidity);//发送湿度值
Serial.println("%");//发送湿度值 Serial.println("%");//发送湿度值
@ -60,16 +95,14 @@ void loop() {
Serial.println("");//发送湿度值 Serial.println("");//发送湿度值
} }
//Check the current connection status //Check the current connection status
Serial.println("------------http start-----------");
if (WiFi.status() == WL_CONNECTED) { 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; HTTPClient http;
WiFiClient client; WiFiClient wifiClient;
String url = "http://"+ String(host) + "/web/temperature/esp/saveEspTemperature?temperature="+Temperature+"&humidity="+Humidity; String url = "http://"+ String(host) + "/web/temperature/esp/saveEspTemperature?temperature="+Temperature+"&humidity="+Humidity;
Serial.println("url:"+url); Serial.println("url:"+url);
// http.begin(client,"http://jsonplaceholder.typicode.com/todos/1"); // 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 int httpCode = http.GET(); //Send the request
//Check for the returning code //Check for the returning code
if (httpCode > 0) { //Check for the returning code if (httpCode > 0) { //Check for the returning code
@ -77,6 +110,7 @@ void loop() {
Serial.println(httpCode); Serial.println(httpCode);
if (httpCode == 200) { if (httpCode == 200) {
Serial.println("http请求 发送温湿度成功"); Serial.println("http请求 发送温湿度成功");
Serial.print("http响应结果");
Serial.println(payload); Serial.println(payload);
} }
} else { } else {
@ -86,6 +120,28 @@ void loop() {
}else{ }else{
Serial.println("Error in WiFi connection"); 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() { void handle_OnConnect() {
@ -119,28 +175,37 @@ String SendHTML(float Temperaturestat,float Humiditystat){
ptr +="</style>\n"; ptr +="</style>\n";
ptr +="</head>\n"; ptr +="</head>\n";
ptr +="<body>\n"; ptr +="<body>\n";
ptr +="<div id=\"webpage\">\n";
ptr +="<div id=\"webpage\">\n"; ptr +="<h1>温湿度检测系统</h1>\n";
ptr +="<div class=\"data\">\n";
ptr +="<h1>温湿度检测系统</h1>\n"; ptr +="</div>\n";
ptr +="<div class=\"data\">\n"; ptr +="<div class=\"side-by-side temperature-text\">温度:</div>\n";
ptr +="</div>\n"; ptr +="<div class=\"side-by-side temperature\">";
ptr +="<div class=\"side-by-side temperature-text\">温度:</div>\n"; ptr +=Temperaturestat;
ptr +="<div class=\"side-by-side temperature\">"; ptr +="<span class=\"superscript\">°C</span></div>\n";
ptr +=Temperaturestat; ptr +="</div>\n";
ptr +="<span class=\"superscript\">°C</span></div>\n"; ptr +="<div class=\"data\">\n";
ptr +="</div>\n"; ptr +="</div>\n";
ptr +="<div class=\"data\">\n"; ptr +="<div class=\"side-by-side humidity-text\">湿度:</div>\n";
ptr +="</div>\n"; ptr +="<div class=\"side-by-side humidity\">";
ptr +="<div class=\"side-by-side humidity-text\">湿度:</div>\n"; ptr +=Humiditystat;
ptr +="<div class=\"side-by-side humidity\">"; ptr +="<span class=\"superscript\">%</span></div>\n";
ptr +=Humiditystat; ptr +="</div>\n";
ptr +="<span class=\"superscript\">%</span></div>\n"; ptr +="</div>\n";
ptr +="</div>\n"; ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
ptr +="</div>\n"; }
ptr +="</body>\n";
ptr +="</html>\n"; void callback(char *topic, byte *payload, unsigned int length) {
return ptr; 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------------");
}

Loading…
Cancel
Save