zondag 11 juni 2017

Domoticz KWh usage input with DDS238-2 SW (Pulse Input)



Hi Everyone,


Today i set up my new breaker board and i installed a DDS238-2 SW KWH meter on it do report my power usage to Domoticz.


This meter has a pulse counter (so no serial port) to send pulses to your controller (In my case a Raspberry Pi 3). So i use 3,3V GPIO input to pin 3 on the KWh meter and pin 4 to the inputpin of the Pi. My version has 1600 pulses per KWh, but i noticed there are versions with different pulse rates. If that is the case, just replace the formula below (replace the 1.6 value with for example 3.2 if you have a 3200 pulse/KWh version)


I will share the steps below in case you want to implement this the same way i did.


1. First create a virtual sensor in domoticz


2. Then create the deamon wich collects the data.

I use a very simple bash script to achieve this:

Replace the X's with your controllers IP and the Y's with the Domoticz Virtual sensor ID


# vi readpulse.sh

-----------------------------
#!/bin/bash
timer=`date +%s`
i="0"
n=0
while [ $i -lt 4 ]
do
kwhpulse=$(gpio read 0)
if [ $kwhpulse -eq 1 ]
then
   n=$((n+1))
   #echo $n
        while [ $kwhpulse -eq 1 ]
                do
                kwhpulse =$(gpio read 0)
                sleep 0.02
                done
fi
sleep 0.03
time_now=`date +%s`
time_elapsed=$((time_now - timer))
if [ $time_elapsed -ge 300 ]
then
   power=$(echo "scale=1;$n/1.6" | bc)
   #echo "KWH laatste 5 minuten" $power "KW"
   timer=`date +%s`
   n=0
   curl -s --connect-timeout 2 --max-time 5 "http://xxx.xxx.xxx.xxx/json.htm?type=command&param=udevice&idx=YY&nvalue=0&svalue=$power"
fi

done
---------------------------

Make it executable and give proper rights;
chmod +x readpulse.sh
chmod 755 readpulse.sh


