add new project

main
wenfei 1 month ago
parent 346cce37a8
commit 65037a326c

@ -195,7 +195,7 @@ void loop() {
// String newValue = "Time since boot: " + String(millis()/1000);//
// 方法2: 使用字符串转换
String hexString = "0B0B6300";//温度测量
String hexString = "0B0B6301";//温度测量
// String hexString = "4854";//温湿度测量
// String hexString = "1F13";//位移测量
uint8_t data[hexString.length()/2];

@ -20,6 +20,7 @@ const char* mqtt_server = "47.242.184.139";
const int mqtt_port = 1883;
const char* mqtt_user = "admin"; // 如果需要认证
const char* mqtt_password = "publish452131wW452131wW$";
const char* topic = "/device/esp8266/003";
WiFiClient espClient;
PubSubClient client(espClient);
@ -93,8 +94,10 @@ void reconnect() {
// 订阅多个主题
// 订阅主题
client.subscribe("esp32/status");
client.subscribe("esp32/sensor/data");
//client.subscribe("esp32/status");
//client.subscribe("esp32/sensor/data");
client.subscribe(topic);
} else {
Serial.print("失败,错误代码: ");
@ -140,7 +143,10 @@ void readAndPublishSensorData() {
String sendStr = String(temperature)+" "+String(humidity);
//两个参数时 QoS 0 - 最多一次
client.publish("esp32/status", sendStr.c_str());
//client.publish("esp32/status", sendStr.c_str());
client.publish(topic, sendStr.c_str());
//topic
//三个参数时 QoS 1 - 至少一次
//client.publish("esp32/status", sendStr.c_str(),true);
//发布json格式温湿度数据

@ -0,0 +1,124 @@
#include <DHT.h>//调用dht11驱动库
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
//安装DHT11库 DHT sensor library
//安装mqtt客户端 PubSubClient 库
#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 = "rehome"; // Enter SSID here
// const char* password = "Ruihong123"; //Enter Password here
const char* ssid = "mac_mini"; // Enter SSID here
const char* password = "452131wW"; //Enter Password here
// MQTT代理配置
const char *mqtt_broker = "47.242.184.139";
const char *topic = "/device/esp8266/003";
// const char* topic01 = "/device/esp8266/006";
const char *mqtt_username = "admin";
const char *mqtt_password = "publish452131wW452131wW$";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
pinMode(D2, OUTPUT);
digitalWrite(D2,HIGH);//IO口2设置为高电平点亮数字2口LED
//digitalWrite(D2,LOW);//IO口2设置为高电平点亮数字2口LED
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());
//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(topic01);
client.subscribe(topic);
}
void loop() {
//mqtt
client.loop();
delay(10000);
Temperature = dht.readTemperature(); // Gets the values of the temperature
Humidity = dht.readHumidity(); // Gets the values of the humidity
//mqtt发布消息
Serial.println("------------mqtt publish start-----------");
if (WiFi.status() == WL_CONNECTED) {
if(client.connected()){
String sendStr = String(Temperature)+" "+String(Humidity);
String wendu = String("Tem:")+String(Temperature);
String shidu = String("Hum:")+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");
// client.subscribe(topic01);
client.subscribe(topic);
}else{
Serial.print("failed with state ");
Serial.print(client.state());
}
}
}
Serial.println("------------mqtt publish end-----------");
}
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------------");
}

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

@ -25,13 +25,17 @@ DHT dht(DHTPIN,DHTTYPE);//DHT11初始化
float Temperature;
float Humidity;
//定义WIFI信息
const char* ssid = "iStoreOS_2.4G"; // Enter SSID here
const char* password = "452131wW"; //Enter Password here
const char* host = "192.168.3.26:8873";
// const char* ssid = "iStoreOS_2.4G"; // Enter SSID here
// const char* password = "452131wW"; //Enter Password here
// 配置信息
const char* ssid = "rehome";
const char* password = "Ruihong123";
// 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 *topic = "/device/esp8266/003";
const char *mqtt_username = "admin";
const char *mqtt_password = "publish452131wW452131wW$";
const int mqtt_port = 1883;
@ -116,32 +120,32 @@ void loop() {
Serial.println("");//发送湿度值
}
//Check the current connection status
Serial.println("------------http start-----------");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
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(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
String payload = http.getString(); //Get the request response payload
Serial.println(httpCode);
if (httpCode == 200) {
Serial.println("http请求 发送温湿度成功");
Serial.print("http响应结果");
Serial.println(payload);
}
} else {
Serial.println("Error on sending request");
}
http.end(); //Close connection
}else{
Serial.println("Error in WiFi connection");
}
Serial.println("------------http end-----------");
// Serial.println("------------http start-----------");
// if (WiFi.status() == WL_CONNECTED) {
// HTTPClient http;
// 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(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
// String payload = http.getString(); //Get the request response payload
// Serial.println(httpCode);
// if (httpCode == 200) {
// Serial.println("http请求 发送温湿度成功");
// Serial.print("http响应结果");
// Serial.println(payload);
// }
// } else {
// Serial.println("Error on sending request");
// }
// http.end(); //Close connection
// }else{
// Serial.println("Error in WiFi connection");
// }
// Serial.println("------------http end-----------");
//mqtt发布消息
Serial.println("------------mqtt publish start-----------");
if (WiFi.status() == WL_CONNECTED) {

Loading…
Cancel
Save