1
- import network , time , machine , ubinascii , os
2
- import usocket
3
- from machine import Pin , PWM , Timer , WDT
4
- from umqtt .simple import MQTTClient
1
+ from webduino import *
2
+ from machine import Pin ,I2C
3
+ import time , ssd1306
5
4
6
- class debug :
7
- def print (msg = "" ,msg2 = "" ):
8
- print (msg ,msg2 )
9
- pass
5
+ # ssd1306
6
+ i2c = I2C (scl = Pin (0 ), sda = Pin (2 ), freq = 100000 ) #Init i2c
7
+ lcd = ssd1306 .SSD1306_I2C (128 ,64 ,i2c ) #create LCD object,Specify col and row
10
8
11
- class LED ():
12
- def __init__ (self ,pin ,pwm = True ):
13
- self .pin = pin
14
- self .strong = 2
15
- self .state = False
16
- self .timer = None
17
- self .isBlink = False
18
- if pwm :
19
- self .pwm = PWM (Pin (pin ))
20
- self .pwm .freq (1024 )
21
- self .pwm .duty (0 )
22
- self .off ()
23
- time .sleep (0.01 )
24
- self .off ()
25
- time .sleep (0.01 )
26
- self .off ()
27
- time .sleep (0.01 )
28
-
29
- def on (self ,strong = - 1 ):
30
- self .state = True
31
- if not strong == - 1 :
32
- self .strong = strong
33
- self .pwm .duty (self .strong )
34
-
35
- def off (self ):
36
- self .state = False
37
- self .pwm .duty (0 )
38
-
39
- def run (self ,n ):
40
- if self .isBlink :
41
- if self .state :
42
- self .off ()
43
- else :
44
- self .on ()
45
-
46
- def blink (self ,peroid ):
47
- _p_ = int (peroid * 1000 )
48
- if _p_ == 0 :
49
- self .isBlink = False
50
- self .off ()
51
- return
52
- else :
53
- self .isBlink = True
54
- self .on ()
55
-
56
- if self .timer == None :
57
- self .timer = Timer (1 )
58
- self .timer .init (period = _p_ , mode = Timer .PERIODIC , callback = self .run )
59
- debug .print ("LED:default use Timer(0)" ,self .timer )
60
- else :
61
- self .timer .deinit ()
62
- self .timer = Timer (1 )
63
- self .timer .init (period = _p_ , mode = Timer .PERIODIC , callback = self .run )
64
-
65
-
66
- class WiFi :
67
-
68
- def disconnect ():
69
- WiFi .sta .disconnect ()
70
-
71
- def check ():
72
- print (WiFi .sta .isconnected ())
73
-
74
- def onlilne (cb ):
75
- WiFi .onlineCallback = cb
76
-
77
- def startKeepConnect ():
78
- WiFi .timer = Timer (0 )
79
- WiFi .timer .init (period = 3000 , mode = Timer .PERIODIC , callback = WiFi .checkConnection )
80
-
81
- def checkConnection (t ):
82
- #print("wifi:",WiFi.sta.isconnected())
83
- if not WiFi .sta .isconnected ():
84
- debug .print ("connect broke ! retry..." )
85
- WiFi .connect (WiFi .ssid ,WiFi .pwd )
86
- debug .print ("!!!! online callback... !!!!" )
87
-
88
- def connect (ssid = "ttt" ,pwd = "webduino" ):
89
- WiFi .ssid = ssid
90
- WiFi .pwd = pwd
91
- WiFi .sta = sta_if = network .WLAN (network .STA_IF )
92
- sta_if .active (True )
93
- debug .print ('connecting to network...' )
94
- WiFi .onlineCallback (False )
95
- if not sta_if .isconnected ():
96
- try :
97
- sta_if .connect (ssid ,pwd )
98
- except :
99
- debug .print ("connect..." )
100
- cnt = 0
101
- while not sta_if .isconnected ():
102
- cnt = cnt + 1
103
- time .sleep (0.5 )
104
- if cnt == 10 :
105
- cnt = 0
106
- debug .print ("retry connect..." )
107
- WiFi .ip = WiFi .sta .ifconfig ()[0 ];
108
- WiFi .onlineCallback (True )
109
- WiFi .startKeepConnect ()
110
- return True
111
-
112
- def ip ():
113
- print (Wifi .sta .ifconfig ())
114
9
115
-
116
- class MQTT ():
10
+ class Board :
117
11
118
- def connect (server = 'mqtt1.webduino.io' ,user = 'webduino' ,pwd = 'webduino' ):
119
- MQTT .server = server
120
- MQTT .user = user
121
- MQTT .pwd = pwd
122
- MQTT .client = MQTTClient ('guest' , server , user = user , password = pwd )
123
- state = True if MQTT .client .connect () == 0 else False
124
- try :
125
- MQTT .subTopic
126
- debug .print ("resubscribe:" ,MQTT .subTopic )
127
- MQTT .sub (MQTT .subTopic ,MQTT .callback )
128
- except :
129
- pass
130
- return state
12
+ def init (self ):
13
+ self .wifi = WiFi
14
+ self .mqtt = MQTT
15
+ self .wifi .onlilne (self .online )
131
16
132
- def pub (topic ,msg ):
133
- MQTT .client .publish (topic ,msg )
134
-
135
- def sub (topic ,cb ):
136
- MQTT .subTopic = topic
137
- MQTT .callback = cb
138
- MQTT .client .set_callback (cb )
139
- MQTT .client .subscribe (topic )
140
-
141
- def checkMsg ():
142
- try :
143
- MQTT .client .check_msg ()
144
- except :
145
- pass
146
-
147
-
148
-
149
- class esp01 ():
150
-
151
- def init ():
152
- esp01 .led = LED (2 )
153
- esp01 .wifi = WiFi
154
- esp01 .mqtt = MQTT
155
- esp01 .wifi .onlilne (esp01 .online )
156
-
157
- def online (status ):
17
+ def online (self ,status ):
158
18
if status :
159
- debug .print ("online,stop blink..." )
160
- esp01 .led .blink (1 )
161
- esp01 .mqtt .connect ()
162
- esp01 .led .blink (0 )
19
+ debug .print ("connect mqtt..." )
20
+ self .mqtt .connect ()
21
+ debug .print ("mqtt OK" )
22
+ lcd .text ("MQTT connnected!" ,0 ,0 )
23
+ lcd .show ()
163
24
else :
164
- debug .print ("offline,blink..." )
165
- esp01 .led .blink (2 )
25
+ debug .print ("offline..." )
166
26
pass
167
27
168
- def connect (ssid = 'webduino.io' ,pwd = 'webduino' ):
28
+ def connect (self , ssid = 'webduino.io' ,pwd = 'webduino' ):
169
29
while True :
170
- if esp01 .wifi .connect (ssid ,pwd ):
171
- if esp01 .mqtt .connect ('mqtt1.webduino.io' ,'webduino' ,'webduino' ):
30
+ if self .wifi .connect (ssid ,pwd ):
31
+ if self .mqtt .connect ('mqtt1.webduino.io' ,'webduino' ,'webduino' ):
172
32
break
173
33
debug .print ("WiFi Ready , MQTT Ready , ready to go..." )
34
+
35
+ def loop (self ):
36
+ debug .print ("run..." )
37
+ now = 0
38
+ while True :
39
+ now = now + 1
40
+ if now % 100 == 0 :
41
+ self .mqtt .client .ping ()
42
+ self .mqtt .checkMsg ()
43
+ time .sleep (0.1 )
44
+
174
45
46
+ # esp01
47
+ esp01 = Board ()
175
48
esp01 .init ()
176
49
esp01 .connect ("KingKit_2.4G" ,"webduino" )
177
50
esp01 .mqtt .pub ("qq123" ,"OKOK" )
178
51
179
- def test (topic ,msg ):
180
- print (topic ,msg )
52
+ def execEval (topic ,msg ):
53
+ topic = topic .decode ("utf-8" )
54
+ msg = msg .decode ("utf-8" )
55
+ print ('topic:' ,topic ,' ,msg:' ,msg )
56
+ if (topic == 'display' ):
57
+ eval (msg )
58
+ lcd .show ()
181
59
182
- esp01 .mqtt .sub (b'esp01/qq123' , test )
183
- print ( "sub...ok" )
60
+ esp01 .mqtt .sub ('debug/display' , execEval )
61
+ esp01 . loop ( )
0 commit comments