-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDHT22_MQTT.py
More file actions
48 lines (41 loc) · 1.13 KB
/
Copy pathDHT22_MQTT.py
File metadata and controls
48 lines (41 loc) · 1.13 KB
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
import network
from mqtt import MQTTClient
from machine import Pin
import dht
import time
##Config
pin = Pin(12)
sensor = dht.DHT22(pin)
def sub_cb(topic, msg):
print(msg)
def DHT_Pub(t):
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
print("Temp,Humi : {} C,{} %".format(t,h))
msg = 'temp:{:.2f},humd:{:.2f}'.format(t,h)
client.publish(topic="DHT22/feeds/temp",msg=msg)
#client.publish(topic="DHT22/feeds/Humid",msg=h
#time.sleep_ms(2000)
# wlan = network.WLAN(network.STA_IF)
# #disable AP Mode
# wlan.active(False)
# #enable STA Mode
# wlan.active(True)
# wlan.connect("Horizon_Plus","1212312121")
# while not wlan.isconnected():
# machine.idle()
# print("Connected to Wifi\n")
client = MQTTClient("ESP_Wemos", "m12.cloudmqtt.com",user="wbpgeuzw", password="ClkZVjUA5HKw", port=17441)
client.set_callback(sub_cb)
client.connect()
tim.init(period=2000,mode=Timer.PERIODIC, callback=blink)
tim1.init(period=1000,mode=Timer.PERIODIC, callback=DHT_Pub)
#for i in range(10):
try:
while True:
except:
tim.deinit()
tim1.deinit()
finally :
client.disconnect()