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

Merry Christmas!
That's a pretty impressive undertakingtedhale 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/
Code: Select all
pi@raspberrypi ~/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
pi@raspberrypi ~/sunwait $ sudo make install
make: *** No rule to make target `install'. Stop.
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.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.
When I try to make the files, it gives this error:
- 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.
Any ideas much appreciated...Code: Select all
pi@raspberrypi ~/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 pi@raspberrypi ~/sunwait $ sudo make install make: *** No rule to make target `install'. Stop.
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;
}
Code: Select all
pi@thirtyone:~/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]
pi@thirtyone:~/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
pi@thirtyone:~/Desktop $ cd sunwait-20041208/
pi@thirtyone:~/Desktop/sunwait-20041208 $ ls
COPYING main.c Makefile print.c sunriset.c sunriset.h TAGS
pi@thirtyone:~/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
Code: Select all
./sunwait -p 43.021N,74.395W
Code: Select all
chmod +x sunwait
Yeah, sunwait doesn't have an installation routine in its makefile. Here's the simplest one I can think of:rfeyer wrote:bash: sunwait: command not found
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