class LoRaWanRadio(Device): def __init__(self, spi, cs_pin, reset_pin, dev_addr, nwk_key, app_key, tx_led=None, name="lrw0", interval=10.0, *args, **kwargs): Device.__init__(self, name=name, interval=interval, *args, **kwargs) from adafruit_tinylora.adafruit_tinylora import TTN, TinyLoRa import digitalio cs = digitalio.DigitalInOut(cs_pin) reset = digitalio.DigitalInOut(reset_pin) self.tx_led = tx_led if self.tx_led: self.tx_led = digitalio.DigitalInOut(tx_led) self.tx_led.direction = digitalio.Direction.OUTPUT ttn_config = TTN(dev_addr, nwk_key, app_key, country='US') # Suppose to be cs, irq, rst, ttn_config? self.lora = TinyLoRa(spi, cs, reset, ttn_config) self.data["tx_buffer"] = None def write(self): if self.tx_led: self.tx_led.value = True if self.data["tx_buffer"]: data = self.data["tx_buffer"] self.lora.send_data(data, len(data), self.lora.frame_counter) print("LORAWAN TX: {}".format(self.data["tx_buffer"])) self.data["tx_buffer"] = None self.lora.frame_counter += 1 if self.tx_led: self.tx_led.value = False
lora = TinyLoRa(spi, cs, irq, ttn_config) # Data Packet to send to TTN data = bytearray(4) while True: temp_val = sensor.temperature humid_val = sensor.relative_humidity print('Temperature: %0.2f C' % temp_val) print('relative humidity: %0.1f %%' % humid_val) # Encode float as int temp_val = int(temp_val * 100) humid_val = int(humid_val * 100) # Encode payload as bytes data[0] = (temp_val >> 8) & 0xff data[1] = temp_val & 0xff data[2] = (humid_val >> 8) & 0xff data[3] = humid_val & 0xff # Send data packet print('Sending packet...') lora.send_data(data, len(data), lora.frame_counter) print('Packet Sent!') led.value = True lora.frame_counter += 1 time.sleep(2) led.value = False
while True: byte_data = uart.read(d_size) # read up to 32 bytes print('Reading UART ({} bytes): {}'.format(d_size, byte_data)) if byte_data is not None: led.value = True # convert bytearray to string #data_string = ''.join([chr(b) for b in data]) #print(data_string, end="") data = struct.unpack(data_format, byte_data) print(data) # Send data packet print("Sending packet...") lora.send_data(byte_data, len(byte_data), lora.frame_counter) print("Packet Sent!") led.value = True lora.frame_counter += 1 #time.sleep(2) led.value = False ''' import board import busio import digitalio # Data Packet to send to TTN data = bytearray(4)
lora = TinyLoRa(spi, cs, irq, ttn_config) # Data Packet to send to TTN data = bytearray(4) while True: temp_val = sensor.temperature humid_val = sensor.relative_humidity print('Temperature: %0.2f C' % temp_val) print('relative humidity: %0.1f %%' % humid_val) # Encode float as int temp_val = int(temp_val * 100) humid_val = int(humid_val * 100) # Encode payload as bytes data[0] = (temp_val >> 8) & 0xff data[1] = temp_val & 0xff data[2] = (humid_val >> 8) & 0xff data[3] = humid_val & 0xff # Send data packet print('Sending packet...') lora.send_data(data, len(data), lora.frame_counter) print('Packet Sent!') led.value = True lora.frame_counter += 1 time.sleep(2) led.value = False
# Create library object using our bus SPI port for radio spi = busio.SPI(board.SCK, MISO=board.MISO, MOSI=board.MOSI) # RFM9x Breakout Pinouts cs = digitalio.DigitalInOut(board.D6) irq = digitalio.DigitalInOut(board.D5) # TTN Device Address, 4 Bytes, MSB devaddr = bytearray([0x26, 0x01, 0x14, 0xE1]) # TTN Network Key, 16 Bytes, MSB nwkey = bytearray([0xAF, 0x6E, 0xE8, 0xC4, 0x20, 0x42, 0x62, 0x86, 0x96, 0xFB, 0xFB, 0xF7, 0x74, 0x00, 0xA7, 0xA1]) # TTN Application Key, 16 Bytess, MSB app = bytearray([0x7F, 0x06, 0x6A, 0xF1, 0xE2, 0xD9, 0xDB, 0x62, 0x53, 0x39, 0x53, 0xA7, 0x90, 0x9D, 0x60, 0xC2]) ttn_config = TTN(devaddr, nwkey, app, country='EU') lora = TinyLoRa(spi, cs, irq, ttn_config) # Data Packet to send to TTN # 7 bytes of Payload are working shortPayload = bytearray(7) # 8 bytes and more won't longPayload = bytearray(8) while True: print('Sending short packet...') lora.send_data(shortPayload, len(shortPayload), lora.frame_counter) lora.frame_counter += 1 print('Sending long packet...') lora.send_data(longPayload, len(longPayload), lora.frame_counter) lora.frame_counter += 1 time.sleep(5)
app = bytearray([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]) ttn_config = TTN(devaddr, nwkey, app, country='US') lora = TinyLoRa(spi, cs, irq, ttn_config, channel=6) # bme data packet bme_d = bytearray(7) while True: # Grab sensor data temp_val = int(bme280.temperature * 100) humid_val = int(bme280.humidity * 100) bme_d[0] = FEATHER_ID # Temperature data bme_d[1] = (temp_val >> 8) & 0xff bme_d[2] = temp_val & 0xff # Humidity data bme_d[3] = (humid_val >> 8) & 0xff bme_d[4] = humid_val & 0xff print('Sending packet...') lora.send_data(bme_d, len(bme_d), lora.frame_counter) print('Packet sent!') lora.frame_counter += 1 time.sleep(1 * 60)
return the temperature """ volts = ((mcp.read_adc(channel)) * (3.3)) / 1024 temperature = round(volts / (0.01), 2) return temperature def moisture(channel): """function to read the moisture from the sensor""" """channel: channel number of the mcp3008 adc to read from return the moisture as a percentage """ read = mcp.read_adc(channel) moisture_percent = ((read / 100) / (3.5)) * 100 return moisture_percent while True: #read the temperature and transmit the data via the gateway temp = bytearray(str(temperature(0)), "utf-8") temp_lora.send_data(temp, len(temp), temp_lora.frame_counter) temp_lora.frame_counter += 1 print("sent") #read the soil moisture and transmit the data via the gateway print(moisture(1)) moist = bytearray(str(moisture(1)), "utf-8") moisture_lora.send_data(moist, len(moist), moisture_lora.frame_counter) moisture_lora.frame_counter += 1 time.sleep(0.1)