当前位置:网站首页>物联网平台thingsboard搭建学习记录
物联网平台thingsboard搭建学习记录
2022-07-01 18:44:00 【无极小卒】
概述:在thingsboard官网下载安装包,根据官网提供的文档进行安装配置thingsboard服务。通过esp32模拟智能节点,使用mqtt协议将数据上报至平台,进行动态的可视化展示。并通过wereshark抓取上报过程中的mqtt报文,进行安全性分析。任务内容做要是做一个安全分析的demo。
搭建所涉及的技术栈:
PostgreSQL+Rabbitmq+MQTTBox-win
产生的相关代码:
esp32运行microPy代码模拟终端产生温度数据:
import network
import time
import random
import machine
import json
import ubinascii
from simple import MQTTClient
SSID="WiFi名称"
PASSWORD="你的wifi密码"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
CONFIG = {
"server": "192.168.31.11", # ThingsBoard地址
"port":1883, # MQTT端口(默认1883)
"mqtt_user": "TCCWopryNcNCSl8iSo3Q", # 设备的访问令牌
"mqtt_password": "TCCWopryNcNCSl8iSo3Q", # 设备的访问令牌
"mqtt_topic": "v1/devices/me/telemetry", # MQTT主题
}
#连接WiFi函数
def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
wlan.connect(ssid,passwd)
while(wlan.ifconfig()[0]=='0.0.0.0'):
time.sleep(1)
#获取温湿度函数
def get_humiture():
d = random.randint(0,100)
return d
#上传温湿度到ThingsBoard函数
def pub_humiture(temperature):
global c
data={'hello':temperature}
c.publish(CONFIG["mqtt_topic"], json.dumps(data))
print(json.dumps(data))
def main():
connectWifi(SSID,PASSWORD)
global c
c = MQTTClient(CLIENT_ID, CONFIG["server"],CONFIG["port"], CONFIG["mqtt_user"], CONFIG["mqtt_password"])
c.connect()
print("Connected to %s, publish to %s topic" % (CONFIG["server"], CONFIG["mqtt_topic"]))
print("即将进入循环!")
while True:
tem=get_humiture()
pub_humiture(tem)
time.sleep(5)
if __name__ == "__main__":
main()
Java 中间版本:
package org.jmqtt.java;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class ThingsboardClient {
public void toTB(String content){
System.out.println("开始执行DHT方法*************************");
String HOST = "tcp://127.0.0.1:1883"; //TBip地址和mqtt端口
String TOPIC = "v1/devices/me/telemetry";//TB遥测数据API接口
String clientid = "jmqttThingsBoardClient";
MqttClient client=null;
MqttConnectOptions options;
MqttTopic topic;
String username="TCCWopryNcNCSl8iSo3Q";//设备秘钥
try {
// host为主机名,clientid即连接MQTT的客户端ID,一般以唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
client = new MqttClient(HOST, clientid, new MemoryPersistence());
// MQTT的连接设置
options = new MqttConnectOptions();
// 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
options.setCleanSession(true);
// 设置连接的用户名
options.setUserName(username);//设备秘钥
// 设置连接的密码
//options.setPassword(passWord.toCharArray());
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
options.setKeepAliveInterval(20);
// 设置回
client.setCallback(new MqttCallbackExtended() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String context = new String(message.getPayload());
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// TODO Auto-generated method stub
}
@Override
public void connectionLost(Throwable cause) {
// TODO Auto-generated method stub
}
@Override
public void connectComplete(boolean reconnect, String serverURI) {
// TODO Auto-generated method stub
}
});
topic = client.getTopic(TOPIC);
//setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息
options.setWill(topic, "close".getBytes(), 2, true);
client.connect(options);
// sendCon = "{\"temperature\":\"25\",\"humidity\":\"12\",\"light\":\"255\"}";//json格式
client.publish(TOPIC, content.getBytes(), 0, false);//推送数据到tb上
} catch (MqttException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} finally {
}
}
}
边栏推荐
- Prices of Apple products rose across the board in Japan, with iphone13 up 19%
- AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
- 生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度
- Viewing technological changes through Huawei Corps (VI): smart highway
- 论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
- ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
- Today, with the popularity of micro services, how does service mesh exist?
- Lefse analysis
- Love business in Little Red Book
- Lake Shore continuous flow cryostat transmission line
猜你喜欢
Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!
Viewing technological changes through Huawei Corps (VI): smart highway
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
制造业SRM管理系统供应商全方位闭环管理,实现采购寻源与流程高效协同
Lake Shore—CRX-EM-HF 型低温探针台
华为游戏初始化init失败,返回错误码907135000
Lake shore M91 fast hall measuring instrument
寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
混沌工程平台 ChaosBlade-Box 新版重磅发布
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
随机推荐
如何使用物联网低代码平台进行个人设置?
How to realize the applet in its own app to realize continuous live broadcast
华为联机对战服务玩家掉线重连案例总结
Three simple methods of ES6 array de duplication
网易游戏,激进出海
Lumiprobe free radical analysis h2dcfda instructions
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
Implement a Prometheus exporter
Chaos engineering platform chaosblade box new heavy release
市值蒸发740亿,这位大佬转身杀入预制菜
VBA simple macro programming of Excel
透过华为军团看科技之变(六):智慧公路
混沌工程平台 ChaosBlade-Box 新版重磅发布
Lumiprobe 自由基分析丨H2DCFDA说明书
组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
[pytorch record] distributed training dataparallel and distributeddataparallel of the model
Team up to learn! 14 days of Hongmeng equipment development "learning, practicing and testing" practical camp, free of charge!
Lake shore M91 fast hall measuring instrument
ETL development of data warehouse (IV)
linux下清理系统缓存并释放内存