本帖最后由 云天 于 2023-6-4 15:13 编辑
【项目背景】
使用行空板加语音识别模块结合SIOT物联网平台,控制多个ESP32-C3台灯。编程软件使用Mind+和米思奇。
【项目准备】
1.改造台灯
将旧台灯内容,除电池,其它拆除,并将其中一根电源线剪断并引出,接到电磁继电器上的NO和COM端口。
电磁继电器接ESP32-C3的引脚5。
2.行空板连接WIFI并开启SIOT服务
获取IP地址:[color=rgba(0, 0, 0, 0.85)]192.168.31.131
开启SIOT服务
新建“主题”:siot/DengKG
【行空板编程】
添加“语音识别模块”
【ESP32-C3编程】
ESP32-C3编程使用米思奇
-
- import machine
-
- # main.py
- import time
- from simple import MQTTClient
- from gc import mem_free
- import network
-
- pin0 = machine.Pin(5, machine.Pin.OUT)
-
-
- def connect_wifi(ssid, passwd):
- global wlan
- wlan = network.WLAN(network.STA_IF) # create a wlan object
- wlan.active(True) # Activate the network interface
- wlan.disconnect() # Disconnect the last connected WiFi
- wlan.connect(ssid, passwd) # connect wifi
- while (wlan.ifconfig()[0] == '0.0.0.0'):
- time.sleep(1)
- wlan_info = wlan.ifconfig()
- print("Wifi is connected with the following information:")
- print(" IP address : " + wlan_info[0])
- print("Subnet mask : " + wlan_info[1])
- print(" Gateway : " + wlan_info[2])
- print(" DNS : " + wlan_info[3])
-
-
- mqtt_client = None
-
-
- def sub_cb(topic, msg):
- print("接收到数据", (topic, msg.decode()))
-
-
- if msg.decode()=="1":
- pin0.value(1)
- elif msg.decode()=="0":
- pin0.value(0)
-
- broker_address = '192.168.31.131' # MQTT服务器地址
- broker_port = '1883' # MQTT服务器端口
- client_id = 'X8jykxFnR104' # MQTT设备id
- user_name = 'siot' # mqtt 设备用户名
- password = 'dfrobot' # mqtt 设备密码
-
- subscribe_topic= 'siot/DengKG' # 订阅主题
-
- SSID = 'sxs' # 无线网名称
- PASSWORD = '***********' #无线网密码
- if __name__ == '__main__':
- try:
- print("\n可用内存1: %s Byte" % str(mem_free()))
- connect_wifi(SSID, PASSWORD)
- print("\n可用内存2: %s Byte" % str(mem_free()))
- mqtt_client = MQTTClient(client_id=client_id, server=broker_address, port=broker_port, user=user_name,
- password=password, keepalive=300)
- mqtt_client.set_callback(sub_cb)
- mqtt_client.connect()
- mqtt_client.subscribe(subscribe_topic)
-
- #mqtt_client.publish(topic=publish_topic, msg="12345678900000", retain=False, qos=1)
- print("mqtt_client 1: %s" % mqtt_client.client_id)
- while True:
- mqtt_client.wait_msg() # wait message
- except Exception as ex_results:
- print('exception1', ex_results)
- finally:
- if (mqtt_client is not None):
- mqtt_client.disconnect()
- wlan.disconnect()
- wlan.active(False)
-
复制代码
if msg.decode()=="1":
pin0.value(1)
elif msg.decode()=="0":
pin0.value(0)
subscribe_topic= 'siot/DengKG' # 订阅主题
物联网指令“1”、“0”控制一号灯开关。
注:需在米思奇软件目录:D:\mixly2.0-win32-x64-rc3\mixly2.0-win32-x64\resources\app\src\board\micropython_esp32c3\build\lib,放置simple.py文件。
simple.zip
【控制单台灯演示】
【控制多台灯演示】
行空板程序
演示视频
|