完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
通过LoRa模块SX1268点对点通信,树莓派接收并发送数据到阿里云物联网平台
前言关于如何使用树莓派将设备上云,我前一篇博客有详细介绍了。这里我主要记录SX1268模块点对点通信的使用和如何通过串口发送和接收数据并上传到阿里云物联网平台 SX1268模块在使用之前需要对它进行配置一些参数: 1、首先将SX1268模块的工作模式设置为配置模式:M0 = 0,M1 = 1 然后使用u***线连接到PC机,打开串口 2、在串口监视器输入以下命令: c1 05 01 01 c1 00 04 03 04 00 61 !我这里配置信道为01,模块地址为0x0304、网络地址为0x00、串口为9600 8N1、空速为1.2K,俩个SX1268模块配置的参数都要相同。配置好之后,这俩个模块就可以正常的通过串口收发数据了。 3、配置好之后就接线,PC机通过u***线接一个SX1268模块,树莓派端直接把SX1268模块插进去就行,进入exmaple示例代码中的thing_alinkk.py文件中,修改设备三元组和把串口通信程序加进去,完整代码在下面,运行成功之后就可以通过PC端串口监视器看到树莓派发过来的数据,PC端发送数据过去之后在树莓派上面可以正常打印出来,而且在阿里云物联网平台上面也可以看到是有数据上传过来了的。 完整代码。 import sys import re from linkkit import linkkit import threading import traceback import inspect import time import logging import serial #串口通信引入的包 # config log __log_format = '%(asctime)s-%(process)d-%(thread)d - %(name)s:%(module)s:%(funcName)s - %(levelname)s - %(message)s' logging.basicConfig(format=__log_format) class CustomerThing(object): def __init__(self): self.__linkkit = linkkit.LinkKit( host_name="cn-shanghai", product_key="xxxxxxxx", #修改设备三元组 device_name="xxxxxxxxxxxxx", device_secret="xxxxxxxxxxxxxxxxxxxxxxx" ) self.__linkkit.enable_logger(logging.DEBUG) self.__linkkit.on_device_dynamic_register = self.on_device_dynamic_register self.__linkkit.on_connect = self.on_connect self.__linkkit.on_disconnect = self.on_disconnect self.__linkkit.on_topic_message = self.on_topic_message self.__linkkit.on_subscribe_topic = self.on_subscribe_topic self.__linkkit.on_unsubscribe_topic = self.on_unsubscribe_topic self.__linkkit.on_publish_topic = self.on_publish_topic self.__linkkit.on_thing_enable = self.on_thing_enable self.__linkkit.on_thing_disable = self.on_thing_disable self.__linkkit.on_thing_event_post = self.on_thing_event_post self.__linkkit.on_thing_prop_post = self.on_thing_prop_post self.__linkkit.on_thing_prop_changed = self.on_thing_prop_changed self.__linkkit.on_thing_call_service = self.on_thing_call_service self.__linkkit.on_thing_raw_data_post = self.on_thing_raw_data_post self.__linkkit.on_thing_raw_data_arrived = self.on_thing_raw_data_arrived self.__linkkit.thing_setup("tsl.json") self.__linkkit.config_device_info("Eth|03ACDEFF0032|Eth|03ACDEFF0031") self.__call_service_request_id = 0 def on_device_dynamic_register(self, rc, value, userdata): if rc == 0: print("dynamic register device success, value:" + value) else: print("dynamic register device fail, message:" + value) def on_connect(self, session_flag, rc, userdata): print("on_connect:%d,rc:%d,userdata:" % (session_flag, rc)) def on_disconnect(self, rc, userdata): print("on_disconnect:rc:%d,userdata:" % rc) def on_topic_message(self, topic, payload, qos, userdata): print("on_topic_message:" + topic + " payload:" + str(payload) + " qos:" + str(qos)) pass def on_subscribe_topic(self, mid, granted_qos, userdata): print("on_subscribe_topic mid:%d, granted_qos:%s" % (mid, str(','.join('%s' % it for it in granted_qos)))) pass def on_unsubscribe_topic(self, mid, userdata): print("on_unsubscribe_topic mid:%d" % mid) pass def on_publish_topic(self, mid, userdata): print("on_publish_topic mid:%d" % mid) def on_thing_prop_changed(self, params, userdata): print("on_thing_prop_changed params:" + str(params)) def on_thing_enable(self, userdata): print("on_thing_enable") def on_thing_disable(self, userdata): print("on_thing_disable") def on_thing_event_post(self, event, request_id, code, data, message, userdata): print("on_thing_event_post event:%s,request id:%s, code:%d, data:%s, message:%s" % (event, request_id, code, str(data), message)) pass def on_thing_prop_post(self, request_id, code, data, message,userdata): print("on_thing_prop_post request id:%s, code:%d, data:%s message:%s" % (request_id, code, str(data), message)) def on_thing_raw_data_arrived(self, payload, userdata): print("on_thing_raw_data_arrived:%s" % str(payload)) def on_thing_raw_data_post(self, payload, userdata): print("on_thing_raw_data_post: %s" % str(payload)) def on_thing_call_service(self, identifier, request_id, params, userdata): print("on_thing_call_service identifier:%s, request id:%s, params:%s" % (identifier, request_id, params)) self.__call_service_request_id = request_id pass def user_loop(self): self.__linkkit.connect_async() tips = "1: disconnectn" + "2 connect&loopn" + "3 subscribe topicn" + "4 unsubscribe topicn" + "5 public topicn" + "" while True: # 获得接受缓冲区字符 count = ser.inWaiting() if count != 0: #读取内容并显示 recv = ser.read(count) print("receive data is : " + str(recv)+'n') #将串口接受到的数据(字节型)转换成字符串 str0 = str(recv) #对b''进行替换 str1 = re.sub("b","",str0) str2 = re.sub("'","",str1) int1 = int(str2) print(int1) #将整形变量int1上传到阿里云中,标识符为WaterLevel prop_data = { "WaterLevel": int1 } self.__linkkit.thing_post_property(prop_data) if __name__ == "__main__": #打开串口 ser = serial.Serial('/dev/ttyS0', 9600) if ser.isOpen == False: ser.open() #打开串口 custom_thing = CustomerThing() custom_thing.user_loop() |
|
|
|
只有小组成员才能发言,加入小组>>
3263 浏览 9 评论
2944 浏览 16 评论
3443 浏览 1 评论
8955 浏览 16 评论
4036 浏览 18 评论
1081浏览 3评论
561浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
554浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2289浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1849浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-10 22:48 , Processed in 0.876935 second(s), Total 50, Slave 40 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号