Skip to content

Instantly share code, notes, and snippets.

@ItKindaWorks
Created August 23, 2018 15:23
Show Gist options
  • Save ItKindaWorks/67b8baa377b94811dba6aa63fef01f1a to your computer and use it in GitHub Desktop.
Save ItKindaWorks/67b8baa377b94811dba6aa63fef01f1a to your computer and use it in GitHub Desktop.
#
#irrigationScript.py
#ItKindaWorks 2018
#github.com/ItKindaWorks
#
import sys
import paho.mqtt.client as mqtt
from weatherbit.api import Api
#Register at weatherbit.io to get an API key
api_key = "**************"
#Find your Latitude and Longitude here: https://www.latlong.net/
lat = 00.00
lon = 00.00
#rainy/wet weather codes
codes=[200, 201, 202, 230, 231, 232, 233, 300, 301, 302, 500, 501, 502, 511, 520, 521, 522]
#set the API key
api = Api(api_key)
# Set the granularity of the API - Options: ['daily','hourly','3hourly']
# Will only affect forecast requests.
api.set_granularity('daily')
# Query by lat/lon
forecast = api.get_forecast(lat=lat, lon=lon)
precipMM = forecast.get_series(['precip'])[0]['precip']
weatherCode = forecast.get_series(['weather'])[0]['weather']['code']
print(weatherCode)
print(precipMM)
for code in codes:
#if the day's code matches a known wet day, cancel watering for the day
if weatherCode == code or precipMM > 3.0:
mqttBroker = mqtt.Client()
mqttBroker.connect("192.168.1.2", 1883, 60)
mqttBroker.publish("/home/backWater", "1")
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment