Issue using RPi.GPIO in greengrass but not from command line

0

So I am trying to read the status of pins on a Raspberry Pi 4 Model B using python. I am using GPi.GPIO library for this. Simply stated when I use the same python logged in a default user (pi) from python3 interpreter RPi.GPIO runs fine. If I execute the same lines in a Greengrass component the I get an error that states that I must not be running on a Pi:

2022-09-07T22:32:59.086Z [WARN] (Copier) com.xxxxxxxxx.Publisher: stderr. RuntimeError: Not running on a RPi!. {scriptName=services.com.xxxxxxxxx.Publisher.lifecycle.Run, serviceName=com.xxxxxxxxx.Publisher, currentState=RUNNING} 2

Tracking this down further it looks like the logic in RPi.GPIO for detecting the processor is here starting at line 43:

   if ((fp = fopen("/proc/device-tree/system/linux,revision", "r"))) {
      uint32_t n;
      if (fread(&n, sizeof(n), 1, fp) != 1) {
         fclose(fp);
         return -1;
      }
      sprintf(revision, "%x", ntohl(n));
      found = 1;
   }
   else if ((fp = fopen("/proc/cpuinfo", "r"))) {
      while(!feof(fp) && fgets(buffer, sizeof(buffer), fp)) {
         sscanf(buffer, "Hardware	: %s", hardware);
         if (strcmp(hardware, "BCM2708") == 0 ||
             strcmp(hardware, "BCM2709") == 0 ||
             strcmp(hardware, "BCM2711") == 0 ||
             strcmp(hardware, "BCM2835") == 0 ||
             strcmp(hardware, "BCM2836") == 0 ||
             strcmp(hardware, "BCM2837") == 0 ) {
            found = 1;
         }
         sscanf(buffer, "Revision	: %s", revision);
      }
   }
   else
      return -1;
   fclose(fp);

The file /proc/device-tree/system/linux,revision exists on my system. When I run cat /proc/device-tree/system/linux,revision I do get some kind of character. In addition the file /proc/cpuinfo exists and does have a line Hardware : BCM2835. It would seem to my non C literate mind that cpu detection should work.

Is this a permissions issue? When I run the python commands manually It works. When I run the python commands in the component as the greengrass user it does not.

EDIT I did discover I can run python as user ggc_user. When I do this I get the error. It definitely has to do with user permissions. How to fix?

 sudo runuser ggc_user -c 'python3'
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>>
>>> channel = 20
>>> GPIO.setmode(GPIO.BCM)
>>> GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Not running on a RPi!
  • Have you added ggc_user to the gpio group?

  • @JoeAtAWS - That fixed my issue. Thanks so much!!!

flycast
asked 2 years ago205 views
1 Answer
3
Accepted Answer

On Raspbian, your user needs to be part of the gpio group to access GPIOs. Make sure ggc_user is part of that group.

sudo useradd ggc_user gpio
AWS
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions