Snap does not recoginzed running from Raspberry Pi


#1

I have bash script that execute python file and turn gpio pins on or off.
When running it on the device it works but as snap the python library (RPi.GPIO) return RuntimeError: This module can only be run on a Raspberry Pi!.

Snapcraft file snapcraft.yaml:

grade: devel
confinement: devmode
parts:
  setup:
    plugin: python
    python-version: python2
    python-packages: [RPi.GPIO]
  app-name :
    plugin: dump
    source: .
    stage-packages:
      - python3

apps:
  app-name:
    command: lightOn.sh $SNAP

Bash script lightOn.sh:

#!/bin/bash
FILE1=$1
python $FILE1/myprogram.py

Python file myprogram.py:

#!/usr/bin/env python2
import RPi.GPIO as GPIO

The code have to run from bash script.
Is there a way to make the python library know that it running on Raspberry Pi from inside the snap?.
Or another way to access gpio pins from bash script inside snap.


#2

while i’m not sure how exactly the python library determines it is running on a pi or not (we’d have to find out to extend the interfaces for this), any snap can access /proc/cpuinfo and read the “Hardware” value from there:

ogra@stream:~$ snap run --shell htop
ogra@stream:/home/ogra$ cat /proc/cpuinfo|grep Hardware
Hardware	: BCM2709

perhaps adding and connecting the hardware-observe interface to your app will give the python lib enough access to system ressources to help here …


#3

You also eventually will need the gpio plugs for your app like this :

apps:
  app-name:
    command: lightOn.sh $SNAP
    plugs: [gpio, hardware-observe]

And then connect the plugs to your gadget snap, i.e. snap connect your-snap:gpio pi3:bcm-gpio-1 for all such gpio pins you want to use with your snap.

I’m not too familiar with the internals of the GPIO python library, but it’s possible it tries to access the device nodes for the gpio pins and that fails with EACCESS or EPERM and instead of raising that error it just assumes that means it’s not running on a Raspberry Pi.


#4

funnily i have hit something similar today when building the picamera pip module, the picamera module allows to override the test via exporting READTHEDOCS=True (sooo intuitive !! :stuck_out_tongue: ) before running pip install …

perhaps that works for you too … my snapcraft.yaml is at:

take a look at the build-environment: option i use in the python plugin.


#5

Sorry guys my bad I used the wrong python library, I am using NanoPi NEO2 and importing the python library is the same on both libraries import RPi.GPIO as GPIO.
The one that worked for me was pre-installed on the device so I though I used the same library.
I changed it and now it works.

Thank you for all your help.