3. Make it into a service. (the initgpio service is allso a custom service, so please change this to reflect your situation(after you exported the GPIO's)

# vi /etc/systemd/system/kwhpulse.service
[Unit]
After=initgpio.service

[Service]
ExecStart=/home/jef/readpulse.sh

[Install]
WantedBy=default.target

4. reload systemctl and enable service
systemctl daemon-reload
systemctl enable kwhpulse.service

systemctl start kwhpulse.service

This script has a pretty high CPU usage since it has to poll the input every 30 milliseconds. It uses between 6 and 9% CPU. While this is not a problem for my envirement, it can be good to know in advance.


That should be all. You should start to see the data coming into Domoticz now. (i had a NTP desync wich i solved while making this, so hence the datagap in the graph, but then atleast i can show something :))



dinsdag 1 november 2016

Reset(clear) Bareos Storage and database

If like me you just learned bareos and tested around a little, and you are now ready for production, follow the stept below to clear your bareos setup and you can start from scratch without having to reconfigure.

Reset Bareos (delete storage and clear database):

Stop all Deamons:

service bareos-dir stop
service bareos-fd stop
service bareos-sd stop

Remove all files:

rm -Rf /var/lib/bareos/storage/
rm  /var/lib/bareos/*

Drop database

cd /usr/lib/bareos/scripts
./drop_bareos_database

Create database

./create_bareos_database
./make_bareos_tables
./grant_bareos_privileges

Restart services

service bareos-dir start
service bareos-sd start
service bareos-fd start

donderdag 7 juli 2016

Raspberry Pi controlled parcel dropbox

Note: since my free time is limited and making a descent post takes allot of time i will most likely do this in pieces. So if its incomplete just visit this page again in a while. I will remove this message when the post is done.

On this post i will show a a nice little project wich i made last year. I order allot of stuff online and since i work fulltime i missed allot of deliveries.


I had a spare raspberry pi lying around and figured i'd make a dropbox. I bought myself an old metal military storage box, ordered a magnetic contact, and a electromagnetic lock and got to work.


Its a while ago i made it but i'll try to include as much info as i can to get it working.


Since i allso have a SMS server at home i linked the dropbox to that using a 'Huawei E1750 3G USB Dongle' I run that in a Windows server using Diafaan (so i can allso send sms's to unlock it etc). In the beginning i allso connected a phone through bluetooth to the raspberry pi and used gnoki to send sms's out. But i couln't receive anything on it(didn't spend to many time looking because i had Diafaan licence allready)

Here you can watch a short youtube vid of the dropbox demonstration :)






So basicly it operates like this. The dropbox is allways unlocked, and from the moment the lid is opened the magnetic lock activates. Opening the lid triggers an SMS to be sent out 'Dropbox lid opened'. When the lid is closed the magnet will latch onto the magnetic lock and stay closed. It sends another SMS out on closing it because that way i can know if the lid was not closed again.


When i get home i can send out an SMS to unlock the dropbox, this is allso very handy when i want to have someone pick something up. I put it in the dropbox, and when they call me they are at my house i unlock the dropbox remotely.


Allso i can lock the dropbox remotely by SMS command. The wiring is a single UTP cable, 1pair for the magnetic sensor, 2 pairs for the lock and since i had a free pair i allso made a normal closed loop, so when the UTP cable is cut, it allso sends out an SMS.


ToDo:

Add plans, drawings scripts and pi config. Add PCB layout...

zaterdag 25 mei 2013

Android lockscreen

Custom android lockscreen without cropping or extra app

Recently i wanted to change my android lockscreen for my SGS2, and had some trouble doing so.

There are different apps on the android market for this but i do not want to install an app for it because i have apps enough allready.

Here i will describe a way to set you lock screen so your selected image is allways full screen, without the need for any extra application. Its very simple, but i didn't find it anywhere on the internets to do it this way so i thought i'd write it up real quick.


Because android allways shows the cropping option for the selected image, even though the image resolution is the same as your screen resolution, leaving you with a background showing only half the image, with no way to zoom or expand the canvas.


Its very simple really. For my SGS2, my screen resolution is 480x800. So to workaround the bug, just create a new image in any image editing program of your choise with DOUBLE the width resolution, for the SGS2 that would be 960x800. Paste your image in the middle and save.

It should look something like this:



Then re-upload it to your phone, and set it again as lockscreen, the image should now appear in the selected box 100%, and the rest is cropped.


FYI: if you cant find how to set a lockscreen, just open the image gallery, browse to the picture you want to set. Then in options -> Set As... -> Lock Screen

vrijdag 19 april 2013

OpenVPN Server on Raspberry Pi

Here i will explain how to quickly install OpenVPN on a Raspberry Pi running raspian

Needed Files:
Get Raspian Wheezy HERE
To get it installed on your SD card you need Win32DiskImager (Download it HERE)

After installing the image boot up your Pi, finish the setup by following instructions on the screen. Open up a command line tool if you booted to X.

First download the needed software packets:
apt-get install openvpn openssl module-init-tools zip dos2unix

Then, enable the tun module:
 modprobe tun

Load the tun module on boot:
echo '# Needed by OpenVPN
tun' >> '/etc/modules'

Create device file:
mkdir --parent '/dev/net'
mknod '/dev/net/tun' c 10 200

Install OpenVPN Tools:
wget 'https://raw.github.com/biapy/howto.biapy.com/master/openvpn/openvpn-tools' \
    --quiet --no-check-certificate --output-document='/usr/bin/openvpn-tools'
chmod +x '/usr/bin/openvpn-tools'

Get the 'update-resolve-conf' script:
wget 'https://raw.github.com/biapy/howto.biapy.com/master/openvpn/update-resolv-conf' \
  --quiet --no-check-certificate --output-document='/etc/openvpn/update-resolv-conf'
chmod +x '/etc/openvpn/update-resolv-conf'

Set SSL Certificate defaults:

openvpn-tools initialize

Setup Logs:
echo "/var/log/openvpn/*/*.log {
  daily
  missingok
  rotate 10
  compress
  delaycompress
  notifempty
  create 600 root root
  postrotate
    /etc/init.d/openvpn restart > /dev/null
  endscript
}" > "/etc/logrotate.d/openvpn"

Create server:
openvpn-tools create-server local-domain.vpn vpn.domain.com

Where:

local-domain.vpn is the VPN name of the new server.
vpn.domain.com is the Internet host name of the VPN server.
Default port is 1194 UDP. If needed, you can change ports later in the config file.

Next, create client configuration and certificates, they will be stored in: " /etc/openvpn/config/local-domain.vpn/clients/Client-name"
openvpn-tools add-client local-domain.vpn client-alias

This will create configuration files and certificates in a zip file. You can retrieve them with PSCP if you do not want to setup an ftp server.

Enable/Disable NAT:
openvpn-tools toggle-nat local-domain.vpn

Toggle Forced use of tunnel for all network traffic on clients:
openvpn-tools toggle-gateway local-domain.vpn

That should to the trick. Setup your client with clientsoftware from the OpenVPN website and copy the contents of the zip file downloaded from the pi into the config folder. U should now be able to use the VPN tunnel succesfully. Be sure to keep in mind to configure port forwarding on your router if needed. If you provider blocks port 1194, you can change that in the config file located at '/etc/openvpn/local-domain.vpn.conf' and allso the client config file!

For safety, allso create a new user with sudo rights and remove user pi.

maandag 25 februari 2013

Quick Start Vyatta OpenVPN Client/Server setup.

Quick Start Vyatta OpenVPN Client/Server setup (Belgium (ISP:Belgacom) with self signed certificates)


In this article i will explain how to setup a VPN server in your home using OpenVPN using Vyatta as a vpn server. The primary router in this setup is a sagem BBOX2 from ISP Belgacom. Router setup changes that have to be made will allso be covered in this guide.

THIS video allso explains from start to end on how to do this, but its pretty long(1h 13m). with this guide you should be able to set this up faster. If your having trouble following the instructions given below i advise you to watch the video anyways and follow it step by step. (exept for the download links use the ones provided below)

We will use the LAN network of 192.168.1.0/24 and VPN net 192.168.100.0/27.

Router IP: 192.168.1.1/24
VPN Server IP(Vyatta): 192.168.1.100
WAN IP/Hostname: I use a DynDNS account for dynamic wan ip updating, but there are other alternatives

Prerequisites:
Vyatta: http://www.vyatta.org/downloads
OpenVPN Client: http://openvpn.net/index.php/download.html (Bottom of page: Community Downloads)

If you do not have dedicated hardware to install the Vyatta iso you can use a virtualization packet of your choise to do this.

Burn or mount Vyatta and boot from the iso.

log in with the standard passwords:
user: Vyatta
Password: Vyatta

or

user:root
Password: Vyatta


To install the system to hard drive, enter the "install-system" command. After install, unmount and reboot.
after login, change the password using the "sudo passwd" command.

First, set your ip adress so you Vyatta router is reachable

Enter config mode:

root@vyatta:~# configure
[edit]
root@vyatta# set interfaces ethernet eth1 address 192.168.1.100/24 
root@vyatta# commit

root@vyatta# saveSaving configuration to '/config/config.boot'...Done[edit]root@vyatta#


The commit is needed in vyatta to actually apply the given command(s),  otherwise your commands will not be written to config. Additionally you need to save the running configuration so that it is preserved after reboot. In this guide i have cut the commit and save commands to save space.

Then setup the VPN interface:
Here i use port 443, whereas the standard port for openVPN is 1194. Most ISP block this port, so the use of port 443 is allso supported by openVPN. For Belgacom ports 80 and 443 can be opened, This is not enabled automaticly though, you have to log in to your E-Services account and configure there to have the ports opened)

