MicroPython动手做(26)——物联网之OneNET

2020-06-012万
AI 快速预览详细 收起
本文介绍了物联网(IoT)的概念及其广阔的应用前景。物联网是指通过互联网和传统电信网等信息承载体,使所有能行使独立功能的普通物体实现互联互通的网络。其应用领域包括运输和物流、工业制造、健康医疗、智能环境等。物联网的概念最早由Kevin Ashton在1999年提出,他因此被称为物联网之父。通过MicroPython动手实践OneNET平台,你可以开始构建自己的物联网项目。
1、物联网(Internet of Things,缩写:IoT)
是基于互联网、传统电信网等信息承载体,让所有能行使独立功能的普通物体实现互联互通的网络。其应用领域主要包括运输和物流、工业制造、健康医疗、智能环境(家庭、办公、工厂)等,具有十分广阔的市场前景。

物联网的概念最早是在1999年由Kevin Ashton在一次演讲中提出来的,当时他是一个RFID研究机构的执行主任,这家研究机构是在宝洁公司和吉列公司的赞助下成立的。而他本人也因此被称为物联网之父。随后麻省理工学院的Neil Gershenfeld教授出版了一本名为《When things Start to Think》的书。以这些为标志,正式揭开了物联网的序幕。

物联网的英文是Internet of Things,缩写为IoT。这里的“物”指的是我身边一切能与网络联通的物品。例如你带的手表、你骑的共享单车、马路上的汽车、家里的冰箱、路边的路灯、甚至是一棵树。只要一件物品能够与网络相连,它就都是物联网中的“物”。而所谓物联网,就是“物”与人,以及“物”与“物”之间,通过网络来传递和处理信息。

MicroPython动手做(26)——物联网之OneNET图1

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(29)
驴友花雕
驴友花雕
作者

驴友花雕
驴友花雕
作者
通过OneNET平台控制小车的进退左右

驴友花雕
驴友花雕
作者
mPython 实验图形编程2



驴友花雕
驴友花雕
作者
mPython 实验图形编程



驴友花雕
驴友花雕
作者

6、通过OneNET平台控制小车的进退左右
[mw_shl_code=python,false]#MicroPython动手做(26)——物联网之OneNET
#通过OneNET平台控制小车的进退左右
from mpython import *
import network
my_wifi = wifi()
my_wifi.connectWiFi('zh', 'zy1567')
import music
import time
from umqtt.simple import MQTTClient
from machine import Timer
import machine
import ubinascii
def OneNET_recv(_msg):pass
_OneNET_msg_list = []
def OneNET_callback(_topic, _msg):
global _OneNET_msg_list
try: _msg = _msg.decode('utf-8', 'ignore')
except: print(_msg);return
OneNET_recv(_msg)
if _msg in _OneNET_msg_list:
eval('OneNET_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()')
tim14 = Timer(14)
_iot_count = 0
def timer14_tick(_):
global _iot, _iot_count
_iot_count = _iot_count + 1
if _iot_count == 1000: _iot.ping(); _iot_count = 0
try: _iot.check_msg()
except: machine.reset()
_iot = None
def OneNET_setup():
global _iot
_iot = MQTTClient('600999469', '183.230.40.39', 6002, '350265', 'l6S0BsjGXsQACV3hBhml7=3pwCo=', keepalive=300)
_iot.set_callback(OneNET_callback)
if 1 == _iot.connect(): print('Successfully connected to MQTT server.')
tim14.init(period=200, mode=Timer.PERIODIC, callback=timer14_tick)
import parrot
def forward():
parrot.set_speed(parrot.MOTOR_1, 80)
parrot.set_speed(parrot.MOTOR_2, 80)
oled.fill(0)
oled.blit(image_picture.load('face/Information/Forward.pbm', 0), 32, 0)
oled.show()
def retreat():
parrot.set_speed(parrot.MOTOR_1, -80)
parrot.set_speed(parrot.MOTOR_2, -80)
oled.fill(0)
oled.blit(image_picture.load('face/Information/Backward.pbm', 0), 32, 0)
oled.show()
def right():
parrot.set_speed(parrot.MOTOR_1, 80)
parrot.set_speed(parrot.MOTOR_2, -0)
oled.fill(0)
oled.blit(image_picture.load('face/Information/Right.pbm', 0), 32, 0)
oled.show()
def Left():
parrot.set_speed(parrot.MOTOR_1, -0)
oled.fill(0)
oled.blit(image_picture.load('face/Information/Left.pbm', 0), 32, 0)
oled.show()
_OneNET_msg_list.append('进')
def OneNET_recv_e8bf9b():
rgb.fill((int(0), int(102), int(0)))
rgb.write()
time.sleep_ms(1)
forward()
_OneNET_msg_list.append('退')
def OneNET_recv_e98080():
rgb.fill((int(51), int(102), int(255)))
rgb.write()
time.sleep_ms(1)
retreat()
_OneNET_msg_list.append('左')
def OneNET_recv_e5b7a6():
rgb[0] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
Left()
_OneNET_msg_list.append('右')
def OneNET_recv_e58fb3():
rgb[2] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
right()
image_picture = Image()
oled.fill(0)
oled.DispChar(' 请准备好物联网控制', 0, 16, 1)
oled.show()
music.play('G5:1')
rgb[1] = (int(255), int(0), int(0))
rgb.write()
time.sleep_ms(1)
OneNET_setup()
while True:
oled.fill(0)
parrot.set_speed(parrot.MOTOR_1, 0)
parrot.set_speed(parrot.MOTOR_2, 0)
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
oled.show()
time.sleep(2)[/mw_shl_code]
驴友花雕
驴友花雕
作者
输入数字1-255(设置字符串格式),则自动调整RGB红灯的不同亮度

