当前位置:网站首页>(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
2022-07-06 09:19: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)
endt--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,失败返回false
boolean 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)))后续再更新可视化操作(应用开发似乎阉割了)或者其他单片机应用
其他接入协议也欢迎交流
整理不易,点赞是我更新的动力

边栏推荐
- Experience summary of autumn recruitment of state-owned enterprises
- Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
- Atomic and nonatomic
- What are the advantages of using SQL in Excel VBA
- Exception: ioexception:stream closed
- 阿里云一面:并发场景下的底层细节 - 伪共享问题
- 10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
- 十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
- Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
- String class
猜你喜欢

Edit distance (multi-source BFS)

面渣逆袭:Redis连环五十二问,三万字+八十图详解。

13 power map

国企秋招经验总结

C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
![[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers

Chromatic judgement bipartite graph

图书管理系统小练习

Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche

Wechat applet development experience
随机推荐
Experience summary of autumn recruitment of state-owned enterprises
记录:下一不小心写了个递归
RTKLIB: demo5 b34f. 1 vs b33
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
Record: solution of 404 error of servlet accessing database in dynamic web project
4.30动态内存分配笔记
初识C语言(上)
[Topic terminator]
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
Role movement in the first person perspective
[GNSS data processing] Helmert variance component estimation analysis and code implementation
Redis介绍与使用
几道高频的JVM面试题
Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
121道分布式面试题和答案
Rt-ppp test using rtknavi
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K