-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThunderStorm.py
More file actions
237 lines (197 loc) · 7.57 KB
/
ThunderStorm.py
File metadata and controls
237 lines (197 loc) · 7.57 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Write your code here :-)
# Simple GPS module demonstration.
# Will wait for a fix and print a message every second with the current location
# and other details.
import time
import board
import busio
import digitalio
import analogio
import adafruit_lsm9ds1
from busio import I2C
import adafruit_bme680
import adafruit_gps
import adafruit_rfm9x
import adafruit_max31865
# Device ID
FEATHER_ID = b'4'
print("start up")
vbat_voltage = analogio.AnalogIn(board.VOLTAGE_MONITOR)
# Define pins connected to the chip, use these if wiring up the breakout according to the guide:
# pylint: disable=c-extension-no-member
CS = digitalio.DigitalInOut(board.D10)
HR = digitalio.DigitalInOut(board.A0)
# pylint: disable=c-extension-no-member
RESET = digitalio.DigitalInOut(board.D11)
# Define the onboard LED
LED = digitalio.DigitalInOut(board.D13)
LED.direction = digitalio.Direction.OUTPUT
# Initialize SPI bus.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Define radio frequency, MUST match gateway frequency.
RADIO_FREQ_MHZ = 433.0
# Initialze RFM radio
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
# Set transmit power to max
rfm9x.tx_power = 23
# Define RX and TX pins for the board's serial port connected to the GPS.
# These are the defaults you should use for the GPS FeatherWing.
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
RX = board.RX
TX = board.TX
# Create a serial connection for the GPS connection using default speed and
# a slightly higher timeout (GPS modules typically update once a second).
uart = busio.UART(TX, RX, baudrate=9600, timeout=1)
#uartPayload = busio.UART(board.A1, RX, baudrate=9600, timeout=30)
# for a computer, use the pyserial library for uart access
#import serial
#uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3000)
# Create a GPS module instance.
#gps = adafruit_gps.GPS(uart, debug=False)
# Initialize the GPS module by changing what data it sends and at what rate.
# These are NMEA extensions for PMTK_314_SET_NMEA_OUTPUT and
# PMTK_220_SET_NMEA_UPDATERATE but you can send anything from here to adjust
# the GPS module behavior:
# https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf
# Turn on the basic GGA and RMC info (what you typically want)
#gps.send_command(b'PMTK314,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
# Turn on just minimum info (RMC only, location):
#gps.send_command(b'PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
# Turn off everything:
#gps.send_command(b'PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
# Tuen on everything (not all of it is parsed!)
#gps.send_command(b'PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0')
# Set update rate to once a second (1hz) which is what you typically want.
#gps.send_command(b'PMTK220,1000')
# Or decrease to once every two seconds by doubling the millisecond value.
# Be sure to also increase your UART timeout above!
#gps.send_command(b'PMTK220,2000')
# You can also speed up the rate, but don't go too fast or else you can lose
# data during parsing. This would be twice a second (2hz, 500ms delay):
#gps.send_command(b'PMTK220,500')
# Main loop runs forever printing the location, etc. every second.
last_print = time.monotonic()
# I2C connection:
i2c = busio.I2C(board.SCL, board.SDA)
#sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
# Create library object using our Bus I2C port
#i2c = I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c, debug=False)
# change this to match the location's pressure (hPa) at sea level
bme680.sea_level_pressure = 980
#SPI connection:
# from digitalio import DigitalInOut, Direction
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# csag = DigitalInOut(board.D5)
# csag.direction = Direction.OUTPUT
# csag.value = True
# csm = DigitalInOut(board.D6)
# csm.direction = Direction.OUTPUT
# csm.value = True
# sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm)
# Main loop will read the acceleration, magnetometer, gyroscope, Temperature
# values every second and print them out.
pt100 = adafruit_max31865.MAX31865(spi, HR, rtd_nominal=100, ref_resistor=430.0, wires=3)
def sendMessage(message):
try:
LED.value = True
rfm9x.send(FEATHER_ID+b','+message)
#print(FEATHER_ID+b','+message)
time.sleep(.5)
LED.value = False
except:
print("Message failed to send")
def getDat():
temp = pt100.temperature
RTemp = str('{0:0.3f}'.format(temp))
B = 'ResTemp,'+ RTemp
t = str(bme680.temperature)
g = str(bme680.gas)
h = str(bme680.humidity)
p = str(bme680.pressure)
a = str(bme680.altitude)
battery_voltage = get_voltage(vbat_voltage)
#print("VBat voltage: {:.2f}".format(battery_voltage))
v = "{:.2f}".format(battery_voltage)
A = t + ',' + g + ',' + h + ',' + p + ',' + a
datstr = "DATA,BME," + A + "," + B + ",vbat," + v + ",eol"
return(datstr)
def get_voltage(pin):
return (pin.value * 3.3) / 65536 * 2
current = time.monotonic()
old = current
newGPS = False
while True:
# Make sure to call gps.update() every loop iteration and at least twice
# as fast as data comes from the GPS unit (usually every second).
# This returns a bool that's true if it parsed new data (you can ignore it
# though if you don't care and instead look at the has_fix property).
#gps.update()
# Every second print out current location details if there's a fix.
#print("\nTemperature: %0.1f C" % bme680.temperature)
#print("Gas: %d ohm" % bme680.gas)
#print("Humidity: %0.1f %%" % bme680.humidity)
#print("Pressure: %0.3f hPa" % bme680.pressure)
#print("Altitude = %0.2f meters" % bme680.altitude)
#t = bme680.temperature
#g = bme680.gas
#sensorString = "str(t),str(g)"
#print(sensorString)
#time.sleep(1)
#time.sleep(1)
#A = [t, g, h, p, a]
#print(A)
current = time.monotonic()
#if current - last_print >= 1.0:
if uart.in_waiting > 0:
gps_string = uart.readline()
print(gps_string)
if "GPGGA" in gps_string:
#old = current
gps_str = str(gps_string)
#print(gps_str)
#sendMessage(gps_str + ",HABET")
#datastr = getDat()
#sendMessage(datastr)
#print(datastr)
newGPS = True
if current-old>5:
old = current
if newGPS:
print(gps_str)
sendMessage(gps_str)
datastr = getDat()
sendMessage(datastr)
print(datastr)
newGPS = False
else:
sendMessage("No GPS")
print("No GPS")
datastr = getDat()
sendMessage(datastr)
print(datastr)
packet = rfm9x.receive(timeout=.1)
if packet is None:
# Packet has not been received
LED.value = False
#print('Received nothing! Listening again...')
else:
# Received a packet!
LED.value = True
# Print out the raw bytes of the packet:
#print('Received (raw bytes): {0}'.format(packet))
# And decode to ASCII text and print it too. Note that you always
# receive raw bytes and need to convert to a text format like ASCII
# if you intend to do string processing on your data. Make sure the
# sending side is sending ASCII data before you try to decode!
try:
rssi = rfm9x.rssi
#print(packet)
if(b'command' in packet):
sendMessage(str(packet))
print(str(packet))
except:
print("invalid ascii")
# Also read the RSSI (signal strength) of the last received message and
# print it.
time.sleep(.05)