-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeshtastic_NOAA_Alert.py
More file actions
66 lines (53 loc) · 1.78 KB
/
Copy pathmeshtastic_NOAA_Alert.py
File metadata and controls
66 lines (53 loc) · 1.78 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
#!/usr/bin/env python3
#https://api.weather.gov/alerts/active?point=38.8966079,-77.0401901
import datetime
import time
import requests
import json
import shlex
import subprocess
import sqlite3
lat = "38.8966079"
lon = "-77.0401901"
datetime = time.strftime("%m-%d-%Y %H:%M:%S", time.localtime())
response = requests.get(f'https://api.weather.gov/alerts/active?point={lat},{lon}').json()
conn = sqlite3.connect('~/data/meshtasticNOAA.sqlite')
c = conn.cursor()
for x in response['features']:
#print("\n*****\n")
#print("areaDesc: " + x['properties']['areaDesc'])
#print(x['properties']['parameters']['NWSheadline'][0])
#print("headline: " + x['properties']['headline'])
#print("id: " + x['properties']['id'])
#print(x['properties']['description'])
try:
txtarea = x['properties']['areaDesc']
except:
txtarea = ""
try:
txtheadline = x['properties']['parameters']['NWSheadline'][0]
except:
txtheadline = ""
try:
txturl = "https://weather.gov"
except:
txturl = ""
msgText = "NOAA ALERT - " + txtheadline + " - " + txtarea + " - " + txturl
if len(msgText) > 230:
msgText = msgText[:225] + "..."
print("alertText: " + msgText)
#Check for existing ID in DB
existAlert = c.execute ("""select count(*) from data where alertid == ?;""", (x['properties']['id'],))
existAlertCount = (existAlert.fetchone()[0])
if existAlertCount < 1:
d = (datetime, x['properties']['id'])
c.execute('insert into data values (?,?)', d)
#Poin to meshtastic CLI install, set USB port and Channel Index
subprocess.run(shlex.split("~/bin/meshtastic --port /dev/ttyUSB0 --ack --sendtext '" + msgText + "' --ch-index 1"))
print("Sending Alert to the Mesh")
break
else:
print("Alert already sent to Mesh")
print("\n*****\n")
conn.commit()
c.close()