完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
一、设计目标
二、ONENET平台搭建
三、硬件准备
[tr]USB转TTL模块ESP8266[/tr]
四、程序烧写
#include #include #include #include #include #define WIFI_DEBUG 1 //1:使用一键配网,其它值则使用默认无线账号密码 #define DHT11PIN 12 //设置DHT引脚为Pin 12 #define ONENET_DISCONNECTED 1 //已经断开 #define ONENET_CONNECTED 2 //已经连接上 #define ONENET_RECONNECT 3 //重连成功 #define VER "ESP8266_MQTT_V1.0" //版本号 const char* ssid = "********";//wifi账号 const char* password = "********";//wifi密码 /*OneNet*/ PubSubClient mqttClient; const char* mqttServer = "183.230.40.39";//mqtt服务器 const uint16_t mqttPort = 6002; //端口号 #define onenet_productId "********" //产品ID #define onenet_deviceId "********" //设备ID #define onenet_apiKey "********" //API_KEY int state; Ticker delayTimer; WiFiClient espClient; dht11 DHT11; /* 延时N秒 */ void delayNs(uint8_t m){ for(uint8_t index = 0;index delay(1000); ESP.wdtFeed(); } } /* 延时重启 */ void delayRestart(float t) { Serial.print("Restart after "); Serial.print(t); Serial.println("s"); delayTimer.attach(t, []() { Serial.println("rnRestart now!"); ESP.restart(); }); } /* 自动连接 */ bool autoConfig() { WiFi.begin(); for (int i = 0; i < 20; i++) { if (WiFi.status() == WL_CONNECTED) { Serial.println("AutoConfig Success"); Serial.printf("SSID:%srn", WiFi.SSID().c_str()); Serial.printf("PSW:%srn", WiFi.psk().c_str()); WiFi.printDiag(Serial); return true; } else { Serial.print("AutoConfig Waiting......"); Serial.println(WiFi.status()); delay(1000); } } Serial.println("AutoConfig Faild!" ); return false; } /* 一键配网 */ void smartConfig() { WiFi.mode(WIFI_STA); Serial.println("rnWait for Smartconfig"); WiFi.beginSmartConfig(); while (1) { Serial.print("."); digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); if (WiFi.smartConfigDone()) { Serial.println("SmartConfig Success"); Serial.printf("SSID:%srn", WiFi.SSID().c_str()); Serial.printf("PSW:%srn", WiFi.psk().c_str()); WiFi.setAutoConnect(true); // 设置自动连接 break; } delay(1000); // 这个地方一定要加延时,否则极易崩溃重启 } } /* 连接OneNet */ int connectToOneNetMqtt(){ int cnt = 0; while(!mqttClient.connected()){ ESP.wdtFeed(); cnt++; Serial.println("Connect to OneNet MQTT..."); if (mqttClient.connect(onenet_deviceId,onenet_productId,onenet_apiKey)) { Serial.println("connect success!"); return ONENET_RECONNECT; } else { Serial.print("connect fail!"); Serial.println(" try again in 5 seconds"); delayNs(5); } if(cnt >=10){ //只做10次连接到OneNet,连接不上重启8266 cnt = 0; delayRestart(1); } } return ONENET_CONNECTED; } /* 云端下发 */ 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); } Serial.println(); if ((char)payload[0] == '1') { digitalWrite(LED_BUILTIN, LOW); } else { digitalWrite(LED_BUILTIN, HIGH); } } /* 发布温度信息 */ void Temp_pubMQTTmsg(uint32_t data){ long lastMsg = 0; char msg[50]; char tmp[28]; char d[3]; snprintf(tmp,sizeof(tmp),"{"Temp":%d}",data); 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); mqttClient.publish("$dp", (uint8_t*)msg,streamLen+3,false); } /* 发布湿度信息 */ void Humi_pubMQTTmsg(uint32_t data){ long lastMsg = 0; char msg[50]; char tmp[28]; char d[3]; snprintf(tmp,sizeof(tmp),"{"Humi":%d}",data); 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); mqttClient.publish("$dp", (uint8_t*)msg,streamLen+3,false); } /* 初始化系统 */ void initSystem(){ int cnt = 0; Serial.begin (115200); Serial.println("rnrnStart ESP8266 MQTT"); Serial.print("Firmware Version:"); Serial.println(VER); Serial.print("SDK Version:"); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); Serial.println(ESP.getSdkVersion()); ESP.wdtEnable(5000); if(WIFI_DEBUG==1)//开启一键配网模式 { if (!autoConfig()) { Serial.println("Start smartConfig"); smartConfig(); } } else { WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); cnt++; Serial.print("."); if(cnt>=40){ cnt = 0; //重启系统 delayRestart(1); } } } Serial.print("WIFI Connect rn"); } /* 初始化ONENET通信 */ void initOneNetMqtt(){ mqttClient.setServer(mqttServer,mqttPort); mqttClient.setClient(espClient); mqttClient.setCallback(callback); } /* 初始化 */ void setup() { initSystem(); initOneNetMqtt(); } /* 主函数 */ void loop() { ESP.wdtFeed(); DHT11.read(DHT11PIN); state = connectToOneNetMqtt(); Serial.println(WiFi.status()); if(state == ONENET_RECONNECT){ mqttClient.loop(); } else if(state == ONENET_CONNECTED){ printf("temp:%d rn",DHT11.temperature); printf("humi:%d rn",DHT11.humidity); Temp_pubMQTTmsg(DHT11.temperature); Humi_pubMQTTmsg(DHT11.humidity); mqttClient.loop(); } delay(2000); }
//以下内容需进行修改 const char* ssid = "********";//wifi账号 const char* password = "********";//wifi密码 #define onenet_productId "********" //产品ID #define onenet_deviceId "********" //设备ID #define onenet_apiKey "********" //API_KEY
五、ONENET平台控件配置
至此本文介绍的内容就都结束了! |
|
|
|
只有小组成员才能发言,加入小组>>
imx6ull 和 lan8742 工作起来不正常, ping 老是丢包
2481 浏览 0 评论
3341 浏览 9 评论
3022 浏览 16 评论
3514 浏览 1 评论
9119 浏览 16 评论
1243浏览 3评论
636浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
627浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2374浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1936浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-1-24 17:40 , Processed in 0.947163 second(s), Total 46, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号