A physical button to take photos

S.L.P. image questions, stereoscopic video livestream and recording, stereoscopic photo capture etc.
stereomaton
Posts: 215
Joined: Tue May 21, 2019 12:33 pm
Location: France

A physical button to take photos

Post by stereomaton »

Today I had another small time to play with stereopi.
I decided to add a physical button on it to take photographs.

The idea is simple : A script launched at startup waits for the button and than call the SLP "make_photo" page as if we pressed the icon on the web interface. (Need SLP with such photo icon in the interface, I use version 0.2.3)

To do so, I added a script in /opt/StereoPi/scripts/button.py (which does the job mentionned) and stated it in /opt/StereoPi/run.sh with "./scripts/button.py &".
Here is the script if you want to reproduce:

Code: Select all

#!/usr/bin/env python

# Configuration
button = 3

# Imports
import RPi.GPIO as GPIO
from time import sleep
from urllib2 import urlopen

# Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Wait button loop
while True:
  GPIO.wait_for_edge(button, GPIO.FALLING)
  print "Shoot!"
  urlopen('http://localhost/make_photo.php')
  sleep(0.5)
Simple, but effective.

Edit: the python script should have execution permissions for this way to work (see messages below)
Last edited by stereomaton on Thu Aug 22, 2019 11:12 pm, edited 1 time in total.
Stereophotographer and hacker
Despite my quite active participation in the forum, I am not in the StereoPi team
StereoPi (v1) Standard Edition + CM3Lite module + a few cameras

User avatar
Realizator
Site Admin
Posts: 900
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: A physical button to take photos

Post by Realizator »

Stereomaton, thank you for your code!
By the way, this 1.44 TFT screen has 3 buttons and 4-position joystick embedded. And all these goodies are connected to GPIOS. I will try to implement your code to one of the buttons!
Eugene a.k.a. Realizator

stereomaton
Posts: 215
Joined: Tue May 21, 2019 12:33 pm
Location: France

Re: A physical button to take photos

Post by stereomaton »

Forgot to mention:
The button in my example is GPIO n°3 as noted in the pinout of the wiki: GPIO03 (SCL1, I2C)
Image

I chose this one because there is a GND pin very close, but it can be anything else not already used (just change the number at the top of script). I let the full configuration in code even if this particular pin seems to already have a pull-up in hardware (according to the library which complains) because you may want another pin without hardware pull-up.
Stereophotographer and hacker
Despite my quite active participation in the forum, I am not in the StereoPi team
StereoPi (v1) Standard Edition + CM3Lite module + a few cameras

Alik
Posts: 9
Joined: Sun Aug 18, 2019 12:07 pm

Re: A physical button to take photos

Post by Alik »

Great idea, but I don't understand how to get it to work!
I did everything as described. But I can't take photos. May be I don't understand what "button" is.
I have shorted the:
GPIO03 (SCL1, I2C) and GND
GPIO03 (SCL1, I2C) and 3,3v

I have insert "./scripts/button.py &" in this place:

./scripts/loop-ws.sh &
./scripts/button.py &

cd ./skybox-server
./skybox-server.js &
cd ..


Nothing works. What did I miss? Can you help me?

User avatar
Realizator
Site Admin
Posts: 900
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: A physical button to take photos

Post by Realizator »

Hi Alik,
1. I hope you've unlocked filesystem before all these steps.

2. You do not need to connect this pin to 3.3V (as you mentioned). As you can see in the code, Stereomaton use fall edge detection in this line

Code: Select all