root@vyatta# set interfaces openvpn vtun0 local-port 443 
root@vyatta# set interfaces openvpn vtun0 protocol udp 
root@vyatta# set interfaces openvpn vtun0 mode server 
root@vyatta# set interfaces openvpn vtun0 openvpn-option "--mssfix --tun-mtu 1488" 
root@vyatta# set interfaces openvpn vtun0 replace-default-route 
root@vyatta# set interfaces openvpn vtun0 server subnet 192.168.100.0/27

Then create and sign your certificates, watch Roggy's video from 32:30 on how to do this.
Set certificate info:

root@vyatta# set interfaces openvpn vtun0 tls ca-cert-file /etc/openvpn/ca.crt 
root@vyatta# set interfaces openvpn vtun0 tls cert-file /etc/openvpn/vpn1.crt 
root@vyatta# set interfaces openvpn vtun0 tls dh-file /etc/openvpn/dh1024.pem 
root@vyatta# set interfaces openvpn vtun0 tls key-file /etc/openvpn/vpn1.key

Set up NATting for the VPN net:
root@vyatta# set nat source rule 10 outbound-interface eth1
root@vyatta# set nat source rule 10 source address 192.168.100.0/27
root@vyatta# set nat source rule 10 translation address masquerade

Set next hop:

root@vyatta# set protocols static route 0.0.0.0/0 next-hop 192.168.1.1


