diff --git a/README.md b/README.md index 76a8650..08b64f8 100644 --- a/README.md +++ b/README.md @@ -367,6 +367,40 @@ When date reads correctly run sudo hwclock -w sudo hwclock -r sudo reboot +## Setting up Systemd Services + +Dencam (Cyclops ONLY) uses a systemd service to turn on/off the PTZ +camera based on time of day. The camera is turned on for daytime and +astronomical twilight and turned off during nighttime. This script is +is located at `dencam/utilities/camera_relay.py.` + +To set up this script to run as a systemd service, navigate to: + + cd /etc/systemd/system + +Create a new file: + + sudo nano camera_relay.service + +Insert the following: + + [Unit] + Description=Camera Relay + + [Service] + Type=exec + ExecStart=/home//.virtualenvs/dencam/bin/python3 -u /home//dencam/utilities/camera_relay.py + + [Install] + Wantedby=multi-user.target + +After saving the file, run: + + sudo systemctl start camera_relay.service + +If the service needs to be stopped, run: + + sudo systemctl stop camera_relay.service # Dencam Pages diff --git a/utilities/camera_relay.py b/utilities/camera_relay.py new file mode 100644 index 0000000..6caf5d8 --- /dev/null +++ b/utilities/camera_relay.py @@ -0,0 +1,43 @@ +''' +Use sun altitude to trigger camera relay and turn +off camera when dark. Camera will stay on during +daylight and astonomical twilight. + +To be used as a systemd service. +''' + +import time +from datetime import datetime +from RPi import GPIO +import ephem + +LAT, LON = 72.2232, 15.6267 # svalbard +ON, OFF = 0, 1 +RELAY_PIN = 19 +SUN_ANGLE = -18 # astonomical twilight + +def main(): + + # GPIO Setup + GPIO.setmode(GPIO.BCM) + GPIO.setup(RELAY_PIN, GPIO.OUT) + + # ephem setup + observer = ephem.Observer() + observer.lat = str(LAT) + observer.lon = str(LON) + sun = ephem.Sun() + + while True: + observer.date = datetime.now() + sun.compute(observer) + + if sun.alt > SUN_ANGLE: + GPIO.output(RELAY_PIN, ON) + else: + GPIO.output(RELAY_PIN, OFF) + + time.sleep(60) # check every min + +if __name__ == '__main__': + main()