User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Raspberry Pi - Sunrise/Sunset Timer for Christmas Lights

Wed Dec 05, 2012 6:42 am

Just in time for the holidays ..

A very simple Raspberry Pi project to control your (exterior) Christmas lights based on the calculated sunrise/sunset times for your location.
http://www.savagehomeautomation.com/pro ... ights.html

Image

Merry Christmas!
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

ski522
Posts: 394
Joined: Sun Sep 30, 2012 2:22 pm

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Wed Dec 05, 2012 11:48 am

Cool...On my NSLU2(http://www.nslu2-linux.org/), which has been hacked to run Linux, I do a similar thing using X10 modules, X10 firecracker and controlling the one/off times of those modules with a PHP program that determines sunset times. Been thinking about porting that over to my PI, but heard that there are some issues with USB/serial adapters on the PI.

User avatar
scruss
Posts: 2633
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Wed Dec 05, 2012 12:44 pm

Nice use of a PowerSwitch Tail there. Looks like the sort of thing that MisterHouse is built for, but if your Java solution works ...

I have Heyu + x10 working here, though I don't know for how long. I'll probably just dedicate a Pi to house control and apply the USB fullspeed hack to add some stability.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
Dweeber
Posts: 606
Joined: Fri Aug 17, 2012 3:35 am
Location: Mesa, AZ
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Wed Dec 05, 2012 1:16 pm

I actually thought of using a Raspberry Pi as a Synchronized Xmas light controller....

In a waterproof box....

o AC power in
o Raspberry Pi
o WiFi Adapter
o Controller board that controls a number of AC switches.
o Plug lights into the various switches
o Connect Audio out to FM Transmitter

Have the Raspberry Play music over the FM Transmitter and have a program time when to turn the AC switches on and off.

Using the WiFi adapter, use the Raspberry Pi as a hotspot so you could go outside and control it from say a tablet.
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com

User avatar
tedhale
Posts: 114
Joined: Thu Sep 20, 2012 4:52 pm
Location: Williamsburg, VA, USA
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Wed Dec 05, 2012 2:18 pm

I have a home automation/alarm system running now on the Pi.
It uses the serial port on the GPIO pins. I have had a lot of problems with USB to Serial adapters on normal Linux systems, so I didn't want to go that route on the Pi.
I haven't written up my blog post on the X10 interface yet, but you can see the other progress I have made on my blog.
http://raspberrypihobbyist.blogspot.com/
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Wed Dec 05, 2012 3:33 pm

tedhale wrote:I have a home automation/alarm system running now on the Pi.
It uses the serial port on the GPIO pins. I have had a lot of problems with USB to Serial adapters on normal Linux systems, so I didn't want to go that route on the Pi.
I haven't written up my blog post on the X10 interface yet, but you can see the other progress I have made on my blog.
http://raspberrypihobbyist.blogspot.com/
That's a pretty impressive undertaking :-)

I will definitely be interested in the X10 interface.
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

fatcinco
Posts: 26
Joined: Tue May 15, 2012 4:50 am
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Sat Dec 08, 2012 11:52 am

I did the same thing with open zwave. Love it the range.

http://thomasloughlin.com/z-wave-christmas-lights/

User avatar
scruss
Posts: 2633
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Mon Dec 10, 2012 12:42 pm

Incidentally, if anyone's looking for a simple sunset/sunrise/dawn/dusk timer, Sunwait is designed to run from cron. It's a very small C program.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

tomhannen
Posts: 17
Joined: Sun Jul 31, 2011 9:27 am

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Sun Jan 06, 2013 6:27 pm

Hi. I'm attempting to install Sunwait, as scruss mentioned - it sounds like the perfect complement to my raspberry-strogonanoff project. I'm pretty new to Linux, and I'm attempting to install it.
  • I've downloaded the .tar.gz file from here, and expanded it into a folder (containing 7 files) on my mac.
  • I've sftp'd the expanded files into a new folder called sunwait in the home folder of my pi.
When I try to make the files, it gives this error:

Code: Select all

[email protected] ~/sunwait $ sudo make
cc -g    -c -o main.o main.c
main.c: In function ‘main’:
main.c:155:14: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
main.c:162:11: warning: incompatible implicit declaration of built-in function ‘strstr’ [enabled by default]
cc -g    -c -o print.o print.c
cc -g    -c -o sunriset.o sunriset.c
cc -o sunwait -g  main.o print.o sunriset.o  -lm
[email protected] ~/sunwait $ sudo make install
make: *** No rule to make target `install'.  Stop.
Any ideas much appreciated...

carrotjuice
Posts: 1
Joined: Mon Jan 07, 2013 1:39 am

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Mon Jan 07, 2013 2:06 am

tomhannen wrote:Hi. I'm attempting to install Sunwait, as scruss mentioned - it sounds like the perfect complement to my raspberry-strogonanoff project. I'm pretty new to Linux, and I'm attempting to install it.
  • I've downloaded the .tar.gz file from here, and expanded it into a folder (containing 7 files) on my mac.
  • I've sftp'd the expanded files into a new folder called sunwait in the home folder of my pi.
When I try to make the files, it gives this error:

Code: Select all

[email protected] ~/sunwait $ sudo make
cc -g    -c -o main.o main.c
main.c: In function ‘main’:
main.c:155:14: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
main.c:162:11: warning: incompatible implicit declaration of built-in function ‘strstr’ [enabled by default]
cc -g    -c -o print.o print.c
cc -g    -c -o sunriset.o sunriset.c
cc -o sunwait -g  main.o print.o sunriset.o  -lm
[email protected] ~/sunwait $ sudo make install
make: *** No rule to make target `install'.  Stop.
Any ideas much appreciated...
The sunwait makefile is short and sweet--it only builds the project, and leaves you to install it yourself. You can either run it in place, or copy it to e.g. /usr/local/bin. Don't forget to make sure the binary has execute permissions as well. Lastly, if sunwait isn't in the $PATH and you're adding it to your crontab, be sure to use the full pathname or cron won't be able to find it. HTH.

User avatar
scruss
Posts: 2633
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Mon Jan 07, 2013 3:45 am

  • those compilation errors are okay. Best not to use 'sudo make'; I just call make on its own.
  • there is no install script or rule; all I did was 'sudo cp sunwait /usr/local/bin'.
The only disadvantage of calling sunwait from cron is that if your machine gets rebooted while sunwait is waiting, the job won't trigger.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
tedhale
Posts: 114
Joined: Thu Sep 20, 2012 4:52 pm
Location: Williamsburg, VA, USA
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Mon Jan 07, 2013 1:37 pm

I solved the sunrise/sunset problem in my home automation code by using CURL and pulling today's times for sunrise and sunset for my location from the US Naval Observatory site.
This is called every time the program starts and when a new day is detected (i.e. it passed midnight)

Here is the code I use.

Code: Select all

// Get today's sunrise and sunset times from USNO 

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <malloc.h>
#include <curl/curl.h>

// US Naval Observatory
char PostURL[] = "http://aa.usno.navy.mil/cgi-bin/aa_pap.pl";

/* put YYYY  MM  DD into this */
char PostDataFmt[] =   
"FFX=1&ID=AA&xxy=%04d&xxm=%02d&xxd=%02d&st=VA&place=Williamsburg&ZZZ=END";

struct MemoryStruct {
  char *memory;
  size_t size;
};

/****************************************************************************/
static size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)data;

  if (mem->memory)
	mem->memory = realloc(mem->memory, mem->size + realsize + 1);
  else
	mem->memory = malloc(realsize + 1);
  if (mem->memory) {
    memcpy(&(mem->memory[mem->size]), ptr, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
  }
  return realsize;
}

