From 4109a16c1e303d16b814717515c4ca958561cf15 Mon Sep 17 00:00:00 2001 From: Sumega Mandadi Date: Tue, 28 Jan 2025 10:02:23 -0800 Subject: [PATCH 1/2] add file to trigger camera relay based on sun position this script calculates sun altitude every minute to determine if it is daytime or nighttime to turn the camera on/off. Currently is set to have camera on during daytime and astronomical twilight. Frequency of checks and sun altitude threshold can be adjusted. --- utilities/camera_relay.py | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 utilities/camera_relay.py 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() From 757c02397011fd7d1d52c11a04e07c7c5cb52b4b Mon Sep 17 00:00:00 2001 From: sumi1194 Date: Tue, 28 Jan 2025 13:43:15 -0800 Subject: [PATCH 2/2] Update README.md add section with instructions on how to set up systemd service to turn camera on/off based on time of day for cyclops --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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