当前位置:网站首页>(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写
2022-06-25 03:42:00 【游戏编程】
目录
前言
上一篇文章已经学习了ds18b20温度传感器的简单使用(后续更新如何编写驱动)以及lcd1602显示,但考虑到一些特殊应用场景,比如采集点距离太远、多点采集等,人工费时费力,需要远程监测?
一开始我是想选择GPRS模块进行传输,一搜索发现连监控小程序都做好了,接上就能用了(bushi),但价格稍微贵了点,直到发现了esp8266模块,这里选择esp8266-01s(建议),价格低廉,功能强大,废话不多说,下边开始准备吧。
实验器材
esp8266-01s,USB to TTL模块(有最好,没有也影响不大),杜邦线若干
DS18B20温度传感器
arduino mega2560(其他型号同理)
实验准备(onenet)
搜索中国移动onenet,注册后登入,进入控制台

图一
选择TCP透传(我这里之前已经添加过一项了)点击右侧添加产品(随意填写)

图二

图三

图四
在设备列表里添加设备

图五

图六

图七
使用luastudio工具打开,lua脚本语句很长,为了帮助刚入门开发者只详细介绍device_data_analyze(dev)函数使用,其他详细可学习开发文档

语句较多,适当了解注释,后续有用

Json
OneNET将系统的基础消息格式设计为json格式,其数据可以映射为虚拟的表,数据中的Key对应表的列,Value对应列值
可以将表示的一组数据转换为字符串,然后就可以在网络或者程序之间轻松地传递这个字符串,并在需要的时候将它还原为各编程语言所支持的数据格式,易于机器解析和生成,并有效地提升网络传输效率

添加设备信息(lua脚本添加)

解析函数添加自定义代码
function device_data_analyze(dev) local t={} local a=0 -- 添加用户自定义代码 -- -- 例如: -- local s = dev:size() add_val(t,"ds18b20_test",a,dev:bytes(1,s),"yqer123") dev:response() dev:send("received") -- return $1,$2 -- -- 例如: -- return s,to_json(t)end t--table数据点存储,“ds18b20_test”数据流名,无需在设备内新建,a--时间戳,为0则表示当前时间
dev:bytes(1,s) 返回数据,1,数据起始点,s数据位数,不修改则默认数据长度,多设备连接时可根据数据包中各设备采集位数情况抓取
后边接上设备鉴权信息,传输到产品里的该设备
修改完毕保存,即上传lua至平台,串口发送报文之后将设备与lua脚本关联
测试esp8266模块(不连接单片机)
这里先加入一个中断,不着急,我们先测试一下esp8266模块是否正常
| esp826601s | USB to TTL |
| 3V3 | 3V3 |
| RX | TX |
| TX | RX |
| GND | GND |
表一

图八
接入计算机,打开串口调试助手调试(顺便打开手机热点设置wifi名和密码,不要用中文,尽量简单)

图九

AT+UART=9600,8,1,0,0
(回车)
设置WiFi应用模式AT+CWMODE=3(1、station模式2、AP模式3、station+AP)

每一步注意回车再发送
图十

AT+CWJAP="HUAWEI nova3","12345678#%"
图十一

图十二

AT+CIPSTART="TCP","183.230.40.40",1811
图十三
图片太多了显得冗长,直接上指令AT+CIPMODE=1(透传模式),返回OK后再AT+CIPSEND(传输数据)

这里是有两步操作
图十四
与服务器通讯
指令 *产品id(不是设备id)#设备鉴权信息#脚本名(不一定是脚本文件名)*

图十五
得到响应,显示在线

图十五
接入单片机
| arduinomega2560 | esp826601s | DS18B20温度传感器 |
| TX | RX | |
| RX | TX | |
| 7 | DAT | |
| GND | GND | GND |
| 5V 3.3V(注意) | 3.3V | VCC |
表二
完整程序
#include <OneWire.h> #include <DallasTemperature.h> //ds18b20库#define Onewire_bus 7 //ds18b20连接7引脚OneWire oneWire(Onewire_bus);DallasTemperature sensors(&oneWire);//boolean 成功返回true,失败返回falseboolean send_cmd(String data, char *keyword){ boolean result = false; if (data != "") //对于tcp连接命令,直接等待第二次回复 { Serial.println(data); //发送AT指令 } if (data == "AT") //寻找esp8266是否正常工作 delay(1000); else while (!Serial.available()); // 等待wifi模块应答 delay(200); if (Serial.find(keyword)) //返回关键词判断,比如ok { return true; } else { return false; } while (Serial.available()) Serial.read(); //清空串口缓存 delay(500); }void setup() { // put your setup code here, to run once: Serial.begin(9600); while (!send_cmd("AT", "OK")); while (!send_cmd("AT+CWMODE=3", "OK")); //工作模式 while (!send_cmd("AT+CWJAP=\"HUAWEI nova3\",\"1234567#%$\"", "OK")); //接入AP while (!send_cmd("AT+CIPSTART=\"TCP\",\"183.230.40.40\",1811", "OK")); //接入服务器 while (!send_cmd("AT+CIPMODE=1", "OK")); //透传模式 while (!send_cmd("AT+CIPSEND", ">")); //开始发送 Serial.println("*514523#yqer123#test1*"); //发送报文信息,产品ID+设备鉴权码+脚本名称 }void loop() { // put your main code here, to run repeatedly: delay(50);// Serial.print("获取温度:"); // 发送几个字 sensors.requestTemperatures(); //获取温度// Serial.println(sensors.getTempCByIndex(0)); float temp=sensors.getTempCByIndex(0); Serial.print(String(temp));}串口监测结果,响应较慢,可以适当增加延时

onenet平台实时输出数据

注意事项
本次测试采用硬串口,烧录程序之前,单片机TX,RX不能接入,否则将无法上传
尽量别带电接线
后添加图片较多,就不排序了
遗漏地方再补上
图方便本次实验只接入一台设备,多设备场景类似


//-------------------------------------分割线-------------------------------------------
前面单设备已经很详细了
多设备其实同理,只需要在lua里将数据流赋给相应的设备(通过设备鉴权信息绑定)就可以了
有需求我就继续更一下吧

添加多台设备,设置不同鉴权信息并记录好

打开lua脚本,添加设备鉴权信息

dev.bytes选择起始点和位数

设备已在线,打开数据流观察

已将截取段发送给各个设备


注意事项2
程序段只需绑定产品id和其中一个设备鉴权信息就可以了,其他设备在脚本里完成
多数据输出格式
Serial.print(String(int(wd))+String(int(sd))+ String(float(deepcm))+String(int(TU_value))+String(int(tdsValue)))后续再更新可视化操作(应用开发似乎阉割了)或者其他单片机应用
其他接入协议也欢迎交流
整理不易,点赞是我更新的动力

作者:麓南的风
来源:CSDN
原文:https://blog.csdn.net/qq_45998204/article/details/124686714
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件
作者:LoveArduinos
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- js工具函数,自己封装一个节流函数
- 亚马逊在中国的另一面
- Demonstration of combination of dream CAD cloud map and GIS
- 股票开户用客户经理发的开户链接安全吗?知道的给说一下吧
- ICML 2022 | 字节跳动 AI Lab 提出多模态模型:X-VLM,学习视觉和语言的多粒度对齐...
- Now, the ear is going into the metauniverse
- 可能是拿反了的原因
- 同花顺证券开户是安全的吗?
- Redis related-03
- Tutorial on installing SSL certificates in Microsoft Exchange Server 2007
猜你喜欢

Easynvr fails to use onvif to detect the device. What is the reason why "no data" is displayed?

TensorFlow,危!抛弃者正是谷歌自己

About sizeof() and strlen in array

X86 CPU, critical! The latest vulnerability has caused heated discussion. Hackers can remotely steal keys. Intel "all processors" are affected

后台页制作01《ivx低代码签到系统制作》

腾讯开源项目「应龙」成Apache顶级项目:前身长期服务微信支付,能hold住百万亿级数据流处理...

Collaboration + Security + storage, cloud box helps Shenzhen edetai restructure its data center

签到功能完成03《ivx低代码签到系统制作》

EasyNVR使用Onvif探测设备失败,显示“无数据”是什么原因?

Does it count as staying up late to sleep at 2:00 and get up at 10:00? Unless you can do it every day
随机推荐
老叶的祝福
香蕉为什么能做随机数生成器?因为,它是水果界的“辐射之王”
There is the word "Internet" in the concept of industrial Internet, but it is an existence that is not related to the Internet
Is it safe to open a stock account with the customer's haircut account link? Tell me what you know
Xidian AI ranked higher than Qingbei in terms of AI majors, and Nantah ranked the first in China in 2022 in terms of soft science majors
Lao Ye's blessing
Two way combination of business and technology to build a bank data security management system
How to choose a securities company when opening an account with a compass? Which is safer
IE寿终正寝,网友们搞起了真·墓碑……
Is it safe to open a stock account on Huatai Securities?
Nacos practice record
Skywalking implements cross thread trace delivery
太极图形60行代码实现经典论文,0.7秒搞定泊松盘采样,比Numpy实现快100倍
The programmer reality show is coming again! Hulan, as the host, carried the lamp to fill the knowledge. The SSS boss had a bachelor's degree in pharmacy. Zhu Jun and Zhang Min from Tsinghua joined th
Insurance app aging service evaluation analysis 2022 issue 06
Peking University has a new president! Gongqihuang, academician of the Chinese Academy of Sciences, took over and was admitted to the Physics Department of Peking University at the age of 15
China's SkyEye found suspicious signals of extraterrestrial civilization. Musk said that the Starship began its orbital test flight in July. Netinfo office: app should not force users to agree to proc
如何使用IDE自动签名调试鸿蒙应用
程序员真人秀又来了!呼兰当主持挑灯狂补知识,SSS大佬本科竟是药学,清华朱军张敏等加入导师团...
Winxp kernel driver debugging