code.py
1 # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 Adafruit IO LoRa Gateway 7 8 Learn Guide: https://learn.adafruit.com/multi-device-lora-temperature-network 9 10 by Brent Rubell for Adafruit Industries 11 """ 12 # Import Python System Libraries 13 import time 14 15 # Import Adafruit IO REST client. 16 from Adafruit_IO import Client 17 18 # Import Blinka Libraries 19 import busio 20 import board 21 from digitalio import DigitalInOut, Direction, Pull 22 23 # Import SSD1306 module. 24 import adafruit_ssd1306 25 26 # Import RFM9x module 27 import adafruit_rfm9x 28 29 # Define radio frequency, must match device. 30 RADIO_FREQ_MHZ = 905.5 31 32 # Button A 33 btnA = DigitalInOut(board.D5) 34 btnA.direction = Direction.INPUT 35 btnA.pull = Pull.UP 36 37 # Button B 38 btnB = DigitalInOut(board.D6) 39 btnB.direction = Direction.INPUT 40 btnB.pull = Pull.UP 41 42 # Button C 43 btnC = DigitalInOut(board.D12) 44 btnC.direction = Direction.INPUT 45 btnC.pull = Pull.UP 46 47 # Create the I2C interface. 48 i2c = busio.I2C(board.SCL, board.SDA) 49 50 # 128x32 OLED Display 51 display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c) 52 # Clear the display. 53 display.fill(0) 54 display.show() 55 width = display.width 56 height = display.height 57 58 # Configure LoRa Radio 59 CS = DigitalInOut(board.CE1) 60 RESET = DigitalInOut(board.D25) 61 spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) 62 rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ) 63 prev_packet = None 64 65 # Set to your Adafruit IO username. 66 # (go to https://accounts.adafruit.com to find your username) 67 ADAFRUIT_IO_USERNAME = 'USER' 68 69 # Set to your Adafruit IO key. 70 ADAFRUIT_IO_KEY = 'KEY' 71 72 # Create an instance of the REST client. 73 aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) 74 75 # Set up Adafruit IO feeds 76 temperature_feed_1 = aio.feeds('feather-1-temp') 77 humidity_feed_1 = aio.feeds('feather-1-humid') 78 pressure_feed_1 = aio.feeds('feather-1-pressure') 79 80 temperature_feed_2 = aio.feeds('feather-2-temp') 81 humidity_feed_2 = aio.feeds('feather-2-humid') 82 pressure_feed_2 = aio.feeds('feather-2-pressure') 83 84 def pkt_int_to_float(pkt_val_1, pkt_val_2, pkt_val_3=None): 85 """Convert packet data to float. 86 """ 87 if pkt_val_3 is None: 88 float_val = pkt_val_1 << 8 | pkt_val_2 89 else: 90 float_val = pkt_val_1 << 16 | pkt_val_2 << 8 | pkt_val_3 91 return float_val/100 92 93 while True: 94 packet = None 95 # draw a box to clear the image 96 display.fill(0) 97 display.text('Adafruit.IO LoRa GTWY', 0, 0, 1) 98 99 # check for packet rx 100 packet = rfm9x.receive() 101 if packet is None or prev_packet: 102 display.show() 103 display.text('- Waiting for PKT -', 10, 20, 1) 104 else: 105 prev_packet = packet 106 print('> New Packet!') 107 # Decode packet 108 temp_val = pkt_int_to_float(packet[1], packet[2]) 109 humid_val = pkt_int_to_float(packet[3], packet[4]) 110 pres_val = pkt_int_to_float(packet[5], packet[6], packet[7]) 111 112 # Display packet information 113 print('Device ID: LoRa Feather #', packet[0]) 114 print("Temp: %0.2f C" % temp_val) 115 print("Humid: %0.2f %% " % humid_val) 116 print("Pressure: %0.2f hPa" % pres_val) 117 118 # Send to Feather 1 feeds 119 if packet[0] == 0x01: 120 display.fill(0) 121 display.text('Feather #1 Data RX''d!', 15, 0, 1) 122 display.text('Sending to IO...', 0, 20, 1) 123 display.show() 124 aio.send(temperature_feed_1.key, temp_val) 125 aio.send(humidity_feed_1.key, humid_val) 126 aio.send(pressure_feed_1.key, pres_val) 127 display.text('Sent!', 100, 20, 1) 128 display.show() 129 # Send to Feather 2 feeds 130 if packet[0] == 0x02: 131 display.fill(0) 132 display.text('Feather #2 Data RX''d!', 15, 0, 1) 133 display.text('Sending to IO...', 0, 20, 1) 134 display.show() 135 aio.send(temperature_feed_2.key, temp_val) 136 aio.send(humidity_feed_2.key, humid_val) 137 aio.send(pressure_feed_2.key, pres_val) 138 display.text('Sent!', 100, 20, 1) 139 display.show() 140 time.sleep(1) 141 142 display.show()