Saturday 28 December 2019

Adafruit PyPortal from Twitter to weather

I have had the Adafruit PyPortal for a while and finally got around to playing with it. The PyPortal cames as part the Adabox011 



1. Getting going

PyPortal contains a colour TFT touch screen; speaker; NeoPixel; sensors for light and temperature sensor;  microSD slot; ports for I2C and pins for either analogue or digital; with built-in 8MB flash memory. It uses both CircuitPython and Arduino. In this post, CircuitPython is used. A more detailed overview of the device is available at https://learn.adafruit.com/adafruit-pyportal 

To set it up I started from https://learn.adafruit.com/adafruit-pyportal/updating-your-pyportal, I already had CircuitPython installed so skipped ahead after updating the firmware to PyPortal CircuitPython Setup and then the enjoyable bit Internet Connect! 


2 From Twitter to Weather
Time to play. There is an interesting example, developed by John Parks (https://learn.adafruit.com/users/johnpark) PyPortal Twitter Follows Trophy (https://learn.adafruit.com/pyportal-twitter-follows-trophy) which turns the PyPortal into a 'scoreboard' for twitter follows. The first few sections describing setting the system up in terms of CircuitPython, internet connection and updating the firmware, similar to the links above. The code starts at https://learn.adafruit.com/pyportal-twitter-follows-trophy/code-pyportal-with-circuitpython the description is really useful and essentially uses JSON information about followers and displays it on the PyPortal.

Using this code as the core of a new project, the device was changed to collect weather data for the current weather (and yes I do know I can just look out of the window but this seems like fun) from a website. 

To get started a source of the data was needed and a company Weather Unlocked: https://developer.weatherunlocked.com/ provides a service for local current weather information via their 'Local Weather API' in JSON form.  You need to sign in and create an APP_ID and APP_KEY, these are needed in the next stage. In the code, modify DATA_SOURCE = "https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names="+TWITTER_NAME to something like DATA_SOURCE = "http://api.weatherunlocked.com/api/current/51.50,-0.12?app_id={APP_ID}&app_key={APP_KEY}" with APP_ID and APP_KEY substituted and 51.50,-0.12 changed to the longitude and Latitude of your choice.

Using the URL Code Beautify (https://codebeautify.org/jsonviewer) and you can look at the JSON output to get a sense of how the data is structured and see the information produced.
There is a weather description wx_desc, so the code was changed to use DATA_LOCATION = ["wx_desc"] the rest is minor tweaks on the original John Parks code.



Change to DATA_LOCATION = ["temp_c"] and get (not surprsingly) the temperature.

The code used is here:





3. Overview
I enjoyed playing with this so far and look forward to playing with it a bit more. Thank you to John Parks for such a good starting point and WeatherUnlocked for such a useful feed.


All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

No comments:

Post a Comment

Top posts on this blog in March 2024

The Top 10 viewed post on this blog in March 2024. Covering areas such as small robots, augmented reality, Scratch programming, robots. Micr...