GPIO.wait_for_edge(button, GPIO.FALLING)
That is photo is taken when you connect this pin to GND. (Some basics about Pi's GPIO can be found here)

3. As for your start line "./scripts/button.py &" insertion, everything looks correct to me. But you need to know, that actually there are two ways to run Python script from the console. First one is to use expression like "python ./path-to-script/script.py", and second one is to use direct run like Stereomaton does, that is "./scripts/button.py &". But to use second way, you need to explain to Raspbian, that this file is executable. To do this, you need to change rights of this file like this:

Code: Select all

sudo chmod +x button.py
Hope this will help you.
Eugene a.k.a. Realizator

stereomaton
Posts: 215
Joined: Tue May 21, 2019 12:33 pm
Location: France

Re: A physical button to take photos

Post by stereomaton »

Awesome answer.
I forgot to mention this execution right because, well I added it by rote. This is likely the problem here and I am not sure I would have thought of it so quickly.

To complete general knowledge you provided, on the two ways to launch python script, there is a small catch: sometimes it is needed to add the specific version of python because most systems (including SLP) do not use the latest (now 3) major version by default. Here, the script was written in the python2 variant.

Also, the final & sign in shell scripts are here to detach execution of the command, ie do not wait for the end of it and let it run alone. It would thus be required also with the first way to launch the script in this context.
Stereophotographer and hacker
Despite my quite active participation in the forum, I am not in the StereoPi team
StereoPi (v1) Standard Edition + CM3Lite module + a few cameras

Alik
Posts: 9
Joined: Sun Aug 18, 2019 12:07 pm

Re: A physical button to take photos

Post by Alik »

Thanks, I did everything but nothing helps.
Then I've added a "python ./scripts/button.py &" to my "run.sh" and now it works!

User avatar
Realizator
Site Admin
Posts: 900
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: A physical button to take photos

Post by Realizator »

Alik wrote:
Tue Aug 20, 2019 8:51 am
Thanks, I did everything but nothing helps.
Then I've added a "python ./scripts/button.py &" to my "run.sh" and now it works!
Alik, I'm glad it works for you. :)
But I recommend you to dig out this problem, it can be useful for you in the future.
I can suggest you have some issues with changing file permissions. To do it correctly, you need to unlock filesystem (and you already do it successfully, as I understand). And run proposed chmod, when you are in the same directory with your button.py
Also you can try to run this command with absolute path, like this:

Code: Select all

sudo chmod +x /opt/StereoPi/scripts/button.py
Eugene a.k.a. Realizator

Alik
Posts: 9
Joined: Sun Aug 18, 2019 12:07 pm

Re: A physical button to take photos

Post by Alik »

The console said: "chmod: invalid mode: 'x' "
I have changed the file's permission throught file's properties in linux.

stereomaton
Posts: 215
Joined: Tue May 21, 2019 12:33 pm
Location: France

Re: A physical button to take photos

Post by stereomaton »

I was surprised by the answer you reported and checked in the sources of coreutils.
The mode indicated in quotes is not altered, so you probably missed the + sign.

On SLP, I noticed that the console in the web interface is a very limited emulation. I have not the board here to check, but perhaps it removes this sign (or you forgot to type it?). Personally, I prefer to use SSH for command line.
Stereophotographer and hacker
Despite my quite active participation in the forum, I am not in the StereoPi team
StereoPi (v1) Standard Edition + CM3Lite module + a few cameras

Alik
Posts: 9
Joined: Sun Aug 18, 2019 12:07 pm

Re: A physical button to take photos

Post by Alik »

I've just copy-past your code line. Also when I "asked" for help about "chmod" in console there was no "x" parameter at all.

User avatar
Realizator
Site Admin
Posts: 900
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: A physical button to take photos

Post by Realizator »

Alik, chmod +x just adds "executable" property to the file. You can do it other way (like using MC editor, already installed in SLP). I agree with Stereomaton, that it is looks like some syntax issues in your case. Linux suggest you several ways to do the same thing, and I'm glad that one of this ways gives you result you need. :)
By the way, are you using SSH connection to console, or WEB console from SLP admin panel? I just need to check, if it is a bug on our side (web BASH implementation).
Eugene a.k.a. Realizator

Alik
Posts: 9
Joined: Sun Aug 18, 2019 12:07 pm

Re: A physical button to take photos

Post by Alik »

I am using WEB console from SLP admin panel.

User avatar
Realizator
Site Admin
Posts: 900
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: A physical button to take photos

Post by Realizator »

Alik wrote:
Thu Aug 22, 2019 7:52 am
I am using WEB console from SLP admin panel.
Ok, I will check this case.
Eugene a.k.a. Realizator

Paul
Posts: 4
Joined: Thu May 23, 2019 2:56 pm

Re: A physical button to take photos

Post by Paul »

I prefer using gpiozero instead of RPi.GPIO.

You can check out the gpiozero documentation to configure a button controlled camera:
https://gpiozero.readthedocs.io/en/stab ... led-camera

Code: Select all

from gpiozero import Button
from picamera import PiCamera
from datetime import datetime
from signal import pause

button = Button(2)
camera = PiCamera()

def capture():
    timestamp = datetime.now().isoformat()
    camera.capture('/home/pi/%s.jpg' % timestamp)

button.when_pressed = capture

pause()

stereomaton
Posts: 215
Joined: Tue May 21, 2019 12:33 pm
Location: France

Re: A physical button to take photos

Post by stereomaton »

My aim was to modify SLP minimally, so that other users can easily add it too.

Thus I used a gpio library that was already installed, and the photo is taken through the SLP system without accessing the cameras directly. That way, other functionalities of the distribution (such as live preview on smartphone) still work.
Stereophotographer and hacker
Despite my quite active participation in the forum, I am not in the StereoPi team
StereoPi (v1) Standard Edition + CM3Lite module + a few cameras

User avatar
Realizator
Site Admin
Posts: 900
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: A physical button to take photos

Post by Realizator »

Paul, your approach is good to create solution from scratch for simple applications. You can use it with stock Raspbian to create functions you need, so it is just another approach. In SLP we tried to collect and integrate a lot of different features with a lot of tricks. For example, tiny webserver for admin panel, simultaneous video stream to several devices (like browser, Android application and Oculus), video recording, auto pause video recording while taking a photo and so on. You can consider SLP as a collection of ideas (and a sample code) to create specific solution for you. This is the main advantage of Open Source and friendly Raspbian ecosystem.

By the way, PiCamera is a key component in our OpenCV images, as we need to process captured data. But for livestream and recording we prefer to use raspivid and raspistill deep under the hood, as they are extremely optimized for this purposes. I mean memory usage and performance. Just try to capture, encode to h264 and livestream FullHD video with PiCamera, also recording it to the file at the same time ;-)
Eugene a.k.a. Realizator

