bit MQTT
ObloqMqttPublish.py 发送数据示例
from microbit import *
import Obloq
SERVER = "iot.dfrobot.com.cn"
PORT = "1883"
SSID = "dfrobotYanfa" # 修改为wifi名称,不要使用中文
PASSWORD = "hidfrobot" # 修改为wifi密码
IOT_ID = "SyWPtKo_Kz" # 修改为IOT_ID
IOT_PWD = "HJfwKFjdtz" # 修改为IOT_PWD
IOT_TOPIC1 = "BysFYi_FM" # 修改为IOT_TOPIC1
IOT_TOPIC2 = "SkmigyM5z" # 修改为IOT_TOPIC2
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin2, rx=pin1)
def ConnectWifi():
while Obloq.connectWifi(SSID,PASSWORD,10000) != True:
display.show(".")
display.scroll(Obloq.ifconfig())
def event(e,param):
if e == "MqttConneted":#MQTT连接成功
display.scroll("OK")
ConnectWifi()
#初始化MQTT的配置
Obloq.mqttConfig(event,SERVER,PORT)
#连接MQTT
Obloq.connectMqtt(IOT_ID,IOT_PWD)
while True:
sleep(10000)
#向服务器的IOT_TOPIC1发送hello
Obloq.publish(IOT_TOPIC1,"hello")
sleep(10000)
Obloq.publish(IOT_TOPIC2,"HELLO")
ObloqMqttSubscribe.py 接收数据示例
from microbit import *
import Obloq
SERVER= "iot.dfrobot.com.cn"
PORT= "1883"
SSID= "dfrobotYanfa"
PASSWORD = "hidfrobot"
IOT_ID = "SyWPtKo_Kz"
IOT_PWD = "HJfwKFjdtz"
IOT_TOPIC1 = "BysFYi_FM"
IOT_TOPIC2 = "SkmigyM5z"
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin2, rx=pin1)
def ConnectWifi():
while Obloq.connectWifi(SSID,PASSWORD,10000) != True:
display.show(".")
display.scroll(Obloq.ifconfig())
#设置MQTT的回调函数
def event(e,param):
if e == IOT_TOPIC1:#收到了订阅的topic的信息
display.scroll(param)
if e == IOT_TOPIC2:
display.scroll(param)
if e == "MqttConneted":#MQTT连接成功
display.scroll("OK")
if e == "SubOk":#订阅成功
display.scroll("OK")
ConnectWifi()
#初始化MQTT的配置
Obloq.mqttConfig(event,SERVER,PORT)
#连接MQTT
Obloq.connectMqtt(IOT_ID,IOT_PWD)
sleep(300)
Obloq.subscribe(IOT_TOPIC1)#订阅Topoic1
sleep(300)
Obloq.subscribe(IOT_TOPIC2)#订阅Topoic2
while True:
pass
easyIoT网站:iot.dfrobot.com.cn