edsulst
Posts: 28
Joined: Sat Nov 16, 2019 7:39 am

Push button to make picture

Sat Nov 16, 2019 7:52 am

Hi there,

I have a Raspberry PI 3B with an USB webcam attached.
With the fwebcam command I can take a photo and store in in a folder.

Now I want to use a push button to make a photo with the fswebcam command.
I've made a takepic.py script: (adapted from a script I found on the internet)
(There is some code to turn a led on and off, but I've chosen not to use an led, so that piece code can be deleted.
Also, the original script added a photo with a number. I want to overwrite the photo, so don't make use of the image_num +1)

Code: Select all

#!/usr/bin/env python

import sys, os, time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) 
GPIO.setup(11, GPIO.IN)     # push button (pin 11)
GPIO.setup(12, GPIO.OUT)    # led (pin 12)

image_num = 1

GPIO.output(12, False)

while True:
	if GPIO.input(11):  # button is pushed
		strImage = str(image_num)
                "fswebcam --no-banner /var/www/html/webcam.jpg"
		image_num = image_num + 1
	        GPIO.output(12, True)    # led ON
	        time.sleep(3)
	        GPIO.output(12, False)   # led OFF

GPIO.cleanup()

Also I've made an createpic.sh script and placed this into /etc/rc.local

Code: Select all

#! /bin/bash
python /home/pi/takepic.py &
I'v connected 3 wires to the push button.
1 wire go's to the GROUND on PIN6
1 wire go's to PIN11 (signal)
1 wire with a 10K resistor go's to the 3.3V on PIN1

Whatever I try, the script will not take a picture of the webcam.
Can anyone see what I'm doing wrong here ?

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Push button to make picture

Sun Nov 17, 2019 10:49 am

So assuming your switch is connected like this

Image

try running this code

Code: Select all

#!/usr/bin/env python

import sys, os, time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) 
GPIO.setup(11, GPIO.IN)     # push button (pin 11)

while True:
    while GPIO.input(11) == 1: #loops waiting for button press
            time.sleep(0.5)
    
    print("Button pressed Take picture")    # remove if you want only there for debugging
    fswebcam --no-banner /var/www/html/webcam.jpg
    
    while GPIO.input(11) == 0: #loops waiting for button release
            time.sleep(0.5)   
    print("Button released")    # remove if you want only there for debugging

We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

edsulst
Posts: 28
Joined: Sat Nov 16, 2019 7:39 am

Re: Push button to make picture

Sun Nov 17, 2019 12:48 pm

pcmanbob,

Even with your code I can't get it to work, so I did some searching and came across this code.
This code seems to work. I can add more commands to be execute and this works.
Also I only need 2 wires. One to the 3.3V and one to pin 10 to take a picture. In the code above I needed 3 wires.

Code: Select all

#!/usr/bin/env python

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import os
import time

def button_takepic(channel):
    print (time.strftime("      %a %d-%m-%Y @ %H:%M:%S"))
    print("Button was pushed!")
    os.system("fswebcam -r 1280x720 --no-banner /var/www/html/image.jpg")
    time.sleep(3)
    print ("\r\n\r\n"),

def button_shutdown(channel):
    print (time.strftime("      %a %d-%m-%Y @ %H:%M:%S"))
    print("Button was pushed!")
    os.system("sudo shutdown -h now")
    time.sleep(3)
    print ("\r\n\r\n"),


GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_takepic) # Setup event on pin 10 rising edge

GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 12 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(12,GPIO.RISING,callback=button_shutdown) # Setup event on pin 12 rising edge


message = input("Waiting for picture\n\n") # Run until someone presses enter
GPIO.cleanup() # Clean up
The only thing that isn't working is to autostart the script.
I've made an createpic.sh script and placed this into /etc/rc.local with this code:

Code: Select all

#! /bin/bash
python /home/pi/takepic.py &
I thought it would start the createpic.sh script and executed the takepic.py in the background, but whatever I do, it won't do anything if I press the button. It only seems working if I excecute takepic.py manually from within the terminal, but I want it to start automatically.

Do you have any tips for me ?

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Push button to make picture

Sun Nov 17, 2019 12:55 pm

The reason my code did not work was I thought you were using the python version of fswebcam

you are don't seem to be hence the need to call it using os.system.

yes you can add more commands to be executed add them to the function " def button_takepic(channel):"
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

edsulst
Posts: 28
Joined: Sat Nov 16, 2019 7:39 am

Re: Push button to make picture

Sun Nov 17, 2019 1:27 pm

Ok, both codes can work.

Now the other problem about autostarting the script.

When I boot my Raspberry Pi, it start in desktop mode and opens Firefox fullscreen at http://localhost (and shows an image)
When I press the button nothing is happening but I want to create a photo with the script and than that photo will be placed in the /var/www/html directory.

