Data Visualization of Weather with Raspberry Pi
The skills the author demoed here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.
I am a big fan of the Rapsberry Pi single board computing data platform, and I've used the devices in numerous projects. I recently acquired a Pimoroni Unicorn Phat (Pico Hardware Attached to Top) - a 4 x 8, 255 color LED display and I decided to see how well a small display would work to convey information at a glance.
I will use a Pi Zero W and the Pimoroni Unicorn Phat (I know, its a silly name!) to display local weather conditions in realtime.
To reach this goal, I will use code written in Python to use Pandas to scrape the current hourly weather info based on a given Zip Code. To make full use of the 4 separate LED columns it will display:
- Wind Speed
Before stepping into the code there is the conceptual problem - how do I output a float as a combination of 8 LEDS of various color?
Since the 'screen' will display 4 separate values, we'll need to code them separately.
First is the temperature. As a resident of the NYC area, the annual temperature can range from 0 F to 95 F in a typical year, so I split the temperature into three groups. Cold, Warm, and Hot, represented by blue, green, and red respectively.
Cold is any below 32 F, Warm is 33 - 74 F, and Hot is 75+ F.
According to the display below, it is ~85 degrees, ~80% humidity, ~20% precipitation, and wind speed of ~16 MPH in NYC
- Temperature (Blue -> cold [0, 32], Green -> warm [33, 74], Red -> hot[75,100])
- Humidity reflects % humidity * 8 / 100
- Precipitation reflects % precipitation * 8 / 100
- Wind Speed in MPH is a log_2 scale
When I took this image it was a bit cooler in Memphis (38101), it is ~75 degrees, ~90% humidity, ~30% precipitation, and wind speed of ~16 MPH in TN
Lastly we have Beaver (25813), ~ 87 degrees, ~65% humidity, ~0% precipitation, and wind speed of ~8 MPH in WVA.
Below is a snipped of code for setting the color and number of active LEDS in a given row.
temp = int(temp)
ticks = linspace
tmp = temp
ticks = pd.Series(ticks)
return max(1,int(ticks[ticks == temp].index))
if int(temp) <= 32:
T = linshuffle(np.linspace(0,32,10),temp)
R,G,B = (0,0,100)
elif int(temp) < 75:
T = linshuffle(np.linspace(32,75,10),temp)
R,G,B = (0,int(np.ceil(3*int(temp))),0)
elif int(temp) >= 75:
T = linshuffle(np.linspace(75,100,10),temp)
R,G,B = (2*int(temp),0,0)
How do we get weather information?
There are two options, the first is to use a hardware sensor and capture the data. The second method is to scrape the relevant information from the web. In this instance, we can get the hourly weather information containing temperature (F), humidity %, precipitation %, and wind speed (MPH)
The data above is from weather.com, their hourly weather information is exactly what we need!
Python & Pandas Code
Here's the Python & Pandas code to quickly scrape the table of data without needing to parse the HTML tree.
Zip_Code = zip_code.strip().replace(' ','').split('-')
if len(Zip_Code) == 5 and Zip_Code.isnumeric():
url = 'https://weather.com/weather/hourbyhour/l/%s:4:US' %(Zip_Code)
url = weather_url()
temp_df = pd.read_html(url)
temp_df = temp_df.iloc[:,1:]
temp_df.columns = ['Time','Description','Temp','Feels','Precip','Humidity','Wind']
temp_df['Time'] = pd.to_datetime(temp_df['Time'])
temp_df['Temp'] = temp_df['Temp'].map(lambda x: int(x[:-1]))
temp_df['Feels'] = temp_df['Feels'].map(lambda x: int(x[:-1]))
temp_df['Precip'] = temp_df['Precip'].map(lambda x: int(x[:-1]))
temp_df['Humidity'] = temp_df['Humidity'].map(lambda x: int(x[:-1]))
temp_df['Wind Direction'] = temp_df['Wind'].map(lambda x: x.split())
temp_df['Wind Speed'] = temp_df['Wind'].map(lambda x: x.split())
temp_df = temp_df[['Description','Temp','Feels','Precip','Humidity','Wind Direction','Wind Speed']]
Lucky for us, the Pandas read_html() method is powerful enough to grab the entire page of data in one go!. The subsequent lines of code are to clean up the content - ie: change strings to ints, remove % symbol and degree sign.
The full code can be found on my github page here https://github.com/edenbaus/unicornhat/blob/master/unicornweather.py