I love e-ink displays. Their ability to make something digital seem just a little bit analogue intrigues me and I just had to make anything that makes use of one. Like many, many other people on the internet that turned out to be a weather station.
The initial aim, seeing as I was already going to use a power efficient display, was to use low power components to eke out as much life as possible out of a battery pack. In the end things turned out a little bit differently and a Raspberry Pi Zero W instead of a more frugal controller was used.
Control
To control the display and retrieve the data, a WEMOS NodeMCU v3.0 was intially selected. This can be programmed from the Arduino IDE following the setup described here. Due to many difficulties with the chosen e-ink display and this controller, as well as limitations in getting the weather forecast data parsed, a Raspberry Pi Zero W replaced in NodeMCU. The main reason probably was laziness to get things working smoothly.
Taking the easier solution, a Raspberry Pi Zero W is ran in headless mode with a Python script controlling the display and retrieving the data to display. As part of setting up the Pi Zero, it is connected via WiFi to the internet to retrieve weather data. The link at the beginning of this paragraph also covers how this is done.
E-Ink
A Waveshare 7.5″ E-Ink display is used to display the data. It has a resolution of 640 x 384 and capable of showing three colours; black, red and white. This display has been discontinued and has been replaced with a higher resolution version at around the same price of $100.
While using the NodeMCU controller the GxEPD library from J-M Zingg was used to write to the display. There is already a video on how to start using this on Youtube. The library is very easy to use and support a variety of E-ink displays.
Moving to the Pi Zero, the libraries recommended by the manufacturer was used.
The Python Script
Getting weather data
Weather data is provided by OpenWeatherMap. There are tons of information on how to setup and use OpenWeatherMap on both the Raspberry Pi as well as the NodeMCU.
In summary, you need to create a free account at OpenWeatherMap.org. This will provide you with an API key with which you can request the weather data. The “request” python library is the used to retrieve the JSON data stream that can then be organised in the data you wish to display.
import requests import json #-------------------------------------------------------------- #OpenWeaterMap setup #-------------------------------------------------------------- #OpenWeaterMap API key api_key = "PutYourKeyHere" #Base URL base_url = "http://api.openweathermap.org/data/2.5/" #City city_name = "London,GB" #Complete URL currentWeather_url = base_url + "weather?appid=" + api_key + "&q=" + city_name forecastWeather_url = base_url + "forecast?appid=" + api_key + "&q=" + city_name currentWeatherResponse = requests.get(currentWeather_url) forecastWeatherResponse = requests.get(forecastWeather_url) #Convert json format data into python format data currentData = currentWeatherResponse.json() forecastData = forecastWeatherResponse.json()
Controlling the display
The display is controlled using the recommended library from the manufacturer. The display is initialised, cleared and then written to in the following piece of example code. The “imagedraw” python library is used to draw the image that is written to the display.
from waveshare_epd import epd7in5bc #bc = 3 colours from PIL import Image,ImageDraw,ImageFont fontdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'fonts') tohoma20 = ImageFont.truetype(os.path.join(fontdir, 'tahoma.ttf'), 20) epd = epd7in5bc.EPD() #Initialise and clear epd.init() epd.Clear() imageBlack = Image.new('1', (epd7in5bc.EPD_WIDTH, epd7in5bc.EPD_HEIGHT), 255) # 255: clear frame imageRed = Image.new('1', (epd7in5bc.EPD_WIDTH, epd7in5bc.EPD_HEIGHT), 255) # 255: clear frame draw = ImageDraw.Draw(imageBlack) draw.text((20,245),'Hello World - Black', font = tohoma20, fill = 0) draw = ImageDraw.Draw(imageRed) draw.text((20,245),'Hello World - Red', font = tohoma20, fill = 0) #Display image epd.display(epd.getbuffer(imageBlack),epd.getbuffer(imageRed)
Complete script
Here is the complete script to use as you please. Additional to the pieces of code described above it
- Takes the weather icon code from the data and selects the appropriate letter from the weather icon font
- Draws the current weather as well as the forecast for the next 15 hours
#-------------------------------------------------------------- #Import #-------------------------------------------------------------- import sys import os import time from datetime import datetime import logging import requests import json fontdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'fonts') libdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib') picdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'pic') if os.path.exists(libdir): sys.path.append(libdir) from waveshare_epd import epd7in5bc #bc = 3 colours from PIL import Image,ImageDraw,ImageFont #-------------------------------------------------------------- #OpenWeaterMap icon map for Meteocons.ttf #-------------------------------------------------------------- def getWeatherIcon(iconCode): switcher = { '01d': 'B', #01 clear sky '01n': 'C', #01 clear sky '02d': 'H', #02 few clouds '02n': 'I', #02 few clouds '03d': 'N', #03 scattered clouds '03n': 'N', #03 scattered clouds '04d': 'Y', #04 broken clouds '04n': 'Y', #04 broken clouds '09d': 'Q', #09 shower rain '09n': 'Q', #09 shower rain '10d': 'R', #10 rain '10n': 'R', #10 rain '11d': 'O', #11 thunderstorm '11n': 'O', #11 thunderstorm '13d': 'W', #13 snow '13n': 'W', #13 snow '50d': 'E', #50 mist '50n': 'E', #50 mist } return switcher.get(iconCode,'B') #-------------------------------------------------------------- #OpenWeaterMap setup #-------------------------------------------------------------- #OpenWeaterMap API key api_key = "PutYourKeyHere" #Base URL base_url = "http://api.openweathermap.org/data/2.5/" #City city_name = "London,GB" #Complete URL currentWeather_url = base_url + "weather?appid=" + api_key + "&q=" + city_name forecastWeather_url = base_url + "forecast?appid=" + api_key + "&q=" + city_name #-------------------------------------------------------------- #Setup and main loop #-------------------------------------------------------------- def main(): try: logging.info("Notification start") #Initialise display #========================== epd = epd7in5bc.EPD() logging.info("Initialise and clear") epd.init() epd.Clear() #Setup fonts #========================== tahomaBig = ImageFont.truetype(os.path.join(fontdir, 'tahoma.ttf'), 155) tohoma20 = ImageFont.truetype(os.path.join(fontdir, 'tahoma.ttf'), 20) tohoma24 = ImageFont.truetype(os.path.join(fontdir, 'tahoma.ttf'), 24) tohoma24bd = ImageFont.truetype(os.path.join(fontdir, 'tahomabd.ttf'), 24) tohoma40 = ImageFont.truetype(os.path.join(fontdir, 'tahoma.ttf'), 40) weatherIconsBig = ImageFont.truetype(os.path.join(fontdir, 'Meteocons.ttf'), 230) weatherIconsSmall = ImageFont.truetype(os.path.join(fontdir, 'Meteocons.ttf'), 75) #Get start time#Get time of update #========================== nowTime = datetime.now() startTime = nowTime.strftime("%H:%M") startDate = nowTime.strftime("%d/%m/%Y") #Get weather data #========================== currentWeatherResponse = requests.get(currentWeather_url) forecastWeatherResponse = requests.get(forecastWeather_url) #Convert json format data into python format data currentData = currentWeatherResponse.json() forecastData = forecastWeatherResponse.json() #Check if city exist and print data if currentData["cod"] != "404": #Store value of main currentWeatherData = currentData["main"] #Current temperature current_temperature = currentWeatherData["temp"] current_tempMin = currentWeatherData["temp_min"] current_tempMax = currentWeatherData["temp_max"] #Current temperature feels like current_temp_feel = currentWeatherData["feels_like"] #Current pressure current_pressure = currentWeatherData["pressure"] #Current humidity current_humidity = currentWeatherData["humidity"] #Weather weatherDescription = currentData["weather"] #Description weatherText = weatherDescription[0]["description"] weatherIcon = weatherDescription[0]["icon"] # print following values else: print("City not found") #Forecast -- Check if city exist and print data foreCastList = [] if forecastData["cod"] != "404": #Store value of main forecastWeatherDataMain = forecastData["list"] #Extract forecast data for i in forecastWeatherDataMain: #loopdata = i["sys"] foreCastItemList = [i["dt_txt"]] #Timestamp foreCastItemList.append(i["main"]["temp"]) #Weather at timestamp foreCastItemList.append(i["weather"][0]["icon"]) #Weather icon at timestamp foreCastList.append(foreCastItemList) else: print("Forecast city not found") #Get time of update #========================== nowTime = datetime.now() currentTime = nowTime.strftime("%H:%M") currentDate = nowTime.strftime("%d/%m/%Y") print("Current time = ", currentTime) print("Current date = ", currentDate) #Write to E-Ink #========================== imageBlack = Image.new('1', (epd7in5bc.EPD_WIDTH, epd7in5bc.EPD_HEIGHT), 255) # 255: clear frame imageRed = Image.new('1', (epd7in5bc.EPD_WIDTH, epd7in5bc.EPD_HEIGHT), 255) # 255: clear frame draw = ImageDraw.Draw(imageBlack) #Last update stamp draw.text((330,350),'Last update: ' + currentDate + ' - ' + currentTime, font = tohoma20, fill = 0) #Black frame #Forecast +3h draw.text((20,245),str(round(foreCastList[0][1]-273.15,1)) + '°C', font = tohoma20, fill = 0) draw.text((14,270),getWeatherIcon(foreCastList[0][2]), font = weatherIconsSmall, fill = 0) #Forecast +6h draw.text((155,245),str(round(foreCastList[1][1]-273.15,1)) + '°C', font = tohoma20, fill = 0) draw.text((149,270),getWeatherIcon(foreCastList[1][2]), font = weatherIconsSmall, fill = 0) #Forecast +9h draw.text((290,245),str(round(foreCastList[2][1]-273.15,1)) + '°C', font = tohoma20, fill = 0) draw.text((284,270),getWeatherIcon(foreCastList[2][2]), font = weatherIconsSmall, fill = 0) #Forecast +12h draw.text((425,245),str(round(foreCastList[3][1]-273.15,1)) + '°C', font = tohoma20, fill = 0) draw.text((419,270),getWeatherIcon(foreCastList[3][2]), font = weatherIconsSmall, fill = 0) #Forecast +15h draw.text((560,245),str(round(foreCastList[4][1]-273.15,1)) + '°C', font = tohoma20, fill = 0) draw.text((554,270),getWeatherIcon(foreCastList[4][2]), font = weatherIconsSmall, fill = 0) #CurrentWeather draw.text((7,7),getWeatherIcon(weatherIcon), font = weatherIconsBig, fill = 0) draw.text((295,170),'Feels like ' + str(round(current_temp_feel-273.15)) + '°C' + ' Humidity ' + str(current_humidity) + '%', font = tohoma20, fill = 0) currentTempString = str(round(current_temperature-273.15,1)) currentTempCharLen = len(currentTempString)-1 dotlength = 55 bigfontwidth = 80 draw.text((280+(currentTempCharLen*bigfontwidth)+dotlength,30),'°C', font = tohoma40, fill = 0) #Red frame #CurrentWeather draw = ImageDraw.Draw(imageRed) draw.text((280,1),currentTempString, font = tahomaBig, fill = 0) #Forecast +3h draw.text((20,220),foreCastList[0][0][11:16], font = tohoma24bd, fill = 0) #Forecast +6h draw.text((155,220),foreCastList[1][0][11:16], font = tohoma24bd, fill = 0) #Forecast +9h draw.text((290,220),foreCastList[2][0][11:16], font = tohoma24bd, fill = 0) #Forecast +12h draw.text((425,220),foreCastList[3][0][11:16], font = tohoma24bd, fill = 0) #Forecast +15h draw.text((550,220),foreCastList[4][0][11:16], font = tohoma24bd, fill = 0) #Display image epd.display(epd.getbuffer(imageBlack),epd.getbuffer(imageRed)) except IOError as e: logging.info(e) except KeyBoardInterrupt: logging.info("ctrl + c:") exit() if __name__ == "__main__": main()
Setting up the OS
A few tweaks are required to the Pi Zero’s OS to finish things off. First /etc/rc.local is updated to update the OS time at start-up. This is done by opening rc.local in nano (sudo nano /etc/rc.local) and adding “timedatectl” to the file before “exit 0”.
timedatectl #python3 /home/pi/scriptToRun.py & exit 0
The script in the previous section will only run once. To get the weather updated periodically you can either
- update the python code to have an infinite loop that sleeps at the end of execution. For this option just uncomment the line after “timedatectl” in the example above. Note the “&” at the end will kick off the task in a branch otherwise the rc.local script will never exit.
- or make use of the Raspberry Pi’s operating system to schedule the python script
Both methods were tested, but it felt “better” to use the operating system to schedule the running of the script. This is done using Crontab. To update crontab type the following in a Putty terminal to the Raspberry Pi
sudo crontab -e
At the bottom add the task you want scheduled with the timing information. The timing is a bit confusing and I have used this webpage to help. To run the script every 10 minutes add this to the bottom of crontab
*/10 * * * * python3 /home/pi/scriptToRun.py
Enclosure and power source
A power bank is used as power source with the enclosure 3D-printed to house this as well as the Raspberry Pi Zero and display with controller board.
Parts list
- Raspberry Pi Zero W – Amazon or cheaper at PiHut
- Waveshare 3 colour e-ink display – eBay
- Wood infused PLA 3D printing filament – Amazon
Gallery
One thought on “Creating an E-ink weather notification board”