meshtastic_forceast/main.py

50 lines
1.5 KiB
Python
Raw Normal View History

2024-12-27 22:55:22 -08:00
#!/usr/bin/env python3
import requests, meshtastic, datetime, time, dateutil, syslog
import meshtastic.tcp_interface
def greet_mesh():
current_time = time.localtime()
hour = current_time.tm_hour
if 5 <= hour < 12:
return "Good morning Mesh!"
elif 12 <= hour < 18:
return "Good afternoon Mesh!"
else:
return "Good evening Mesh!"
def message_broadcast(interface, message):
print(message)
interface.send(message,'all')
def fetch_forecasts():
URL = 'https://api.weather.gov/zones/forecast/CAZ307/forecast'
response = requests.get(URL)
return response.json()['properties']
# Connect to the device
interface = meshtastic.tcp_interface.TCPInterface("192.168.1.102") # Replace with the IP address of your device
forecast = fetch_forecasts()
d = dateutil.parser.parse(forecast['updated'])
time_now = d.strftime('%m/%d/%Y %H:%M %z')
start_message = greet_mesh() + ' Here is the NWS Forecast for Fresno County as of ' + time_now
first_period_message = forecast['periods'][0]['name'] + ': ' + forecast['periods'][0]['detailedForecast']
second_period_message = forecast['periods'][1]['name'] + ': ' + forecast['periods'][1]['detailedForecast']
print(start_message)
print(first_period_message)
print(second_period_message)
interface.sendText(start_message)
time.sleep(10)
interface.sendText(first_period_message)
time.sleep(10)
interface.sendText(second_period_message)
time.sleep(10)
# Close the interface
interface.close()