-
Notifications
You must be signed in to change notification settings - Fork 0
/
lora_thingsboard.py
151 lines (125 loc) · 4.14 KB
/
lora_thingsboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Adafruit IO LoRa Gateway
Learn Guide: https://learn.adafruit.com/multi-device-lora-temperature-network
by Brent Rubell for Adafruit Industries
"""
#/usr/bin/python3
# Import Python System Libraries
import time
import os
import json
import paho.mqtt.client as mqtt
# Import Blinka Libraries
import busio
import board
from digitalio import DigitalInOut, Direction, Pull
# Import SSD1306 module.
import adafruit_ssd1306
# Import RFM9x module
import adafruit_rfm9x
# Define radio frequency, must match device.
RADIO_FREQ_MHZ = 905.5
# Button A
btnA = DigitalInOut(board.D5)
btnA.direction = Direction.INPUT
btnA.pull = Pull.UP
# Button B
btnB = DigitalInOut(board.D6)
btnB.direction = Direction.INPUT
btnB.pull = Pull.UP
# Button C
btnC = DigitalInOut(board.D12)
btnC.direction = Direction.INPUT
btnC.pull = Pull.UP
# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
# 128x32 OLED Display
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c)
# Clear the display.
display.fill(0)
display.show()
width = display.width
height = display.height
# Configure LoRa Radio
CS = DigitalInOut(board.CE1)
RESET = DigitalInOut(board.D25)
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
prev_packet = None
# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
# THINGSBOARD_USERNAME = os.getenv('THINGSBOARD_USERNAME')
# M0_FEATHER_ACCESS_TOKEN = os.environ.get('M0_FEATHER_ACCESS_TOKEN')
THINGSBOARD_HOST = '192.168.1.128'
THINGSBOARD_PORT = 49389
KEEPALIVE = 60
ACCESS_TOKEN = 'A1tO9zcKIjDe1DlqQ5ff'
# Create an instance of the mqtt client.
client = mqtt.Client()
# set the access token
client.username_pw_set(ACCESS_TOKEN)
#connect to things board instance, host, mqtt port, keepalive time
client.connect(THINGSBOARD_HOST, THINGSBOARD_PORT, KEEPALIVE)
sensor_data = {'temperature': 0, 'humidity': 0, 'pressure': 0}
temp_val = 0
humid_val = 0
pres_val = 0
def pkt_int_to_float(pkt_val_1, pkt_val_2, pkt_val_3=None):
"""Convert packet data to float.
"""
if pkt_val_3 is None:
float_val = pkt_val_1 << 8 | pkt_val_2
else:
float_val = pkt_val_1 << 16 | pkt_val_2 << 8 | pkt_val_3
return float_val/100
while True:
packet = None
# draw a box to clear the image
display.fill(0)
display.text('Adafruit.IO LoRa GTWY', 0, 0, 1)
# check for packet rx
packet = rfm9x.receive()
if packet is None:
display.show()
display.text('- Waiting for PKT -', 10, 20, 1)
else:
prev_packet = packet
print('> New Packet!')
# Decode packet
print(packet[1])
print(packet[2])
print(packet[3])
print(packet[4])
print(packet[-1])
sensor_data['temperature'] = pkt_int_to_float(packet[1], packet[2])
sensor_data['humidity'] = humid_val = pkt_int_to_float(packet[3], packet[4])
sensor_data['pressure'] = pres_val = pkt_int_to_float(packet[5], packet[6], packet[7])
# batt_val = pkt_int_to_float(packet[8])
# Display packet information
print('Device ID: LoRa Feather #', packet[0])
print("Temp: %0.2f C" % temp_val)
print("Humid: %0.2f %% " % humid_val)
print("Pressure: %0.2f hPa" % pres_val)
# Send to Feather 1 feeds
if packet[0] == 0x01:
display.fill(0)
display.text('Feather #1 Data RX''d!', 15, 0, 1)
display.text('Sending to IO...', 0, 20, 1)
display.show()
print(temp_val)
print(humid_val)
print(pres_val)
client.publish('v1/devices/me/telemetry', json.dumps(sensor_data), 1)
display.text('Sent!', 100, 20, 1)
display.show()
# Send to Feather 2 feeds
if packet[0] == 0x02:
display.fill(0)
display.text('Feather #2 Data RX''d!', 15, 0, 1)
display.text('Sending to IO...', 0, 20, 1)
display.show()
client.publish('v1/devices/me/telemetry', json.dumps(sensor_data), 1)
display.text('Sent!', 100, 20, 1)
display.show()
time.sleep(1)
display.show()