- #include <ESP8266WiFi.h> //esp8266wifi模块库
- #include <ESP8266WiFiMulti.h> //自动链接最强wifi库
- #include <DNSServer.h> //WiFiManager库要用的
- #include <PubSubClient.h> //MQTT协议中的库
- #include <Ticker.h> //时间记录库
- #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 Serial1
- ////////////////////////////////////////////////////////
- #define PRODUCT_ID "450667"//"400740"//Onenote产品名(需修改)
- #define API_KEY "OqwCIDaMoJUzNb1iG5TcZmLF1pU="//"CDglbagwPMllUURDk9mO=qKTozU="//产品密钥(需修改)
- #define DEVICE_ID "763751519" //设备名(需修改),与开发板没有关系,由平台生成的
- 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
- ESP8266WiFiMulti wifiMulti; //自动链接wifi
- DHTesp dht; //温度
- WiFiClient wifiClient; //
- PubSubClient mqttClient(wifiClient); //
- Ticker ticker; //用于记录时间
- //////////////////////////////////////////////
- int count=0,aa =0,bt=0,ii=0,nn=20,n=5,m=1; //一分钟发nn=20 //定义参数变量
- char msgJson[400];//msgJson数据长度
- char msgJson1[400];//msgJson数据长度
- char msg_buf[400];//
- char msg_buf1[400];//
- char dataTemplate1[] = "{"RH1":%.2f,"T1":%.2f,"RH2":%.2f,"T2":%.2f,"RH3":%.2f,"T3":%.2f,"RH4":%.2f,"T4":%.2f,"RH5":%.2f,"T5":%.2f,"RH6":%.2f,"T6":%.2f}"; //间隔指定时间的数据
- char dataTemplate[] = "{"rtRH1":%.2f,"rtT1":%.2f,"rtRH2":%.2f,"rtT2":%.2f,"rtRH3":%.2f,"rtT3":%.2f,"rtRH4":%.2f,"rtT4":%.2f,"rtRH5":%.2f,"rtT5":%.2f,"rtRH6":%.2f,"rtT6":%.2f,"m":%d}"; //rt代表real time 实时数据
- extern float T1 = 0.0, RH1 = 0.0,T2 = 0.0, RH2 = 0.0,T3 = 0.0, RH3 = 0.0,T4 = 0.0, RH4 = 0.0,T5 = 0.0, RH5 = 0.0,T6 = 0.0, RH6 = 0.0,T7 = 0.0, RH7 = 0.0,T8 = 0.0, RH8 = 0.0,T9 = 0.0, RH9 = 0.0;
- extern float T1sum = 0.0, RH1sum = 0.0,T2sum = 0.0, RH2sum = 0.0,T3sum = 0.0, RH3sum = 0.0,T4sum = 0.0, RH4sum = 0.0,T5sum = 0.0, RH5sum = 0.0,T6sum = 0.0, RH6sum = 0.0,T7sum = 0.0, RH7sum = 0.0,T8sum = 0.0, RH8sum = 0.0,T9sum = 0.0, RH9sum = 0.0;
- ////////////////////////////////////////////
- void setup() { //里面的函数只执行一次
- JXBSInit();
- debugSerial.begin(9600);
- ///////////////////////////////////////////////
- wifiMulti.addAP("CMCC-PbWq","9bf07201");//添加多个wifi网络
- wifiMulti.addAP("4G-UFI-19DE","seuseucyd");
- wifiMulti.addAP("esp8266","esp82661");
- wifiMulti.addAP("4G-UFI-5FD6","onenetesp8266");
- wifiMulti.addAP("MERCURY_6A1A","datalogging");
- wifiMulti.addAP("4G-CPE-6CAA","12345678");
- wifiMulti.addAP("YWGCJD-WIFI","12345678");
- /////////////////////////////////////////////////
- WiFi.mode(WIFI_STA); //设置wifi模式
- // dht.setup(D4, DHTesp::DHT11); //温湿度模块DHT11初始化,D4脚位
- connectWifi(); //链接Wifi函数
- mqttClient.setServer(mqttServer, mqttPort); //mqttServer是onenet地址,mqtt接口端口mqttPort
- mqttClient.setCallback(receiveCallback); //设置MQTT订阅回调,函数
- connectMQTTServer(); //链接MQTT服务器函数
- ticker.attach(1, tickerCount);
- }
- 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[topicString.length() + 1];
- 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[length] = '\0'; // 添加一个空值使其转化为字符数组
- String payload1=(char *)payload;
- m = atoi((char *)payload);//将字符数组转化为整型数组
- n=nn*m;
- analogWrite(LED2, 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[topicString.length() + 1];
- 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+ii;T1=temper_1*0.01;
- //---------------2
- readJXBS2();delay(500);JXBSListen2();RH2=RH_2*0.01; T2=temper_2*0.01;
- //---------------3
- readJXBS3();delay(500);JXBSListen3();RH3=RH_3*0.01; T3=temper_3*0.01;
- //---------------4
- readJXBS4();delay(500);JXBSListen4();RH4=RH_4*0.01; T4=temper_4*0.01;
- //---------------5
- readJXBS5();delay(500);JXBSListen5();RH5=RH_5*0.01; T5=temper_5*0.01;
- //---------------6
- readJXBS6();delay(500);JXBSListen6();RH6=RH_6*0.01; T6=temper_6*0.01;
- ////////////////////////////////////////////////////////////
- snprintf(msgJson, 400, dataTemplate,RH1,T1,RH2,T2,RH3,T3,RH4,T4,RH5,T5,RH6,T6,m);//将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
- int json_len = strlen(msgJson); //msgJson的长度
- msg_buf[0] = char(0x03); //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
- msg_buf[1] = char(json_len >> 8); //数据第二位是要发送的数据长度的高八位
- msg_buf[2] = char(json_len & 0xff); //数据第三位是要发送数据的长度的低八位
- memcpy(msg_buf + 3, msgJson, strlen(msgJson)); //从msg_buf的第四位开始,放入要传的数据msgJson
- msg_buf[3 + strlen(msgJson)] = 0; //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
- mqttClient.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
- }
- //////////////////////////////////////////////////
- snprintf(msgJson1, 400, dataTemplate1,RH1,T1,RH2,T2,RH3,T3,RH4,T4,RH5,T5,RH6,T6);
- int json_len1 = strlen(msgJson1); //msgJson的长度
- msg_buf1[0] = char(0x03); //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
- msg_buf1[1] = char(json_len1 >> 8); //数据第二位是要发送的数据长度的高八位
- msg_buf1[2] = char(json_len1 & 0xff); //数据第三位是要发送数据的长度的低八位
- memcpy(msg_buf1 + 3, msgJson1, strlen(msgJson1)); //从msg_buf的第四位开始,放入要传的数据msgJson
- msg_buf1[3 + strlen(msgJson1)] = 0; //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
- mqttClient.publish("$dp", (uint8_t *)msg_buf1, 3 + strlen(msgJson1)); //发送数据到主题$dp
-
- }
- void connectWifi() { //wifi连接函数
- int ii=0;
- while (wifiMulti.run() != 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地址
- const unsigned char readcmd1[8]={MYADRESS1,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B}; //读取温湿度指令
- const unsigned char readcmd2[8]={MYADRESS2,0x03,0x00,0x00,0x00,0x02,0xC4,0x38}; //读取温湿度指令
- const unsigned char readcmd3[8]={MYADRESS3,0x03,0x00,0x00,0x00,0x02,0xC5,0xE9}; //读取温湿度指令
- const unsigned char readcmd4[8]={MYADRESS4,0x03,0x00,0x00,0x00,0x02,0xC4,0x5E}; //读取温湿度指令
- const unsigned char readcmd5[8]={MYADRESS5,0x03,0x00,0x00,0x00,0x02,0xC5,0x8F}; //读取温湿度指令
- const unsigned char readcmd6[8]={MYADRESS6,0x03,0x00,0x00,0x00,0x02,0xC5,0xBC}; //读取温湿度指令
- const unsigned char readcmd7[8]={MYADRESS7,0x03,0x00,0x00,0x00,0x02,0xC4,0x6D}; //读取温湿度指令
- const unsigned char readcmd8[8]={MYADRESS8,0x03,0x00,0x00,0x00,0x02,0xC4,0x92}; //读取温湿度指令
- const unsigned char readcmd9[8]={MYADRESS9,0x03,0x00,0x00,0x00,0x02,0xC5,0x43}; //读取温湿度指令
- 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; //湿度
- void JXBSInit()
- {Serial.begin(9600);}
- unsigned char com_buf[48]; //接收数据缓存变量
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS1 && com_buf[1]==0x03){temper_1=com_buf[3]*256+com_buf[4];RH_1=com_buf[5]*256+com_buf[6];
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS2 && com_buf[1]==0x03){temper_2=com_buf[3]*256+com_buf[4];RH_2=com_buf[5]*256+com_buf[6];
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS3 && com_buf[1]==0x03){temper_3=com_buf[3]*256+com_buf[4];RH_3=com_buf[5]*256+com_buf[6];
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS4 && com_buf[1]==0x03){temper_4=com_buf[3]*256+com_buf[4];RH_4=com_buf[5]*256+com_buf[6];
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS5 && com_buf[1]==0x03){temper_5=com_buf[3]*256+com_buf[4];RH_5=com_buf[5]*256+com_buf[6];
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS6 && com_buf[1]==0x03){temper_6=com_buf[3]*256+com_buf[4];RH_6=com_buf[5]*256+com_buf[6];
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS7 && com_buf[1]==0x03){temper_7=com_buf[3]*256+com_buf[4];RH_7=com_buf[5]*256+com_buf[6];
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS8 && com_buf[1]==0x03){temper_8=com_buf[3]*256+com_buf[4];RH_8=com_buf[5]*256+com_buf[6];
- 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[com_num] = inChar;com_num++;delay(2);}
- if(com_buf[0]==MYADRESS9 && com_buf[1]==0x03){temper_9=com_buf[3]*256+com_buf[4];RH_9=com_buf[5]*256+com_buf[6];
- if (temper_9>32768){temper_9=temper_9-65536;}} }
复制代码
|