Skip to content

Commit 875f1bc

Browse files
author
Marty
committed
add d1motor ,mqtt username
1 parent f4d12dd commit 875f1bc

13 files changed

+477
-184
lines changed

Diff for: esp01.py

+43-165
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,61 @@
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
54

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
108

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())
1149

115-
116-
class MQTT():
10+
class Board:
11711

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)
13116

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):
15818
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()
16324
else:
164-
debug.print("offline,blink...")
165-
esp01.led.blink(2)
25+
debug.print("offline...")
16626
pass
16727

168-
def connect(ssid='webduino.io',pwd='webduino'):
28+
def connect(self,ssid='webduino.io',pwd='webduino'):
16929
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'):
17232
break
17333
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+
17445

46+
# esp01
47+
esp01 = Board()
17548
esp01.init()
17649
esp01.connect("KingKit_2.4G","webduino")
17750
esp01.mqtt.pub("qq123","OKOK")
17851

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()
18159

182-
esp01.mqtt.sub(b'esp01/qq123',test)
183-
print("sub...ok")
60+
esp01.mqtt.sub('debug/display',execEval)
61+
esp01.loop()

Diff for: esp01_buzzer.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os,machine,math,time
2+
from machine import ADC, I2C, Pin
3+
4+
class Buzzer():
5+
# 262 294 330 349 392 440 494
6+
def __init__(self):
7+
self.queue = []
8+
self.isPause = False
9+
gnd = machine.Pin(1)
10+
gnd.off()
11+
12+
def play(self,freq=300,delay=0.1):
13+
pin25 = machine.PWM(machine.Pin(3), duty=512)
14+
pin25.freq(freq)
15+
time.sleep(delay)
16+
machine.PWM(machine.Pin(3), duty=0)
17+
18+
def playList(self,list):
19+
self.queue.extend(list)
20+
21+
def pause(self,b):
22+
self.isPause = b
23+
24+
def stop(self):
25+
self.queue = []
26+
27+
def run(self):
28+
while True:
29+
if len(self.queue)>0 and not self.isPause:
30+
note = self.queue[0]
31+
del self.queue[0]
32+
self.play(note[0],note[1])
33+
34+
buzzer = Buzzer()
35+
buzzer.play(262,0.15)
36+
buzzer.play(294,0.1)
37+
buzzer.play(330,0.1)
38+
buzzer.play(349,0.1)
39+
buzzer.play(392,0.1)
40+
buzzer.play(440,0.1)
41+
buzzer.play(494,0.1)

Diff for: esp01_dht22.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import dht, machine
2+
3+
d = dht.DHT22(machine.Pin(2))
4+
print('d.measure():',d.measure())
5+
print('d.temperature():',d.temperature())
6+
print('d.humidity():',d.humidity())

Diff for: esp01_ssd1306.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import ssd1306
33

44
i2c = I2C(scl=Pin(0), sda=Pin(2), freq=100000) #Init i2c
5-
lcd=ssd1306.SSD1306_I2C(64,48,i2c) #create LCD object,Specify col and row
5+
lcd=ssd1306.SSD1306_I2C(128,64,i2c) #create LCD object,Specify col and row
66
lcd.text("ESP8266",0,0)
77
lcd.text("test",0,16)
88
lcd.text("123456",0,32)

Diff for: esp01_ssd1306_mqtt.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from webduino import *
2+
from machine import Pin,I2C
3+
import time, ssd1306
4+
5+
6+
# ssd1306
7+
i2c = I2C(scl=Pin(0), sda=Pin(2), freq=100000) #Init i2c
8+
lcd = ssd1306.SSD1306_I2C(128,64,i2c) #create LCD object,Specify col and row
9+
10+
11+
class Board:
12+
13+
def init(self):
14+
self.wifi = WiFi
15+
self.mqtt = MQTT
16+
self.wifi.onlilne(self.online)
17+
18+
def online(self,status):
19+
if status:
20+
debug.print("connect mqtt...")
21+
self.mqtt.connect()
22+
debug.print("mqtt OK")
23+
lcd.text("MQTT connnected!",0,0)
24+
lcd.show()
25+
else:
26+
debug.print("offline...")
27+
pass
28+
29+
def connect(self,ssid='webduino.io',pwd='webduino'):
30+
while True:
31+
if self.wifi.connect(ssid,pwd):
32+
if self.mqtt.connect('mqtt1.webduino.io','webduino','webduino'):
33+
break
34+
debug.print("WiFi Ready , MQTT Ready , ready to go...")
35+
36+
def loop(self):
37+
debug.print("run...")
38+
while True:
39+
self.mqtt.checkMsg()
40+
time.sleep(0.1)
41+
42+
43+
# esp01
44+
esp01 = Board()
45+
esp01.init()
46+
esp01.connect("KingKit_2.4G","webduino")
47+
48+
def execEval(topic,msg):
49+
topic = topic.decode("utf-8")
50+
msg = msg.decode("utf-8")
51+
print('topic:',topic,' ,msg:',msg)
52+
if(topic == 'debug/display'):
53+
try:
54+
eval(msg)
55+
lcd.show()
56+
except:
57+
pass
58+
59+
esp01.mqtt.sub('debug/display',execEval)
60+
esp01.loop()

Diff for: getLibrary.py

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def exe(dir):
231231
Res.exe('lib/heltec/sx127x.py') #LoRa
232232
Res.exe('lib/dfplayer.py') #MP3
233233
Res.exe('lib/scanplayer.py') #MP3
234+
Res.exe('lib/d1motor.py') # d1motor
234235

235236
#Res.exe('lib/animations.py')
236237
#Res.exe('lib/pixelart.py')

0 commit comments

Comments
 (0)