Enable SSH access(to upload/download cert files)
root@vyatta# set service ssh port 22

Your config should look like this:

interfaces {
    ethernet eth1 {
        address 192.168.1.100/24
        duplex auto
        hw-id 9a:6f:ae:18:cd:bf
        smp_affinity auto
        speed auto
    }


    openvpn vtun0 {
        local-port 443
        mode server
        openvpn-option "--mssfix --tun-mtu 1488"
        protocol udp
        replace-default-route {
        }
        server {
            subnet 192.168.100.0/27
        }
        tls {
            ca-cert-file /etc/openvpn/ca.crt
            cert-file /etc/openvpn/vpn1.crt
            dh-file /etc/openvpn/dh1024.pem
            key-file /etc/openvpn/vpn1.key
        }
    }
}
nat {
    source {
        rule 10 {
            outbound-interface eth1
            source {
                address 192.168.100.0/27
            }
            translation {
                address masquerade
            }
        }
    }
}
protocols {
    static {
        route 0.0.0.0/0 {
            next-hop 192.168.1.1 {
            }
        }
    }
}
service {
    ssh {
        allow-root
        port 22
    }
}

Next, configure you BBOX2:
Log in to your BBOX through your webbrowser (usually ip 192.168.1.1)

Then Advanced Settings -> LAN Servers:

Add port forwarding for port 443 and point it to your vyatta router interface (192.168.1.100)


Disable your router's firewall, or if you dont want to do that add a port trigger rule and afvanced filteringrule for port 443. 


Next, you need to change your WAN MTU because on the autodetect function your VPN connection will fail!! The standard MTU value is 1500, but this wil generate errors, so we will set it to match the BBOX maximums MTU of 1488. If you have another ISP, you can just try with the standard settings first.


Set the MTU value to 1488. 



Next, setup your OpenVPN client. After installation go the C:\Program Files\OpenVPN\config\ folder and place there your certificate-, key and dh file. Next go to your network inferfaces and rename the "TAP-Windows Adapter V9"'s name to Tap0. Now open the client.ovpn and change the following value's

change following parameters to:
#TAP Adapter name
dev-node Tap0

#protocol
proto udp

#server hostname/ip
remote "YOUR WAN IP" 443

#SSL/TLS params:
ca ca.crt
cert ***.crt
key ***.key

comment out comp-lzo

Add the folowing parameters:
--tun-mtu 1488
--mssfix
--script-security 2

Save the config and now you can start the openVPN client software. It is very important to run it with administrator rights because changing the routing for the VPN tunnel required admin rights.


Now you should be ready to use your VPN tunnel. If i should have forgotten any steps just mention it the comments and i will add it later on. I typed this up rather quick and my network setup is different then the one displayed here, but wanted to make a guide that showed a working config for this ISP. I hope this helps you setup a VPN tunnel quickly.

If you have any configuring just leave a comment and i will try to help.