-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_esp01_dht22.py
63 lines (49 loc) · 1.49 KB
/
board_esp01_dht22.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from webduino import *
from machine import Pin,I2C
import time, webrepl, dht, machine, _thread
class Board:
def init(self):
self.wifi = WiFi
self.mqtt = MQTT
self.wifi.onlilne(self.online)
def online(self,status):
if status:
debug.print("connect mqtt...")
self.mqtt.connect()
debug.print("mqtt OK")
else:
debug.print("offline...")
pass
def connect(self,ssid='webduino.io',pwd='webduino'):
while True:
if self.wifi.connect(ssid,pwd):
if self.mqtt.connect('mqtt1.webduino.io','webduino','webduino'):
break
debug.print("WiFi Ready , MQTT Ready , ready to go...")
def loop(self):
debug.print("run...")
now = 0
while True:
now = now + 1
if now % 100 == 0:
self.mqtt.client.ping()
self.mqtt.checkMsg()
time.sleep(0.1)
# esp01
esp01 = Board()
esp01.init()
esp01.connect("KingKit_2.4G","webduino")
d = dht.DHT22(machine.Pin(2))
def read():
print('d.measure():',d.measure())
print('d.temperature():',d.temperature())
print('d.humidity():',d.humidity())
def runCode(topic,msg):
topic = topic.decode("utf-8")
msg = msg.decode("utf-8")
print('topic:',topic,' ,msg:',msg)
if(topic == 'debug/exec'):
eval(msg)
esp01.mqtt.sub('debug/exec',runCode)
_thread.start_new_thread(esp01.loop, ())
webrepl.start()