-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwemos.py
66 lines (54 loc) · 1.65 KB
/
wemos.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
64
65
66
from webduino import *
from machine import Pin,SoftI2C
import time, ssd1306
time.sleep(1)
# ssd1306
i2c = SoftI2C(scl=Pin(4), sda=Pin(5), freq=100000) #Init i2c
lcd = ssd1306.SSD1306_I2C(128,64,i2c) #create LCD object,Specify col and row
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")
lcd.text("MQTT connnected!",0,0)
lcd.show()
else:
debug.print("offline...")
pass
def connect(self,ssid='KingKit_2.4G',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.wifi.checkConnection(0)
self.mqtt.checkMsg()
time.sleep(0.1)
# wemos
wemos = Board()
wemos.init()
wemos.connect("KingKit_2.4G","webduino")
def execEval(topic,msg):
topic = topic.decode("utf-8")
msg = msg.decode("utf-8")
print('topic:',topic,' ,msg:',msg)
if(topic == 'debug/display'):
try:
eval(msg)
lcd.show()
except:
pass
wemos.mqtt.sub('debug/display',execEval)
wemos.loop() # mqtt ping & check wifi & check mqtt Msg