Skip to content

Instantly share code, notes, and snippets.

@TylerCode
Created October 8, 2024 15:57
Show Gist options
  • Save TylerCode/182a58d8c9211c4287d57156d754f108 to your computer and use it in GitHub Desktop.
Save TylerCode/182a58d8c9211c4287d57156d754f108 to your computer and use it in GitHub Desktop.
Plant Freeze Text! - Get a text message when your plants hit freezing temperatures.
import requests
import os
from twilio.rest import Client
from datetime import datetime
# OpenWeatherMap API configuration
OWM_API_KEY = "YOUR_OPENWEATHERMAP_API_KEY"
ZIP_CODE = "YOUR US ZIP CODE"
COUNTRY_CODE = "US"
# Twilio configuration
TWILIO_ACCOUNT_SID = "YOUR_TWILIO_ACCOUNT_SID"
TWILIO_AUTH_TOKEN = "YOUR_TWILIO_AUTH_TOKEN"
TWILIO_PHONE_NUMBER = "YOUR_TWILIO_PHONE_NUMBER"
YOUR_PHONE_NUMBER = "YOUR_PHONE_NUMBER"
def get_weather():
url = f"http://api.openweathermap.org/data/2.5/forecast?zip={ZIP_CODE},{COUNTRY_CODE}&appid={OWM_API_KEY}&units=imperial"
response = requests.get(url)
return response.json()
def check_freezing_temps(weather_data):
for forecast in weather_data['list'][:8]: # Check next 24 hours (3-hour intervals)
temp = forecast['main']['temp']
if temp <= 32: # 32°F is freezing point
return True
return False
def send_text_message(message):
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
client.messages.create(
body=message,
from_=TWILIO_PHONE_NUMBER,
to=YOUR_PHONE_NUMBER
)
def main():
weather_data = get_weather()
if check_freezing_temps(weather_data):
message = f"Alert: Freezing temperatures expected in {ZIP_CODE}. Bring your plants inside!"
send_text_message(message)
print(f"Alert sent at {datetime.now()}")
else:
print(f"No freezing temperatures expected. Checked at {datetime.now()}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment