User avatar
richiepp
Posts: 129
Joined: Wed Dec 19, 2012 4:56 pm

Light switching @ Sunrise and Sunset

Sat Jun 11, 2016 2:07 pm

I'm developing an outdoor lighting system that turns lights on at sunset and truns them off at dawn.

PHP has a function that returns the time for sunrise and sunset based on the coordinates you set. So I'm running a cron job at 2am daily to write to a file with that days times. So my log file for today has 2 lines and looks like this

Code: Select all

05:58
21:05
So now that I have the variables I need to set my timers. One option would be to set up another cron that runs once a minute that compares the times and if they match turn the pin high or low.

Should I be concerned about the overhead of a cron once every minute? I know node.js has timers but can it handle 24 hour long cycles?

Looking for best practices.

Thanks
Rich

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Light switching @ Sunrise and Sunset

Sat Jun 11, 2016 5:40 pm

Use the sunwait program.

viewtopic.php?p=523912#p523912
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
SteveDee
Posts: 343
Joined: Thu Dec 29, 2011 2:18 pm
Location: Sunny Southern England
Contact: Website

Re: Light switching @ Sunrise and Sunset

Sun Jun 26, 2016 7:52 am

...or you could use a SINE function, as in my over-complicated auto-light controller: http://captainbodgit.blogspot.co.uk/201 ... oller.html

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Light switching @ Sunrise and Sunset

Sun Jun 26, 2016 10:25 am

SteveDee wrote:...or you could use a SINE function, as in my over-complicated auto-light controller: http://captainbodgit.blogspot.co.uk/201 ... oller.html
That only works for your latitude. (Longitude is just an offset for the value calculated for latitude.)

Take a look at http://www.risacher.org/sunwait/sunwait-20041208.tar.gz you'll be interested in the maths that's done in that program, which takes your "sin()" function and moves it to work anywhere (including a correction for the ecliptic inclination).

Sunwait also calculates four alternative curves for sunrise/sunset (sun at horizon), civil twilight (6° below the horizon), nautical twilight (-12°) and astronomical twilight (-18°).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
richiepp
Posts: 129
Joined: Wed Dec 19, 2012 4:56 pm

Re: Light switching @ Sunrise and Sunset

Sun Jun 26, 2016 2:19 pm

I continued building this out and it came to three files and a couple of cron jobs.

Each day at 2a.m. I run the following php from a cron job.

Code: Select all

<?php
$myfile = fopen("/usr/lib/cgi-bin/timers/srss_today.txt", "w") or die("Unable to open file!");

$txt = date_sunrise(time(),  SUNFUNCS_RET_STRING, 42.3536, -83.0083, 90, -4) . ' - Lights Off - $
fwrite($myfile, $txt);

$txt =  date_sunrise(time(),  SUNFUNCS_RET_STRING, 42.3536, -83.0083, 90, -4) . ' Sunrise, ' .da$
fwrite($myfile, $txt . "\n");

