2

I am trying to run the following script from reboot:

from gpiozero import MotionSensor
from gpiozero import Motor
from picamera import PiCamera
import datetime as dt
from time import sleep

camera = PiCamera()
pir = MotionSensor(4, 1, 100, .6, False)
motor = Motor(3,14) #first number is forwards, second is backwards
recordingTime = 10

#wait for motion, then move the motor back and forth
pir.wait_for_motion() 
print("*Experiment* subject is peeing")
#moves motor forward for 3 seconds at 25% speed
motor.forward(.25)
sleep(3) 
motor.stop()
sleep(5) #leaves strip out for 5 seconds
#moves motor backward for 3 seconds at 50% speed
motor.backward(.5)
sleep(3) 
motor.stop()

#Start recording
filename = dt.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264") #saves file as a date
camera.start_recording(filename) #starts recording and saves it as filename
camera.annotate_text = dt.datetime . now() . strftime("%Y-%m-%d_%H.%M.%S")
start=dt.datetime.now()

#Keep recording until time runs out
while (dt.datetime.now() - start).seconds < recordingTime: 
    camera.annotate_text = dt.datetime.now(). strftime("%Y-%m-%d_%H.%M.%S")
    camera.wait_recording(.2)
camera.stop_recording() #stops recording

I've tried numerous methods, including editing the crontab, creating a shell script that will run automatically, going into the init.d and creating a script there, and nothing works. Instead of running the code above, the motor just keeps on spinning incessantly until I manually go into the code and click 'run'. Also, once the video files are recorded, is there any way for them to automatically upload to my PC on a different network? What should I do? What am I missing? Thank you for the help

2 Answers 2

2

When you made your script in /etc/init.d, did you followed the syntax shown in the example file /etc/init.d/skeleton ?

Did you set a correct file permissison ?
sudo chmod +x /etc/init.d/mylauncher

Did you activated your service ?
sudo insserv mylauncher

7
  • 1. I did not follow that format. I used the format described here: [stuffaboutcode.com/2012/06/… but substituted all instances of noip for mylauncher, as well as deleted the stuff between BEGIN INIT INFO and END INIT INFO 2. when i did that terminal returned: sudo: unable to resolve host raspberry 3. When I did that, terminal returned: sudo: unable to resolve host raspberry insserv: warning: script 'EXPERIMENTAL_DETECTOR' missing LSB tags and overrides
    – NeonCop
    Commented Jul 5, 2017 at 23:30
  • The part btw BEGIN INIT INFO and END INIT INFO should be keeped untouched. It looks like comments, but contains boot dependencies and runlevels where the program will be launched and stopped. Commented Jul 5, 2017 at 23:42
  • Ok, I did that, and something interesting happened. Now, when I rebooted the computer, the script started to take the video, but the motor wouldn't stop spinning. In terms of trying #2 and #3, when I put them into the terminal it just skips to the next line without anything happening. Should my Python script be named mylauncher too, or does that not matter? How would I delete or change names of files in init.d (I have a duplicate .save copy of mylauncher)? Definitely a step in the right direction though, thank you!
    – NeonCop
    Commented Jul 5, 2017 at 23:55
  • You can use the name of your choice for the launcher in /etc/init.d, but avoid renaming this launcher once it is installed, some symbolic links would fail. Use sudo insserv -r mylauncher to remove these links. And you can also use the name and the location of your choice for your own program. You can't launch your program twice, as it use picamera. Use sudo killall yourprogram.py to kill the auto-launched program before launching it again from terminal. Commented Jul 6, 2017 at 0:08
  • OK, now I wiped my whole SD card clean, used the following modified code: pastebin.com/Xzc0pu3K , I did everything as per your instructions, though nothing of note seemed to happen when I put it in terminal, and yet I still get "Starting mylauncher from: can't read /var/mail/gpiozero from: can't read /var/mail/gpiozero from: can't read /var/mail/picamera /home/pi/Detector.py: 4: /home/pi/Detector.py: import: not found from: can't read /var/mail/time /home/pi/Detector.py: 7: /home/pi/Detector.py: Syntax error: "(" unexpected " Any idea what it could be this time?
    – NeonCop
    Commented Jul 6, 2017 at 17:20
1

Ok, I figured it out. Turns out my Python code was missing a while True: before starting the main body of code, which was making it run once instead of multiple times. In addition to that, I had deleted some critical components from the init.d shell file that appeared to be comments but were really essential information about when to start and stop it. Also, I had to put #!/usr/bin/python before my Python script so the init.d file would know it was reading it in python. This website was very useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.