/****************************************************************************/

int GetSunRise(time_t *sunrise, time_t *sunset)
{
	CURL		*curl;
	CURLcode	res;
	char		postdata[100];
	time_t		now;
	struct tm	*today;
	struct tm	tmx;
	char		temp[20];
	char		*p;
	struct MemoryStruct chunk;

	chunk.memory=NULL; 
	chunk.size = 0;    

	// init cURL 
	curl = curl_easy_init();
	if(!curl) {
		Log("GetSunRise: curl_easy_init failed");
		return 1;
	}
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

	// who ya gonna call
	curl_easy_setopt(curl, CURLOPT_URL, PostURL);

	// fill in the post buffer with today's date
	time(&now);
	today=localtime(&now);
	snprintf(postdata,sizeof(postdata),PostDataFmt,
				today->tm_year+1900,today->tm_mon+1,today->tm_mday);
	postdata[sizeof(postdata)-1]=0;
	///Log("GetSunRise: posting %s",postdata);
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);

	// set the write callback
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

	// do the dirty work
	res = curl_easy_perform(curl);

	// cleanup 
	curl_easy_cleanup(curl);

	// dump what we got
	///Log("GetSunRise got back:\n%s",chunk.memory);

	// find sunrise
	p = strstr(chunk.memory,"Sunrise   ");
	if (p) {
		strncpy(temp,p+26,10);
		temp[10]=0;
		Log("SUNRISE IS %s",temp);
		// convert to time_t
		p=strchr(temp,':');
		*p=0;
		today->tm_hour=atoi(temp);
		today->tm_min=atoi(++p);
		today->tm_sec=0;
		*sunrise = mktime(today);
	}

	// find sunset
	p = strstr(chunk.memory,"Sunset    ");
	if (p) {
		strncpy(temp,p+26,10);
		temp[10]=0;
		Log("SUNSET IS %s",temp);
		// convert to time_t
		p=strchr(temp,':');
		*p=0;
		today->tm_hour=atoi(temp)+12;
		today->tm_min=atoi(++p);
		today->tm_sec=0;
		*sunset = mktime(today);
	}

	free(chunk.memory);
	return 0;
}
Of course, you have to modify the posted data for your location.
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com