$txt = date_sunset(time(),  SUNFUNCS_RET_STRING, 42.3536, -83.0083, 90, -4) . ' - Lights On - ';
fwrite($myfile, $txt);
$txt = date_sunset(time(),  SUNFUNCS_RET_STRING, 42.3536, -83.0083, 90, -4) . ' Sunset, ' .date($
fwrite($myfile, $txt . "\n");

fclose($myfile);
?>
This writes to a file called srss_today.txt which looks like this

Code: Select all

6:01 - Lights Off - 06:01 Sunrise, Sun Jun 26 2016
21:09 - Lights On - 21:09 Sunset, Sun Jun 26 2016
I then run a cron every minute to see if it's sunrise or sunset

Code: Select all

<?php
$sunrise = file('/usr/lib/cgi-bin/timers/srss_today.txt')[0];
$sunrise = substr("$sunrise", 0, 5);

$sunset = file('/usr/lib/cgi-bin/timers/srss_today.txt')[1];
$sunset = substr("$sunset", 0, 5);

$now =  date("H:i", time());

if($sunrise === $now){
    echo ("Here comes the sun do de do dah");
    exec ('/usr/bin/python /usr/lib/cgi-bin/timers/switches/off.py');
}

elseif($sunset === $now){
    echo ("Bring on the night");
    exec ('/usr/bin/python /usr/lib/cgi-bin/timers/switches/on.py');
}
else{
    echo ("Nothing to report");
}
This all works as intended but I know it's not good to be running a cron every minute. It'd be cool to check it once an hour and then set a timer based on the minutes. So if sunrise is at 06:15 the script would see if it's 06 and set a timer for 15 minutes. How could I write my code to do that?
Thanks
Rich

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Light switching @ Sunrise and Sunset

Sun Jun 26, 2016 2:47 pm

If you switch to sunwait you end up with two cron jobs that each run once per day.

Code: Select all

3 3 * * * root /usr/local/bin/sunwait civ up +0:01:00 51.0N 1.1W ; /usr/local/bin/sunny.sh light;
3 14 * * * root /usr/local/bin/sunwait civ down -0:01:00 51.0N 1.1W ; /usr/local/bin/sunny.sh dark;

Code: Select all

#!/bin/bash

NOW=$(date '+%T %d/%m')
echo $1' @ '$NOW > /var/run/lightordark

if [ "$1" == "light" ]; then
  /usr/local/bin/gpio -g pwm 18 0
else
  /usr/local/bin/gpio -g pwm 18 1023
fi
that also sets GPIO 18 on when it gets dark and off when it's light.

In one of my C programs I read /var/run/lightordark and set the brightness on a MAX7219 driven 8*7-seg LED strip.

Code: Select all

   FILE *fp;
    int bright;
    char lightordark[40];

    char *LoDstringp = lightordark;
    char *LoDtoken;
    char *LoDtime;
    char charst[30];

    const char filename[21] = "/var/run/lightordark";
    const char *delim = " ";

     /* Set the display brightness at dawn / dusk */
     fp = fopen(filename,"r");
     fgets(lightordark, sizeof lightordark, fp);
     LoDtoken = strsep(&LoDstringp, delim);
     LoDtime = strsep(&LoDstringp, delim); /* skip @ */
     LoDtime = strsep(&LoDstringp, delim); /* got Time */
     sprintf(charst,"%8s",LoDtime);
     writeDigits(&header, charst); /* display on LED */
     sleep(4);

     if (strcmp(LoDtoken, "dark") == 0) bright = 7;
     else bright = 3;
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

drkmsmithjr
Posts: 7
Joined: Mon Aug 29, 2016 12:42 am
Location: Dana Point California
Contact: Website

Re: Light switching @ Sunrise and Sunset

Mon Aug 29, 2016 12:53 am

This project documents the way I accomplished the task using Python, the GPIO pins, and the ephem.py module.

https://github.com/drkmsmithjr/LConnect

I also set up a website to be allow remote monitoring and control via a web page.

Hope this helps.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Light switching @ Sunrise and Sunset

Mon Aug 29, 2016 2:54 pm

If you are building the lighting system why not use a solar powerd system, where a solar pannel charges a battery, and a buffer of some form (with a delta resistor series between to bring down the detected) can be put between the panel and RPi, when the charging voltage back of the diod drops off you switch on, when it comes back in you switch on.

Simple
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

Heater
Posts: 13933
Joined: Tue Jul 17, 2012 3:02 pm

Re: Light switching @ Sunrise and Sunset

Mon Aug 29, 2016 3:31 pm

Rich,
Should I be concerned about the overhead of a cron once every minute?
Given that whatever you run every minute probably only takes a few milliseconds to execute why worry about it?

I can't imagine you have anything else going on that is desperate not to lose that time.
I know node.js has timers but can it handle 24 hour long cycles?
Node timers work with 32 bit values. The timers have a one millisecond resolution. So that is timing up to about 49 days.
....Looking for best practices.
We all are :)
Memory in C++ is a leaky abstraction .

Return to “Other programming languages”