pythonraspberry-piaws-iotaws-iot-greengrass

How to run RPI.GPIO module in AWS Greengrass Core on a Raspberry PI 4


Looking for help using RPi.GPIO with Raspberry Pi 4.

I have a simple Greengrass Lambda function which is attempting to collect data from a resistive soil moisture sensor using the RPi.GPIO module. My Lambda function relies on Python3.7 and version 0.7.0 of the RPi.GPIO module. I've verified the GPIO module is installed and working correctly on the device outside of Greengrass using a simplified Python script.

 import RPi.GPIO as GPIO
 import RPIO
 import time

 channel = 21
 GPIO.setmode(GPIO.BCM)
 GPIO.setup(channel, GPIO.IN)

 def callback(channel):
   if GPIO.input(channel):
     print("No water detected")
   else:
     print("Water detected")

 GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) 
 GPIO.add_event_callback(channel, callback)

 while True:
   time.sleep(1)

The local /dev/gpiomem filesystem is made accessible to the Greengrass Lambda via a local resource defined in the IoT group. I'm using Classic (V1).

However, when I attempt to deploy the Lambda function I get the following error:

-lambda_runtime.py:382,Failed to initialize Lambda runtime due to exception: This module can only be run on a Raspberry Pi!

I have two versions of Python installed on my RPi (2.7.16 and 3.7.3, the Greengrass runtime uses 3.7) and have verified both have the version 0.7.0 of the RPi.GPIO module.

My Lambda function explicitly imports the module

import logging
import platform
import sys
import RPi.GPIO as GPIO
import time

from threading import Timer
import greengrasssdk
   

Has anyone experienced this problem and what resolution would you recommend?


Solution

  • I've been testing after experiencing exactly the same issue

    One thing I tried was to give the Lambda (container) access to SYS in the Lambda configuration, at which point I got another different error relating to overlay fs issues.

    I found that there was a fix for the overlay fs issue I was getting in Greengrass 1.10.1 (I was running 1.10.0) so I then updated Greengrass (using OTA update job) to 1.11.3 and after doing the GG update everything started working again :-)

    It may not need the SYS access as I have yet to change that back and see if it was just the Greengrass update that was needed.

    Have re-tested and confirmed it DOES need the /sys access for the container

    For info, I had tried various things before coming up the the resolution around doing the usual apt-get update/upgrade and making sure the RPi.GPIO was the latest, but none of these made any difference.