MicroPython动手做(29)——物联网之SIoT
2020-06-051.7万
AI 快速预览详细
MicroPython动手做(29)介绍了SIoT,这是一个为教育定制的跨平台MQTT服务器程序,支持Win10、Win7、Mac、Linux等操作系统,无需注册即可使用。SIoT特别适合中小学生理解物联网原理,并基于物联网技术开发各种创意应用。物联网是互联网的一个延伸,终端是硬件设备,而MQTT协议是一种轻量、简单、开放和易于实现的协议,适用范围广泛。
![]() SIoT 一个为教育定制的跨平台的MQTT服务器程序,S指科学(Science)、简单(simple)的意思。SIoT支持Win10、Win7、Mac、Linux等操作系统,一键启动,无需注册即可使用。SIoT针对学校场景的开源免费的MQTT服务器软件,可一键创建本地物联网服务器,摆脱联网困扰。SIoT重点关注物联网数据的收集和导出,是采集科学数据的最好选择之一。 那什么是物联网?什么又是MQTT呢? 物联网(Internet of Things,缩写IoT)是互联网的一个延伸,互联网的终端是计算机(PC、服务器),而物联网的终端是硬件设备,无论是家电、工业设备、汽车、监测仪器,所有这些终端都可以互联,可以总结为万物互联。 MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛。 SIoT为“虚谷物联”项目的核心软件,是为了帮助中小学生理解物联网原理,并且能够基于物联网技术开发各种创意应用。 |






