-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_temp_data.py
More file actions
124 lines (99 loc) · 3.65 KB
/
Copy pathtx_temp_data.py
File metadata and controls
124 lines (99 loc) · 3.65 KB
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
"""
Purpose: Send "ack" LoRa messages with temperature data
Organization: San Diego Wildlife Association: Conservation Technology Lab (CTL)
Author: Trent Moca
Date: 11/10/22
Collect temperature data using gpiozero CPU temperature, then acknowledge send over LoRa.
Additionally, display internet data and LoRa status on the builtin OLED. If the packet was
unable to reach the indended destination (ie: ack was not received), indicate on the display.
This code may be used on the transmitting node in a 2-node test with the receiver running the
"rx_csv_make.py" code.
SOURCE
Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
Author: Brent Rubell for Adafruit Industries
"""
# Import Python System Libraries
import time
import subprocess
# Import Blinka Libraries
import busio
from digitalio import DigitalInOut, Direction, Pull
import board
# Import the SSD1306 module.
import adafruit_ssd1306
# Import RFM9x
import adafruit_rfm9x # https://github.com/adafruit/Adafruit_CircuitPython_RFM9x/blob/main/adafruit_rfm9x.py
# For accessing CPU temperature data
from gpiozero import CPUTemperature
btnA = DigitalInOut(board.D5)
btnA.direction = Direction.INPUT
btnA.pull = Pull.UP
def configDisplay():
# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
# 128x32 OLED Display
reset_pin = DigitalInOut(board.D4)
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, reset=reset_pin)
# Clear the display.
display.fill(0)
display.show()
return display
def getTemperature():
cpu = CPUTemperature()
return cpu.temperature # float, in degrees celsius
def configRadio():
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, 915.0)
rfm9x.tx_power = 23
rfm9x.ack_delay = 0.1
rfm9x.node = ord('t') # t as in tx -> inconsequential
rfm9x.destination = ord('r') # r as in rx -> rx_csv_make.py
return rfm9x
def sendTemp(rfm9x):
curr_time = int(time.time())
temp_str = str(curr_time) + ": " + str(getTemperature())
temp_data = bytes(temp_str, "utf-8")
return rfm9x.send_with_ack(temp_data)
# returns hostname, first ip, and second ip
def getHostData(): # derived from scrubcam "little_readout.py"
cmd = "hostname"
hostname = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "hostname -I | cut -d' ' -f1"
ip1 = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "hostname -I | cut -d' ' -f2"
ip2 = subprocess.check_output(cmd, shell=True).decode("utf-8")
return hostname, ip1, ip2
def getIP1():
host, i1, i2 = getHostData()
return i1
if __name__ == '__main__':
rfm9x = configRadio()
display = configDisplay()
spacing = int(display.height/4)
hostname, ip1, ip2 = getHostData()
host_text = "Host: " + hostname
ip1_text = "IP1: " + ip1
ip2_text = "IP2: " + ip2
# Send LoRa, but mostly fill and refresh the display
while True:
if not btnA.value:
display.fill(0)
display.text(ip1_text, 0, spacing, 1)
display.show()
break
time.sleep(3)
got_ack = sendTemp(rfm9x)
display.fill(0)
display.show()
display.text(host_text, 0, 0, 1)
if len(ip1) < 2:
ip1_text = 'IP1: ' + getIP1() # update ip if none at start
display.text(ip1_text, 0, spacing, 1)
# Update LoRa status
if (got_ack):
display.text('Sent Successfully', 0, 3*spacing, 1)
else:
display.text('Unable to reach destination', 0, 3*spacing, 1)
display.show()