https://item.taobao.com/item.htm?spm=a230r.1.14.37.18fc571duVy4WM&id=677030943784&ns=1&abbucket=6#detail
另一种参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <WiFi.h> #include "DHT.h" #include "PubSubClient.h" #include "Ticker.h" #define DHTPIN 21 / / ` #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHT11); int god = 0 ; const int led = 15 ; / / 灯的接口 const char * ssid = "yaoyao" ; / / wifi名称 const char * password = "love123456" ; / / wifi密码 const char * mqtt_server = "183.230.40.96" ; / / onenet 的 IP地址 const int port = 6002 ; / / 端口号 #define mqtt_devid "1056007204" //设备ID #define mqtt_pubid "578961" //产品ID / / 鉴权信息 #define mqtt_password "dada" //鉴权信息 / / #define onenet_apiKey "tnTFSvTHqR0Zxt=CU7AtDZ4Zr60=" //产品API_KEY WiFiClient espClient; / / 创建一个WIFI连接客户端 PubSubClient client(espClient); / / 创建一个PubSub客户端, 传入创建的WIFI客户端 Ticker tim1; / / 定时器,用来循环上传数据 void setupWifi(){ WiFi.begin(ssid, password); while (WiFi.status() ! = WL_CONNECTED) { delay( 500 ); Serial. print ( "正在连接中----\n" ); } Serial. print ( "WIFI已连接\n" ); Serial.println(WiFi.localIP()); } / * 发布多个传感器信息 String sendmsg 打包数据 { "senseor1" : 1213 , "senseor2" : 313.3 , "senseor3" : 543.09 , "senseor4" : 645.0 } 注意 ” 转义字符串 \“ * / void pubMQTTmsg_more_name_value(String sendmsg_in) { long lastMsg = 0 ; int sendlen = 800 ; / / 28 / / 数组大小 char msg[sendlen + 22 ]; / / 数组大小 char tmp[sendlen]; char d[ 3 ]; String sendmsg = sendmsg_in; sendmsg.toCharArray(tmp, sendlen); / / Serial.println(tmp); uint16_t streamLen = strlen(tmp); d[ 0 ] = '\x03' ; d[ 1 ] = (streamLen >> 8 ); d[ 2 ] = (streamLen & 0xFF ); snprintf(msg,sizeof(msg), "%c%c%c%s" ,d[ 0 ],d[ 1 ],d[ 2 ],tmp); client.publish( "$dp" , (uint8_t * )msg,streamLen + 3 ,false); / / 发送数据到主题 / / delay( 500 ); } / / 收到主题下发的回调, 注意这个回调要实现三个形参 1 :topic 主题, 2 : payload: 传递过来的信息 3 : length: 长度 void callback(char * topic, byte * payload, unsigned int length) { Serial. print ( "Message arrived [" ); Serial. print (topic); Serial. print ( "] " ); / / for ( int i = 0 ; i < length; i + + ) { / / Serial. print ((char)payload[i]); / / } Serial.println(); if ((char)payload[ 0 ] = = '0' ) { digitalWrite(led, LOW); / / god = 0 ; } if ((char)payload[ 0 ] = = '1' ) { digitalWrite(led, HIGH); / / god = 1 ; } else {} } void clientReconnect() { while (!client.connected()) / / 再重连客户端 { Serial. print ( "reconnect MQTT..." ); if (client.connect(mqtt_devid, mqtt_pubid, mqtt_password)) { Serial.println( "connected" ); } else { Serial. print ( "failed" ); Serial. print (client.state()); Serial. print ( "try again in 5 sec" ); delay( 5000 ); } } } void setup() { Serial.begin( 115200 ); pinMode(led,OUTPUT); / / 输出 setupWifi(); / / 调用函数连接WIFI Serial.println(F( "DHT11 test!" )); dht.begin(); client.setServer(mqtt_server, port); / / 设置客户端连接的服务器,连接Onenet服务器, 使用 6002 端口 client.connect(mqtt_devid, mqtt_pubid, mqtt_password); / / 客户端连接到指定的产品的指定设备.同时输入鉴权信息 if (client.connected()) { Serial.println( "OneNet is connected!" ); / / 判断以下是不是连好了. } / / 设置好客户端收到信息是的回调 / / client.setCallback(callback); / / 订阅命令下发主题 / / tim1.attach( 5 , sendTempAndHumi); / / 定时每 5 秒调用一次发送数据函数sendTempAndHumi } void loop(){ / / int h = int (dht.readHumidity()); / / int t = int (dht.readTemperature()); / / if (isnan(h) || isnan(t) ) { / / / / h = 0 ; / / t = 0 ; / / Serial.println( "error t h" ); / / / / } / / Serial.println(h); if (!WiFi.isConnected()) / / 先看WIFI是否还在连接 { setupWifi(); } if (!client.connected()) / / 如果客户端没连接ONENET, 重新连接 { clientReconnect(); delay( 100 ); } else { String msg = String(" ")+" {" + "\"空气质量\":" + String( 21 ) + "," + "\"湿度\":" + String( 21 ) + "," + "\"温度\":" + String( 21 ) + "}" ; pubMQTTmsg_more_name_value(msg); client.loop(); / / 客户端循环检测 Serial. print ( "public the data:" ); Serial.println(msg); delay( 3000 ); } } |
综合例程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | #include <WiFi.h> #include "PubSubClient.h" #include "Ticker.h" int god = 0 ; const int led = 15 ; / / 灯的接口 const int pin_mq = 34 ; #include "DHT.h" #define DHTPIN 16 / / ` #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHT11); const char * ssid = "yaoyao" ; / / wifi名称 const char * password = "love123456" ; / / wifi密码 const char * mqtt_server = "183.230.40.96" ; / / onenet 的 IP地址 const int port = 6002 ; / / 端口号 #define mqtt_devid "1056007204" //设备ID #define mqtt_pubid "578961" //产品ID / / 鉴权信息 #define mqtt_password "dada" //鉴权信息 / / #define onenet_apiKey "tnTFSvTHqR0Zxt=CU7AtDZ4Zr60=" //产品API_KEY WiFiClient espClient; / / 创建一个WIFI连接客户端 PubSubClient client(espClient); / / 创建一个PubSub客户端, 传入创建的WIFI客户端 Ticker tim1; / / 定时器,用来循环上传数据 void setupWifi(){ WiFi.begin(ssid, password); while (WiFi.status() ! = WL_CONNECTED) { delay( 500 ); Serial. print ( "正在连接中----\n" ); } Serial. print ( "WIFI已连接\n" ); Serial.println(WiFi.localIP()); } / * 发布多个传感器信息 String sendmsg 打包数据 { "senseor1" : 1213 , "senseor2" : 313.3 , "senseor3" : 543.09 , "senseor4" : 645.0 } 注意 ” 转义字符串 \“ * / void pubMQTTmsg_more_name_value(String sendmsg_in) { long lastMsg = 0 ; int sendlen = 800 ; / / 28 / / 数组大小 char msg[sendlen + 22 ]; / / 数组大小 char tmp[sendlen]; char d[ 3 ]; String sendmsg = sendmsg_in; sendmsg.toCharArray(tmp, sendlen); / / Serial.println(tmp); uint16_t streamLen = strlen(tmp); d[ 0 ] = '\x03' ; d[ 1 ] = (streamLen >> 8 ); d[ 2 ] = (streamLen & 0xFF ); snprintf(msg,sizeof(msg), "%c%c%c%s" ,d[ 0 ],d[ 1 ],d[ 2 ],tmp); client.publish( "$dp" , (uint8_t * )msg,streamLen + 3 ,false); / / 发送数据到主题 / / delay( 500 ); } / / 收到主题下发的回调, 注意这个回调要实现三个形参 1 :topic 主题, 2 : payload: 传递过来的信息 3 : length: 长度 void callback(char * topic, byte * payload, unsigned int length) { Serial. print ( "Message arrived [" ); Serial. print (topic); Serial. print ( "] " ); / / for ( int i = 0 ; i < length; i + + ) { / / Serial. print ((char)payload[i]); / / } Serial.println(); if ((char)payload[ 0 ] = = '0' ) { digitalWrite(led, LOW); / / god = 0 ; } if ((char)payload[ 0 ] = = '1' ) { digitalWrite(led, HIGH); / / god = 1 ; } else {} } void clientReconnect() { while (!client.connected()) / / 再重连客户端 { Serial. print ( "reconnect MQTT..." ); if (client.connect(mqtt_devid, mqtt_pubid, mqtt_password)) { Serial.println( "connected" ); } else { Serial. print ( "failed" ); Serial. print (client.state()); Serial. print ( "try again in 5 sec" ); delay( 5000 ); } } } void setup() { Serial.begin( 115200 ); pinMode(led,OUTPUT); / / 输出 / / pinMode(pin_mq, INPUT ); / / 输出 / / set the resolution to 12 bits ( 0 - 4096 ) analogReadResolution( 12 ); setupWifi(); / / 调用函数连接WIFI Serial.println(F( "DHT11 test!" )); dht.begin(); client.setServer(mqtt_server, port); / / 设置客户端连接的服务器,连接Onenet服务器, 使用 6002 端口 client.connect(mqtt_devid, mqtt_pubid, mqtt_password); / / 客户端连接到指定的产品的指定设备.同时输入鉴权信息 if (client.connected()) { Serial.println( "OneNet is connected!" ); / / 判断以下是不是连好了. } / / 设置好客户端收到信息是的回调 / / client.setCallback(callback); / / 订阅命令下发主题 / / tim1.attach( 5 , sendTempAndHumi); / / 定时每 5 秒调用一次发送数据函数sendTempAndHumi } void loop(){ / / 读取温湿度 float h = float (dht.readHumidity()); float t = float (dht.readTemperature()); if (isnan(h) || isnan(t) ) { h = 0 ; t = 0 ; Serial.println( "error t h" ); } / / String showms = String(h) + " " + String(t); / / Serial.println(showms); / / 读取气体 int mq_value = analogRead(pin_mq); if (!WiFi.isConnected()) / / 先看WIFI是否还在连接 { setupWifi(); } if (!client.connected()) / / 如果客户端没连接ONENET, 重新连接 { clientReconnect(); delay( 100 ); } else { String msg = String(" ")+" {" + "\"空气质量\":" + String(mq_value) + "," + "\"湿度\":" + String(h) + "," + "\"温度\":" + String(t) + "}" ; pubMQTTmsg_more_name_value(msg); client.loop(); / / 客户端循环检测 Serial. print ( "public the data:" ); Serial.println(msg); delay( 3000 ); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!