Project 9b: 16×2 LCD Screen Weather Report

What you will need:

  • Raspberry Pi
  • Breadboard
  • 16 x 2 LCD Screen
  • 10kΩ Potentiometer
  • 8 Female to Male Jumper Wires
  • 7 Male to Male Jumper Wires

Instructions:

The second project my son and I created utilizing a 16 x 2 LCD screen uses the identical breadboard layout from Project 9 and simply displays a basic weather report by using the API from Weather Underground.

Because this experiment builds upon Project 9 and requires the Adafruit Raspberry Pi Python Code Library, be sure to follow the instructions in our Previous Project outlining the installation procedures before you continue. Also, stop by the Weather Underground API site to sign up for a free API. You will need the API code to make this project work.

Once you have your breadboard populated, the Adafruit Library cloned, and an API key from the Weather Underground, enter the Python script below using your favorite editor:

#!/usr/bin/env python
#
#  weather_LCD.py
#
# A short program using the wunderground.com API and
# Adafruit's 'Adafruit_CharLCD' library to monitor
# current weather conditions in a city and to then
# update a 16x2 LCD display in 4 bit mode
#
# Get an API key for Weather Underground at:
# http://wunderground.com/weather/api
#
# LCD Screen 16x2 Pins
# --------------------
# Pin 1  to GND
# Pin 2  to 5v
# Pin 3  to variable resistor then to GND 'Contrast Adjustment'
# Pin 4  to GPIO 25 for 'Command or Data Mode Switching'
# Pin 5  to GND for 'Read/Write Mode'
# Pin 6  to GPIO 24 for 'Enable Pin'
# Pin 7  to Not Connected
# Pin 8  to Not Connected
# Pin 9  to Not Connected
# Pin 10 to Not Connected
# Pin 11 to GPIO 23 for 'Data 4'
# Pin 12 to GPIO 17 for 'Data 5'
# Pin 13 to GPIO 21 for 'Data 6'
# Pin 14 to GPIO 22 for 'Data 7'
# Pin 15 to 5v for 'Backlight Anode' - Could use variable resistor as a 'Brightness Control'
# Pin 16 to GND for 'LED Backlight Cathode'
#
#  Copyright 2015  Ken Powers
#

# Import the required libraries
from Adafruit_CharLCD import Adafruit_CharLCD
import urllib2
import json
import time
import RPi.GPIO as GPIO

# Set GPIO pins to Broadcom numbering system
GPIO.setmode(GPIO.BCM)

lcd = Adafruit_CharLCD()
RUNNING = True

# Set your access keys as configured
myapi = "YOUR_API_KEY_FROM_WEATHER_UNDERGROUND"

try:
	while RUNNING:
		# Use JSON to obtain weather information using
		# Wunderground.com API call.

		f = urllib2.urlopen('http://api.wunderground.com/api/' + myapi + '/geolookup/conditions/q/WA/Tacoma.json')
		json_string = f.read()
		parsed_json = json.loads(json_string)
		location = parsed_json['location']['city']
		temp_f = parsed_json['current_observation']['temp_f']
		feelslike_f = parsed_json['current_observation']['feelslike_f']
		wind_mph = parsed_json['current_observation']['wind_mph']
		wind_dir = parsed_json['current_observation']['wind_dir']
		f.close()
		print ("Current temperature in %s is: %s F" % (location, temp_f))
		print ("Feels like: %s degrees F" % (feelslike_f))
		print ("Current wind speed in %s is: %s MPH" % (location, wind_mph))
		print ("Current wind direction is: %s\n\n" % (wind_dir))

		for x in range(0, 5):
			lcd.clear()
			lcd.home()
			lcd.message("Temp: " + str(temp_f) + " F\nFeels As: " + str(feelslike_f) + " F")
			time.sleep(3)
			lcd.clear()
			lcd.home()
			lcd.message("Wind: " + str(wind_mph) + " MPH\nWind Dir: " + str(wind_dir))
			time.sleep(3)

except KeyboardInterrupt:
	RUNNING = False
	print "\nQuitting"

# Clear LCD screen upon exit
# Don't forget to clean up after so we
# can use the GPIO next time
finally:
	lcd.clear()
	GPIO.cleanup()

Code Analysis:

Like all our other projects, our Raspberry Pi’s Python script begins by letting the interpreter know the language we are using as well as providing some brief comments that indicate our script’s purpose. We have also chosen to indicate which pins from our LCD screen are attached to our Raspberry Pi’s GPIO.

