数码之家

 找回密码
 立即注册
搜索
查看: 1042|回复: 4

[Arduino] [esp8266_micropython] 读取dht11温湿度数据并通过mqtt上报

[复制链接]
发表于 2023-7-15 12:46:31 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册

x


代码
  1. import dht
  2. from machine import Pin
  3. import time
  4. import network
  5. from umqtt.simple import MQTTClient
  6. import json
  7. import gc


  8. device_id = "esp0002"
  9. wifi_ssid='FAST_20CC'
  10. wifi_password='409409409'
  11. mqtt_serverip='192.168.1.113'
  12. mqtt_serverport=1883
  13. mqtt_clientid=device_id
  14. mqtt_publishtopic=b'espiot'

  15. led=Pin(2,Pin.OUT)
  16. led.on()

  17. # D2-DATA
  18. dht_sim = dht.DHT11(Pin(4))

  19. def connectionWifi(ssid, password):
  20.   wlan = network.WLAN(network.STA_IF)
  21.   wlan.active(True)
  22.   wlan.connect(ssid, password)
  23.   while True:
  24.     if not wlan.isconnected():
  25.       led.on()
  26.       time.sleep_ms(1000)
  27.       led.off()
  28.       time.sleep_ms(1000)
  29.     else:
  30.       print('connected to network')
  31.       led.on()
  32.       break

  33. def dht11_temperature():
  34.   dht_sim.measure()
  35.   return dht_sim.temperature()
  36.   
  37. def dht11_humidity():
  38.   dht_sim.measure()
  39.   return dht_sim.humidity()

  40. if __name__=='__main__':
  41.   print ("start")
  42.   
  43.   #message_template="{'source_device':'$source_device','target_device':'server','temperature':'$temperature'}"
  44.   #message_template = message_template.replace("$source_device",mqtt_clientid)
  45.   message_template = {}
  46.   message_template['source_device'] = mqtt_clientid
  47.   message_template['target_device'] = 'server'
  48.   message_template['msg_type'] = 'ReportTempHumi'
  49.   
  50.   connectionWifi(wifi_ssid,wifi_password)
  51.   
  52.   client = MQTTClient(mqtt_clientid,mqtt_serverip,mqtt_serverport)
  53.   client.connect()
  54.   
  55.   
  56.   while True:
  57.     time.sleep(3)
  58.    
  59.     temperature = dht11_temperature()
  60.     message_template['temperature'] = str(temperature)
  61.    
  62.     humidity = dht11_humidity()
  63.     message_template['humidity'] = str(humidity)
  64.    
  65.     client.publish(mqtt_publishtopic,json.dumps(message_template))

  66.     gc.collect()
复制代码
截图

dht11代码.JPG
mqtt数据接收端.JPG

打赏

参与人数 1家元 +60 收起 理由
家睦 + 60

查看全部打赏

发表于 2023-7-15 13:19:20 | 显示全部楼层
怎么mqtt数据中没有看到温度数据
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-15 15:14:34 | 显示全部楼层
zhonsir 发表于 2023-7-15 13:19
怎么mqtt数据中没有看到温度数据
  1. {
  2.   "temperature": "37",
  3.   "source_device": "esp0002",
  4.   "humidity": "43",
  5.   "target_device": "server",
  6.   "msg_type": "ReportTempHumi"
  7. }
复制代码

temperature字段是温度
humidity字段是湿度
回复 支持 反对

使用道具 举报

发表于 2023-7-16 16:59:50 | 显示全部楼层
  做的很好

不知mqtt server是怎么搭的?


回复 支持 反对

使用道具 举报

发表于 2023-7-16 23:36:52 | 显示全部楼层
mqtt是怎么样工作的,我一直没搞清楚
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-4-27 18:52 , Processed in 0.093600 second(s), 13 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

快速回复 返回顶部 返回列表