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
Code: Select all
apt-get install linux-modules
Put the Tellstick in the Bubba USB
To install the Tellstick:
Code: Select all
modprobe ftdi_sio vendor=0x1781 product=0x0c30
Code: Select all
update-modules
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
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
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
Code: Select all
rfcmd /dev/ttyUSB0 NEXA A 1 0
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
Code: Select all
*/10 * * * * /usr/bin/php /home/web/tellstick/sunset.php >/dev/null 2>&1
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