0

RASPBERRY PI 4

Hello there, I have this script that should shut off a power outlet when it detects a movement.

The rpi-rf_send code is from this repo.

The question is: if the current goes away, how do I restart the script?

I tried to insert the script in rc.local (probably in the wrong way) but it doesn't work.

import time
from gpiozero import MotionSensor
import subprocess

pir = MotionSensor(pin=14,pull_up=False)

def restart_plug():
print("Starting plug...")
subprocess.run(["rpi-rf_send","8652300","-g", "3", "-p", "172"])

def stop_plug():
print("Stop plug...")
subprocess.run(["rpi-rf_send","8652292","-g", "3", "-p", "172"])
time.sleep(5)
restart_plug()

while True:
pir.wait_for_motion()
print("Waiting for motion...")

if pir.is_active:
  print("Motion detected!")
  stop_plug()
2
  • 2
    How exactly did you add it to rc.local. Or in other words. What is the exact command that you type in the commad line to get your script started and as which user did you start the command. And what exact command did you try to add to rc.locale. And what exactly do you mean with " it doesn't work." Does the raspberry pi still work normally, but your code is not started. do you get any funny errors. The reason I'm asking is, that it is difficult to help without that info
    – gelonida
    Nov 4, 2019 at 22:22
  • Your python script cannot work. It has wrong indents.
    – Ingo
    Nov 5, 2019 at 10:56

2 Answers 2

2

Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. You should use a systemd Unit file to start your script as service. Start with the following Unit file. If it doesn't work you can adapt it to run your your script. Create a new service with:

rpi ~$ sudo systemctl --force --full edit MotionSensor.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Motion Sensor
After=multi-user.target

[Service]
ExecStart=/full/path/to/MotionSensor.py

[Install]
WantedBy=multi-user.target

Enable and monitor the new service with:

rpi ~$ sudo systemctl enable MotionSensor.service
rpi ~$ systemctl status MotionSensor.service

Reboot. If it doesn't work just give a comment. Then we will see how to improve the service.

1
  • Thank you very much for your support. Have a nice day!
    – matto98
    Nov 5, 2019 at 18:08
0

The error was in te location of the rpi-rf_send script. It was located in the local path of the user instead of being located in the /usr/bin path

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.