I’ve been trying to put together a Python script that collects the weather forecast then displays the results on the Display-o-Tron 3000 and I’ve almost got it figured out. I forked a project that used Dark Sky’s API and based the code on the initial example, but I’m struggling to get the updated weather forecast displayed correctly.
At the moment, everything works but the updated forecast isn’t being displayed on the DOT3K, so was hoping somebody here might be able to help out. I suspect it’s down to how the functions are created and how the variables are passed. Here’s my code so far:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from dot3k import joystick as nav
from dot3k import lcd
from dot3k import backlight as backlight
backlight.use_rbg() # Required for early-batch DOT3K's as the RGB LEDs are RBG.
from time import sleep
import os
from ConfigParser import ConfigParser
# Import details from config file to save typing
config = ConfigParser()
config.read('config/config.ini')
api_key = config.get('darksky', 'key')
latitude = config.get('darksky', 'latitude')
longitude = config.get('darksky', 'longitude')
try:
import forecastio
except ImportError:
exit("This script requires the forecastio module\nInstall with: sudo pip install forecastio")
# Get the forecast details from Dark Sky
forecast = forecastio.load_forecast(api_key,latitude,longitude)
current = forecast.currently()
# Get the temperature, humidity and chance of rain then
# convert to strings to display on DOT3K.
temp = current.temperature
temp = str(temp)
humidity = current.humidity*100
humidity = str(humidity)
rainInt = current.precipProbability*100
rain = str(rainInt)
def updateWeather():
forecast.update()
return current
def rainWarning():
if rainInt <= 33:
backlight.rgb(165, 255, 0) # Green (good)
elif (rainInt > 34) and (rainInt <= 74):
backlight.rgb(226, 111, 11) # Orange (warning)
else:
backlight.rgb(0, 225, 255) # Blue (you'll get wet!)
def graph():
if rainInt ==0:
backlight.set_graph(0)
elif (rainInt >1) and (rainInt <=9):
backlight.set_graph(0.1)
elif (rainInt >= 10) and (rainInt <=19):
backlight.set_graph(0.2)
elif (rainInt >= 20) and (rainInt <=29):
backlight.set_graph(0.3)
elif (rainInt >= 30) and (rainInt <=39):
backlight.set_graph(0.4)
elif (rainInt >= 40) and (rainInt <=49):
backlight.set_graph(0.5)
elif (rainInt >= 50) and (rainInt <=59):
backlight.set_graph(0.6)
elif (rainInt >= 60) and (rainInt <=69):
backlight.set_graph(0.7)
elif (rainInt >= 70) and (rainInt <=79):
backlight.set_graph(0.8)
elif (rainInt >= 80) and (rainInt <=89):
backlight.set_graph(0.9)
else:
backlight.set_graph(1.0)
# Press the button on the joystick to exit
@nav.on(nav.BUTTON)
def handle_button(pin):
lcd.clear()
backlight.rgb(0, 0, 0)
backlight.set_graph(0)
os._exit(1)
def display():
try:
current = updateWeather()
lcd.clear()
lcd.set_cursor_position(0, 0)
print("Temperture: "+temp+" C")
lcd.write("Temp: "+temp+" C")
lcd.set_cursor_position(0, 1)
print("Humidity: "+humidity+"%")
lcd.write("Humidity: "+humidity+"%")
lcd.set_cursor_position(0, 2)
print("Rain: "+rain+"%")
lcd.write("Rain: "+rain+"%")
except:
lcd.write("Connection Error")
while 1:
updateWeather()
display()
rainWarning()
graph()
sleep(120)
My full repo is available here.
Basically, the weather data is pulled from Dark Sky and the current temperature, humidity and chance of rain are displayed.
If the chance of rain is low, the backlight is green and the bar graph is off. If there is a medium chance of rain, the backlight is orange and the bar graph lights up depending on the percentage and if the rain chance is high, the backlight is blue and the bar graph shows more lights.