torsdag 21 april 2016

Save temperature from 1-Wire to OpenNetHome

sudo mkdir /usr/local/share/opennethome/


sudo nano /usr/local/share/opennethome/Read1-WireTemperature.pl
------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;

if(@ARGV < 1){
        print "Usage: [DeviceId] [optional:quiet]\n";
}else{
        my $quiet = 0;

        if(@ARGV > 1 && lc($ARGV[1]) eq "quiet"){
                $quiet = 1;
        }

        my $device = $ARGV[0];

        if($quiet == 0){
                print "Reading device: '" . $device . "'\n";
        }

        #my $sensordata = `cat /sys/bus/w1/devices/$device/w1_slave 2>&1`;
        my $sensordata = `less -FX /mnt/1wire/$device/temperature`;
        if($quiet == 0){
                print "Device read, temperature: '$sensordata'\n";
        }else{
                print $sensordata;
        }
}

------------------------------------------------------------
sudo chmod +x /usr/local/share/opennethome/Read1-WireTemperature.pl

sudo nano /usr/local/share/opennethome/ReadCPUTemp.pl
------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;

my $raw = `/opt/vc/bin/vcgencmd measure_temp`;

$raw =~ s/^\s+|\s+$//g;

#print "Raw: '" . $raw . "'\n";

my @result = split("=", $raw);
my @value = split("'", $result[1]);
my $final = $value[0];

print $final;


------------------------------------------------------------
sudo chmod +x /usr/local/share/opennethome/ReadCPUTemp.pl

#Test:
/usr/local/share/opennethome/ReadCPUTemp.pl

sudo nano /usr/local/share/opennethome/SendUPMThermometerToOpennethome.pl
------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;

if(@ARGV < 3){
        print "Usage: [HouseCode] [DeviceCode] [Temperature] [optional:quiet]\n";
}else{
        my $quiet = 0;

        if(@ARGV > 3 && lc($ARGV[3]) eq "quiet"){
                $quiet = 1;
        }

        my $houseCode = $ARGV[0];
        my $deviceCode = $ARGV[1];
        my $temperature = $ARGV[2];
        my $k = 0.0625;
        my $m = -50;

        if($quiet == 0){
                print "Subtract with m: " . $temperature . " - " . $m ." = " . ( $temperature - $m ) . "\n";
        }
        $temperature = $temperature - $m;

        if($quiet == 0){
                print "Divide by k: " .  $temperature . " / " . $k . " = " . ( $temperature / $k ) . "\n";
        }
        $temperature = $temperature / $k;

        my $sock = IO::Socket::INET->new(
                Proto    => 'udp',
                PeerPort => 8005,
                PeerAddr => '127.0.0.1',
        ) or die "Could not create socket: $!\n";

        my $message = "event,UPM_Message,Direction,In,UPM.HouseCode,$houseCode,UPM.DeviceCode,$deviceCode,UPM.Primary,$temperature";

        if($quiet == 0){
                print "Send to server:\n";
                print $message . "\n";
        }
        print $sock $message;
}

------------------------------------------------------------
sudo chmod +x /usr/local/share/opennethome/SendUPMThermometerToOpennethome.pl

sudo nano /usr/local/share/opennethome/SaveSensorValues.pl
------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;

my $temp;
my $device;
my $houseCode;
my $deviceCode;

#CPU Temperature
$houseCode = 2;
$deviceCode = 1;
$temp = `/usr/local/share/opennethome/ReadCPUTemp.pl`;
#print "CPU Temperature: '" . $cputemp . "'\n";
`/usr/local/share/opennethome/SendUPMThermometerToOpennethome.pl $houseCode $deviceCode $temp quiet`;

$device = "28.746FE0020000";
$houseCode = 1;
$deviceCode = 1;
$temp = `/usr/local/share/opennethome/Read1-WireTemperature.pl $device quiet`;
#print "One Wire Temperature: '" . $temp . "'\n";
`/usr/local/share/opennethome/SendUPMThermometerToOpennethome.pl $houseCode $deviceCode $temp quiet`;

------------------------------------------------------------

sudo chmod +x /usr/local/share/opennethome/SaveSensorValues.pl

