编程语言
首页 > 编程语言> > 智能温室管理系统 2 树莓派lora收发程序python、NBiot发送数据到华为云平台

智能温室管理系统 2 树莓派lora收发程序python、NBiot发送数据到华为云平台

作者:互联网

第二部分是树莓派的部分

用的python3写的

功能

实现lora收发消息

通过spi口实现

收发NBIOT的数据

通过串口实现,注意树莓派的串口开始是不能用的,需要配置,当时我这个渣渣配了一天,我这个另起一个教程

可以直接复制下面的代码在树莓派里跑,但是需要lora的库。不下载的可以看一下代码思路

链接:是我树莓派上的工程源码,还有单独的树莓派收发程序,lora的库,找不到库的同学可以直接下我的

https://download.csdn.net/download/publicdust/11389151

 

 

直接上代码1

import sys
from time import sleep
from SX127x.LoRa import *
from SX127x.LoRaArgumentParser import LoRaArgumentParser
from SX127x.board_config import BOARD
BOARD.setup()

import serial
import time
import string
ser =serial.Serial("/dev/ttyAMA0",9600)
class LoRaAlldata(LoRa):
    
    def __init__(self, verbose=False):
        super(LoRaAlldata, self).__init__(verbose)
        self.set_mode(MODE.SLEEP)
    def on_rx_done(self):

        print("\nRxDone")
        self.clear_irq_flags(RxDone=1)
        payload = self.read_payload(nocheck=True)
        length =int(len(payload)/2)
        print("older length")
        print(length)
        print("older data")
        print(bytes(payload).decode("utf-8",'ignore'))
        flowing=str(bytes(payload).decode("utf-8",'ignore'))

        hum=flowing[12:22]
        temp=flowing[32:42]
        light=flowing[56:62]
        redlight=flowing[82:84]
        smoke=flowing[98:102]
        soil=flowing[114:120]
        
        merge=hum+temp+light+redlight+smoke+soil
        if flowing[1]=='1':
            merge_send="00"+merge
        elif flowing[1]=='2':
            merge_send="01"+merge
        length_merge_send=int(len(merge_send)/2)
        print(length_merge_send)
        print(merge_send)

        ser.write("AT+NMGS=".encode("utf-8"))
        ser.write(str(length_merge_send).encode("utf-8"))
        ser.write(",".encode("utf-8"))
        ser.write(merge_send.encode("utf-8"))
        ser.write("\n".encode("utf-8"))
        
        
        self.set_mode(MODE.SLEEP)
        self.reset_ptr_rx()
        self.set_mode(MODE.RXCONT)
        
    def on_tx_done(self):
        print("\nTxDone")
        
        self.set_mode(MODE.STDBY)
        self.clear_irq_flags(TxDone=1)
        self.write_payload([int(commend_one),int(commend_two),int(commend_three)])
        self.set_mode(MODE.TX)
        self.set_dio_mapping([0,0,0,0,0,0])
        self.set_mode(MODE.RXCONT)
        
        
    def on_cad_done(self):
        print("\non_CadDone")
        print(self.get_irq_flags())

    def on_rx_timeout(self):
        print("\non_RxTimeout")
        print(self.get_irq_flags())

    def on_valid_header(self):
        print("\non_ValidHeader")
        print(self.get_irq_flags())

    def on_payload_crc_error(self):
        print("\non_PayloadCrcError")
        print(self.get_irq_flags())

    def on_fhss_change_channel(self):
        print("\non_FhssChangeChannel")
        print(self.get_irq_flags())

    commend_one=''
    commend_two=''
    commend_three=''
    def start(self):
        global commend_one
        global commend_two
        global commend_three

        
        self.set_dio_mapping([0,0,0,0,0,0])
        self.set_mode(MODE.RXCONT)
        while True:
            slien=""
            ok=ser.readline()# read port data
            mmo=str(ser.readline())
            slien=mmo[10:-5]

            if len(slien)==0:
                self.set_dio_mapping([0,0,0,0,0,0])
                self.set_mode(MODE.RXCONT)
            else :
                commend_one=slien[3]
                commend_two=slien[5]
                commend_three=slien[7]
                
                print(commend_one)
                print(commend_two)
                print(commend_three)
                self.write_payload([int(commend_one),int(commend_two),int(commend_three)])
                self.set_dio_mapping([1,0,0,0,0,0])
                self.set_mode(MODE.TX)
            sleep(1)




lora = LoRaAlldata(verbose=False)
lora.set_pa_config(pa_select=1)
assert(lora.get_agc_auto_on() == 1)
try:
    input("Press enter to start...")
except: pass
try:
    
    lora.start()
except KeyboardInterrupt:
    sys.stdout.flush()
    print("")
    sys.stderr.write("KeyboardInterrupt\n")
finally:
    sys.stdout.flush()
    print("")
    lora.set_mode(MODE.SLEEP)
    print(lora)
    BOARD.teardown()

这个代码没写注释,我稍微解释一下,我再start里面默认lora处于接收模式,当接收到来自nbiot的数据,也就是当读到串口的数据的时候就切换成发送模式将数据通过lora发送给其他在同一频段的lora模块,之前没有接触过python,学了一上午语法乱写的,虽然不美观,但是可以用。

 


 

 

标签:树莓,set,NBiot,merge,self,commend,print,发送数据,lora
来源: https://blog.csdn.net/publicdust/article/details/96494673