What I thought was that when putting a bash script in rc.local it started when booting the Pi.
Is this true, or is it only working when bootin into xterm instead of the desktop ?

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Push button to make picture

Sun Nov 17, 2019 1:31 pm

edsulst wrote:
Sun Nov 17, 2019 1:27 pm
Ok, both codes can work.

Now the other problem about autostarting the script.

When I boot my Raspberry Pi, it start in desktop mode and opens Firefox fullscreen at http://localhost (and shows an image)
When I press the button nothing is happening but I want to create a photo with the script and than that photo will be placed in the /var/www/html directory.

What I thought was that when putting a bash script in rc.local it started when booting the Pi.
Is this true, or is it only working when bootin into xterm instead of the desktop ?
how do you know its not working ?

you get no output to the screen when running things from rc.local, and what does your bash script contain ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

edsulst
Posts: 28
Joined: Sat Nov 16, 2019 7:39 am

Re: Push button to make picture

Sun Nov 17, 2019 2:01 pm

I've added exec > /tmp/rc-local.out 2>&1;set -x in the rc.local file.

And the result of the rc-local.out file is:

Traceback (most recent call last):
File "/home/pi/takepic.py", line 32, in <module>
message = input("Waiting for picture\n\n") # Run until someone presses enter
EOFError: EOF when reading a line

My bash script is:

Code: Select all

#! /bin/bash
python /home/pi/takepic.py &

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Push button to make picture

Sun Nov 17, 2019 2:07 pm

edsulst wrote:
Sun Nov 17, 2019 2:01 pm
I've added exec > /tmp/rc-local.out 2>&1;set -x in the rc.local file.

And the result of the rc-local.out file is:

Traceback (most recent call last):
File "/home/pi/takepic.py", line 32, in <module>
message = input("Waiting for picture\n\n") # Run until someone presses enter
EOFError: EOF when reading a line

My bash script is:

Code: Select all

#! /bin/bash
python /home/pi/takepic.py &

what is the full line in the rc.local file that you entered ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

edsulst
Posts: 28
Joined: Sat Nov 16, 2019 7:39 am

Re: Push button to make picture

Sun Nov 17, 2019 2:12 pm

This is my complete rc.local file:

Code: Select all

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
exec > /tmp/rc-local.out 2>&1;set -x
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
/home/pi/createpic.sh
exit 0
As you can see I only added /home/pi/createpic.sh to the bottom of the rc.local file, before the exit 0 code

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Push button to make picture

Sun Nov 17, 2019 2:23 pm

As your program is now running it the background may be the as there is now no way for an input to capture any keypresses this is causing the error .

try replacing the last lines of your code like this

Code: Select all

#!/usr/bin/env python

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import os
import time

def button_takepic(channel):
    print (time.strftime("      %a %d-%m-%Y @ %H:%M:%S"))
    print("Button was pushed!")
    os.system("fswebcam -r 1280x720 --no-banner /var/www/html/image.jpg")
    time.sleep(3)
    print ("\r\n\r\n"),

def button_shutdown(channel):
    print (time.strftime("      %a %d-%m-%Y @ %H:%M:%S"))
    print("Button was pushed!")
    os.system("sudo shutdown -h now")
    time.sleep(3)
    print ("\r\n\r\n"),


GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_takepic) # Setup event on pin 10 rising edge

GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 12 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(12,GPIO.RISING,callback=button_shutdown) # Setup event on pin 12 rising edge


while true:
    time sleep(2)
    
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

edsulst
Posts: 28
Joined: Sat Nov 16, 2019 7:39 am

Re: Push button to make picture

Sun Nov 17, 2019 3:42 pm

I've changed the code and it doesn't seem to work.

The error I've got in rc-local.out is:

+ hostname -I
+ _IP=
+ [ ]
+ /home/pi/createpic.sh
File "/home/pi/takepic.py", line 33
time sleep(2)
^
SyntaxError: invalid syntax

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Push button to make picture

Sun Nov 17, 2019 3:48 pm

Its just a typo

Code: Select all

time.sleep(2)
you could have looked that up on line.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

edsulst
Posts: 28
Joined: Sat Nov 16, 2019 7:39 am

Re: Push button to make picture

Sun Nov 17, 2019 6:35 pm

It's working now !!

Many thanks @pcmanbob

There was another error after changing time.sleep(2)
I also needed to change the word true in the while. It has to be a capital T according to the error log.

My code is now this:

Code: Select all

while True:
    Time.sleep(2)
After booting to Firefox in full screen I can press the button and a snapshot is made and saved in /var/www/html

Again many, many thanks !!

Return to “Python”