Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOLVED python script help
#1
Hey, super new to python and after following a few different tutorials and making a complete mess I was able to create a file to capture an image from my raspberry pi. however I cant figure out why or how to have the file always waiting for a button press. It works the first time then I assume stops. No errors.

Program was created in Thonny and F5 to run it. (not sure if its intended to only run once?) idk how else to start the script.

what I wanted to achieve was have it run and wait for a button press then take a picture and save it, which is does currently, however when I press the button a second time nothing happens.


This is my super messy code.

import picamera
from time import sleep
from subprocess import call
from gpiozero import Button
from datetime import datetime

button = Button(17)

#filepath
filePath = "/home/pi/Desktop/mmPictures/images/"

#setting up date and time
#grab the current time
currentTime = datetime.now()
#create filename for our picture
picTime = currentTime.strftime("%Y.%m.%d-%H%m%S")
picName = picTime + '.jpg'
completeFilePath = filePath + picName

button.wait_for_press()
#setup the camera such that it closes when we are done with it
print("Setting up")
sleep(1)

commandStop = "sudo systemctl stop motioneye"
call ([commandStop], shell=True)
print("Motioneye stopped")
sleep(1)

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    #camera.capture("/home/pi/Desktop/mmPictures/images/newimage.jpg")
    camera.capture(completeFilePath)
    
print("Picture taken")
sleep(1)
commandStart = "sudo systemctl start motioneye"
call([commandStart], shell=True)
print("Motioneye started")
Reply
#2
You need to put your button.wait_for_press and subsequent code in a loop. For starters, I suggest adding
while True:
at line 19 and indent all of your code after that one level. You will need to ctrl-C to get out of the program, but once it prints Motioneye started it will go back to the top of the loop and wait for press again.
Reply
#3
(Nov-15-2020, 02:17 PM)jefsummers Wrote: You need to put your button.wait_for_press and subsequent code in a loop. For starters, I suggest adding
while True:
at line 19 and indent all of your code after that one level. You will need to ctrl-C to get out of the program, but once it prints Motioneye started it will go back to the top of the loop and wait for press again.

Sorta works, however when i press the button again it runs though the loop but it overwrites the same image? but when i run it without the loop it saves to a new image..

EDIT: it creates a new save for the image if i start and stop the program, but if i dont stop the program it overwrites the current image.
Reply
#4
Move your while to line 11 so you get a new filename based on time
Reply
#5
(Nov-15-2020, 10:24 PM)jefsummers Wrote: Move your while to line 11 so you get a new filename based on time

I have figured it out, just forgot to post another update! but thank you for pointing me in the right direction!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,009 Jun-29-2023, 11:57 AM
Last Post: gologica
  math formula does not give the same result as bash script [SOLVED] AlphaInc 3 921 Apr-02-2023, 07:21 PM
Last Post: AlphaInc
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 894 Nov-16-2022, 07:58 PM
Last Post: Winfried
  [SOLVED] Requiring help running an old Python script (non Python savvy user) Miletkir 13 5,309 Jan-16-2021, 10:20 PM
Last Post: Miletkir
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,793 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,254 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,583 May-11-2019, 08:12 PM
Last Post: keames
  How to run python script which has dependent python script in another folder? PrateekG 1 3,105 May-23-2018, 04:50 PM
Last Post: snippsat
  How to call one python script and use its output in another python script lravikumarvsp 3 32,288 May-16-2018, 02:08 AM
Last Post: lravikumarvsp
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,789 Jul-07-2017, 08:59 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020