hi I am trying to get 2 webcam outputs to be displayed in a webbrowser which runs in full screen mode.
So far it works aslong as I keep the fullscreen/kiosk disabled, once the full screen is on, my script pauses.
When I close the webbrowser the script continues and I can use my buttons which open a new browser and then the script pauses again (the purpose is that the script loops endlessly without pauses)
import RPi.GPIO as GPIO
from time import sleep
import os
url1 = 'http://10.0.0.31/mjpg/video.mjpg'
url2 = 'http://10.0.0.32/mjpg/video.mjpg'
GPIO.setmode(GPIO.BCM)
array = [4,17]
GPIO.setup(array, GPIO.IN, pull_up_down=GPIO.PUD_UP)
os.system('chromium-browser --app=http://10.0.0.31/mjpg/video.mjpg --kiosk')
while True:
if ( GPIO.input(17) == False ):
print('Button 1 Pressed')
os.system('date')
os.system('pkill chromium-browser')
print (GPIO.input(17))
os.system('chromium-browser --app=http://10.0.0.32/mjpg/video.mjpg --kiosk')
sleep(0.5)
if ( GPIO.input(4) == False ):
print('Button 2 Pressed')
os.system('date')
os.system('pkill chromium-browser')
print (GPIO.input(4))
os.system('chromium-browser --app=http://10.0.0.31/mjpg/video.mjpg --kiosk')
sleep(0.5)
else:
os.system('clear')
print ('Press a key')
sleep(0.1)
Is there a way to fix this without big changes (as I noticed a lot of people are fan of selenium, that's not an option) and using subprocess instead of os.system gives same result.
The reason this is happening is because os.system() waits for a process to finish, and won't continue your code until this happens. By using the "&" at the end of your command, the command does not have to finish, and your buttons should still be useable.
#Import libraries
import RPi.GPIO as GPIO
from time import sleep
import os
#Set up the URLs
url1 = 'http://10.0.0.31/mjpg/video.mjpg'
url2 = 'http://10.0.0.32/mjpg/video.mjpg'
#Set up GPIO pins
GPIO.setmode(GPIO.BCM)
array = [4,17]
GPIO.setup(array, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#Open the first webpage
os.system('chromium-browser --app=http://10.0.0.31/mjpg/video.mjpg --kiosk & ')
while True:
if ( GPIO.input(17) == False ):
print('Button 1 Pressed')
os.system('date')
os.system('pkill chromium-browser')
print (GPIO.input(17))
os.system('chromium-browser --app=http://10.0.0.32/mjpg/video.mjpg --kiosk &')
sleep(0.5)
if ( GPIO.input(4) == False ):
print('Button 2 Pressed')
os.system('date')
os.system('pkill chromium-browser')
print (GPIO.input(4))
os.system('chromium-browser --app=http://10.0.0.31/mjpg/video.mjpg --kiosk &')
sleep(0.5)
else:
os.system('clear')
print ('Press a key')
sleep(0.1)
Related
Apologies for the newbie Python post, but a bit of Googling and I still can't find what I need.
I have the following Pi Hat; https://github.com/modmypi/Jam-HAT, ...and I'm using their docs to guide me; https://gpiozero.readthedocs.io/en/stable/recipes.html
My idea is pretty simple;
run script when button pressed
wait for it to run, showing flashy lights whilst running
if I press another button, stop/kill script
However, I'm stuck on the part where I need to stop the script with a button
#!/usr/bin/env python3
import gpiozero
import subprocess
from signal import pause
from time import sleep
button1 = gpiozero.Button(18)
button2 = gpiozero.Button(19)
green1 = gpiozero.LED(16)
green2 = gpiozero.LED(17)
red1 = gpiozero.LED(5)
red2 = gpiozero.LED(6)
script = ""
yellow1 = gpiozero.LED(12)
yellow2 = gpiozero.LED(13)
def kill_hello():
global script
script.kill()
print("Killed PID: ", script.pid)
red1.on()
sleep(.5)
red1.off()
red2.on()
sleep(.5)
red2.off()
def say_hello():
global script
green1.on()
sleep(.5)
green1.off()
green2.on()
sleep(.5)
green2.off()
script = subprocess.Popen(['/home/kali/hello.sh'])
print("Script PID: ", script.pid)
while script.poll() is None:
if not button2.when_pressed:
green1.on()
sleep(.5)
green1.off()
sleep(1)
button2.when_pressed = kill_hello
print("Press Ctrl & C to Quit")
try:
button1.when_pressed = say_hello
pause()
except KeyboardInterrupt:
print( "\n...Quit!")
I've tried a try/except where that while loop is, but that has also not worked (not like the KeyboardInterrupt). It doesn't recognise the button has been pressed, and I'm assuming because it's not within a valid catch/else/something block.
Any suggestions for a simple break out of loop, please?
It seems the issue is around a single process is hogging Python, thus creating multiple threads helped;
#!/usr/bin/env python3
import gpiozero
import subprocess
import time
import threading
from signal import pause
button1 = gpiozero.Button(18)
button2 = gpiozero.Button(19)
green1 = gpiozero.LED(16)
green2 = gpiozero.LED(17)
red1 = gpiozero.LED(5)
red2 = gpiozero.LED(6)
script = ""
yellow1 = gpiozero.LED(12)
yellow2 = gpiozero.LED(13)
switch = True
def blink():
def run():
while (switch == True):
green1.on()
time.sleep(.2)
green1.off()
time.sleep(.2)
if switch == False:
break
thread = threading.Thread(target=run)
thread.start()
def switchon():
global switch
global script
switch = True
print('switch on')
script = subprocess.Popen(['/home/kali/hello.sh'])
blink()
def switchoff():
global switch
global script
print('switch off')
script.kill()
switch = False
try:
button1.when_pressed = switchon
button2.when_pressed = switchoff
pause()
except KeyboardInterrupt:
print( "\n...Quit!")
I'm configuring some leds and buttons on my raspberry pi, and when running the script the system warns me a channel is already in use. I tried looking for a solution, and a tip was to do a GPIO.Cleanup() in a finally clause. I tried, but it keeps failing. Any idea why? The error points to GPIO.setup(button_pin, GPIO.IN) but not sure to add these lines of code in my try clause?
#!/usr/bin/env python
import RPi.GPIO as GPIO
import os
import time
# Hide Warnings
# GPIO.setwarnings(False)
# Assign GPIO pin numbers
button_pin = 3 # Input Pin
button_led = 14 # Output Pin
green_led = 22 # Output Pin
red_led = 27 # Output Pin
# ir_receiver_pin = 17
# Use BCM pin numbering
GPIO.setmode(GPIO.BCM)
# 1. GPIO Setup
GPIO.setup(button_pin, GPIO.IN)
GPIO.setup(button_led, GPIO.OUT)
GPIO.setup(green_led, GPIO.OUT)
GPIO.setup(red_led, GPIO.OUT)
# 2. Button Led Behaviour on Startup
def button_led_on():
GPIO.output(button_led, GPIO.HIGH)
def button_led_off():
GPIO.output(button_led, GPIO.LOW)
def button_flicker_startup():
a = 1
while a < 4:
button_led_on()
time.sleep(0.3)
button_led_off()
time.sleep(0.3)
a = a + 1
button_led_on() # LED is high when Pi is on
# 3. Define front led behaviour on startup
def green_led_on():
GPIO.output(green_led, GPIO.HIGH)
def green_led_off():
GPIO.output(green_led, GPIO.LOW)
def red_led_on():
GPIO.output(red_led, GPIO.HIGH)
def red_led_off():
GPIO.output(red_led, GPIO.LOW)
def boot_flicker():
time.sleep(1.0)
green_led_on()
time.sleep(0.5)
green_led_off()
time.sleep(0.2)
green_led_on()
time.sleep(0.3)
green_led_off()
red_led_on()
time.sleep(0.3)
red_led_off()
time.sleep(0.2)
green_led_on() # LED is high when Pi is on
# 4. Main program
try:
button_flicker_startup()
boot_flicker()
GPIO.wait_for_edge(button_pin, GPIO.FALLING)
os.system("sudo shutdown -h now")
except:
pass
finally:
GPIO.cleanup()
I could use GPIO.setwarnings(False) but this is just hiding the error and would like to avoid that.
EDIT: My Raspberry Pi is using a Hifiberry dac who is using GPIO 2 and 3 for configuration. Could it be related to that?
I would change the main bit to this:
x = True
try:
while x:
button_flicker_startup()
boot_flicker()
GPIO.wait_for_edge(button_pin, GPIO.FALLING)
os.system("sudo shutdown -h now")
except:
pass
finally:
x = False
GPIO.cleanup()
because the functions are running, now we force them to stop running and then we cleanup.
I've used this video to help me, https://www.youtube.com/watch?v=LEi_dT9KDJI&t=257s :
I would watch it if I was you, I goes in depth about what you need/want.
I am trying to make a door swipe card system in Python for my Raspberry Pi. I broke the program into two: A Door Alarm and a Card Swipe Recording system. The two programs work individually but how do I combine the two programs into one python file? I've tried threading but it doesn't seem to work.
Below are the programs:
1.) Door Alarm: If door is left open for a certain duration, an led will blink, then an alarm will ring
import time
import RPi.GPIO as gpio
led = 37
buzzer = 11
door = 16
gpio.setmode(gpio.BOARD)
gpio.setwarnings(False)
gpio.setup(buzzer, gpio.OUT)
gpio.setup(led, gpio.OUT)
gpio.setup(door, gpio.IN, pull_up_down=gpio.PUD_UP)
def blink(buzzer):
gpio.output(buzzer, True)
time.sleep(0.1)
gpio.output(buzzer, False)
time.sleep(0.1)
return
def blink(led):
gpio.output(led, True)
time.sleep(1)
gpio.output(led, False)
time.sleep(1)
return
while True:
if gpio.input(door):
time.sleep(3)
for i in range(0,5):
blink(led)
for i in range (0,5):
blink(buzzer)
else:
gpio.output(buzzer, False)
gpio.cleanup()
2.) Card Swipe Recording System: When someone swipes their card, the led blinks and a picture is taken
import datetime
import time
import os
import RPi.GPIO as gpio
led = 37
t = datetime.datetime.now()
gpio.setmode(gpio.BOARD)
gpio.setwarnings(False)
gpio.setup(led, gpio.OUT)
def blink(led):
gpio.output(led, True)
time.sleep(0.1)
gpio.output(led, False)
time.sleep(0.1)
while True:
card = raw_input()
f = open("Laptop Sign Out" + '.txt', 'a')
f.write("OneCard Number: " + card[1:10] + " Time: " + t.strftime("%m-%d-%Y %H:%M:%S"))
f.write('\n')
f.write(';')
f.write('\n')
f.close()
time.sleep(1)
for i in range(0,3):
blink(led)
os.system('fswebcam ~/Desktop/Photos/%H%M%S.jpeg')
time.sleep(3)
gpio.cleanup()
(UPDATE) Also, below is my attempt at threading:
import time
import RPi.GPIO as gpio
import os
import datetime
from threading import Thread
led = 37
buzzer = 11
door = 16
t = datetime.datetime.now()
gpio.setmode(gpio.BOARD)
gpio.setwarnings(False)
gpio.setup(buzzer, gpio.OUT)
gpio.setup(led, gpio.OUT)
gpio.setup(door, gpio.IN, pull_up_down=gpio.PUD_UP)
def blink(buzzer):
gpio.output(buzzer, True)
time.sleep(0.1)
gpio.output(buzzer, False)
time.sleep(0.1)
return
def blink(led):
gpio.output(led, True)
time.sleep(1)
gpio.output(led, False)
time.sleep(1)
return
def doorsensor():
while True:
if gpio.input(door):
time.sleep(3)
for i in range(0,5):
blink(led)
for i in range (0,5):
blink(buzzer)
else:
gpio.output(buzzer, False)
def cardreader():
while True:
card = raw_input()
f = open("Laptop Sign Out" + '.txt', 'a')
f.write("OneCard Number: " + card[1:10] + " Time: " + t.strftime("%m-%d-%Y %H:%M:%S"))
f.write('\n')
f.write(';')
f.write('\n')
f.close()
time.sleep(1)
for i in range(0,3):
blink(led)
os.system('fswebcam ~/Desktop/Photos/%H%M%S.jpeg')
time.sleep(3)
f1 = Thread(target = doorsensor())
f2 = Thread(target = cardreader())
f2.start()
f1.start()
gpio.cleanup()
You need to pass your thread functions as the target arguments, not their return values:
import sleep
f1 = Thread(target=doorsensor) # Remove parentheses after doorsensor
f1.daemon = True
f1.start()
f2 = Thread(target=cardreader) # Remove parentheses after cardreader
f2.daemon = True
f2.start()
# Use a try block to catch Ctrl+C
try:
# Use a while loop to keep the program from exiting and killing the threads
while True:
time.sleep(1.0)
except KeyboardInterrupt:
pass
gpio.cleanup()
The daemon property is set on each thread so that the program will exit when only the daemon threads are left:
A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the daemon property.
I'm presenting a thread-less approach.
The idea is to turn your while bodies into update functions, and call them alternatively.
First off, your door loop becomes
def update_door():
if gpio.input(door):
time.sleep(3)
for i in range(0,5):
blink(led)
for i in range (0,5):
blink(buzzer)
else:
gpio.output(buzzer, False)
Then your card swipe recording system becomes
def update_card():
card = raw_input()
f = open("Laptop Sign Out" + '.txt', 'a')
f.write("OneCard Number: " + card[1:10] + " Time: " + t.strftime("%m-%d-%Y %H:%M:%S"))
f.write('\n')
f.write(';')
f.write('\n')
f.close()
time.sleep(1)
for i in range(0,3):
blink(led)
os.system('fswebcam ~/Desktop/Photos/%H%M%S.jpeg')
time.sleep(3)
Finally, your main loop becomes:
while True:
update_door()
update_card()
But a problem arises: time.sleep in update_card will delay update_door as well.
Here, you have three solutions:
1 - It's ok if update_door is delayed
Well, ok.
2 - It's not ok if update_door is delayed, but it's ok if update_card is not delayed
Then just remove time.sleep(3).
3 - You need update_door not to be delayed, and update_card to be delayed
Then you can set a manual timer, using the time module.
lastCardUpdate = time.time()
while True:
update_door()
now = time.time()
if now - lastCardUpdate >= 3:
update_card()
lastCardUpdate = now
But raw_input in update_card is a blocking method, waiting for a user input.
If you do need this user input to happen every three seconds, then this approach cannot be used.
If you can move it before the while, ie outside of the update_card function, then it's fine.
Else, you will indeed need to use threads.
If you are attempting to run these two programs simultaneously, then you will have to use either threading or multiprocessing, which you say you have attempted. If you have, may we see your attempt as we may be then able to help you with your issue there.
One other issue is that all of your methods are named Blink, which is not allowed in Python, in python all of your methods should have different names.
Edit: For threading make sure to type threading.Thread(target = target) as your code
Regardless of any other mistake, you need to join one of your threads once they have been started.
f1 = Thread(target = doorsensor())
f2 = Thread(target = cardreader())
f2.start()
f1.start()
f1.join()
What does f1.join() do?
Basically, it tells Python to wait until f1 has finished running.
If you don't do so, the program will start f1 and f2, then will exit.
Upon exiting, Python will release all the resources, including those two threads, whose execution will stop.
So, I am programming in python on my pi. Through a cwiid library I am connecting my wii controller. This works fine.
But, after my wii controller connects trough bluetooth, suddenly the leds won't work.
The same line of code works before connecting, but doesn't work anymore after connecting. How is this possible?
My complete code:
if __name__ == '__main__':
from sys import path
path.append("..")
import RPi.GPIO as GPIO
from time import sleep
import os
import cwiid
from MotorPwm2 import MotorPwm
from GPIOFuckUp import GPIOFuckUp
from Basis import Basis
GPIOFuckUp()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
motor = MotorPwm([10, 9, 8, 7])
basis = Basis()
startbool = False
running = True
left_light = 21
right_light = 20
left_siren = 16
right_siren = 26
GPIO.setup(left_light, GPIO.OUT)
GPIO.setup(right_light, GPIO.OUT)
GPIO.setup(left_siren, GPIO.OUT)
GPIO.setup(right_siren, GPIO.OUT)
left_pin = GPIO.PWM(left_light, 1000)
right_pin = GPIO.PWM(right_light, 1000)
left_pin_s = GPIO.PWM(left_siren, 1000)
right_pin_s = GPIO.PWM(right_siren, 1000)
left_pin.start(100)
right_pin.start(100)
left_pin_s.start(0)
right_pin_s.start(0)
# Code above works. Leds turn on, with no problems.
def turn_on_left():
dim_left(100)
def turn_on_right():
dim_right(100)
def turn_on_lefts():
dim_lefts(100)
def turn_on_rights():
dim_rights(100)
def turn_on():
print 'aan'
dim_left(100)
dim_right(100)
def turn_off_lefts():
dim_lefts(0)
def turn_off_rights():
dim_rights(0)
def turn_off_left():
dim_left(0)
def turn_off_right():
dim_right(0)
def turn_off():
dim_left(0)
dim_right(0)
def dim(percentage):
dim_left(percentage)
dim_right(percentage)
def dim_left(percentage):
left_pin.ChangeDutyCycle(percentage)
print 'left'
def dim_right(percentage):
right_pin.ChangeDutyCycle(percentage)
print 'right'
def dim_lefts(percentage):
left_pin_s.ChangeDutyCycle(percentage)
def dim_rights(percentage):
right_pin_s.ChangeDutyCycle(percentage)
def siren_loop():
while running:
dim_lefts(100)
dim_rights(100)
sleep(0.05)
turn_off_lefts()
turn_off_rights()
sleep(0.05)
dim_rights(100)
sleep(0.05)
turn_on_rights()
sleep(0.05)
dim_lefts(100)
sleep(0.05)
dim_lefts(0)
sleep(0.05)
# Def things above also work. Copied the code in another file just to try
the leds, and that also worked.
button_delay = 0.1
print 'Press 1 + 2 on your Wii Remote now ...'
# Here the controller is connecting. After it is connected the leds turn of
# Before the following print lines are printed.
# Connect to the Wii Remote. If it times out
# then quit.
try:
wii = cwiid.Wiimote()
#GPIO.output(PIN_LED, 0)
except RuntimeError:
print "Error opening wiimote connection"
#GPIO.output(PIN_LED, 0)
# Uncomment this line to shutdown the Pi if pairing fails
os.system("sudo reboot")
quit()
print 'Wii Remote connected...\n'
print 'Press some buttons!\n'
print 'Press PLUS and MINUS together to disconnect and quit.\n'
wii.rpt_mode = cwiid.RPT_BTN
while True:
buttons = wii.state['buttons']
# If Plus and Minus buttons pressed
# together then rumble and quit.
if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
print '\nClosing connection ...'
wii.rumble = 1
#GPIO.output(PIN_LED, 1)
sleep(1)
wii.rumble = 0
#GPIO.output(PIN_LED, 0)
os.system("sudo reboot")
exit(wii)
# Button presses below work and print the correct lines. Just no leds :(
if (buttons & cwiid.BTN_LEFT):
print 'Left pressed'
motor.left_forward(50)
sleep(button_delay)
elif (buttons & cwiid.BTN_RIGHT):
print 'Right pressed'
motor.right_forward(50)
sleep(button_delay)
elif (buttons & cwiid.BTN_UP):
print 'Up pressed'
motor.forward(100)
sleep(button_delay)
elif (buttons & cwiid.BTN_DOWN):
print 'Down pressed'
motor.backward(100)
sleep(button_delay)
elif (buttons & cwiid.BTN_A):
print 'A'
if startbool == False:
print 'start aan'
startbool = True
sleep(0.5)
else:
print 'start uit'
startbool = False
sleep(0.5)
elif (buttons & cwiid.BTN_1):
if running:
turn_on()
sleep(5)
print '1'
print 'Licht 5 sec aan'
elif (buttons & cwiid.BTN_2):
print '2'
print 'sirene 5 sec aan'
siren_loop()
sleep(5)
else:
GPIO.cleanup()
So, for example, the start values turn the leds on when I run the program. This happens, the leds light up. But then, after the wii remote connects, they turn off and are impossible to turn on again.
After trying for another few hours, I finally found the problem.
The last line said:
else:
GPIO.cleanup()
Which resetted all the pins, hence they were not working. Deleting the else made everything work again.
Now I know the problem it seems so simple xD
I`m writing simple script for my home security system. it consist of button, led, buzzer, and sensor. It works like this:
- button pressed, - alarm function on, - it detects me ( everything is fine), - button pressed again, - alarm off(still fine) - button pressed again - and heres the problem: when a wave if front of the alarm when it was off, it still catches the movement, keeps this signal somehow, and the first thing after - alarm on is spotted signal, even if sensor didnt sent signal.
Hope i explain it clear enough. Here's the code:
import RPi.GPIO as GPIO
import time
import os
from itertools import cycle
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(04, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)#switch
GPIO.setup(17, GPIO.OUT)#led
GPIO.setup(18, GPIO.OUT)#buzzer
GPIO.setup(21, GPIO.IN)#sensor
#### definicje funkcji #####
############################
############################
def led_on(czas):
GPIO.output(17,1)
time.sleep(czas)
GPIO.output(17,0)
return;
#led_on(10)
############################
def buzzer_on(czas):
GPIO.output(18,1)
time.sleep(czas)
GPIO.output(18,0)
return;
#buzzer_on(0.1)
############################
def alarm_on():
print "alarm on"
while True:
if (GPIO.input(21)):
print "spotted!"
buzzer_on(0.1)
led_on(0.1)
time.sleep(1)
if (GPIO.input(04) == 1):
next(wlacznik)
alarm_off();
led_on(1)
break
return;
def alarm_off():
print "alarm off"
return;
############################
########################################### PROGRAM ###########################################
###############################################################################################
###############################################################################################
### WLACZNIK_ALARMU ######
wlacznik = cycle(range(2))
print "wartosc wlacznika:"
print next(wlacznik)
prev_input = 0 #toggle dla przycisku
while True:
input = GPIO.input(04)
#if the last reading was low and this one high, print
if ((not prev_input) and input):
print("Button pressed")
if (next(wlacznik) == 1):
led_on(0.1)
time.sleep(0.1)
led_on(0.1)
time.sleep(1)
alarm_on();
#update previous input
prev_input = input
#slight pause to debounce
time.sleep(0.05)
########################################### KONIEC ############################################
GPIO.cleanup()