|
|
|
@ -23,15 +23,18 @@ float Temperature;
|
|
|
|
float Humidity;
|
|
|
|
float Humidity;
|
|
|
|
|
|
|
|
|
|
|
|
//定义WIFI信息
|
|
|
|
//定义WIFI信息
|
|
|
|
const char* ssid = "rehome"; // Enter SSID here
|
|
|
|
// const char* ssid = "rehome"; // Enter SSID here
|
|
|
|
const char* password = "Ruihong123"; //Enter Password here
|
|
|
|
// const char* password = "Ruihong123"; //Enter Password here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char* ssid = "mac_mini"; // Enter SSID here
|
|
|
|
|
|
|
|
const char* password = "452131wW"; //Enter Password here
|
|
|
|
|
|
|
|
|
|
|
|
//定义http服务端接口IP 端口 地址
|
|
|
|
//定义http服务端接口IP 端口 地址
|
|
|
|
const char* host = "192.168.2.210:8873";
|
|
|
|
// const char* host = "192.168.2.210:8873";
|
|
|
|
|
|
|
|
|
|
|
|
// MQTT代理配置
|
|
|
|
// MQTT代理配置
|
|
|
|
const char *mqtt_broker = "47.242.184.139";
|
|
|
|
const char *mqtt_broker = "47.242.184.139";
|
|
|
|
const char *topic = "/device/esp8266/003";
|
|
|
|
const char *topic = "/device/esp8266/005";
|
|
|
|
const char *mqtt_username = "admin";
|
|
|
|
const char *mqtt_username = "admin";
|
|
|
|
const char *mqtt_password = "publish452131wW452131wW$";
|
|
|
|
const char *mqtt_password = "publish452131wW452131wW$";
|
|
|
|
const int mqtt_port = 1883;
|
|
|
|
const int mqtt_port = 1883;
|
|
|
|
@ -87,6 +90,7 @@ void setup() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 发布和订阅
|
|
|
|
// 发布和订阅
|
|
|
|
//client.publish(topic, "hello emqx");
|
|
|
|
//client.publish(topic, "hello emqx");
|
|
|
|
|
|
|
|
// client.subscribe(topic01);
|
|
|
|
client.subscribe(topic);
|
|
|
|
client.subscribe(topic);
|
|
|
|
|
|
|
|
|
|
|
|
/* 2. oled屏幕初始化 */
|
|
|
|
/* 2. oled屏幕初始化 */
|
|
|
|
@ -116,32 +120,32 @@ void loop() {
|
|
|
|
Serial.println("℃");//发送湿度值
|
|
|
|
Serial.println("℃");//发送湿度值
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Check the current connection status
|
|
|
|
//Check the current connection status
|
|
|
|
Serial.println("------------http start-----------");
|
|
|
|
// Serial.println("------------http start-----------");
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
// if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
HTTPClient http;
|
|
|
|
// HTTPClient http;
|
|
|
|
WiFiClient wifiClient;
|
|
|
|
// 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(wifiClient,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
|
|
|
|
String payload = http.getString(); //Get the request response payload
|
|
|
|
// String payload = http.getString(); //Get the request response payload
|
|
|
|
Serial.println(httpCode);
|
|
|
|
// Serial.println(httpCode);
|
|
|
|
if (httpCode == 200) {
|
|
|
|
// if (httpCode == 200) {
|
|
|
|
Serial.println("http请求 发送温湿度成功");
|
|
|
|
// Serial.println("http请求 发送温湿度成功");
|
|
|
|
Serial.print("http响应结果:");
|
|
|
|
// Serial.print("http响应结果:");
|
|
|
|
Serial.println(payload);
|
|
|
|
// Serial.println(payload);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
} else {
|
|
|
|
// } else {
|
|
|
|
Serial.println("Error on sending request");
|
|
|
|
// Serial.println("Error on sending request");
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
http.end(); //Close connection
|
|
|
|
// http.end(); //Close connection
|
|
|
|
}else{
|
|
|
|
// }else{
|
|
|
|
Serial.println("Error in WiFi connection");
|
|
|
|
// Serial.println("Error in WiFi connection");
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
Serial.println("------------http end-----------");
|
|
|
|
// Serial.println("------------http end-----------");
|
|
|
|
//mqtt发布消息
|
|
|
|
//mqtt发布消息
|
|
|
|
Serial.println("------------mqtt publish start-----------");
|
|
|
|
Serial.println("------------mqtt publish start-----------");
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
@ -167,6 +171,7 @@ void loop() {
|
|
|
|
Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
|
|
|
|
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)) {
|
|
|
|
if(client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
|
|
|
|
Serial.println("Public emqx mqtt broker connected");
|
|
|
|
Serial.println("Public emqx mqtt broker connected");
|
|
|
|
|
|
|
|
// client.subscribe(topic01);
|
|
|
|
client.subscribe(topic);
|
|
|
|
client.subscribe(topic);
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
Serial.print("failed with state ");
|
|
|
|
Serial.print("failed with state ");
|
|
|
|
|