Angel__
Posts: 6
Joined: Sun Mar 13, 2016 12:29 am

GPIO without keyboard (Python)

Sun Mar 13, 2016 12:51 am

I have succedded in reading and writing digital signals with import RPi.GPIO (in Python).
However I was forced to write in the terminal something like "sudo python example_gpio.py" in order to have root privilegies.
Assuming that I have to use the Raspberry pi without keyboard (and with a touch screen), how can I manage to do that?
How can I start this example from the desktop?
Thank you

qwertyuiopzxcvbnm
Posts: 13
Joined: Fri May 01, 2015 7:23 pm

Re: GPIO without keyboard (Python)

Sun Mar 13, 2016 6:27 pm

If the raspberry pi has wifi, or you are able to connect wifi to it through USB, you could always SSH in from another computer and run the program from there. Raspberry Pi have a great tutorial on setting up SSH here:
https://www.raspberrypi.org/documentati ... ccess/ssh/

Angel__
Posts: 6
Joined: Sun Mar 13, 2016 12:29 am

Re: GPIO without keyboard (Python)

Mon Mar 14, 2016 4:01 pm

Thank you very much for your contribution Mr qwert...

I am pursuing the Rpi 3 (I will have it in a week), which has wifi. The problem is that I want my RPi to work by itself, as a portable central unit that controls something; and forcing it to be connected to a laptop reduces the portability.

I have been told about other packages (gpio zero) that does not require root privilegies. I am afraid that I have to move to this package ( the one that I have used is RPio). It is not a major problem, but entails some hours.

Thank you again anyway.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: GPIO without keyboard (Python)

Mon Mar 14, 2016 4:18 pm

Am I missing something? Assuming your script runs without the need for any keyboard input, then why don't you just set the script to run as root at boot? There's no need to SSH in at that point. Plenty of threads in the forum about doing that.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Angel__
Posts: 6
Joined: Sun Mar 13, 2016 12:29 am

Re: GPIO without keyboard (Python)

Tue Mar 15, 2016 11:21 am

Gracias elParaguayo

I have tried that too. But I had some problems because I also have graphical items (using Tkinter) and there appear some errors related to importing the library tkinter. In fact I do not know if the operating system is corrupted. I will install Jessie and hope to solve the problems.

Anyway, I will insist in launching during startup, although I would prefer that user can decide when to launch it from the desktop.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: GPIO without keyboard (Python)

Tue Mar 15, 2016 12:15 pm

Now I'm confused, if the user can decide to launch from the desktop, what's the input method that will be used? I thought the whole point was that you didn't have any input devices other than the GPIO.

I'd be amazed if there isn't another thread in this forum about getting Tkinter scripts to launch at boot.
Last edited by elParaguayo on Tue Mar 15, 2016 11:06 pm, edited 1 time in total.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Angel__
Posts: 6
Joined: Sun Mar 13, 2016 12:29 am

Re: GPIO without keyboard (Python)

Tue Mar 15, 2016 10:20 pm

Just for your curiosity, I am using a touch screen to introduce some basic options and to represent evolution of digitial inputs.
I am new in Raspberry and in Linux and I am afraid that I might have modified some files.
Since I can see that there is not a straightforward line of actuation, I am decided to install Jessie and begin to explore in forums for these issues.

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: GPIO without keyboard (Python)

Wed Mar 16, 2016 12:10 am

Create a bash script that launches your python script.

Code: Select all

#!/bin/bash
sudo python example_gpio.py
Clicking on the bash script will launch the python script with sudo
as long as you don't need to type in a password it will work.

User avatar
DougieLawson
Posts: 37594
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: GPIO without keyboard (Python)

Thu Mar 17, 2016 6:47 pm

tom.slick wrote:Create a bash script that launches your python script.

Code: Select all

#!/bin/bash
sudo python example_gpio.py
Clicking on the bash script will launch the python script with sudo
as long as you don't need to type in a password it will work.
Why not add a sensible shebang line to your python program and avoid needing yet another bash task running to launch your python task?

Code: Select all

#!/usr/bin/sudo /usr/bin/python
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

HankB
Posts: 127
Joined: Fri Jan 01, 2016 2:45 pm

Re: GPIO without keyboard (Python)

Thu Mar 17, 2016 8:07 pm

The RPi.GPIO module does not require root to run. The user it runs as must be in the gpio group (in /etc/group)

Code: Select all

[email protected]:~ $ grep gpio /etc/group
gpio:x:997:pi,hbarta
[email protected]:~ $ 
Pi is there by default and I added myself using 'sudo' and my favorite text editor.

Ordinarily I try really hard not to run stuff as root, but if this is an 'appliance' with no network connection there seems to be little risk.

I found instructions here for one way to autostart a GUI program on a Raspberry Pi: http://raspberrypi.stackexchange.com/qu ... ut-desktop I'm sure there are others.

HTH,
hank

Angel__
Posts: 6
Joined: Sun Mar 13, 2016 12:29 am

Re: GPIO without keyboard (Python)

Sun Mar 20, 2016 11:28 am

Thank you very much Dougie and Tom.
Your solutions solved my problem at the very first attempt.

With regard to Hank's solution, I am new in Linux and I do not exactly how to include pi in the gpio's group (although it does not seem difficult). Since the problem is solved,I have not tried this solution. I apologize for, despite polite Hank's suggestion, not trying it.

Additional note:
I also had another problem related to importing Tkinter
At first I wrote "import tkinter" (or "from tkinter import *") with low cases, which gave me no error if I ran from the idle editor.
However, if I tried to launch the script in the way you have suggested I got the error "ImportError: No module named 'Tkinter' ".
I think that it is an error related to the python version.
Changing to Tkinter (with first upper case), everything now is fine: I can launch the script from the desktop with graphical interface and I can access the gpio.

Angel__
Posts: 6
Joined: Sun Mar 13, 2016 12:29 am

Re: GPIO without keyboard (Python)

Sun Mar 20, 2016 11:30 am

I forgot. Thank you very much Hank aswell

Return to “Python”