tomhannen
Posts: 17
Joined: Sun Jul 31, 2011 9:27 am

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Wed Jan 09, 2013 11:01 pm

Thanks all - I didn't realise the sunwait executable had been created. When I saw the errors, I assumed it had crashed out, but sure enough, it's there and it works!

STPi
Posts: 1
Joined: Thu Apr 04, 2013 2:27 pm

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Thu Apr 04, 2013 2:32 pm

There is a very good standard Linux command "remind" that can trigger events based on sunrise/sunset times (once GEO location is provided) and many other fancy things:

http://linux.die.net/man/1/remind

Gadjetnut
Posts: 18
Joined: Fri Apr 05, 2013 9:13 pm

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Fri Apr 05, 2013 11:09 pm

If you are building an alarm system, please check out this website:

http://www.projects.privateeyepi.com/home

They have full instructions on how to create an alarm system with email alerts, web based dashboard to view and control you alarm. No fees.

http://www.privateeyepi.com/image/dashboard.jpg

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Wed May 11, 2016 11:49 pm

Hope it's not too late to re-activate this thread, but I am getting the following error trying to 'make' the Sunwait app.:
Folder Sunrise contains the extracted Sunwait tar


[email protected] ~/Projects/Sunrise $ sudo make
cc -g -c -o main.o main.c
main.c:4:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.
make: *** [main.o] Error 1

User avatar
scruss
Posts: 2633
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Thu May 12, 2016 2:06 am

Do you have build-essential installed?

Don't build things as root. You never know what can lurk in a makefile.

Here's the log of me downloading and building it without problems on a Raspberry Pi 3:

Code: Select all

[email protected]:~/Desktop $ wget http://www.risacher.org/sunwait/sunwait-20041208.tar.gz
--2016-05-11 21:47:01--  http://www.risacher.org/sunwait/sunwait-20041208.tar.gz
Resolving www.risacher.org (www.risacher.org)... 52.21.12.129
Connecting to www.risacher.org (www.risacher.org)|52.21.12.129|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 15865 (15K) [application/x-gzip]
Saving to: ‘sunwait-20041208.tar.gz’

sunwait-20041208.ta 100%[=====================>]  15.49K  --.-KB/s   in 0.07s  

2016-05-11 21:47:02 (232 KB/s) - ‘sunwait-20041208.tar.gz’ saved [15865/15865]

