I recently got around to compiling the Sunwait package for my router hardware. Bit of a hassle as you have to download the OpenWRT SDK of 150MB to compile an 8kb package file.
Also, I generated scripts that work to flip the cams to day or night profiles depending on the time of day. Included a startup script that's run after the router boots up. It checks the time against the sunrise/sunset times and runs the appropriate script.
This is the script for daytime. Change the password to your cameras' password and change the IPs to your cameras' IPs.
To switch to night, change the "Config[0]=0" to "Config[0]=1". Oh, and change the log message accordingly.
In my crontab (under System -> Scheduled Tasks):
Add this under System -> Startup -> Local Startup so the startup script is run after the router boots:
The script for router restarts:
I have the script sleep for 5 minutes (300 seconds) to allow the router to connect to the 4G network in case it's slow in connecting. This guarantees that the router has the correct time from the NTP server(s) and so that the script can run properly with the current time and makes sure all the IP cams have booted up.
It's quick and dirty. I haven't coded in years really and I really struggle with the syntax. The concepts are easy but the bloody syntax is making me nuts.
Other than banging my head against the wall with the syntax, this is fairly simple. The router this is being run on is being used as a bridge between the LAN and IP cams which are, of course, on different subnets. This router is also powered via POE with a splitter. So if the cams/NVR have power, the router/bridge, has power. Everything should be set correctly even after a power-off situation. The cams/NVR/bridge are run off a UPS powered by a 12v 150ah battery so save for cases of long power-cuts, the system doesn't power off.
Any suggestions/constructive criticisms are welcome.
Also, I generated scripts that work to flip the cams to day or night profiles depending on the time of day. Included a startup script that's run after the router boots up. It checks the time against the sunrise/sunset times and runs the appropriate script.
This is the script for daytime. Change the password to your cameras' password and change the IPs to your cameras' IPs.
Code:
#!/bin/sh
curl -v -s -g --digest -u admin:admin "http://x.x.x.11/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.12/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.13/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.15/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.16/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.17/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.18/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.19/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.20/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.21/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.12/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.23/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.23/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[1].Config[0]=0" &&
curl -v -s -g --digest -u admin:admin "http://x.x.x.24/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" &&
logger "IP cams changed to day mode" # log action in system log for debugging purposes
To switch to night, change the "Config[0]=0" to "Config[0]=1". Oh, and change the log message accordingly.
In my crontab (under System -> Scheduled Tasks):
Code:
45 3 * * * reboot # router reboots at 3:45am every day
0 04 * * * sunwait civ up 25 23.11N 82.37W; sh sunwait/day_openwrt # have sunwait wait until 25 minutes after civil sunrise
0 16 * * * sunwait civ down -30 23.11N 82.37W; sh sunwait/night_openwrt # have sunwai wait until 30 minutes before civil sunset
Add this under System -> Startup -> Local Startup so the startup script is run after the router boots:
Code:
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
ash sunwait/startup_openwrt # run startup script for IP cam profile change
exit 0
The script for router restarts:
Code:
#!/bin/sh
sleep 300 # sleep for 5 minutes to allow router to get correct time from NTP server(s)
now="$(date +"%H%M")" # return system time in HHMM format
sunrise="$(sunwait -p 23.11N 82.37W | awk 'FNR == 14 {print $4}')" # return civil sunrise (14th line, 4th column)
sunset="$(sunwait -p 23.11N 82.37W | awk 'FNR == 14 {print $7}')" # return civil sunset (14th line, 7th column)
if [ $now -lt $sunrise ]; then # if pre-sunrise run script to set cameras to night mode
ash sunwait/night_openwrt
logger "IP cams changed to night mode (pre-daylight)"; # log script run (night)
elif [ $now -ge $sunset ]; then #if sunset or post-sunset run script to set cameras to night mode
ash sunwait/night_openwrt;
logger "IP cams changed to night mode (post-daylight)"; # log script run (night)
else
ash sunwait/day_openwrt;
logger "IP cams changed to day mode (daylight)"; # log script run (day)
fi
I have the script sleep for 5 minutes (300 seconds) to allow the router to connect to the 4G network in case it's slow in connecting. This guarantees that the router has the correct time from the NTP server(s) and so that the script can run properly with the current time and makes sure all the IP cams have booted up.
It's quick and dirty. I haven't coded in years really and I really struggle with the syntax. The concepts are easy but the bloody syntax is making me nuts.
Other than banging my head against the wall with the syntax, this is fairly simple. The router this is being run on is being used as a bridge between the LAN and IP cams which are, of course, on different subnets. This router is also powered via POE with a splitter. So if the cams/NVR have power, the router/bridge, has power. Everything should be set correctly even after a power-off situation. The cams/NVR/bridge are run off a UPS powered by a 12v 150ah battery so save for cases of long power-cuts, the system doesn't power off.
Any suggestions/constructive criticisms are welcome.