I’ve been playing with the Adafruit PyPortal recently, a 3.5 inch touchscreen display with in built functionality to quickly build Internet of Things, IoT, projects.
The first thing I built was a viewer for my Luftdaten Air Quality Sensor. I had connected my sensor to opensensemap as I liked the way that they displayed data. They also provide a really nice API interface.
The code I used to access my sensor is provided below
""" This example will access the opensensemap API, grab the air quality values pm10, pm2.5, temperature... and display it on a screen! if you can find something that spits out JSON data, we can display it """ import time import board from adafruit_pyportal import PyPortal # Set up where we'll be fetching data from DATA_SOURCE = "https://api.opensensemap.org/boxes/5c6ec1ae15451500198f5abe" # pylint: disable=line-too-long PM10 = ["sensors",0, "lastMeasurement","value"] PM25 = ["sensors",1, "lastMeasurement","value"] TEMP = ["sensors",2, "lastMeasurement","value"] # the current working directory (where this file is) cwd = ("/"+__file__).rsplit('/', 1) # Initialize the pyportal object and let us know what data to fetch and where # to display it pyportal = PyPortal(url=DATA_SOURCE, json_path=(PM10, PM25, TEMP), status_neopixel=board.NEOPIXEL, default_bg=cwd+"/IMG_6562.bmp", text_font=cwd+"/fonts/Arial-ItalicMT-17.bdf", text_position=((50, 225), (100, 225), (160, 225)), text_color=(0x00FF00,0x00FF00,0x00FF00), caption_text="PM10, PM2.5, Temperature", caption_font=cwd+"/fonts/Arial-ItalicMT-17.bdf", caption_position=(50,200), caption_color=0x00FF00) # track the last value so we can play a sound when it updates while True: try: value = pyportal.fetch() print("Response is", value) except (ValueError, RuntimeError) as e: print("Some error occured, retrying! -", e) time.sleep(180) # wait a minute before getting again
It’s all done with CircuitPython.
I quite liked the result shown below. The picture in the background is of a local lake.