[email protected]:~/Desktop $ tar xvzf sunwait-20041208.tar.gz 
sunwait-20041208/
sunwait-20041208/TAGS
sunwait-20041208/Makefile
sunwait-20041208/main.c
sunwait-20041208/sunriset.c
sunwait-20041208/sunriset.h
sunwait-20041208/COPYING
sunwait-20041208/print.c
[email protected]:~/Desktop $ cd sunwait-20041208/
[email protected]:~/Desktop/sunwait-20041208 $ ls
COPYING  main.c  Makefile  print.c  sunriset.c  sunriset.h  TAGS
[email protected]:~/Desktop/sunwait-20041208 $ make
cc -g    -c -o main.o main.c
main.c: In function ‘main’:
main.c:155:14: warning: incompatible implicit declaration of built-in function ‘strlen’
  && (argv[i][strlen(argv[i])-1] == 'E' ||
              ^
main.c:162:11: warning: incompatible implicit declaration of built-in function ‘strstr’
       if (strstr(argv[i], options[j].label)) {
           ^
cc -g    -c -o print.o print.c
cc -g    -c -o sunriset.o sunriset.c
cc -o sunwait -g  main.o print.o sunriset.o  -lm
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Thu May 12, 2016 10:30 am

That worked perfectly - I must have had a problem with 'tar xvcf' (not setting correct permission)
I'm on my way :)

Again, TY for your help

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Thu May 12, 2016 10:50 am

OK - you can probably tell I am not a computer genius. I downloaded, tarred, but trying to execute gives not found error:

[email protected] ~/sunwait-20041208 $ make
cc -g -c -o main.o main.c
main.c: In function ‘main’:
main.c:155:14: warning: incompatible implicit declaration of built-in function ‘strlen’
&& (argv[strlen(argv)-1] == 'E' ||
main.c:162:11: warning: incompatible implicit declaration of built-in function ‘strstr’
if (strstr(argv, options[j].label)) {
cc -g -c -o print.o print.c
cc -g -c -o sunriset.o sunriset.c
cc -o sunwait -g main.o print.o sunriset.o -lm

[email protected] ~/sunwait-20041208 $ sunwait -p 43.021N,74.395W

bash: sunwait: command not found

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Thu May 12, 2016 11:02 am

Code: Select all

 ./sunwait -p 43.021N,74.395W
If that alone is not enough you might also need to mark the file executable with

Code: Select all

chmod +x sunwait


User avatar
scruss
Posts: 2633
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Thu May 12, 2016 12:07 pm

rfeyer wrote:bash: sunwait: command not found
Yeah, sunwait doesn't have an installation routine in its makefile. Here's the simplest one I can think of:
  1. mkdir ~/bin
  2. log out
  3. log back in again
  4. cd wherever you put sunwait
  5. cp sunwait ~/bin
  6. sunwait -p 43.021N 74.395W
which returns:

Code: Select all

Using location:             43.021000N, 74.395000W
Date:                       12 May 2016 
Local time:                  8:02 
Day length:                 14:39 hours
With civil twilight         15:41 hours
With nautical twilight      17:03 hours
With astronomical twilight  18:35 hours
Length of twilight:  civil   0:30 hours
                  nautical   1:11 hours
              astronomical   1:57 hours
Current specified time zone: EDT (-4 from UTC) 
Sun transits meridian 1253 EDT
                   Sun rises 0535 EDT, sets 2012 EDT
       Civil twilight starts 0503 EDT, ends 2044 EDT
    Nautical twilight starts 0422 EDT, ends 2125 EDT
Astronomical twilight starts 0335 EDT, ends 2211 EDT
If you need to run this from crontab, remember you'll need to call it as /home/pi/bin/sunwait
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Light

Thu May 12, 2016 3:51 pm

Love it!
This little RPi is amazing - so far have been able to automate our chicken coop doors/ water heaters etc.
Now working on sprinkler system and fans inside house.
TY all for the above info, I most likely will have more questions :)

Len-Tikular
Posts: 24
Joined: Tue Dec 24, 2013 5:54 pm

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Lights

Sun Dec 16, 2018 10:02 pm

Smashing project,

Now can somebody modify it for me,
I want to raise and lower a flag on a flagpole at sunrise and sunset, This has to be done automatically and most times unattended
We can use dc motor or a steppe to raise and lower the flag on a chord.
A stepper would be preferred because the length of the pole and the travel of the flag can be set by the number of steps made.
I would need some method of setting and displaying the time and date
I thought if a stepper is used there may be no need for limit switches.
I have a Pi-1 with a 2 line display and some stepper motors.

Can this be done ? or has it already been done.

Cheers all.

User avatar
scruss
Posts: 2633
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Raspberry Pi - Sunrise/Sunset Timer for Christmas Lights

Mon Dec 17, 2018 4:15 am

Yes, it can be done. Will we do it for you? Unlikely. We'll help you learn, but the process is all yours.

How big a flag? How tall a pole? Do you have mains power nearby?

You'll need limit switches for stepper motors, unless you can guarantee the flag mechanism will never slip.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Return to “Automation, sensing and robotics”