Paul
Posts: 4
Joined: Thu May 23, 2019 2:56 pm

Re: A physical button to take photos

Post by Paul »

Thanks stereomaton and Relizator,

Yes, I was using the stock Raspbian image. I forgot that you mentioned that you were using the SLP image.

Since the SLP image boots up automatically, where do you need to put the button program startup script?

User avatar
Realizator
Site Admin
Posts: 900
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: A physical button to take photos

Post by Realizator »

Paul, there are several ways to do autostart (look at this article).
We prefer to use init.d approach. The simpest way I use for quick tests is a bash.rc approach. So you may choose the way you like.
Eugene a.k.a. Realizator

MauriceCyril
Posts: 4
Joined: Fri Feb 21, 2020 3:13 am

Re: A physical button to take photos

Post by MauriceCyril »

Hi stereomaton and Relizator,

Just wanted to confirm the steps:

0) if using the SLP image RPi.GPIO and urllib is already installed

1) First wire up the physical button to the GPIO Pins (assuming the button has two contacts or using 2 contacts if the button has 4):

Button + -----> Connect to GPIO Pin [In this example: Pin 5 or GPIO03 (SCL1, I2C)]
Button - -----> Connect to GND Pin [In this example: Pin 6 which is GND in the raspberry pi pinouts]

2) Power up the StereoPi and use SSH or the terminal to do the following:

Make the filesystem writable:

Code: Select all

$ sudo mount -o rw,remount /
Create a "button.py" python script using the following code.

For example create the script using nano

Code: Select all

$ cd /opt/StereoPi/scripts/
$ nano button.py 
and use the following script:

Code: Select all

#!/usr/bin/env python

# Configuration
# Connect to GPIO Pin [In this example: Pin 5 or GPIO03 (SCL1, I2C)]
button = 3

# Imports
import RPi.GPIO as GPIO
from time import sleep
from urllib2 import urlopen

# Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Wait button loop
while True:
  GPIO.wait_for_edge(button, GPIO.FALLING)
  print "Shoot!"
  urlopen('http://localhost/make_photo.php')
  sleep(0.5)
Close and save the script.

3) CHMOD the script to make it executable

Code: Select all

$ sudo chmod +x button.py
4) Add the script line item into the "run.sh" in "/opt/StereoPi/"

Code: Select all

$ sudo nano /opt/StereoPi/run.sh
and add the following line:

Code: Select all

./scripts/button.py &
For example

Code: Select all


killall -q fbi

/usr/bin/fbi -d /dev/fb0 -a -fitwidth -noverbose -T 2  ./imag$# start wlans manager
./scripts/wlan-switch.php &                                   
./scripts/video-source.sh &
#./scripts/loop-rtsp.sh &                                     ./scripts/loop-mpegts.sh &                                    ./scripts/loop-mpegts-skybox.sh &
./scripts/loop-rtmp.sh &
./scripts/usb-video.sh &
./scripts/loop-record.sh &
./scripts/record-watcher.sh &
./scripts/loop-ws.sh &

./scripts/button.py &

cd ./skybox-server
./skybox-server.js &
cd ..

systemctl start pigpiod > /dev/null 2>&1
sysctl -w net.ipv6.conf.all.disable_ipv6=1
Last edited by MauriceCyril on Thu Mar 05, 2020 1:23 am, edited 3 times in total.

Post Reply