Create a Serverless Martian Weather Display Using CircuitPython and AWS Lambda

Create a Serverless Martian Weather Display Using CircuitPython and AWS LambdaMore Info

In this project, we’ll build a standalone digital display that showcases the latest weather updates from Mars, along with images from the Mars Curiosity Rover. Using an Adafruit PyPortal, an IoT touch display, we can easily program this device with CircuitPython, a streamlined version of Python designed for embedded hardware. Programming the PyPortal is as simple as copying your code onto the device, just like transferring files to a USB drive.

The backend of this project, hosted in the cloud to manage all the heavy lifting, is deployed using the AWS Serverless Application Repository (SAR). The PyPortal sends a REST API request to this backend to retrieve data from the NASA Mars Rover Photos API and the InSight: Mars Weather Service API. The backend processes the requests, converting and resizing the images before sending the results back to the PyPortal for display.

Prerequisites

To complete this project, you will need:

  • An Adafruit PyPortal
  • A microSD card
  • An AWS account (you can use the AWS Free Tier for this project)

Deploying the Backend Application

Utilizing a serverless backend minimizes the workload on the PyPortal. The device queries the backend API and receives a concise JSON object containing the necessary data. This also allows for modifications to the logic of data retrieval without needing to physically access the PyPortal.

The backend comprises an AWS Lambda function written in Python and is accessible via an Amazon API Gateway endpoint. When triggered, the FetchMarsData function requests data from two distinct NASA APIs. It fetches the latest images from the Mars Curiosity Rover, typically from the day before, and selects one at random. The image is resized and converted to bitmap format before being uploaded to Amazon S3 with public read permissions, allowing the PyPortal to download it later.

The function also retrieves weather data from the InSight: Mars Weather Service API, including average air temperature, wind speed, pressure, season, solar day (sol), and timestamps for daily sampling. The API returns these details along with the S3 image URL as a JSON object.

To create the backend, I use the AWS Serverless Application Model (SAM), which can be deployed via the AWS SAM CLI or directly from the AWS Management Console:

  1. Obtain a free NASA API key at api.nasa.gov to access the NASA data APIs.
  2. Navigate to the aws-serverless-pyportal-mars-weather-display application in the Serverless Application Repository.
  3. Click Deploy.
  4. On the next page, under Application Settings, input the parameter NasaApiKey.
  5. After completion, select View CloudFormation Stack.
  6. Go to the Outputs tab and note the MarsApiUrl, as it is needed to configure the PyPortal.
  7. Visit the MarsApiKey URL listed in the Outputs tab and reveal the API key for use in the PyPortal’s requests.

Setting Up the PyPortal

Follow Adafruit’s instructions to install the latest version of the CircuitPython bootloader (at the time of writing, it is 5.2.0). Next, install the latest Adafruit CircuitPython library bundle (version 5.x). Update the onboard Wi-Fi coprocessor as instructed.

Insert the microSD card into the slot at the back of the device. Optionally, you may want to install the Mu Editor, which is a versatile code editor and serial debugger for Adafruit CircuitPython boards, useful for troubleshooting. If you own a 3D printer, consider printing a case for your PyPortal to protect it and enhance its display on a desk.

Programming the PyPortal

As with standard Python, CircuitPython code does not require compilation to execute. Updating the firmware on the PyPortal is as simple as copying the necessary files to a mounted volume. The device runs code.py whenever it starts or when files are updated.

Connect the PyPortal to your computer using a USB cable and wait for the CIRCUITPY volume to become available. Download the project from GitHub and copy the contents from the /circuit-python directory to the CIRCUITPY volume. Open and edit the secrets.py file to include your Wi-Fi credentials, MarsApiKey, and MarsApiUrl API Gateway endpoint.

Save the file to restart the device. It may take a moment to connect to Wi-Fi and make the initial request. If you installed the Mu Editor, you can use the “Serial” feature to monitor the device’s logs.

Understanding CircuitPython’s API Gateway Communication

The main file, code.py, contains a while loop that periodically fetches and displays images from the Curiosity Rover and weather data from the InSight Mars lander. The code utilizes helper functions to manage API calls and data processing.

The callAPIEndpoint function sends a request to the API Gateway using the URL from the secrets.py file, including the MarsApiKey in the header. The timeout is set to 30 seconds to accommodate integrations with services like Lambda and API Gateway, as the CircuitPython code runs on a microcontroller that may require longer wait times for requests.

The JSON object received by the PyPortal is defined in the Lambda function’s handler. You can explore this in the GitHub project under src/app.py.

If you wish to quickly add new properties to the response, you can edit the Lambda function directly through the AWS Lambda Console. For example, adding a key hello with a value world can be done here:

In the CircuitPython code.py file, this new key will now be available in the JSON response from API Gateway. You can print the value in the Mu Editor Serial debugger to verify the output.

For more detailed insights, you can check out another blog post here. Additionally, https://chvnci.com/?p=48 is an excellent authority on this subject. If you’re looking for further resources, you can also explore this great opportunity here.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *