Automatic turn on lights when the sun goes down (Tellstick)

A collection of tips on howto tweak your Bubba.
Filip
Posts: 30
Joined: 06 Feb 2007, 12:27
Location: Lund, Sweden

Automatic turn on lights when the sun goes down (Tellstick)

Post by Filip » 13 Sep 2008, 11:57

This post will describe how to make your lights in your home turn on when the sun goes down and then turn off at a specific time.

Step 1: Preparation

Needed hardware:
1 Tellstick from Telldus (http://www.telldus.se)
Click on "Återförsäljare" to see where you can buy it
1-100 Rf receiver to your lights (compatible models at: http://www.telldus.se/wiki/index.php?title=Mottagare)


You need ftdi_sio.
(description stolen from: http://forum.excito.net/viewtopic.php?t ... ht=ftdisio)

Log in to a shell in bubba. Become root.

Code: Select all

apt-get update
and then

Code: Select all

apt-get install linux-modules
Step 2: Install the Tellstick

Put the Tellstick in the Bubba USB
To install the Tellstick:

Code: Select all

modprobe ftdi_sio vendor=0x1781 product=0x0c30
And then

Code: Select all

update-modules
Find where the Tellstick is mounted

Code: Select all

ls -l /dev/ttyU*

Go to:
http://svn.telldus.se/svn/tellstick/
Go to: trunk/rfcmd
And download the files in an empty folder

Code: Select all

make
/* Start troubleshooting
If you can't run the make commando, I think it will be solved by running the following two commands:

Code: Select all

sudo apt-get install make
sudo apt-get install build-essentials
*/ End troubleshooting

Code: Select all

cp rfcmd /usr/local/bin



Now test with: (replace USB0 with your own number and of course "NEXA A 1" to reflect one of your devices)

Code: Select all

rfcmd /dev/ttyUSB0 NEXA A 1 1 
And turn it off with

Code: Select all

rfcmd /dev/ttyUSB0 NEXA A 1 0
If you are having ownership problems with the tellstick, run the following command (and change to your device number):

Code: Select all

chmod 666 /dev/ttyUSB0

If you now are able to turn on and off your lights, proceed to next step.
If not, then reply to this thread or go to www.telldus.se/forum/ for support.


Step 3: Make the lights turn on/off automatically

Copy and save the source code of sunset.php (included in this post) in /home/web/tellstick and make all necessary settings in the file


Use sun_class.php available from:
http://phpclasses.mkdata.net/browse/file/11404.html
And of course included in this post.
Save it to the same directory as sunset.php


Use

Code: Select all

crontab -e
Add the following line to the crontab file: (will run the file every 10 minute)

Code: Select all

*/10 * * * * /usr/bin/php /home/web/tellstick/sunset.php >/dev/null 2>&1
Save the file and exit the editor.

Finished!


Please give me feedback on the instructions!



Source code

sunset.php

Code: Select all

<?php

$user = exec('id -nu');
#print $user;
                                                                            

require_once('sun_class.php');

################################################################
# Begin Settings


# To get your lat/long, go to Google Maps, choose your position and write the following in the URL field in your web browser:
# javascript:void(prompt('',gApplication.getMap().getCenter()));

$lat = 55.000000;
$long = 13.000000;
$timezone = 1;

# During how long interval shall we try to turn on or off?
# Must be longer than the intervall between the program is run with crontab 
$interval = 11;  // in minutes

# Do not choose a turn off time just before midnight! (the lights will then not turn off)
$turnoffh = "00"; // The hour to turn off the lights 00-23
$turnoffm = "15"; // The minute to turn off the lights 00-59

# Settings Telldus
$dev = "/dev/ttyUSB0";

# Where rfcmd is found
$rfcmd = "/home/filip/rfcmd/rfcmd";

# Units
$enheter;
$enheter["Sovrummet"] = "NEXA C 3";
$enheter["Trädgårdsfönstret"] = "NEXA C 2";
$enheter["Balkongen"] = "NEXA C 1";

# End Settings
################################################################


# Creates a new sun
$sun = new sun($lat, $long, $timezone);

# The unix time of when to turn off the lights
$off = mktime($turnoffh, $turnoffm, 0, date('m'), date('d'), date('Y'));
# The unix time of when to turn on the lights
$on = $sun->sunset();
# The current unix time
$now = time();
#print date('H:i',$on);
$onstop = $on+$interval*60;
$offstop = $off+$interval*60;

if($now>$on && $now<$onstop) {
    print "Turning on!<BR>"; 
    lightswitch($dev, $rfcmd, $enheter, 1);
    
}
else if($now>$off && $now<$offstop) {
    print "Turning off!<BR>";
    lightswitch($dev, $rfcmd, $enheter, 0);
    
}

function lightswitch($dev, $rfcmd, $enheter, $switch) {
    foreach( $enheter as $key => $value){
        $splita = explode(" ", $value);
        $modell = $splita[0]; // Modell
        $kod = $splita[1]; // Huskod
        $id = $splita[2]; // Huskod id
        $cmd = $rfcmd." ".$dev." ".$modell." ".$kod." ".$id." ".$switch;
        $returncmd .= $cmd." ";           
        exec($cmd);
        
    }
    return $returncmd;
}
  
             
?> 



sun_class.php

Code: Select all


<?php
/***************************************************************************
*                              sun_class.php
*                       -------------------
*   last modification    : 27.10.2005
*   copyright            : (C) 2005 radzio
*   email                : radziupiekarz@poczta.fm
*
*
*
***************************************************************************/

/***************************************************************************
* This class calculates sunrise and sunset.
* This script includes is_daylight_time() function from Steve Edberg
* and perl code translated from the perl module Astro-SunTime-0.01.
*
*
*
*
*
*
***************************************************************************/

class sun
{
    var $latitude;     #szerokosc geograficzna
    var $longitude;    #dlugosc geograficzna
    var $timezone;     #strefa czasowa
        function sun ($latitude, $longitude, $timezone)
            {
                $this->latitude = $latitude;
                $this->longitude = $longitude;
                $this->timezone = $timezone;
                $this->yday = date("z");
                $this->mon = date("n");
                $this->mday = date("j");
                $this->year = date("Y");
                #---------------------
                $this->DST=$this->is_daylight_time(date("U"));
                    if ($this->DST)
                       {
                          $this->timezone = ($this->timezone + 1);
                       }
                    if ($this->timezone == "13")
                       {
                          $this->timezone = "-11";
                       }
                #---------------------
                $this->A = 1.5708;
                $this->B = 3.14159;
                $this->C = 4.71239;
                $this->D = 6.28319;
                $this->E = 0.0174533 * $this->latitude;
                $this->F = 0.0174533 * $this->longitude;
                $this->G = 0.261799  * $this->timezone;
                #---------------------
                  # For astronomical twilight, use
                  #$this->R = -.309017;
                  # For     nautical twilight, use
                  #$this->R = -.207912;
                  # For        civil twilight, use
                  #$this->R = -.104528;
                  # For     sunrise or sunset, use
                  $this->R = -.0145439;
                  
                  # For something between sunset and twilight
                  #$this->R = -0.0595359;
                #---------------------
                
            }
        function is_daylight_time($time)
            {
               list($dom, $dow, $month, $hour, $min) = explode(":", date("d:w:m:H:i", $time));
               if   ($month > 4 && $month < 10)
                  {
                         $this->retval = 1;        # May thru September
                  }
               elseif ($month == 4 && $dom > 7)
                  {
                         $this->retval = 1;        # After first week in April
                  }
               elseif ($month == 4 && $dom <= 7 && $dow == 0 && $hour >= 2)
                  {
                         $this->retval = 1;        # After 2am on first Sunday ($dow=0) in April
                  }
               elseif ($month == 4 && $dom <= 7 && $dow != 0 && ($dom-$dow > 0))
                  {
                         $this->retval = 1;        # After Sunday of first week in April
                  }
               elseif ($month == 10 && $dom < 25)
                  {
                         $this->retval = 1;        # Before last week of October
                  }
               elseif ($month == 10 && $dom >= 25 && $dow == 0 && $hour < 2)
                  {
                         $this->retval = 1;        # Before 2am on last Sunday in October
                  }
               elseif ($month == 10 && $dom >= 25 && $dow != 0 && ($dom-24-$dow < 1) )
                  {
                         $this->retval = 1;        # Before Sunday of last week in October
                  }
               else
                  {
                         $this->retval = 0;
                  }

                  

                return $this->retval;
        }
    function sunrise()
        {
            $J =  $this->A;
            $K = $this->yday + (($J - $this->F) / $this->D);
            $L = ($K * .017202) - .0574039;              # Solar Mean Anomoly
            $M = $L + .0334405 * sin($L);                # Solar True Longitude
            $M += 4.93289 + (3.49066E-04) * sin(2 * $L);
                if ($this->D == 0)
                  {
                     echo "Trying to normalize with zero offset..."; exit;
                  }
                while ($M < 0)
                  {
                     $M = ($M + $this->D);
                  }
                while ($M >= $this->D)
                  {
                     $M = ($M - $this->D);
                  }
                if (($M / $this->A) - intval($M / $this->A) == 0)
                  {
                     $M += 4.84814E-06;
                  }
            $P = sin($M) / cos($M);                   # Solar Right Ascension
            $P = atan2(.91746 * $P, 1);
            # Quadrant Adjustment
                if ($M > $this->C)
                  {
                     $P += $this->D;
                  }
                else
                  {
                     if ($M > $this->A)
                        {
                           $P += $this->B;
                        }
                  }

            $Q = .39782 * sin($M);            # Solar Declination
            $Q = $Q / sqrt(-$Q * $Q + 1);     # This is how the original author wrote it!
            $Q = atan2($Q, 1);
            $S = $this->R - (sin($Q) * sin($this->E));
            $S = $S / (cos($Q) * cos($this->E));
                if (abs($S) > 1)
                    {
                        echo 'none';
                    }     # Null phenomenon
            $S = $S / sqrt(-$S * $S + 1);
            $S = $this->A - atan2($S, 1);
            $S = $this->D - $S ;
            $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time
            $U = $T - $this->F;                            # Universal timer
            $V = $U + $this->G;                            # Wall clock time
            # Quadrant Determination
                if ($this->D == 0)
                    {
                        echo "Trying to normalize with zero offset..."; exit;
                    }
                while ($V < 0)
                    {
                        $V = ($V + $this->D);
                    }
                while ($V >= $this->D)
                    {
                        $V = ($V - $this->D);
                    }
            $V = $V * 3.81972;
            $hour = intval($V);
            $min  = intval((($V - $hour) * 60) + 0.5);
            return mktime($hour,$min,0,$this->mon,$this->mday,$this->year);
            #echo date( "G:i ", mktime($hour,$min,0,$this->mon,$this->mday,$this->year) );

        }
    function sunset()
        {
            $J =  $this->C;
            $K = $this->yday + (($J - $this->F) / $this->D);
            $L = ($K * .017202) - .0574039;              # Solar Mean Anomoly
            $M = $L + .0334405 * sin($L);                # Solar True Longitude
            $M += 4.93289 + (3.49066E-04) * sin(2 * $L);
                if ($this->D == 0)
                  {
                     echo "Trying to normalize with zero offset..."; exit;
                  }
                while ($M < 0)
                  {
                     $M = ($M + $this->D);
                  }
                while ($M >= $this->D)
                  {
                     $M = ($M - $this->D);
                  }
                if (($M / $this->A) - intval($M / $this->A) == 0)
                  {
                     $M += 4.84814E-06;
                  }
            $P = sin($M) / cos($M);                   # Solar Right Ascension
            $P = atan2(.91746 * $P, 1);
            # Quadrant Adjustment
                if ($M > $this->C)
                  {
                     $P += $this->D;
                  }
                else
                  {
                     if ($M > $this->A)
                        {
                           $P += $this->B;
                        }
                  }

            $Q = .39782 * sin($M);            # Solar Declination
            $Q = $Q / sqrt(-$Q * $Q + 1);     # This is how the original author wrote it!
            $Q = atan2($Q, 1);
            $S = $this->R - (sin($Q) * sin($this->E));
            $S = $S / (cos($Q) * cos($this->E));
                if (abs($S) > 1)
                    {
                        echo 'none';
                    }     # Null phenomenon
            $S = $S / sqrt(-$S * $S + 1);
            $S = $this->A - atan2($S, 1);
            #$S = $this->D - $S ;
            $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time
            $U = $T - $this->F;                            # Universal timer
            $V = $U + $this->G;                            # Wall clock time
            # Quadrant Determination
                if ($this->D == 0)
                    {
                        echo "Trying to normalize with zero offset..."; exit;
                    }
                while ($V < 0)
                    {
                        $V = ($V + $this->D);
                    }
                while ($V >= $this->D)
                    {
                        $V = ($V - $this->D);
                    }
            $V = $V * 3.81972;
            $hour = intval($V);
            $min  = intval((($V - $hour) * 60) + 0.5);
            return mktime($hour,$min,0,$this->mon,$this->mday,$this->year);
            #echo date( "G:i ", mktime($hour,$min,0,$this->mon,$this->mday,$this->year) );
        }

}
?> 


edit: Changed the Crontab information
edit 2: Found a small error in sunset.php
Last edited by Filip on 14 Sep 2008, 14:14, edited 3 times in total.

Cheeseboy
Posts: 789
Joined: 08 Apr 2007, 12:16

Post by Cheeseboy » 14 Sep 2008, 05:49

Thanks!

Now I have to buy even more hardware... :-)

Filip
Posts: 30
Joined: 06 Feb 2007, 12:27
Location: Lund, Sweden

Post by Filip » 09 Oct 2008, 17:07

I have a small addition to the instructions.
If the bubba is rebooted, it will not work. Therefor you will have to write the following commands to make it work again after a reboot:

Code: Select all

modprobe -r ftdi_sio
modprobe ftdi_sio vendor=0x1781 product=0x0c30
update-modules
chmod 666 /dev/ttyUSB0
[/code]

adegert
Posts: 4
Joined: 05 May 2009, 08:56

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by adegert » 06 Jun 2009, 20:13

The URL for the rfcmd sources did not work, I got them from

Code: Select all

http://svn.telldus.se/
(Subversion Repository TellStick, trunc, telldus-core, tarball of rfcmd)

The command for the installation of the build environment should be

Code: Select all

apt-get install build-essential libftdi-dev

hawkman
Posts: 16
Joined: 23 Apr 2007, 06:58

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by hawkman » 01 Jul 2009, 15:22

Hello,

I am trying to build the rfcmd. When running make, I get the error:

Code: Select all

gcc -O2	-Wall -I/usr/local/include -o find_telldus find_telldus.c -L/usr/local/lib -R/usr/local/lib -lftdi -lusb
gcc: unrecognized option `-R/usr/local/lib'
find_telldus.c: In function `main':
find_telldus.c:20: warning: implicit declaration of function `ftdi_usb_find_all'
find_telldus.c:21: warning: implicit declaration of function `ftdi_get_error_string'
find_telldus.c:21: warning: format argument is not a pointer (arg 4)
find_telldus.c:30: warning: implicit declaration of function `ftdi_usb_get_strings'
find_telldus.c:30: error: dereferencing pointer to incomplete type
find_telldus.c:31: warning: format argument is not a pointer (arg 4)
find_telldus.c:35: error: dereferencing pointer to incomplete type
find_telldus.c:38: warning: implicit declaration of function `ftdi_list_free'
make: *** [find_telldus] Error 1
Used the source code from http://svn.telldus.se/svn/tellstick/tru ... ore/rfcmd/

Anyone could point me in right direction? :D

/H

carl
Posts: 474
Joined: 07 May 2008, 04:41

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by carl » 02 Jul 2009, 07:34

hawkman wrote:Hello,

I am trying to build the rfcmd. When running make, I get the error:

Code: Select all

gcc -O2	-Wall -I/usr/local/include -o find_telldus find_telldus.c -L/usr/local/lib -R/usr/local/lib -lftdi -lusb
gcc: unrecognized option `-R/usr/local/lib'
find_telldus.c: In function `main':
find_telldus.c:20: warning: implicit declaration of function `ftdi_usb_find_all'
find_telldus.c:21: warning: implicit declaration of function `ftdi_get_error_string'
find_telldus.c:21: warning: format argument is not a pointer (arg 4)
find_telldus.c:30: warning: implicit declaration of function `ftdi_usb_get_strings'
find_telldus.c:30: error: dereferencing pointer to incomplete type
find_telldus.c:31: warning: format argument is not a pointer (arg 4)
find_telldus.c:35: error: dereferencing pointer to incomplete type
find_telldus.c:38: warning: implicit declaration of function `ftdi_list_free'
make: *** [find_telldus] Error 1
Used the source code from http://svn.telldus.se/svn/tellstick/tru ... ore/rfcmd/

Anyone could point me in right direction? :D

/H
Should probably be "`-L/usr/local/lib" instead
/Carl
/Carl Fürstenberg, Excito Software Developer
http://www.excito.com
support@excito.com

hawkman
Posts: 16
Joined: 23 Apr 2007, 06:58

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by hawkman » 02 Jul 2009, 08:43

Hi,

Thanks for your reply. I tried to change that in the Makefile, but still got an error:

Code: Select all

gcc -O2 -Wall -I/usr/local/include -o find_telldus find_telldus.c -L/usr/local/lib -lftdi -lusb
find_telldus.c: In function `main':
find_telldus.c:20: warning: implicit declaration of function `ftdi_usb_find_all'
find_telldus.c:21: warning: implicit declaration of function `ftdi_get_error_string'
find_telldus.c:21: warning: format argument is not a pointer (arg 4)
find_telldus.c:30: warning: implicit declaration of function `ftdi_usb_get_strings'
find_telldus.c:30: error: dereferencing pointer to incomplete type
find_telldus.c:31: warning: format argument is not a pointer (arg 4)
find_telldus.c:35: error: dereferencing pointer to incomplete type
find_telldus.c:38: warning: implicit declaration of function `ftdi_list_free'
I have updated the linux-modules, and when I do

Code: Select all

modprobe ftdi_sio vendor=0x1781 product=0x0c30
I just get back to the prompt (which I guess is ok).

When I run update-modules afterwards, I gett

Code: Select all

Architecture-specific modutils configuration not found, using defaults
. Do I need to change any other configuration regarding this?

Suggestions are much appreciated :D

/H
Last edited by hawkman on 02 Jul 2009, 08:46, edited 1 time in total.

adegert
Posts: 4
Joined: 05 May 2009, 08:56

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by adegert » 02 Jul 2009, 08:44

hawkman wrote:Hello,

I am trying to build the rfcmd. When running make, I get the error:

Code: Select all

gcc -O2	-Wall -I/usr/local/include -o find_telldus find_telldus.c -L/usr/local/lib -R/usr/local/lib -lftdi -lusb
gcc: unrecognized option `-R/usr/local/lib'
find_telldus.c: In function `main':
find_telldus.c:20: warning: implicit declaration of function `ftdi_usb_find_all'
find_telldus.c:21: warning: implicit declaration of function `ftdi_get_error_string'
find_telldus.c:21: warning: format argument is not a pointer (arg 4)
find_telldus.c:30: warning: implicit declaration of function `ftdi_usb_get_strings'
find_telldus.c:30: error: dereferencing pointer to incomplete type
find_telldus.c:31: warning: format argument is not a pointer (arg 4)
find_telldus.c:35: error: dereferencing pointer to incomplete type
find_telldus.c:38: warning: implicit declaration of function `ftdi_list_free'
make: *** [find_telldus] Error 1
Used the source code from http://svn.telldus.se/svn/tellstick/tru ... ore/rfcmd/

Anyone could point me in right direction? :D

/H
You need a newer libftdi for that but you should be ok as long as rfcmd was built successfully (find_telldus only tries to detect the stick, normally you don't need that).

hawkman
Posts: 16
Joined: 23 Apr 2007, 06:58

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by hawkman » 02 Jul 2009, 09:27

Hi,
Thanks for your reply!
rfcmd is not building. The make is stopped due to errors as stated above. So I should try to update libftdi?
How do I do that? I have tried to apt-get install libftdi-dev but there does not seem to be any newer.

/H

hawkman
Posts: 16
Joined: 23 Apr 2007, 06:58

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by hawkman » 06 Jul 2009, 14:42

No other things I could try? :(
I am really stuck here...
Perhaps someone can provide the rfcmd binary compiled for bubba? :P

BR
H

hawkman
Posts: 16
Joined: 23 Apr 2007, 06:58

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by hawkman » 06 Jul 2009, 14:58

Hmm. All of a sudden it is working :D

tor
Posts: 703
Joined: 06 Dec 2006, 12:24
Contact:

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by tor » 28 Oct 2009, 03:39

Hi all,

Cool howto, nice work Filip. I have a short question, does anyone of you know of a device such as the Tellstick but with a receiver as well? Reading up on this the Tellstick only has the sender part. I would be interested in for example to read out data from weather station sensors etc.

/Tor
Co-founder OpenProducts and Ex Excito Developer

6feet5
Posts: 269
Joined: 13 Apr 2007, 17:32
Location: Gnesta, Sweden
Contact:

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by 6feet5 » 28 Oct 2009, 14:10

Hi Tor,

I've been searching for a receiver as well. The closest I got was a description on http://wiki.nethome.nu/doku.php/modupm. This one uses the base unit of a wireless thermometer as the receiver. I bought one from ClasOhlson, but haven't had time to try it yet. I'm thinking of building a new USB device and hook it up to one of these. Perhaps not the most beautiful solution, but if that is what it takes... :-)

I actually had a receiver in the NEXA transmitter I built, but the signal was far too noisy, so I never managed to figure out how to decode it.

/Johan

6feet5
Posts: 269
Joined: 13 Apr 2007, 17:32
Location: Gnesta, Sweden
Contact:

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by 6feet5 » 29 Oct 2009, 16:33

I just remembered I actually did find a couple of receivers while searching for antennas, but they were a bit too expensive for me. Unfortunately it seems I forgot to bookmark the page (I think it was a german company), but I did find this http://www.cheapertronics.com/product_d ... item_id=25. The devices look a lot like the ones the german company had. Price is about the same as well.

/Johan

tor
Posts: 703
Joined: 06 Dec 2006, 12:24
Contact:

Re: Automatic turn on lights when the sun goes down (Tellstick)

Post by tor » 03 Nov 2009, 04:08

Hi 6feet5,

Thx for the reply. I think i have seen the device that you linked to unfortunately that one only has the receiver part :(

/Tor
Co-founder OpenProducts and Ex Excito Developer

Post Reply