完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
接上一个项目
【DFRobot tinkerNode NB-IoT 物联网开发板免费试用】+第一个项目 在阿里云平台上IoT初学者开发套件中 案例4 温湿度监测 在Arduino IDE中 点击文件->示例->DFRobot_IoTAliyun->Aliyun->SmartDHT11,如下图所示。 增加 #define BEDROOD_LIGHT D4 上次项目的灯控制。 与上次一样在阿里云平台上创建设备,根据下载Excel文件更改相应配置 选择正确板和com端口,验证发现无"DFRobot_DHT11.h"文件, 在https://wiki.dfrobot.com.cn/_SKU_DFR0067_DHT11%E6%95%B0%E5%AD%97%E6%B8%A9%E6%B9%BF%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8V2网上可下载201709141149593byvtx.zip文件中包含DHT11文件包 把解包文件拷贝到ArduinolibrariesDHT11文件夹中 变更后的配置: #include //增加原来的灯控制set部分 pinMode(BEDROOD_LIGHT,OUTPUT); pinMode(DHT11_PIN,INPUT); loop()中增加 if(DHT.temperature > 31){ openLight(); }else closeLight(); 作为超过31度指示并执行的程序。 将原来程序半分钟一次变为1分钟(由60变为120) 程序如下: DFRobot_Iot myIot; WiFiClient espClient; PubSubClient client(espClient); dht11 DHT;//DFRobot_DHT11 DHT; static void openLight(){//led控制 digitalWrite(BEDROOD_LIGHT, HIGH); } static void closeLight(){//led控制 digitalWrite(BEDROOD_LIGHT, LOW); } void connectWiFi(){ Serial.print("Connecting to "); Serial.println(WIFI_SSID); WiFi.begin(WIFI_SSID,WIFI_PASSWORD); while(WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.print("IP Adderss: "); Serial.println(WiFi.localIP()); } void callback(char * topic, byte * payload, unsigned int len){ Serial.print("Recevice ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < len; i++){ Serial.print((char)payload); } Serial.println(); //增加原来的灯控制set部分 StaticJsonBuffer<300> jsonBuffer; JsonObject& root = jsonBuffer.parseObject((const char *)payload); if(!root.success()){ Serial.println("parseObject() failed"); return; } uint8_t SocketStatus1,ledStatus ;//定义局部变量 const char* ledtval = root["params"][ledIdentifier];//判断Identifier是否存在,不存在返回NULL if( ledtval != NULL ) { // Serial.println("------------"); ledStatus = root["params"][ledIdentifier]; Serial.print("ledStatus="); Serial.print(ledStatus); if(ledStatus == 1){ /*打开灯*/ openLight(); }else{ /*关掉灯*/ closeLight(); } } String tempMsegled = "{"id":"+ClientId+","params":{""+ledIdentifier+"":"+(String)ledStatus+"},"method":"thing.event.property.post"}"; char ledsendMseg[tempMsegled.length()]; strcpy(ledsendMseg,tempMsegled.c_str()); client.publish(pubTopic,ledsendMseg); } void ConnectCloud(){ while(!client.connected()){ Serial.print("Attempting MQTT connection..."); /*A device connected to the cloud platform based on an automatically calculated username and password*/ if(client.connect(myIot._clientId,myIot._username,myIot._password)){ Serial.println("connected"); client.subscribe(subTopic); }else{ Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void setup(){ Serial.begin(115200); pinMode(BEDROOD_LIGHT,OUTPUT); pinMode(DHT11_PIN,INPUT); closeLight(); /*Connect to WIFI*/ connectWiFi(); /*Initialize the configuration of Aliyun*/ /*初始化Alinyun的配置,可自动计算用户名和密码*/ myIot.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret); client.setServer(myIot._mqttServer,PORT); /*Set the callback function to execute the callback function when receiving the subscription information*/ /*设置回调函数,当收到订阅信息时会执行回调函数*/ client.setCallback(callback); /*Connect to the cloud platform*/ /*连接到Aliyun*/ ConnectCloud(); } uint8_t tempTime = 0; void loop(){ if(!client.connected()){ ConnectCloud(); } /*One minute publish temperature and humidity information*/ /*一分钟上报次温湿度信息*/ if(tempTime > 120){ tempTime = 0; DHT.read(DHT11_PIN); Serial.print("DHT.temperature="); Serial.println(DHT.temperature); Serial.print("DHT.humidity"); Serial.println(DHT.humidity); client.publish(pubTopic,("{"id":"+ClientId+","params":{""+TempIdentifier+"":"+DHT.temperature+",""+HumiIdentifier+"":"+DHT.humidity+"},"method":"thing.event.property.post"}").c_str()); }else{ tempTime++; delay(500); } if(DHT.temperature > 31){ openLight(); }else closeLight(); client.loop(); } 选择正确板和com端口,验证后上传 运行下,在MQTT连接完成后,在https://studio.iot.aliyun.com/可以在设备管理界面看到设备显示在线,
在线调试 灯没问题后去阿里云平台物联网平台web编辑,编辑案例4 温湿度监测 设置好后温度显示正常,但湿度无变化为0,设备正常上传数据 阿里云平台服务不太好 但是5天后在预览时就都正常了,不知为什么。 |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
3456个成员聚集在这个小组
加入小组物联网工程师必备:怎么选择不同的无线连接威廉希尔官方网站 ,本指南帮你忙!
3329 浏览 1 评论
【DFRobot TinkerNode NB-IoT 物联网开发板试用连载】WIFI功能测试
3979 浏览 0 评论
【DFRobot TinkerNode NB-IoT 物联网开发板试用连载】Arduino的替代SublimeText3+STino
3481 浏览 0 评论
使用端口扩展器轻松高效地向IIoT端点添加具有成本效益的子节点
4057 浏览 1 评论
20736 浏览 11 评论
模组有时候复位重启后输出日志为“REBOOT_CAUSE_SECURITY_PMU_POWER_ON_RESET”的原因?
882浏览 2评论
1168浏览 2评论
1108浏览 1评论
1252浏览 1评论
443浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-2-9 07:36 , Processed in 0.872099 second(s), Total 69, Slave 53 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191