Created
August 23, 2018 15:23
-
-
Save ItKindaWorks/67b8baa377b94811dba6aa63fef01f1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
#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