crontab -e
# m h  dom mon dow   command
*/5 *  *   *   *     /usr/local/share/opennethome/SaveSensorValues.pl


onsdag 20 april 2016

ShellInABox on Raspberry Pi

#***************************************************
#**********            INSTALL            **********
#***************************************************

#Upgrade your system
sudo apt-get update && sudo apt-get upgrade -y

#Install dependencies
sudo apt-get install -y git libssl-dev libpam0g-dev zlib1g-dev dh-autoreconf

#Download source
git clone https://github.com/shellinabox/shellinabox.git /tmp/shellinabox

#Move to source folder

cd /tmp/shellinabox

#Build backage
dpkg-buildpackage -b

#Install package
sudo dpkg -i /tmp/shellinabox_*.deb


#Test:
#https://myip:4200/

#***************************************************
#**********            Misc               **********
#***************************************************


#Official site:
#https://github.com/shellinabox/shellinabox

#Config file:
/etc/default/shellinabox

lördag 2 april 2016

OpenNetHome

#***************************************************
#**********           INSTALL             **********
#***************************************************

#Upgrade your system
sudo apt-get update && sudo apt-get upgrade -y

#Install Java JDK
sudo apt-get install -y oracle-java8-jdk

#Download zipped OpenNetHome
#Latest source can be found here:
#http://opennethome.org/download/
wget http://wiki.nethome.nu/lib/exe/fetch.php/nethomeservernightly.zip
-O /tmp/nethomeservernightly.zip


#Unzip OpenNetHome
unzip /tmp/nethomeservernightly.zip -d /tmp
#Remove zipped OpenNetHome
sudo rm /tmp/nethomeservernightly.zip


#Add Execute access to install script
chmod +x /tmp/nethomeservernightly/install/raspbian/*.sh

#Install OpenNetHome

sudo /tmp/nethomeservernightly/install/raspbian/install.sh

#Sometimes a reboot is needed to find tellstick
sudo reboot

#Open browser
#http://MyRaspberryPi:8020/home


#***************************************************
#**********     Sample Reverse Proxy      **********
#***************************************************


#Create folder for password file
sudo mkdir /var/www/passwd

#Generate password file, add user "myuser"
sudo htpasswd -c /var/www/passwd/proxy_opennethome.passwd myuser

sudo nano /etc/apache2/sites-available/opennethome.jockeg.se-ssl.conf
<VirtualHost *:443>
        ServerName opennethome.mysite.org

        ProxyPreserveHost On
        ProxyRequests Off


        <Proxy *>
                Order deny,allow
                Allow from all
                AuthType Basic
                AuthName "Password Required"
                AuthUserFile /var/www/passwd/proxy_opennethome.passwd
                Require valid-user
        </Proxy>

        ProxyPass / http://mypi:8020/
        ProxyPassReverse / http://mypi:8020/

        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/opennethome.mysite.org/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/opennethome.mysite.org/privkey.pem


        RewriteEngine on
        RewriteRule   "^/$"  "/home"  [R]
 

</VirtualHost>


sudo nano /etc/apache2/sites-available/onh.mysite.org.conf
<VirtualHost *:80>
   ServerName onh.mysite.org
   Redirect permanent / https://opennethome.mysite.org/
</VirtualHost>


sudo nano /etc/apache2/sites-available/opennethome.mysite.org.conf
<VirtualHost *:80>
        ServerName opennethome.mysite.org
        <IfModule mod_proxy.c>
                ProxyRequests Off
                ProxyPreserveHost On
                ProxyVia On
                <Proxy *>
                        Order deny,allow
                        Allow from all
                        AuthType Basic
                        AuthName "Password Required"
                        AuthUserFile /var/www/passwd/proxy_opennethome.passwd

                        Require valid-user
                </Proxy>
                ProxyPass / http://mypi:8020/
        </IfModule>
        RewriteEngine on
        RewriteRule   "^/$"  "/home"  [R]
</VirtualHost>


#***************************************************#**********            Misc               **********
#***************************************************

#Official site:
http://opennethome.org/

#Upgrade nightly:
sudo nethome upgrade-nightly

Config directory:
/etc/opt/nethome