Skip to content

Latest commit

 

History

History
76 lines (44 loc) · 4.9 KB

README.md

File metadata and controls

76 lines (44 loc) · 4.9 KB

MicroPython Workshop: Raspberry Pi Pico W Component

Menu screen running on the Pico W

Overview

This component is written in MicroPython and runs on the Rasperry Pi Pico W hardware with the Pimoroni GFX Pack display.

It connects to the Redis database running elsewhere for the Streams functionality and also contains a demonstration of how to call an API and display the results graphically. This latter functionality doesn't use Redis.

Prerequisites

To get this component up and running you'll need:

Setup and Configuration

If your Pico W / GFX Pack was provided to you by your instructor, then the software will already be loaded up and mostly ready to go. You may have to configure some wifi network credentials in the secrets.py file - ask your instructor.

Open the Thonny IDE, then open the picow folder from this repository in Thonny.

Open the file secrets.py and make sure that the following parameters are set to the values that your instructor provided (or to your wifi credentials if you're using this alone):

WIFI_SSID = "simonberyl"
WIFI_PASSWORD = "goodlife"
REDIS_HOST = "192.168.8.245"
REDIS_PORT = 6379
REDIS_USER = ""
REDIS_PASSWORD = ""
CARBON_INTENSITY_URL = "http://192.168.8.245:5000/regional/postcode/OX1"

Save your changes.

Now, connect the Pico W to your machine using the USB to Micro USB cable. Select the correct device in Thonny (bottom right - ask your instructor if needed) then press the Stop button in the ribbon towards the top of the Thonny window.

If you made changes to secrets.py, right click the file and select "Upload to /" from the menu that appears.

If your device didn't have the software pre-installed, select ALL .py files, right click then select "Upload to /" from the menu that appears.

Restart the device by pressing the Reset button on the rear of the GFX Pack.

Hopefully the device will boot, connect to the network and display a menu. If it fails to do this, check in with your instructor for help.

The software on the device performs four distinct tasks, which are described in the following sections... Use buttons A-D on the GFX pack to access each.

The code for the menu screen / startup process is contained in the main.py file.

The Producer (Button A)

The producer waits until the user presses a button then randomly generates a new job and places it into the Redis Stream. It shows the ID that Redis assigns to the job, then waits a few seconds before allowing the user to create more jobs.

The code for this screen is contained in the producer.py file.

The Consumer (Button B)

The consumer works as part of the "staff" consumer group to collaboratively process jobs from the stream. When the user presses a button, it requests the next available job, waits a few seconds to simulate doing the work, then acknowledges that job has been done by updating Redis.

The code for this screen is contained in the consumer.py file.

The Stream Status Display (Button C)

This displays the overall status of the jobs stream, showing the number of consumers assigned to the "staff" group, the number of jobs currently in process (read but not yet acknowledged as complete) and the lag (the number of jobs not being worked on - how far behind the consumers are with processing the stream).

The code for this screen is contained in the streamstatus.py file.

Carbon Intensity Display (Button D)

This doesn't use Redis, instead it makes an API call to the UK carbon intensity API for a configurable postcode (see secrets.py). A summary of the make up of the current electricity generation mix is displayed as a bar graph. The display automatically updates periodically.

The code for this screen is contained in the carbonintensity.py file.