赞
踩
目标:实现PacketTracer中的设备连接到MCU芯片,并通过MCU微控制器实现数据上传至云平台、通过云平台控制设备、云平台应用搭建等功能。
1:进入Onenet官网
2:注册登录
3:点击首页控制台
4:点击旧版
5:点击左侧全部产品→多协议接入
6:点击TCP透传→添加产品
7:设置您的产品名称,其余根据下图进行选择,然后点击确定
8:点击立即添加设备
9:鼠标放在脚本解析旁的问号上,下载脚本验证工具
10:下载后找到sample.lua文件,不需要打开,这个文件就是我们需要的文件
11:点击上传脚本解析,选择sample.lua文件
12:点击添加设备
13:设备名称根据需要填写,鉴权信息一定要记住,勾选私有,添加
14:按照实验,构造拓扑图如下
15:编写如下代码,并根据图示修改为自己的信息
补充:
1)创建文件
2)编辑代码
3)获取产品ID
4)获取鉴权信息
5)获取api-key
6)开启
7)代码:
from gpio import * from time import * from realtcp import * url = 'http://api.heclouds.com/devices/xx/datapoints?type=3' head={"api-key":"xx","Content-Type":"application/json"} serverIP="dtu.heclouds.com" serverPort=1811 auth='*xx#xx#xx*' client=RealTCPClient() on=[1] def onTCPConnectionChange(type): if int(type)==3: client.send(auth) def onTCPReceive(data): if data=="Open": on[0]=1 elif data=="Close": on[0]=0 client.onConnectionChange(onTCPConnectionChange) client.onReceive(onTCPReceive) client.connect(serverIP,serverPort) sleep(1) while True: switch=analogRead(3) tem=analogRead(2) print("Switch:",switch) tem=round((tem-511)/5.12,2) print("tem:",tem) s1=str(tem) s="Temperature:\n"+s1+" C" if(switch != 0 and on[0]!=0): customWrite(1,s) elif (switch != 0 and on[0]==0): customWrite(1,"") elif (switch ==0): customWrite(1,"") client.send(tem) sleep(1)
16:运行代码后设备成功在线
17:点击数据流可以查看接收到的数据
18:点击更多操作中的下发命令→下发命令→发送字符串便可以控制mcu芯片了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。