[mw_shl_code=python,false]#MicroPython动手做(29)——物联网之SIoT
#通过主题消息“on”和“off”开关LED灯
from mpython import *
import network
from umqtt.simple import MQTTClient
import music
import time
from machine import Timer
import ubinascii
my_wifi = wifi()
my_wifi.connectWiFi("zh", "zy1567")
mqtt = MQTTClient("ok", "192.168.31.248", 1883, "siot", "dfrobot", keepalive=30)
try:
mqtt.connect()
print('Connected')
except:
print('Disconnected')
def mqtt_topic_6561676c6572382f7a6b6231(_msg):
global i
if str(_msg) == "on":
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
oled.DispChar(" 开灯", 0, 32, 1)
oled.show()
if str(_msg) == "off":
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
oled.DispChar(" 关灯", 0, 32, 1)
oled.show()
def on_button_a_down(_):
global i
time.sleep_ms(10)
if button_a.value() == 1: return
mqtt.publish("eagler8/zkb1", "on")
music.play('E5:1')
def on_button_b_down(_):
global i
time.sleep_ms(10)
if button_b.value() == 1: return
mqtt.publish("eagler8/zkb1", "off")
music.play('D5:1')
def mqtt_callback(topic, msg):
try:
topic = topic.decode('utf-8', 'ignore')
_msg = msg.decode('utf-8', 'ignore')
eval('mqtt_topic_' + bytes.decode(ubinascii.hexlify(topic)) + '("' + _msg + '")')
except: print((topic, msg))
mqtt.set_callback(mqtt_callback)
mqtt.subscribe("eagler8/zkb1")
def timer14_tick(_):
mqtt.ping()
tim14 = Timer(14)
tim14.init(period=20000, mode=Timer.PERIODIC, callback=timer14_tick)
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
mqtt.publish("eagler8/zkb1", "hello")
music.play('G5:1')
oled.fill(0)
oled.DispChar(" 连接SIoT成功", 0, 16, 1)
oled.show()
rgb[1] = (int(0), int(51), int(0))
rgb.write()
time.sleep_ms(1)
while True:
mqtt.wait_msg()[/mw_shl_code]
Mind+ 实验图形编程
[mw_shl_code=python,false]#MicroPython动手做(29)——物联网之SIoT
#读取环境光强度发送至SIoT网页端
from umqtt.simple import MQTTClient
from mpython import *
import network
import music
import time
brightness=9
# 事件回调函数
def on_button_a_down(_):
global g_my_variable
time.sleep_ms(10)
if button_a.value() == 1: return
oled.DispChar(" 采集光线值", 0, (3-1)*16, 1)
oled.show()
while True:
mqtt.publish("eagler8/zkb1",(str(light.read())))
time.sleep(2)
my_wifi = wifi()
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
my_wifi.connectWiFi("zh","zy1567")
while not (my_wifi.sta.isconnected()):
pass
mqtt = MQTTClient("", "192.168.31.248", 1883, "siot", "dfrobot")
try:
mqtt.connect()
print('Connected')
except:
print('Disconnected')
rgb[1] = (0*brightness//9, 102*brightness//9, 0*brightness//9)
rgb.write()
music.pitch(392, 50)
mqtt.publish("eagler8/zkb1","hello")
oled.invert(0)
oled.DispChar(" SIoT连接成功", 0, (2-1)*16, 1)
oled.show()[/mw_shl_code]
[mw_shl_code=python,false]#MicroPython动手做(29)——物联网之SIoT
#发布“hello”至主题“eagler8/zkb1”
# MindPlus
# mpython
from umqtt.simple import MQTTClient
from mpython import *
import network
import music
brightness=9
my_wifi = wifi()
my_wifi.connectWiFi("zh","zy1567")
while not (my_wifi.sta.isconnected()):
pass
oled.invert(0)
oled.DispChar(" Wifi连接成功", 0, (2-1)*16, 1)
mqtt = MQTTClient("", "192.168.31.248", 1883, "siot", "dfrobot")
try:
mqtt.connect()
print('Connected')
except:
print('Disconnected')
rgb[1] = (0*brightness//9, 102*brightness//9, 0*brightness//9)
rgb.write()
music.pitch(392, 50)
mqtt.publish("eagler8/zkb1","hello")
oled.invert(0)
oled.DispChar(" SIoT连接成功", 0, (2-1)*16, 1)
oled.show()[/mw_shl_code]
reader: read tcp 192.168.31.248:1883->192.168.31.221:53734: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
读取器:读取tcp 192.168.31.24:1883->192.168.31.221:53734:wsarecv:连接尝试失败,因为一段时间后被连接方未正确响应,或者由于连接的主机未能响应而建立连接失败。
[mw_shl_code=python,false]#MicroPython动手做(29)——物联网之SIoT
#尝试在SIoT平台输入消息,控制点亮或熄灭板载LED灯
from umqtt.simple import MQTTClient
from machine import Timer
from mpython import *
import ubinascii
import network
import music
_mqtt_topic_list = []
def timer14_tick(_):
global mqtt
mqtt.ping()
def mqtt_callback(topic, msg):
try:
topic = topic.decode('utf-8', 'ignore')
_msg = msg.decode('utf-8', 'ignore')
eval('mqtt_topic_' + bytes.decode(ubinascii.hexlify(topic)) + '("' + _msg + '")')
except:
print((topic, msg))
brightness=9
# 事件回调函数
def mqtt_topic_6561676c6572382f7a6b6231(_msg):
global g_my_variable
if (_msg == on):
music.pitch(196, 50)
rgb.fill((255*brightness//9, 255*brightness//9, 153*brightness//9))
rgb.write()
if (_msg == off):
music.pitch(784, 50)
rgb.fill( (0, 0, 0) )
rgb.write()
my_wifi = wifi()
tim14 = Timer(14)
my_wifi.connectWiFi("zh","zy1567")
mqtt = MQTTClient("", "192.168.31.248", 1883, "siot", "dfrobot")
try:
mqtt.connect()
print('Connected')
except:
print('Disconnected')
mqtt.set_callback(mqtt_callback)
tim14.init(period=20000, mode=Timer.PERIODIC, callback=timer14_tick)
mqtt.subscribe("eagler8/zkb1")
rgb[1] = (0*brightness//9, 102*brightness//9, 0*brightness//9)
rgb.write()
music.pitch(392, 50)
mqtt.publish("eagler8/zkb1","on")[/mw_shl_code]