本帖最后由 王大富 于 2021-8-9 22:11 编辑
主程序
- #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 <WiFiManager.h> //WiFiManager库
- #include "DHTesp.h" //温度DHT11模块函数库
- #include "FastLED.h" //FastLED库用于调用随机函数库
- #include <stdlib.h> //接收数据的转化
- #include <stdio.h>
- #include "JXBS.h" //土壤湿度库
- #define debugSerial Serial1
- #define PRODUCT_ID ""//Onenote产品名(需修改)
- #define API_KEY ""//产品密钥(需修改)
- #define DEVICE_ID "" //设备名(需修改),与开发板没有关系,由平台生成的
- const char* mqttServer = "183.230.40.39"; //onenet地址(不修改)
- const uint16_t mqttPort = 6002; //mqtt接口端口(不修改)
- #define TOPIC "ceshitopic1" //订阅主题
- #define LED1 D2
- #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; //定义参数变量
- char msgJson[400];//msgJson数据长度
- char msg_buf[400];//
- char dataTemplate[] = "{"RH1":%.2f,"T1":%.2f,"RH2":%.2f,"T2":%.2f,"RH3":%.2f,"T3":%.2f,"RH4":%.2f,"T4":%.2f,"RH5":%.2f,"T5":%.2f, "RH6":%.2f,"T6":%.2f,"RH7":%.2f,"T7":%.2f,"RH8":%.2f,"T8":%.2f,"RH9":%.2f,"T9":%.2f}";
- 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;
- extern float T6 = 0.0, RH6 = 0.0,T7 = 0.0, RH7 = 0.0,T8 = 0.0, RH8 = 0.0,T9 = 0.0, RH9 = 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");
-
- 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()函数发送数据到云端
- delay(3000);
- 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服务器
- // Serial.println("MQTT Server Connected.");
- // Serial.println("Server Address: ");
- // Serial.println(mqttServer);
- // Serial.println("ClientId:");
- // Serial.println(clientId);
- subscribeTopic(); //订阅指定主题
- } else {
- //Serial.print("MQTT Server Connect Failed. Client State:");
- // Serial.println(mqttClient.state());
- 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)){ //通过串口监视器输出是否成功订阅主题以及订阅的主题名称
- //Serial.println("Subscrib Topic:");
- //Serial.println(subTopic);
- } else {
- //Serial.print("Subscribe Fail...");
- }
- }
- void receiveCallback(char* topic, byte* payload, unsigned int length) {//反调函数用于接收平台下发的消息
- //Serial.print("Message Received [");
- // Serial.print(topic);
- //Serial.print("]");
- for (int i = 0; i < length; i++){ //按字符一个一个输出
- // Serial.print((char)payload[i]);
- }
- //Serial.println("");
- // Serial.print("Message Length(Bytes)");
- // Serial.println(length);
- payload[length] = '\0'; // 添加一个空值使其转化为字符数组
- String payload1=(char *)payload;
- int aNumber = atoi((char *)payload);//将字符数组转化为整型数组
- //analogWrite(LED2, aNumber);
- if (payload1 == "21"){//测试下发数据,如果收到的信息以“1”为开始
- //digitalWrite(LED2, HIGH); // 点亮LED。
- // Serial.println("LED ON");
- } else if (payload1 == "20"){
- // digitalWrite(LED2, LOW); //熄灭LED。
- // Serial.println("LED OFF");
- }
- }
-
- void pubMQTTmsg() { //onenet数据点上传系统主题
- String topicString = "$dp";
- char publishTopic[topicString.length() + 1];
- strcpy(publishTopic, topicString.c_str()); //c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同,json数据转换为数组
- ////////////////////////////////////////////////////////////
- //---------------1
- readJXBS1();
- delay(500);
- JXBSListen1();
- RH1=RH_1*0.01;
- 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;
- //---------------7
- readJXBS7();
- delay(500);
- JXBSListen7();
- RH7=RH_7*0.01;
- T7=temper_7*0.01;
- //---------------8
- readJXBS8();
- delay(500);
- JXBSListen8();
- RH8=RH_8*0.01;
- T8=temper_8*0.01;
- //---------------9
- readJXBS9();
- delay(500);
- JXBSListen9();
- RH9=RH_9*0.01;
- T9=temper_9*0.01;
- ////////////////////////////////////////////////
-
- snprintf(msgJson, 400, dataTemplate,RH1,T1,RH2,T2,RH3,T3,RH4,T4,RH5,T5,RH6,T6,RH7,T7,RH8,T8,RH9,T9);//将模拟温湿度数据套入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准备好了
- //Serial.print("public message:");Serial.println(msgJson);//向串口发送订阅信息
- mqttClient.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
- // Serial.println(strlen(msgJson));
- }
- void connectWifi() { //wifi连接函数
- //Serial.print("Connecting...");
- //WiFiManager wifiManager;// 建立WiFiManager对象
- //wifiManager.resetSettings();// 清除ESP8266所存储的WiFi连接信息以便测试WiFiManager工作效果
- //wifiManager.autoConnect("AutoConnectAP");// 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称
- int ii=0;
- while (wifiMulti.run() != WL_CONNECTED) { //等待WiFi连接,成功连接后输出成功信息
- delay(1000);
- //Serial.print("ii++");Serial.print(" ");
- }
- //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;}
- }
- }
复制代码
|