ESP32采集温湿度
本帖最后由 王大富 于 2021-10-6 22:07 编辑开发板网址:https://www.arduino.cn/package_esp32_dev_index.json
开发板库中找:esp32
注意esp32的wifi直接用通用的WiFi.h库
主程序
#include <WiFi.h>
#include <DNSServer.h> //WiFiManager库要用的
#include <PubSubClient.h> //MQTT协议中的库
#include <ArduinoJson.h> //调用Json数据库
#include <Wire.h>
#include "DHTesp.h" //温度DHT11模块函数库
#include "FastLED.h" //FastLED库用于调用随机函数库
#include <stdlib.h> //接收数据的转化
#include <stdio.h>
#include "JXBS.h" //土壤湿度库
#define debugSerial Serial
////////////////////////////////////////////////////////
#define PRODUCT_ID "450485" //Onenote产品名(需修改)
#define API_KEY "qcUpl=nNTa1G4ueHll=6tEpyzyw="//"CDglbagwPMllUURDk9mO=qKTozU="//产品密钥(需修改)
#define DEVICE_ID"757291565" //设备名(需修改),与开发板没有关系,由平台生成的
const char* mqttServer = "183.230.40.39"; //onenet地址(不修改)
const uint16_t mqttPort = 6002; //mqtt接口端口(不修改)
#define TOPIC "ceshitopic1" //订阅主题
////////////////////////////////////////////////////////////
#define LED1 D4
#define LED2 D3
#define LVDT_pin A0
DHTesp dht; //温度
WiFiClient wifiClient; //
PubSubClient mqttClient(wifiClient); //
//////////////////////////////////////////////
int count=0,aa =0,bt=0,ii=0,nn=5,n=20,m=1; //一分钟发nn=20 //定义参数变量
char msgJson;//msgJson数据长度
char msgJson1;//msgJson数据长度
char msg_buf;//
char msg_buf1;//
char dataTemplate0[] = "{\"rtRH1\":%.2f,\"rtT1\":%.2f,\"rtRH2\":%.2f,\"rtT2\":%.2f,\"rtRH3\":%.2f,\"rtT3\":%.2f,\"rtRH4\":%.2f,\"rtT4\":%.2f,\"rtRH5\":%.2f,\"rtT5\":%.2f,\"m\":%d}"; //rt代表real time 实时数据
char dataTemplate1[] = "{\"RH1\":%.2f,\"T1\":%.2f,\"RH2\":%.2f,\"T2\":%.2f,\"RH3\":%.2f,\"T3\":%.2f,\"RH4\":%.2f,\"T4\":%.2f,\"RH5\":%.2f,\"T5\":%.2f}"; //间隔指定时间的数据
extern floatTemp1=0.0, RH1=0.0,Temp2=0.0,RH2=0.0,Temp3=0.0,RH3=0.0,Temp4=0.0,RH4=0.0,Temp5=0.0,RH5=0.0;
///////////////////////////////////////////////
const char* ssid = "esp8266";
const char* password ="esp82661";
/////////////////////////////////////////////////
void setup() { //里面的函数只执行一次
JXBSInit();
debugSerial.begin(9600);
WiFi.mode(WIFI_STA); //设置wifi模式
WiFi.begin(ssid, password);
connectWifi(); //链接Wifi函数
mqttClient.setServer(mqttServer, mqttPort); //mqttServer是onenet地址,mqtt接口端口mqttPort
mqttClient.setCallback(receiveCallback); //设置MQTT订阅回调,函数
connectMQTTServer(); //链接MQTT服务器函数
}
void loop() { //里面的函数无限循环执行
if (mqttClient.connected()) { //如果开发板成功连接服务器
pubMQTTmsg(); //调用pubMQTTmsg()函数发送数据到云端
mqttClient.loop(); //调用回调函数,接收平台下发的数据
} else { //如果开发板未能成功连接服务器
connectMQTTServer(); //则尝试连接服务器
}
}
int tickerCount() {count++;}
void connectMQTTServer() { //通过MQTT协议链接Onenet
String clientId = DEVICE_ID;
String productId = PRODUCT_ID;
String apiKey = API_KEY;
if (mqttClient.connect(clientId.c_str(), productId.c_str(), apiKey.c_str())) {//连接MQTT服务器
subscribeTopic(); //订阅指定主题
} else {
delay(3000);
}
}
void subscribeTopic() {//订阅指定主题,主题名称以Taichi-Maker-Sub为前缀,后面添加设备的MAC地址
String topicString = "temperature"+ WiFi.macAddress();
char subTopic;
strcpy(subTopic, topicString.c_str()); //
if (mqttClient.subscribe(subTopic)){ //通过串口监视器输出是否成功订阅主题以及订阅的主题名称
} else {
}
}
///////////////////////////////////
void receiveCallback(char* topic, byte* payload, unsigned int length) {//反调函数用于接收平台下发的消息
for (int i = 0; i < length; i++){//按字符一个一个输出
}
payload = '\0'; // 添加一个空值使其转化为字符数组
String payload1=(char *)payload;
m = atoi((char *)payload);//将字符数组转化为整型数组
n=nn*m;
if (payload1 == "21"){//测试下发数据,如果收到的信息以“21”为开始
digitalWrite(LED1, HIGH);// 点亮LED。
} else if (payload1 == "20"){
digitalWrite(LED1, LOW);//熄灭LED。
}
}
////////////////////////////////////////
void pubMQTTmsg() { //onenet数据点上传系统主题
String topicString = "$dp";
char publishTopic;
strcpy(publishTopic, topicString.c_str()); //c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同,json数据转换为数组
/////////////////////////////////////////////////////////////
for(ii=1;ii<=n;ii++){
connectMQTTServer();mqttClient.loop(); //则尝试连接服务器
//////////////////////////////////////////////////////////////
////---------------1
readJXBS1();delay(500);JXBSListen1();RH1=RH_1*0.01; Temp1=temper_1*0.01; connectMQTTServer();mqttClient.loop();
//---------------2
readJXBS2();delay(500);JXBSListen2();RH2=RH_2*0.01; Temp2=temper_2*0.01; connectMQTTServer();mqttClient.loop();
//---------------3
readJXBS3();delay(500);JXBSListen3();RH3=RH_3*0.01; Temp3=temper_3*0.01; connectMQTTServer();mqttClient.loop();
//---------------4
readJXBS4();delay(500);JXBSListen4();RH4=RH_4*0.01; Temp4=temper_4*0.01;connectMQTTServer();mqttClient.loop();
//---------------5
readJXBS5();delay(500);JXBSListen5();RH5=RH_5*0.01; Temp5=temper_5*0.01; connectMQTTServer();mqttClient.loop();
//---------------6
//readJXBS6();delay(500);JXBSListen6();RH6=RH_6*0.01; T6=temper_6*0.01; connectMQTTServer();mqttClient.loop();
////---------------7
//readJXBS7();delay(500);JXBSListen7();RH7=RH_7*0.01; T7=temper_7*0.01; connectMQTTServer();mqttClient.loop();
////---------------8
//readJXBS8();delay(500);JXBSListen8();RH8=RH_8*0.01; T8=temper_8*0.01; connectMQTTServer();mqttClient.loop();
////---------------9
//readJXBS9();delay(500);JXBSListen9();RH9=RH_9*0.01; T9=temper_9*0.01; connectMQTTServer();mqttClient.loop();
////---------------10
//readJXBS10();delay(500);JXBSListen10();RH10=RH_10*0.01; T10=temper_10*0.01; connectMQTTServer();mqttClient.loop();
//////////////////////////////////////////////////////////////
snprintf(msgJson, 800, dataTemplate0,RH1,Temp1,RH2,Temp2,RH3,Temp3,RH4,Temp4,RH5,Temp5,m);//将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
int json_len = strlen(msgJson); //msgJson的长度
msg_buf = char(0x03); //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
msg_buf = char(json_len >> 8); //数据第二位是要发送的数据长度的高八位
msg_buf = char(json_len & 0xff); //数据第三位是要发送数据的长度的低八位
memcpy(msg_buf + 3, msgJson, strlen(msgJson)); //从msg_buf的第四位开始,放入要传的数据msgJson
msg_buf = 0; //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
mqttClient.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
}
//////////////////////////////////////////////////
snprintf(msgJson1, 800, dataTemplate1,RH1,Temp1,RH2,Temp2,RH3,Temp3,RH4,Temp4,RH5,Temp5);
int json_len1 = strlen(msgJson1); //msgJson的长度
msg_buf1 = char(0x03); //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
msg_buf1 = char(json_len1 >> 8); //数据第二位是要发送的数据长度的高八位
msg_buf1 = char(json_len1 & 0xff); //数据第三位是要发送数据的长度的低八位
memcpy(msg_buf1 + 3, msgJson1, strlen(msgJson1)); //从msg_buf的第四位开始,放入要传的数据msgJson
msg_buf1 = 0; //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
mqttClient.publish("$dp", (uint8_t *)msg_buf1, 3 + strlen(msgJson1)); //发送数据到主题$dp
}
void connectWifi() { //wifi连接函数
int ii=0;
while (WiFi.status()!= WL_CONNECTED) { //等待WiFi连接,成功连接后输出成功信息
delay(1000);
}
Serial.println('\n'); // WiFi连接成功后
Serial.print("Connected to "); // NodeMCU将通过串口监视器输出。
Serial.println(WiFi.SSID()); // 连接的WiFI名称
Serial.print("IP address:\t"); // 以及IP
Serial.println(WiFi.localIP()); // NodeMCU的IP地址
}子程序
#define MYADRESS1 0x01//485地址
#define MYADRESS2 0x02//485地址
#define MYADRESS3 0x03//485地址
#define MYADRESS4 0x04//485地址
#define MYADRESS5 0x05//485地址
#define MYADRESS6 0x06//485地址
#define MYADRESS7 0x07//485地址
#define MYADRESS8 0x08//485地址
#define MYADRESS9 0x09//485地址
#define MYADRESS10 0x0A//485地址
const unsigned charreadcmd1={MYADRESS1,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B}; //读取温湿度指令
const unsigned charreadcmd2={MYADRESS2,0x03,0x00,0x00,0x00,0x02,0xC4,0x38}; //读取温湿度指令
const unsigned charreadcmd3={MYADRESS3,0x03,0x00,0x00,0x00,0x02,0xC5,0xE9}; //读取温湿度指令
const unsigned charreadcmd4={MYADRESS4,0x03,0x00,0x00,0x00,0x02,0xC4,0x5E}; //读取温湿度指令
const unsigned charreadcmd5={MYADRESS5,0x03,0x00,0x00,0x00,0x02,0xC5,0x8F}; //读取温湿度指令
const unsigned charreadcmd6={MYADRESS6,0x03,0x00,0x00,0x00,0x02,0xC5,0xBC}; //读取温湿度指令
const unsigned charreadcmd7={MYADRESS7,0x03,0x00,0x00,0x00,0x02,0xC4,0x6D}; //读取温湿度指令
const unsigned charreadcmd8={MYADRESS8,0x03,0x00,0x00,0x00,0x02,0xC4,0x92}; //读取温湿度指令
const unsigned charreadcmd9={MYADRESS9,0x03,0x00,0x00,0x00,0x02,0xC5,0x43}; //读取温湿度指令
const unsigned charreadcmd10={MYADRESS10,0x03,0x00,0x00,0x00,0x02,0xC5,0x70}; //读取温湿度指令
unsigned int RH_1=0,temper_1=0,RH_2=0,temper_2=0,RH_3=0,temper_3=0,RH_4=0,temper_4=0,RH_5=0,temper_5=0;//湿度
unsigned int RH_6=0,temper_6=0,RH_7=0,temper_7=0,RH_8=0,temper_8=0,RH_9=0,temper_9=0,RH_10=0,temper_10=0;//湿度
void JXBSInit()
{Serial.begin(9600);}
unsigned char com_buf; //接收数据缓存变量
bool rec_flag=false;
bool rec_flag1=false;
bool receive_success_flag=false;
bool receive_success_flag1=false;
////////////////////////////////////////// 1 ////////////////////////////////////////
void readJXBS1(){Serial.write(readcmd1,8);}//发射数据
void JXBSListen1(){unsigned char com_num = 0;
while (Serial.available()){char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS1 && com_buf==0x03){temper_1=com_buf*256+com_buf;RH_1=com_buf*256+com_buf;
if (temper_1>32768){temper_1=temper_1-65536;}}}
/////////////////////////////////////////// 2 ////////////////////////////////////////
void readJXBS2(){Serial.write(readcmd2,8);}
void JXBSListen2(){unsigned char com_num = 0;
while (Serial.available()) {char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS2 && com_buf==0x03){temper_2=com_buf*256+com_buf;RH_2=com_buf*256+com_buf;
if (temper_2>32768){temper_2=temper_2-65536;}}}
/////////////////////////////////////////// 3 ////////////////////////////////////////
void readJXBS3(){Serial.write(readcmd3,8);}
void JXBSListen3(){unsigned char com_num = 0;
while (Serial.available()) {char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS3 && com_buf==0x03){temper_3=com_buf*256+com_buf;RH_3=com_buf*256+com_buf;
if (temper_3>32768){temper_3=temper_3-65536;}}}
/////////////////////////////////////////// 4 ////////////////////////////////////////
void readJXBS4(){Serial.write(readcmd4,8);}
void JXBSListen4(){unsigned char com_num = 0;
while (Serial.available()) {char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS4 && com_buf==0x03){temper_4=com_buf*256+com_buf;RH_4=com_buf*256+com_buf;
if (temper_4>32768){temper_4=temper_4-65536;}}}
/////////////////////////////////////////// 5 ////////////////////////////////////////
void readJXBS5(){Serial.write(readcmd5,8);}
void JXBSListen5(){unsigned char com_num = 0;
while (Serial.available()) {char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS5 && com_buf==0x03){temper_5=com_buf*256+com_buf;RH_5=com_buf*256+com_buf;
if (temper_5>32768){temper_5=temper_5-65536;}}}
/////////////////////////////////////////// 6 ////////////////////////////////////////
void readJXBS6(){Serial.write(readcmd6,8);}
void JXBSListen6(){unsigned char com_num = 0;
while (Serial.available()) {char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS6 && com_buf==0x03){temper_6=com_buf*256+com_buf;RH_6=com_buf*256+com_buf;
if (temper_6>32768){temper_6=temper_6-65536;}}}
/////////////////////////////////////////// 7 ////////////////////////////////////////
void readJXBS7(){Serial.write(readcmd7,8);}
void JXBSListen7(){unsigned char com_num = 0;
while (Serial.available()) {char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS7 && com_buf==0x03){temper_7=com_buf*256+com_buf;RH_7=com_buf*256+com_buf;
if (temper_7>32768){temper_7=temper_7-65536;}}}
/////////////////////////////////////////// 8 ////////////////////////////////////////
void readJXBS8(){Serial.write(readcmd8,8);}
void JXBSListen8(){unsigned char com_num = 0;
while (Serial.available()){char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS8 && com_buf==0x03){temper_8=com_buf*256+com_buf;RH_8=com_buf*256+com_buf;
if (temper_8>32768){temper_8=temper_8-65536;}}}
/////////////////////////////////////////// 9 ////////////////////////////////////////
void readJXBS9(){Serial.write(readcmd9,8);}
void JXBSListen9(){unsigned char com_num = 0;
while (Serial.available()){char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS9 && com_buf==0x03){temper_9=com_buf*256+com_buf;RH_9=com_buf*256+com_buf;
if (temper_9>32768){temper_9=temper_9-65536;}}}
///////////////////////////////////////////10///////////////////////////////////////
void readJXBS10(){Serial.write(readcmd10,8);}
void JXBSListen10(){unsigned char com_num = 0;
while (Serial.available()){char inChar = (char)Serial.read();com_buf = inChar;com_num++;delay(2);}
if(com_buf==MYADRESS10 && com_buf==0x03){temper_10=com_buf*256+com_buf;RH_10=com_buf*256+com_buf;
if (temper_10>32768){temper_10=temper_10-65536;}}}
页:
[1]