功能:
- WIFI-蓝牙配网
- MQTT连接和推送订阅
- 设备管理(LED, 电机)
- 设备状态:在线 - 离线
- 产品状态:打开 - 关闭
- 注:wifi,蓝牙,mqtt连接代码,线路连接和硬件自行处理
主程序:
from machine import Pin
from machine import Timer
from time import sleep_ms
import time
import ntptime
import bluetooth
import ujson
## wifi 连接
import wifi
## 设备信息管理
import device
## 蓝牙连接
from ble import ESP32_BLE, BLE_EVENT
## MQTT 连接
from mqtt import MQTTClient, mqtt_connect
## 连接配置
from configtxt import ConfigTxt, get_data
def ble_back(BLE_MSG):
# type::data 1: led 2:wifi
print(BLE_MSG)
ble_type = ""
ble_data = ""
bs = BLE_MSG.split("::")
if bs[0] == '1':
ble_type = BLE_MSG
elif bs[0] == '2':
ble_type = '2'
ble_data = bs[1]
get_data()['ble_type'] = ble_type
get_data()['ble_data'] = ble_data
def ble_listen(led):
ble_type = get_data()['ble_type']
ble_data = get_data()['ble_data']
if ble_type != "":
print(ble_type, ble_data)
if ble_type == '1::1':
led.value(1)
#_ble.send('LED is ON.' if led.value() else 'LED is OFF')
elif ble_type == '1::0':
led.value(0)
# _ble.send('LED is ON.' if led.value() else 'LED is OFF')
elif ble_type == '2':
wifiSsid = get_data()['wifiSsid']
# ssid|||password
wifidata = ble_data.split("|||")
print(ble_type, wifiSsid, wifidata)
if wifiSsid == None or wifiSsid != wifidata[0] :
get_data()['wifiSsid'] = wifidata[0]
get_data()['wifiKey'] = wifidata[1]
get_data()['ble_type'] = ""
def mqtt_back(topic, msg): # 回调函数,收到服务器消息后会调用这个函数
try:
topic = topic.decode('utf-8')
msg = msg.decode('utf-8')
did = config.get_deviceid()
print("mqtt_back : ", topic, msg)
if topic == 'device/'+did:
print(msg)
if topic == 'device/product/'+did:
mqtt_back_product(msg)
except Exception as e :
print(e)
def mqtt_back_product(msg):
print('product', msg)
pd = ujson.loads(msg)
if 'name' in pd:
if pd['name'] == 'led26':
led26.value(pd['status'])
device.set_product_data(26, "led", pd['status'])
if pd['name'] == 'motor25':
if pd['status'] > 0 :
motor25.off()
else:
motor25.on()
device.set_product_data(25, "motor", pd['status'])
def mqtt_init(mqttc):
mqerror = get_data()['mqerror']
if mqttc == None and mqerror > 0:
did = config.get_deviceid()
mqttHost, mqttPort, mqttUser, mqttPassword = config.get_mqtt()
print("mqtt connect: ", did, mqttHost, mqttPort, mqttUser)
mqttc = mqtt_connect(did, mqttHost, mqttPort, mqttUser, mqttPassword, mqtt_back)
if mqttc != None:
print("mqtt ok ")
mqttc.subscribe(b"device/"+did) # 监控ledctl这个通道,接收控制命令
mqttc.subscribe(b"device/product/"+did)
# mqttc.publish(b"device/"+did, "ping")
device.set_data("mqttConnect", 1)
ntptime.settime()
else :
print("mqtt error : ", mqerror, did, mqttHost)
mqerror = mqerror - 1
get_data()['mqerror'] = mqerror
return mqttc
def wifi_listen(wlan):
wifiSsid = get_data()['wifiSsid']
wifiKey = get_data()['wifiKey']
if wifiSsid != None and wifiSsid != "":
print("wifi connect : ", wifiSsid, wifiKey)
wlan = wifi.wifi_connect(wifiSsid, wifiKey)
if wlan != None and wlan.isconnected():
print("wifi ok")
get_data()['wifiSsid'] = None
config.set_wifi(wifiSsid, wifiKey)
# time.sleep(10)
# wlan = wifi.wifi_disconnect(wlan)
device.set_data("wifiConnect", 1)
else:
print("wifi error : ", wifiSsid)
get_data()['wifiSsid'] = None
return wlan
def mqtt_listen(wlan, mqttc):
if mqttc == None and wlan != None and wlan.isconnected():
mqttc = mqtt_init(mqttc)
if mqttc != None:
mqttc.check_msg()
elif mqttc != None:
mqttc.check_msg()
return mqttc
def device_listen(mqttc, did):
info = device.get_device_info()
if info != None and mqttc != None:
# print("device_listen", info)
mqttc.publish(b"device/config/"+did, info, qos=2)
def init_product(led26, motor25):
led26.value(0)
device.set_product_data(26, "led", 0)
motor25.on()
device.set_product_data(25, "motor", 0)
def run_main(wlan, mqttc):
try:
ble_listen(led)
wlan = wifi_listen(wlan)
mqttc = mqtt_listen(wlan, mqttc)
device_listen(mqttc, did)
sleep_ms(1000)
except Exception as e :
print(e)
return wlan, mqttc
def run_main_init():
print("ble-wifi run ")
get_data()['config'] = config
name = "ESP32BLE"
_event = BLE_EVENT(2, ble_back)
_ble = ESP32_BLE(_event, name)
get_data()['ble_type'] = ""
get_data()['ble_data'] = ""
# but = Pin(0, Pin.IN)
# but.irq(trigger=Pin.IRQ_FALLING, handler=buttons_irq)
get_data()['mqerror'] = 1
wifiSsid, wifiKey = config.get_wifi()
get_data()['wifiSsid'] = wifiSsid
get_data()['wifiKey'] = wifiKey
init_product(led26, motor25)
led26 = Pin(26, Pin.OUT)
motor25 = Pin(25, Pin.OUT)
led = Pin(2, Pin.OUT)
config = ConfigTxt("config.txt")
did = config.get_deviceid()
if __name__ == "__main__":
run_main_init()
wlan = None
mqttc = None
while True:
wlan, mqttc = run_main(wlan, mqttc)
运行日志
ble-wifi run
bytearray(b'\x02\x01\x02\t\tESP32BLE')
wifi connect : 123 123456789
wifi_disconnect
'192.168.191.2', '255.255.255.0', '192.168.191.1', '192.168.191.1')
wifi ok
mqtt connect: 64b70861cb2c 127.0.0.1 1883 test
mqtt ok
b'device/64b70861cb2c'
b'device/product/64b70861cb2c'