驴友花雕
驴友花雕
作者
原来也能传送汉字



驴友花雕
驴友花雕
作者
在电脑查看掌控板实时的光线值



驴友花雕
驴友花雕
作者
OneNET平台上的下发命令记录

驴友花雕
驴友花雕
作者
在电脑查看掌控板实时的光线值


驴友花雕
驴友花雕
作者
mPython X 实验图形编程

驴友花雕
驴友花雕
作者

5、在电脑查看掌控板实时的光线值
掌控板上显示电脑上发送的命令(字符串),并点亮红色灯;当发送“open”命令时,掌控板传送“光线值”的数据流到电脑,并点亮蓝色灯,发送“close”,蓝灯熄灭。
[mw_shl_code=python,false]#MicroPython动手做(26)——物联网之OneNET
#在电脑查看掌控板实时的光线值
from mpython import *
import network
from umqtt.simple import MQTTClient
from machine import Timer
import machine
import ubinascii
import time
import json
my_wifi = wifi()
my_wifi.connectWiFi("zh", "zy1567")
def OneNET_recv(_msg):pass
_OneNET_msg_list = []
def OneNET_callback(_topic, _msg):
global _OneNET_msg_list
try: _msg = _msg.decode('utf-8', 'ignore')
except: print(_msg);return
OneNET_recv(_msg)
if _msg in _OneNET_msg_list:
eval('OneNET_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()')
tim14 = Timer(14)
_iot_count = 0
def timer14_tick(_):
global _iot, _iot_count
_iot_count = _iot_count + 1
if _iot_count == 1000: _iot.ping(); _iot_count = 0
try: _iot.check_msg()
except: machine.reset()
_iot = None
def OneNET_setup():
global _iot
_iot = MQTTClient("600844609", "183.230.40.39", 6002, "350265", "l6S0BsjGXsQACV3hBhml7=3hgCo=", keepalive=300)
_iot.set_callback(OneNET_callback)
if 1 == _iot.connect(): print('Successfully connected to MQTT server.')
tim14.init(period=200, mode=Timer.PERIODIC, callback=timer14_tick)
def pubdata(_dic):
print(_dic)
_list = []
for _key in list(_dic.keys()):
_d = {'id':_key,'datapoints':[{'value':_dic[_key]}]}
_list.append(_d)
_data = {'datastreams': _list}
j_d = json.dumps(_data)
j_l = len(j_d)
arr = bytearray(j_l + 3)
arr[0] = 1
arr[1] = int(j_l / 256)
arr[2] = j_l % 256
arr[3:] = j_d.encode('ascii')
return arr
def OneNET_recv(_msg):
oled.fill(0)
oled.DispChar(_msg, 0, 0, 1)
oled.show()
if _msg.isdigit():
rgb.fill( (int((int(_msg))), int(0), int(0)) )
rgb.write()
time.sleep_ms(1)
_OneNET_msg_list.append('open')
def OneNET_recv_6f70656e():
rgb.fill((int(0), int(0), int(153)))
rgb.write()
time.sleep_ms(1)
_OneNET_msg_list.append('close')
def OneNET_recv_636c6f7365():
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
OneNET_setup()
while True:
_iot.publish('$dp', pubdata({"light":light.read()}))
time.sleep(3)[/mw_shl_code]
驴友花雕
驴友花雕
作者
OneNET平台控制远程RGB灯

驴友花雕
驴友花雕
作者
输入“开灯”命令。注意要选择字符串格式。


驴友花雕
驴友花雕
作者
mPython X 实验图形编程

驴友花雕
驴友花雕
作者
相关参考使用文档
多协议接入
https://open.iot.10086.cn/doc/multiprotocol/#多协议接入
MQTT物联网套件
https://open.iot.10086.cn/doc/mqtt/

驴友花雕
驴友花雕
作者
先添加产品,然后再添加设备,方可得到设备ID并显示在线,为连接成功。

驴友花雕
驴友花雕
作者
后来尝试在多协议接入---MQTT(旧版)上重新添加产品
链接 https://open.iot.10086.cn/develop/global/product/#/public?protocol=3&other=1

驴友花雕
驴友花雕
作者
返回OneNET平台,在设备列表里面查看设备状态,设备依旧是“离线”,代表连接设备不成功。

驴友花雕
驴友花雕
作者
烧入程序之后,不知哪里出错.......连不上平台



串口输出:



Traceback (most recent call last):

File "main.py", line 54, in

File "main.py", line 38, in OneNET_setup

File "umqtt/simple.py", line 99, in connect

MQTTException: 4

MicroPython v2.0.1-18-gbe8fbdd-dirty on 2020-04-24; mpython with ESP32



屏显