#!/usr/bin/env python
#
#  weather_LCD.py
#
# A short program using the wunderground.com API and
# Adafruit's 'Adafruit_CharLCD' library to monitor
# current weather conditions in a city and to then
# update a 16x2 LCD display in 4 bit mode
#
# Get an API key for Weather Underground at:
# http://wunderground.com/weather/api
#
# LCD Screen 16x2 Pins
# --------------------
# Pin 1  to GND
# Pin 2  to 5v
# Pin 3  to variable resistor then to GND 'Contrast Adjustment'
# Pin 4  to GPIO 25 for 'Command or Data Mode Switching'
# Pin 5  to GND for 'Read/Write Mode'
# Pin 6  to GPIO 24 for 'Enable Pin'
# Pin 7  to Not Connected
# Pin 8  to Not Connected
# Pin 9  to Not Connected
# Pin 10 to Not Connected
# Pin 11 to GPIO 23 for 'Data 4'
# Pin 12 to GPIO 17 for 'Data 5'
# Pin 13 to GPIO 21 for 'Data 6'
# Pin 14 to GPIO 22 for 'Data 7'
# Pin 15 to 5v for 'Backlight Anode' - Could use variable resistor as a 'Brightness Control'
# Pin 16 to GND for 'LED Backlight Cathode'
#
#  Copyright 2015  Ken Powers
#

Next, we import the necessary libraries and modules, define our constants, instantiate an LCD using the Adafruit function library, and define a constant called myapi for our Weather Underground API key. The API key will be unique for you and your purposes which is why we can’t show the one we used in our code.

# Import the required libraries
from Adafruit_CharLCD import Adafruit_CharLCD
import urllib2
import json
import time
import RPi.GPIO as GPIO

# Set GPIO pins to Broadcom numbering system
GPIO.setmode(GPIO.BCM)

lcd = Adafruit_CharLCD()
RUNNING = True

# Set your access keys as configured
myapi = "YOUR_API_KEY_FROM_WEATHER_UNDERGROUND"

Our main loop builds a URL using our unique API key. Notice that our URL in the code ends with a /WA/Tacoma.json. The WA is for the state of Washington in the United States of America and Tacoma is the town in which we reside.

It will be necessary for you to modify the location portion of the URL in order to get information that is correct for your locale. The location can be inside or outside the USA and more information can be found on the Weather Underground API Geolookup page.

Our script then parses the returned information from Weather Underground so we have a location, temperature in Fahrenheit, a windchill temperature, wind speed, and wind direction.

Next, we print this information to our main screen so we can make sure everything is being retrieved correctly.

Finally, we clear the screen of our 16×2 LCD, home the cursor, and display the temperature and windchill. After a 3 second delay, we clear the screen and display the wind speed and direction.

The main Python loop continues until Ctrl + C is depressed.

try:
	while RUNNING:
		# Use JSON to obtain weather information using
		# Wunderground.com API call.

		f = urllib2.urlopen('http://api.wunderground.com/api/' + myapi + '/geolookup/conditions/q/WA/Tacoma.json')
		json_string = f.read()
		parsed_json = json.loads(json_string)
		location = parsed_json['location']['city']
		temp_f = parsed_json['current_observation']['temp_f']
		feelslike_f = parsed_json['current_observation']['feelslike_f']
		wind_mph = parsed_json['current_observation']['wind_mph']
		wind_dir = parsed_json['current_observation']['wind_dir']
		f.close()
		print ("Current temperature in %s is: %s F" % (location, temp_f))
		print ("Feels like: %s degrees F" % (feelslike_f))
		print ("Current wind speed in %s is: %s MPH" % (location, wind_mph))
		print ("Current wind direction is: %s\n\n" % (wind_dir))

		for x in range(0, 5):
			lcd.clear()
			lcd.home()
			lcd.message("Temp: " + str(temp_f) + " F\nFeels As: " + str(feelslike_f) + " F")
			time.sleep(3)
			lcd.clear()
			lcd.home()
			lcd.message("Wind: " + str(wind_mph) + " MPH\nWind Dir: " + str(wind_dir))
			time.sleep(3)

except KeyboardInterrupt:
	RUNNING = False
	print "\nQuitting"

# Clear LCD screen upon exit
# Don't forget to clean up after so we
# can use the GPIO next time
finally:
	lcd.clear()
	GPIO.cleanup()

This simple Raspberry Pi demonstration of the 16×2 LCD screen really shows how the Raspberry Pi and a simple display can be very versatile for use in more complex future projects. Feel free to modify the code above to extract different information from Weather Underground. Above all, however, have fun!