|
|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
|
#include <ESP8266WebServer.h>
|
|
|
|
|
#include <ESP8266HTTPClient.h>
|
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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 +="</style>\n";
|
|
|
|
|
ptr +="</head>\n";
|
|
|
|
|
ptr +="<body>\n";
|
|
|
|
|
|
|
|
|
|
ptr +="<div id=\"webpage\">\n";
|
|
|
|
|
|
|
|
|
|
ptr +="<h1>温湿度检测系统</h1>\n";
|
|
|
|
|
ptr +="<div class=\"data\">\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="<div class=\"side-by-side temperature-text\">温度:</div>\n";
|
|
|
|
|
ptr +="<div class=\"side-by-side temperature\">";
|
|
|
|
|
ptr +=Temperaturestat;
|
|
|
|
|
ptr +="<span class=\"superscript\">°C</span></div>\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="<div class=\"data\">\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="<div class=\"side-by-side humidity-text\">湿度:</div>\n";
|
|
|
|
|
ptr +="<div class=\"side-by-side humidity\">";
|
|
|
|
|
ptr +=Humiditystat;
|
|
|
|
|
ptr +="<span class=\"superscript\">%</span></div>\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="</body>\n";
|
|
|
|
|
ptr +="</html>\n";
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
ptr +="<div id=\"webpage\">\n";
|
|
|
|
|
ptr +="<h1>温湿度检测系统</h1>\n";
|
|
|
|
|
ptr +="<div class=\"data\">\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="<div class=\"side-by-side temperature-text\">温度:</div>\n";
|
|
|
|
|
ptr +="<div class=\"side-by-side temperature\">";
|
|
|
|
|
ptr +=Temperaturestat;
|
|
|
|
|
ptr +="<span class=\"superscript\">°C</span></div>\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="<div class=\"data\">\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="<div class=\"side-by-side humidity-text\">湿度:</div>\n";
|
|
|
|
|
ptr +="<div class=\"side-by-side humidity\">";
|
|
|
|
|
ptr +=Humiditystat;
|
|
|
|
|
ptr +="<span class=\"superscript\">%</span></div>\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="</div>\n";
|
|
|
|
|
ptr +="</body>\n";
|
|
|
|
|
ptr +="</html>\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------------");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|