/ pi_radio / radio_lorawan.py
radio_lorawan.py
  1  # SPDX-FileCopyrightText: 2018 Brent Rubell for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  """
  6  Example for using the RFM9x Radio with Raspberry Pi and LoRaWAN
  7  
  8  Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
  9  Author: Brent Rubell for Adafruit Industries
 10  """
 11  import threading
 12  import time
 13  import subprocess
 14  import busio
 15  from digitalio import DigitalInOut, Direction, Pull
 16  import board
 17  # Import thte SSD1306 module.
 18  import adafruit_ssd1306
 19  # Import Adafruit TinyLoRa
 20  from adafruit_tinylora.adafruit_tinylora import TTN, TinyLoRa
 21  
 22  # Button A
 23  btnA = DigitalInOut(board.D5)
 24  btnA.direction = Direction.INPUT
 25  btnA.pull = Pull.UP
 26  
 27  # Button B
 28  btnB = DigitalInOut(board.D6)
 29  btnB.direction = Direction.INPUT
 30  btnB.pull = Pull.UP
 31  
 32  # Button C
 33  btnC = DigitalInOut(board.D12)
 34  btnC.direction = Direction.INPUT
 35  btnC.pull = Pull.UP
 36  
 37  # Create the I2C interface.
 38  i2c = busio.I2C(board.SCL, board.SDA)
 39  
 40  # 128x32 OLED Display
 41  reset_pin = DigitalInOut(board.D4)
 42  display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, reset=reset_pin)
 43  # Clear the display.
 44  display.fill(0)
 45  display.show()
 46  width = display.width
 47  height = display.height
 48  
 49  # TinyLoRa Configuration
 50  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
 51  cs = DigitalInOut(board.CE1)
 52  irq = DigitalInOut(board.D22)
 53  rst = DigitalInOut(board.D25)
 54  
 55  # TTN Device Address, 4 Bytes, MSB
 56  devaddr = bytearray([0x00, 0x00, 0x00, 0x00])
 57  # TTN Network Key, 16 Bytes, MSB
 58  nwkey = bytearray([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 59                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
 60  # TTN Application Key, 16 Bytess, MSB
 61  app = bytearray([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 62                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
 63  # Initialize ThingsNetwork configuration
 64  ttn_config = TTN(devaddr, nwkey, app, country='US')
 65  # Initialize lora object
 66  lora = TinyLoRa(spi, cs, irq, rst, ttn_config)
 67  # 2b array to store sensor data
 68  data_pkt = bytearray(2)
 69  # time to delay periodic packet sends (in seconds)
 70  data_pkt_delay = 5.0
 71  
 72  
 73  def send_pi_data_periodic():
 74      threading.Timer(data_pkt_delay, send_pi_data_periodic).start()
 75      print("Sending periodic data...")
 76      send_pi_data(CPU)
 77      print('CPU:', CPU)
 78  
 79  def send_pi_data(data):
 80      # Encode float as int
 81      data = int(data * 100)
 82      # Encode payload as bytes
 83      data_pkt[0] = (data >> 8) & 0xff
 84      data_pkt[1] = data & 0xff
 85      # Send data packet
 86      lora.send_data(data_pkt, len(data_pkt), lora.frame_counter)
 87      lora.frame_counter += 1
 88      display.fill(0)
 89      display.text('Sent Data to TTN!', 15, 15, 1)
 90      print('Data sent!')
 91      display.show()
 92      time.sleep(0.5)
 93  
 94  while True:
 95      packet = None
 96      # draw a box to clear the image
 97      display.fill(0)
 98      display.text('RasPi LoRaWAN', 35, 0, 1)
 99  
100      # read the raspberry pi cpu load
101      cmd = "top -bn1 | grep load | awk '{printf \"%.1f\", $(NF-2)}'"
102      CPU = subprocess.check_output(cmd, shell = True )
103      CPU = float(CPU)
104  
105      if not btnA.value:
106          # Send Packet
107          send_pi_data(CPU)
108      if not btnB.value:
109          # Display CPU Load
110          display.fill(0)
111          display.text('CPU Load %', 45, 0, 1)
112          display.text(str(CPU), 60, 15, 1)
113          display.show()
114          time.sleep(0.1)
115      if not btnC.value:
116          display.fill(0)
117          display.text('* Periodic Mode *', 15, 0, 1)
118          display.show()
119          time.sleep(0.5)
120          send_pi_data_periodic()
121  
122  
123      display.show()
124      time.sleep(.1)