Switch webcam by time

Post Reply
User avatar
MrData
Observer
Observer
Posts: 35
Joined: Tue Oct 31, 2017 3:02 pm
Location: Southeast Wisconsin, USA
Station model: Davis Vantage Pro II
Software: Meteobridge
Contact:

Switch webcam by time

Post by MrData » Fri Feb 28, 2020 10:28 pm

I have 2 webcams. One is an outdoor cam and has IR lights, the other is an indoor cam looking out of a window. Both of them show pretty much the same view but I wanted to see if I could use a conditional statement to switch between the two based on time of day.
Found some PHP help on the Wide World of Webs and gave it a try. This seems to work nicely....

<?php
// webcam settings file
// Version: 6.0
// Created: 2020-02-27 00:12:49
date_default_timezone_set('America/Chicago');
$now = new Datetime("now");
$begintime = new DateTime('06:00');
$endtime = new DateTime('17:00');

if($now >= $begintime && $now <= $endtime){
$webcamURLs = 'http://foo.bar.net:3415/cam_2.cgi';
} else{
$webcamURLs = 'http://foo.bar.net:3415/cam_1.cgi';
}
$webcamTitles = "Backyard Webcam";
$webcamPositions = '';
$refreshInterval = '1';
$webcamBlockOpacity = false;
$mapPosition = 'BR';
?>

Switches to cam 2 during the day and cam 1 at night. Of course I'm going to have to manually adjust $begintime and $endtime as the year goes on.
Perhaps there is a way to make this conditional on sunrise and sunset times.
Gotta believe those values are already floating around in Meteotemplate somewhere, just have to figure out how to access them.
HEALTH WARNING: Caution...these posts may contain traces of nut.
Image
My Backyard Webcam temporarily offline
My Meteotemplate Weather Station temporarily offline

User avatar
Asobig
Advisor
Advisor
Posts: 72
Joined: Mon Aug 21, 2017 7:05 am
Location: Almere
Station model: Davis Vantage VP2+
Software: Meteobridge (Red)
Contact:

Re: Switch webcam by time

Post by Asobig » Sat Feb 29, 2020 3:41 pm

The webcam plugin has that routine.
It is in the file 'updateWebcam.php', perhaps you can modify that to your needs?

Code: Select all

if($recordDay==false){
					$updateWebcam = true; // interval passed, recording enabled during night as well
				}
				else{ // only record during the day
					$dateTimeZone = new DateTimeZone($stationTZ);
					$dateTime = new DateTime("now", $dateTimeZone);
					$my = $dateTimeZone->getOffset($dateTime);
					$offset = $my/3600;
					
					$sunRiseTodayHours = date_sunrise(time(),SUNFUNCS_RET_STRING,$stationLat,$stationLon,90.5,$offset);
					$sunSetTodayHours = date_sunset(time(),SUNFUNCS_RET_STRING,$stationLat,$stationLon,90.5,$offset);
					
					$sunRiseTimestamp = strtotime(date("Y")."-".date('m')."-".date('d')." ".$sunRiseTodayHours) - (60*60); // subtract one hour to start recording before sunrise
					$sunSetTimestamp = strtotime(date("Y")."-".date('m')."-".date('d')." ".$sunSetTodayHours) + (60*60); // add one hour to stop recording after sun set
					
					if(time()>=$sunRiseTimestamp && time()<=$sunSetTimestamp){
						$updateWebcam = true;
					}
					else{
						echo "Night time.<br>";
					}

User avatar
MrData
Observer
Observer
Posts: 35
Joined: Tue Oct 31, 2017 3:02 pm
Location: Southeast Wisconsin, USA
Station model: Davis Vantage Pro II
Software: Meteobridge
Contact:

Re: Switch webcam by time

Post by MrData » Sun Mar 01, 2020 2:22 am

Thanx, I'll look into that.
I'm going to try this for now.........

date_default_timezone_set('America/Chicago');
$now = date('H:i');
$sunrise = date_sunrise(time(), SUNFUNCS_RET_STRING, 43.1141, -88.5101, 90, -6);
$sunset = date_sunset(time(), SUNFUNCS_RET_STRING, 43.1141, -88.5101, 90, -6);

if($now >= $sunrise && $now <= $sunset){
$webcamURLs = 'http://foo.bar:3415/cam_2.cgi';
} else{
$webcamURLs = 'http://foo.bar:3415/cam_1.cgi';
}
HEALTH WARNING: Caution...these posts may contain traces of nut.
Image
My Backyard Webcam temporarily offline
My Meteotemplate Weather Station temporarily offline

Post Reply