Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<User>/.virtualenvs/dencam/bin/python3 -u /home/<User>/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

Expand Down
43 changes: 43 additions & 0 deletions utilities/camera_relay.py
Original file line number Diff line number Diff line change
@@ -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()