ThingsBoard obtains DHT11 temperature and humidity through ESp8266 (MicroPython firmware)

ThingsBoard obtains DHT11 temperature and humidity through ESp8266 (MicroPython firmware)

1. Upload the following code to ESP8266

1. Before uploading the code, you must make sure that the firmware version of your ESP8266 (hereinafter collectively referred to as WIFI module) is MicroPython, if not [ please see this article ].
2. After flashing the firmware, read the steps in this article to upload the following code to the WIFI module
3. Import the required library simple.py , and upload the downloaded file directly to the WiFi module

import network
import time
import dht
import machine
import json
import ubinascii
from simple import MQTTClient


SSID="WIFI名称"
PASSWORD="WiFi密码"

CLIENT_ID = ubinascii.hexlify(machine.unique_id())

CONFIG = {
    
    
    "server": "192.168.1.57",  // ThingsBoard地址
    "port":1883,  // MQTT端口(默认1883"mqtt_user": "Jy4MRL5P1o7YV90PhKcR",  // 设备的访问令牌
    "mqtt_password": "Jy4MRL5P1o7YV90PhKcR",  // 设备的访问令牌
    "mqtt_topic": "v1/devices/me/telemetry",  // MQTT主题(无需更改)
}

//连接WiFi函数
def connectWifi(ssid,passwd):
    global wlan
    wlan=network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.disconnect()
    wlan.connect(ssid,passwd)
    while(wlan.ifconfig()[0]=='0.0.0.0'):
        time.sleep(1)

//获取温湿度函数
def get_humiture():
    d = dht.DHT11(machine.Pin(2))
    d.measure()
    return d.temperature(),d.humidity()

//上传温湿度到ThingsBoard函数
def pub_humiture(humidity, temperature):
    global c
    data={
    
    'humidity':humidity, 'temperature':temperature} 
    c.publish(CONFIG["mqtt_topic"], json.dumps(data))
    print(json.dumps(data))


def main():
    connectWifi(SSID,PASSWORD)
    global c
    c = MQTTClient(CLIENT_ID, CONFIG["server"],CONFIG["port"], CONFIG["mqtt_user"], CONFIG["mqtt_password"])
    c.connect()
    print("Connected to %s, publish to %s topic" % (CONFIG["server"], CONFIG["mqtt_topic"]))
    while True:
        tem, hum=get_humiture()
        pub_humiture(hum, tem)
        time.sleep(5)    

if __name__ == "__main__":
    main()

Insert picture description here

2. Physical connection

The IO2 of the WiFi module is connected to the OUT of DHT11. Others only need to connect the positive and the negative, and try to connect the WiFi module and DHT11 in the same circuit. Otherwise, DHT11 may not be able to read data. Remember that the WiFi module must be connected to 3.3 V.
The approximate connection diagram is as follows:
Insert picture description here

2. Test whether the temperature can be obtained normally

According to the sequence of operations in the figure below, you can see the following output on the left to confirm that ESP8266 can obtain the temperature and humidity

Insert picture description here
Finally, in the latest telemetry of the Things board platform, is it possible to see the temperature and humidity?

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_25886111/article/details/107076907