uPyCraft-micropython教程(dfrobot MQTT实现双向控制)
此实验代码有些问题,楼主调整中,请先不要测试此实验紧接着单向控制实现,在做本实验之前,请阅读玩MQTT单向控制章节
准备工作:
硬件平台: FireBeetle-ESP32x2
IO扩展板 x1
Button模块 x1
软件平台: uPyCraft-V0.21
物联网网站:https://iot.dfrobot.com.cn
本实验实现的效果
设备A订阅TOPIC_LEDA,设备B按键后向这个设备A发送toggle消息,让LEDA状态反转
设备B订阅TOPIC_LEDB,设备A定时向设备B发送on off消息,控制LEDB状态
从而实现双向控制
开始干活:
第一步,第二步,第三步:和上次实验相同,申请两个TOPIC,一个LED_A,一个LED_B
第四步:
设备A执行下面代码from umqtt.simple import MQTTClient
from machine import Pin,Timer
import network
import time
SSID="DFROBOT_AP"
PASSWORD="12345678"
led=Pin(2, Pin.OUT, value=0)
tim = Timer(-1)
SERVER = "182.254.130.180"
CLIENT_ID = "S1grx_VmNW1"
TOPIC = b"B1rl_47NZ"
username='H1UGxqD4-'
password='BkeLGeqvVb'
stateA = 0
stateB = 0
def sub_cb(topic, msg):
global state
print((topic, msg))
if msg == b"on":
led.value(0)
stateA = 0
print("1")
elif msg == b"off":
led.value(1)
stateA = 1
print("0")
elif msg == b"toggle":
# LED is inversed, so setting it to current state
# value will make it toggle
led.value(stateA)
stateA = 1 - stateA
def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
wlan.connect(ssid,passwd)
while(wlan.ifconfig()=='0.0.0.0'):
time.sleep(1)
def timerOnOff(t):
global stateB,c
if(stateB == 1):
c.publish(TOPIC_LEDB, b'on')
stateB = 0
else:
c.publish(TOPIC_LEDB, b'off')
stateB = 1
connectWifi(SSID,PASSWORD)
server=SERVER
c = MQTTClient(CLIENT_ID, server,0,username,password)
c.set_callback(sub_cb)
c.connect()
c.subscribe(TOPIC)
print("Connected to %s, subscribed to %s topic" % (server, TOPIC))
tim.init(period=3000, mode=Timer.PERIODIC, callback=timerOnOff)
try:
while 1:
c.wait_msg()
finally:
c.disconnect()
tim.deinit()
设备B执行下面代码
from umqtt.simple import MQTTClient
from machine import Pin,Timer
import network
import time
SSID="DFROBOT_AP"
PASSWORD="12345678"
SERVER = "182.254.130.180"
CLIENT_ID = "S1grx_VmNW2"
TOPIC_LEDA = b"B1rl_47NZ"
TOPIC_LEDB = b'S1LowEX4b'
username='H1UGxqD4-'
password='BkeLGeqvVb'
stateA = 0
stateB = 0
def sub_cb(topic, msg):
global state
print((topic, msg))
if msg == b"on":
led.value(0)
stateB = 0
print("1")
elif msg == b"off":
led.value(1)
stateB = 1
print("0")
elif msg == b"toggle":
# LED is inversed, so setting it to current state
# value will make it toggle
led.value(stateB)
stateB = 1 - stateB
def buttonFunc(v):
global c
time.sleep_ms(50)
if(button.value() == 0):
return
while(button.value() == 1):
time.sleep_ms(100)
time.sleep_ms(100)
c.publish(TOPIC_LEDA,b'toggle')
def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
wlan.connect(ssid,passwd)
while(wlan.ifconfig()=='0.0.0.0'):
time.sleep(1)
connectWifi(SSID,PASSWORD)
server=SERVER
c = MQTTClient(CLIENT_ID, server,0,username,password)
c.set_callback(sub_cb)
c.connect()
c.subscribe(TOPIC)
print("Connected to %s, subscribed to %s topic" % (server, TOPIC_LEDB))
button = Pin(25, Pin.IN)
button.irq(trigger=Pin.IRQ_RISING, handler=buttonFunc
try:
while 1:
c.wait_msg()
finally:
c.disconnect()
第五步:设备A执行代码,设备B执行代码,与预期结果相同
页:
[1]