All posts by Łukasz Jokiel

Domoticz part 2: WiFi remote thermometer for weather station, dual digit precision – it’s ESP8266 for beginners

Last time we installed Domoticz, and soon we will move to Z-Wave, but first – let’s take a look at fantastic little gadget: ESP8266

What is an ESP8266?

ESP8266 (also called  ESP here) is a SoC – System on a Chip, which in our case is – CPU/RAM/FLASH with GPIO and WiFi interface – so another mini komputer with wireless interface. You can think of it as scaled down version of Raspberry Pi, less power hungry, but with own WLAN and a lot (minimum 2) GPIOs. Ad it runs it’s own WWW server!

  • CPU – 80MHz  (much faster than Arduino, much slower than Pi)
  • RAM – 64kB for instructions and 96kB
  • ROM – 64kB (just for boot)
  • FLASH: 4Mb (512kB)
  • WLAN: radio 2,4GHz – 802.11 b/g/n with WPA2
  • GPIO, I2C, ADC, SPI, PWM
  • Power: 3,3V (just what we have available using PIN 1 in Raspbery Pi)
  • Interpreter and LUA compiler (sic!). Or pure C using Arduino IDE
  • Price: $3. Yes. 3 US dollars

The SoC’s manufacturer is Espressif, other companies produce ready boards with interfaces and antennas:

    • ESP-01 (v1 and v2) – you get pins for +3,3V, GND, TX,RX and GPIO 0 i GPIO 2 (they spawn pull-up). WiFi antenna is printed on the module. Very popular due to price, best for beginners – it’s easy to connect it to Pi
    • ESP-03 – we get 8x GPIO
    • ESP-12 – SoC with shielding, better antenna and 10x GPIO
ESP-12
ESP-12

But why?

Let’s summarize: we have a possibility to use two or more GPIOs on WiFi enabled device, using very little power (10x less than RPi) – ideal for a weather station sensor. We can write a small program that would update the values in Domoticz (i.e. sensors) – because we can not only use the internal Web server but also sent periodically data to Domoticz – using nodejs or JSON format URL that Domoticz understands well. The latter is much more stable – and it’s great for starters.
It’s decided then! We’re building remote thermometer – based on ready examples.

Hardware

  • Raspberry Pi (any model – A, B, B+, 2 or 3 or even Pi Zero)

    Raspberyy Pi B+
    Raspberyy Pi B+
  • ESP8266 (any version, the best for start is ESP-01 with two GPIOs)

    ESP-8266 01 PINout
    ESP-8266 01 PINout
  • Dallas DS18b20 Thermometr – we can get chip or waterproof versions

    Dallas ds18b20 thermometr
    Dallas ds18b20 thermometer
  • 4,7k resistor – allows good readout from thermometer (pulls-up the DATA PIN), but if you are not getting results – change it to 2,2k:
    Rezystor 4,7k
  • OPTIONAL: Step-down module which will provide stable 3,3V out of input 5V (just make a search on your favorite portal for: step-down 3.3V)
    pi-esp-1
  • OPTIONAL: One or two Li-Ion type 18650 batteries – you can extract them from old notebook battery packs or power banks – but be carefull when disasembling the pack – 18650 are very dangerous when short or punctured! It could also cell phone battery – any Li-Ion that you can easily set-up. Fully charged battery is about max 4,2V and total capacity of around 2300mAh-2500mAh
  • OPTIONAL: charging module for working at 5V to properly charge and protect Li-Ion battery – it has USB and micro-USB connectors for input and output and soldiering pads for 18650. Check on how you can attach 18650 (be extra careful when soldering!)

    Regulator i zasilacz dla baterii Li-Ion
    Charging module for Li-Ion batteries

Connecting the ESP8266 module and updating the firmware

The ESP8266 has a standard UART – with TX,RX PINs, 3V logic – and it’s great since we can use the Raspberry Pi as a console!. In my optionion this is the best way to deal with this module. Our Raspberry Pi UART port is by default used as RPi own system console, so we have to free it to use as communications channel to ESP8266. To do that we have to delete in /boot/cmdline.txt this part: console=/dev/ttyAMA0,115000. Now let’s reboot RPi.
We should install now:

Raspberry Pi + ESP8266
Raspberry Pi with ESP8266 connected

Since ESP8266 is often used as Arduino module it comes to us with a firmware that uses AT commands – just like modems do. We will use the NodeMCU firmware – that uses LUA intereter/compiler – another very nice BASIC-like type of software (very powerfull!). At the time of writting we had: nodemcu_integer_0.9.6-dev_20150704.bin version available with the tool – esptool.py. Let’s hook up the ESP8266 to Raspberry Pi:

  • We will be using 3,3V as VCC form RPi: +3,3V connecting to VCC and CH_PD on ESP (look at the picture). Now it’s not recommended for a longer period, because ESP8266 can draw 3x more than 3,3V PIN on RPi can support!
  • GND from RPi to GND at ESP
  • PIN 13 on RPi to TX on ESP, and PIN 14 on RPi to RX on ESP
  • Additionaly, we have to connect GPIO 0 to GND on ESP-01, to allow firmware upgrade (and re-power ESP after connecting). Upgrading is as easy as this:

Installing proper serial support library for python:

sudo apt-get install python-serial

Updating the firmware:

python esptool.py -p /dev/ttyAMA0 write_flash 0x000000 nodemcu_integer_0.9.6-dev_20150704.bin

If GPIO0 is NOT connected to GND you will see this error:

[email protected] ~/esp8266 $ python esptool.py -p /dev/ttyAMA0 write_flash 0x000000 nodemcu_integer_0.9.6-dev_20150704.bin
Connecting...
Traceback (most recent call last):
  File "esptool.py", line 353, in 
    esp.connect()
  File "esptool.py", line 138, in connect
    raise Exception('Failed to connect')
Exception: Failed to connect

If GPIO0 is connected to GND and re-powering of the ESP unit – firmware will be upgraded:

python esptool.py -p /dev/ttyAMA0 write_flash 0x000000 nodemcu_integer_0.9.6-dev_20150704.bin
Connecting...
Erasing flash...
Writing at 0x00005800... (5 %)
Writing at 0x00031800... (45 %)
Writing at 0x00057c00... (80 %)
Writing at 0x0006dc00... (100 %)

Leaving...

After successful upgrade we should get to know our new toy. To do that we need serial console -let’s install picocom and enter:

picocom -b 9600 --omap crcrlf /dev/ttyAMA0

We can see the “>” prompt, so if you already know LUA you can start testing or use: official examples form NodeMCU..
First, let’s get IP address via WiFi, enter the commands line per line:

--nil
print(wifi.sta.getip())
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
print(wifi.sta.getip())
--192.168.1.12

You need to change the SSID and password to correct values for your home network – and ESP will connec to Access Point and will ask for IP address via DHCP client. Great, we have IP address now. Now, let’s connect the Dallas DS18b20. It’s a very cheap sensor, that is supported by NodeMCU example – download from the module library the file : ds18b20.lua.
But we need changes, since we will get only single digit precision in Celsius mode – find the line:

 t = t / 10000

and replace with:

 t = t / 100

Double digit precision is ready! Changed file looks like this:

--------------------------------------------------------------------------------
-- DS18B20 one wire module for NODEMCU
-- NODEMCU TEAM
-- LICENCE: http://opensource.org/licenses/MIT
-- Vowstar <[email protected]>
-- 2015/02/14 sza2 <[email protected]> Fix for negative values
--------------------------------------------------------------------------------

-- Set module name as parameter of require
local modname = ...
local M = {}
_G[modname] = M
--------------------------------------------------------------------------------
-- Local used variables
--------------------------------------------------------------------------------
-- DS18B20 dq pin
local pin = nil
-- DS18B20 default pin
local defaultPin = 9
--------------------------------------------------------------------------------
-- Local used modules
--------------------------------------------------------------------------------
-- Table module
local table = table
-- String module
local string = string
-- One wire module
local ow = ow
-- Timer module
local tmr = tmr
-- Limited to local environment
setfenv(1,M)
--------------------------------------------------------------------------------
-- Implementation
--------------------------------------------------------------------------------
C = 0
F = 1
K = 2
function setup(dq)
  pin = dq
  if(pin == nil) then
    pin = defaultPin
  end
  ow.setup(pin)
end

function addrs()
  setup(pin)
  tbl = {}
  ow.reset_search(pin)
  repeat
    addr = ow.search(pin)
    if(addr ~= nil) then
      table.insert(tbl, addr)
    end
    tmr.wdclr()
  until (addr == nil)
  ow.reset_search(pin)
  return tbl
end

function readNumber(addr, unit)
  result = nil
  setup(pin)
  flag = false
  if(addr == nil) then
    ow.reset_search(pin)
    count = 0
    repeat
      count = count + 1
      addr = ow.search(pin)
      tmr.wdclr()
    until((addr ~= nil) or (count > 100))
    ow.reset_search(pin)
  end
  if(addr == nil) then
    return result
  end
  crc = ow.crc8(string.sub(addr,1,7))
  if (crc == addr:byte(8)) then
    if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
      -- print("Device is a DS18S20 family device.")
      ow.reset(pin)
      ow.select(pin, addr)
      ow.write(pin, 0x44, 1)
      -- tmr.delay(1000000)
      present = ow.reset(pin)
      ow.select(pin, addr)
      ow.write(pin,0xBE,1)
      -- print("P="..present)
      data = nil
      data = string.char(ow.read(pin))
      for i = 1, 8 do
        data = data .. string.char(ow.read(pin))
      end
      -- print(data:byte(1,9))
      crc = ow.crc8(string.sub(data,1,8))
      -- print("CRC="..crc)
      if (crc == data:byte(9)) then
        t = (data:byte(1) + data:byte(2) * 256)
        if (t > 32767) then
          t = t - 65536
        end
        if(unit == nil or unit == C) then
          t = t * 625
        elseif(unit == F) then
          t = t * 1125 + 320000
        elseif(unit == K) then
          t = t * 625 + 2731500
        else
          return nil
        end
        t = t / 100
        -- print("Temperature="..t1.."."..t2.." Centigrade")
        -- result = t1.."."..t2
        return t
      end
      tmr.wdclr()
    else
    -- print("Device family is not recognized.")
    end
  else
  -- print("CRC is not valid!")
  end
  return result
end

function read(addr, unit)
  t = readNumber(addr, unit)
  if (t == nil) then
    return nil
  else
    return t
  end
end

-- Return module table
return M

Save it for later – we will upload this to ESP in a moment- first let’s see how to autostart programs on ESP

Autostarting programs on ESP8266

 

ESP8266 with NodeMCU firmware after booting executes the init.lua, which is residing on the flash. It is extremely important that init.lua is TESTED and won’t put the ESP in boot-loop. We all heard horror stories about bad loop that executes indefinitely. Rule of a thumb – our init.lua should be short, minimal. In our case – let’s just get IP address and run the domoticz.lua which will report the temperature back to domoticz. Also – let’s try to have a backup WiFi in case we cannot connect with main – we can use the second WiFi network for debug purposes. Another important part of init.lua is the pause. Before getting/asking for IP address we should have enough time to issue a command to delete the init.lua file, so we can replace it in case no WiFi network is available. Our init.lua should look like this example:

--init.lua
 wifi.setmode (wifi.STATION)
 wifi.sta.config("MAIN SSID 1","extra_secret_no_1")
 wifi.sta.connect()
 tmr.alarm(1, 6000, 1, function()
  if wifi.sta.getip()== nil then
  print("No IP address, trying again...")
  wifi.sta.config("BACKUP SSID","other_secret_pass")
  wifi.sta.connect()
 else
  tmr.stop(1)
 print("WiFi mode ESP8266: " .. wifi.getmode())
 print("MAC address ESP8266: " .. wifi.ap.getmac())
 print("Success, IP is: "..wifi.sta.getip())
 dofile ("domoticz.lua")
 end
 end)

This init.lua changes ESP mode into AP client (ESP is also used as AP), sets the SSID, password and issues command to connect to home AP. Next – after 6000ms we check if wifi.sta.getip() returns something else than “nil” (nothing). If not – we move to configure backup network and we repeat that every 6000ms until ESP gets IP address. This 6000ms is here to login via serial console and delete the init.lua if we do not have any WiFi network. But if all goes well – ESP runs via  dofile, another script that does the readout of temperature and sends it to domoticz – it’s called:  domoticz.lua

Main temperature readout program, sending data to domoticz

--domoticz.lua
require('ds18b20')
gpio0 =3
gpio2 =4
ds18b20.setup(gpio2)
t=ds18b20.read()
print("RAW output from ds28b20:" .. t .. " C\n")
if(t==nil) then
t=0
end
tmr.alarm(0,240000, 1, function()
 t=ds18b20.read()
 tpre=t/100
 tsub=string.sub(t,-2)
 temp=tpre .. "." .. tsub
 print("Sent temperature:" .. temp .. " C\n")
 conn=net.createConnection(net.TCP, 0)
 conn:on("receive", function(conn, payload) print(payload) end )
 conn:connect(8080,"192.168.1.15")
 conn:send("GET /json.htm?type=command¶m=udevice&idx=11&nvalue=0&svalue=" .. temp .. " HTTP/1.1\r\nHost: thermometer.sheed\r\n".."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
end)

This main program/script has the following structure; first it includes the attached previously library from NodeMCU (with the slight modification – remember?). Next it changes the GPIOs to allow for temperature readout in the Dallas mode – using GPIO2. For debugging we display both raw readout to check if we got 4 digits. This is the hack that allows for spliting the data into XY.ZW format and constructs simple HTTP request in JSON to send to domoticza, which has IP of 192.168.1.15 and port 8080. Since we need a virtual sensor – we need to set proper IDX here, so the data land in proper place. Now – test and wait for negative temperatures to see what’s going to happen. Check the comments section to find out for an alternative version. Let’s put those file on ESP8266’s flash.

Uploading files to ESP8266

Thanks to another python program, that we downloaded – we can quickly put files onto ESP8266 – using luatool.py:

python luatool.py -p /dev/ttyAMA0 --src init.lua --dest init.lua
python luatool.py -p /dev/ttyAMA0 --src domoticz.lua --dest domoticz.lua
python luatool.py -p /dev/ttyAMA0 --src ds18b20.lua --dest ds18b20.lua

Since we already connected via picocom, luatool.py will just work. It’s a good idea to test init.lua NOW because we can later end up in a loop.

OPTIONAL: Powering via solar module with battery backup

As we said before the RPi is not a perfect way to power ESP, because it cannot sustain it (too much mAps are drawn). And more importantly – this is a remote temp sensor, so it should be in WiFi range but not next to RPi. Let’s build our solar powered system:

  • one, two or three Li-Ion type 18650 battery
  • integrated charge controller from power bank with 5V input
  • step down module with output of 500mA max 3,3V
  • solar panel of 5V and 400mAp
  • WARNING: Do not use if temperature is OUTSIDE the 18650 spec, this can be dangerous! Do not use the 18650 battery if any parameter is out of it’s spec.

Solar panel should be mounted on some pice of plastic and secured with glue to allow operation in different weather conditions. The perfect angle towards sun is 45 degrees.

Panel fotowoltaniczny 5V, zamontowany na pleksi
Photovoltanic panel, rated 5V, 400mAp

Gluing the panel (I know – lousy job here…):

Tył panelu fotowoltanicznego
Tył panelu fotowoltanicznego

And then we connect it to power back charge controller via USB:

ESP8266 na NodeMCU Amica Board z zasilaniem bateryjnym oraz czujnikiem DHT22
ESP8266 with NodeMCU on Amica Boardwith battery power and DHT22 sensor

Above you can see the next version of this project – moving to more advanced firmware and hardware – the ESPEasy on ESP-12E using Amica Board with +5V VCC.




OpenVPN Raspberry Pi – split-tunnel with Internet access

Would you like to connect from any place, usually using open WiFi network in town, at friends or airport – with your’re own home network? Virtual Private Network (VPN) is here to allow you to log-in securely to your home network, with additional perk of using it to get secure Internet access.

Raspberyy Pi B+
Raspberyy Pi B+

What will you need?

Hardware:

  • Rapberry Pi (A, A+ or B, B+, 2, Zero)
  • Home Router with Port Forwarding or Virtual Server support – any will do

Software:

  • Raspbian – standard Linux distribution for Raspberry Pi
  • OpenVPN – free OpenVPN server
  • OpenVPN – clients – different for Linux, Android, MS Windows etc.

As you see nothing fancy.

Security

Let’s talk about how we should secure our access:

  • physical access to your Raspberry Pi should be controlled (only you)
  • access to /etc should be restricted to the user that OpenVPN is running under
  • the files with .key are TOTALLY SECRET
  • the certificate .crt or .crs – are to be sent, lost, revoked – could be always renewed.
  • each device connecting to OpenVPN server (PC, tablet, smartphone) has it’s own key/certificate pair

Let’s do it

Well, as always the first thing is to update out RPi – let’s login via ssh or console:

sudo apt-get update && sudo apt-get upgrade

Now, lets install OpenVPN package:
sudo apt-get install openvpn

All commands used require root privileges, so I suggest to enter:
sudo su -

Next we should configure the server, first to generate SSL certificates, that allow securing of the connection and also – authentication.
All files should be stored in /etc, let’s create appropriate folder and copy over the example files to create certs, which reside in Raspbianie Wheezy here:
cd /etc/openvpn
mkdir easy-rsa
cp -R /usr/share/doc/openvpn/examples/easy-rsa/2.0/* easy-rsa/

If you’re on the newer Jessie, as noted in comments by Dom/OloX – folder with examples is located in /usr/share:
cd /etc/openvpn
mkdir easy-rsa
cp -R /usr/share/easy-rsa/* /etc/openvpn/easy-rsa/

Examples provided by easy-rsa package default to USA based data. Wait, what ? It’s all about the fact that the certificate has to have identification – who’s the authority that issued it. Since it’s going to be ours – we should put some familiar data here to identify it later.

Let’s edit the file ( or accept the fact that all certificates are from San Francisco…):

sudo nano /etc/openvpn/easy-rsa/vars

The changes are about the folder where we store the certs and keys, length of the asymetrical key – it’s usually1024 – but 2048 might be too much from RPi 1, or even 2 or 3. Let’s use the value of 1024:
export EASY_RSA=”/etc/openvpn/easy-rsa”
export KEY_SIZE=1024
export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_CN=SerwerOVPN
export KEY_ALTNAMES="OVPN"

Country code – PL,  county/voivodeship: WA and so on – use what you like or don’t, but the KEY_CN – this should be changed.
Let’s prepare to create the keys:
cd /etc/openvpn/easy-rsa
touch keys/index.txt
echo 01 > keys/serial
. ./vars  # set environment variables
./clean-all

Now we will build a certificate authority::
./build-ca

All you need to do is to confirm values previously entered in vars:
Generating a 1024 bit RSA private key
..........++++++
..........................++++++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [PL]

The folder keys will now have ca.key and ca.cert.

Now, important port – generating the key to our system:

./build-key-server server

We will be asked to provide password. STOP. OK, enter a password – but then you will also have to enter it with each connection. We can remove the password and lower our security – the choice is yours!
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:

Answer yes to remaining questions and you will get in folder keys new files: server.crt and server.key in (/etc/openvpn/easy-rsa/keys/), they will be signed with your root certificate.
Now let’s build the Diffie-Hellmann parameters required for client and server to talk to each other. The process is quite interesting – check out  https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange 
./build-dh

This produces 1024.pem file, change the name to dh1024.pem
Now lets build the pairs – files – for a client:
./build-key client1

…along with the password:
./build-key-pass client1

That’s nearly the end – now let’s generate common HMAC key for TLS:
openvpn --genkey --secret /etc/openvpn/easy-rsa/keys/tls.key

Let’s copy all the files /etc/openvpn/mykeys/
cd /etc/openvpn/easy-rsa/keys
mkdir -p /etc/openvpn/mykeys
cp dh1024.pem /etc/openvpn/mojeklucze
cp ca.crt /etc/openvpn/mojeklucze
cp server.crt /etc/openvpn/mojeklucze
cp server.key /etc/openvpn/mojeklucze
cp tls.key /etc/openvpn/mojeklucze

It’s time to configure our OpenVPN server using certificates and keys.
First create the /etc/openvpn/openvpn.conf file with following entries:
push "redirect-gateway"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 192.168.11.1"
server 192.168.22.0 255.255.255.0
dev tun
verb 5
proto udp
keepalive 10 120
port 1194
dh /etc/openvpn/mykeys/dh1024.pem
ca /etc/openvpn/mykeys/ca.crt
cert /etc/openvpn/mykeys/server.crt
key /etc/openvpn/mykeys/server.key

user nobody
group nogroup

persist-key
persist-tun
status /var/log/openvpn-status.log
verb 3
client-to-client                                                                                                                                      
log-append /var/log/openvpn

The configuration of the server will get all the traffic coming from it (apart from static routes on the client – accessing the other LAN) and steer it to tunnel. Thanks to this we will also have Internet access through our tunnel and VPN (but check below!). The resolvers for DNS operation – let’s use Google and our local as secondary. The important line is ‘server’ this creates a network used only for the actual connection – so use private network you don’t use anywhere. The port is 1194 – we will have to reconfigure our home router for connection – see below for Port Forwading options. The protocol used is UDP.

Port forwarding

The forwarding of the public port from your router to private port on Raspberry Pi is quite easy to configure:

Port Forwarding - Linksys
Port Forwarding – Linksys
Port Forwarding - D-Link DIR
Port Forwarding – D-Link DIR
Port Forwarding - Archer VR900
Port Forwarding – Archer VR900
Port Forwarding - TP-Link
Port Forwarding – TP-Link
Port Forwarding - TP-Link
Port Forwarding – TP-Link

Split-tunnel – safe access to Internet by means of VPN

To allow split-tunnel – we need to add configuration at the end of the /etc/rc.local file.
Do mind that you need to change the example RPi address of 192.168.11.11 to yours, and as mentioned above – choose unused VPN – here: 192.168.22.0/24

The file should have following lines:

echo "Enabling routing for proper VPN operation..."
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "Remote port mapping for VPN service..."
iptables -t nat -A INPUT -i eth0 -p udp -m udp --dport 1194 -j ACCEPT
echo "SNAT of the VPN addresses to allow split-tunneling..."
iptables -t nat -A POSTROUTING -s 192.168.22.0/24 -o eth0 -j SNAT --to-source 192.168.11.11

exit 0

Client

Let’s configure the client – first mobile devices – Android. Download the OpenVPN (check for fakes!) and after installing – it’s going to ask for .ovpn file. Let’s create it, don’t forget to change the IP_ADDRESS_OR_FQDN_OF_THE_SERVER to your OpenVPN server.

Back to console of your Raspberry Pi:

cd /etc/openvpn/mojeklucze/
echo "client" > client.ovpn
echo "verb 4" >> client.ovpn
echo "dev tun" >> client.ovpn
echo "connect-retry-max 5" >> client.ovpn
echo "connect-retry 5" >> client.ovpn
echo "resolv-retry infinite" >> client.ovpn
echo "proto udp" >> client.ovpn
echo "remote IP_ADDRESS_OR_FQDN_OF_THE_SERVER 1194" >> client.ovpn
echo "nobind" >> client.ovpn
echo "persist-key" >> client.ovpn
echo "persist-tun" >> client.ovpn
echo "keepalive 10 900" >> client.ovpn
echo "inactive 3600" >> client.ovpn
echo "set CLIENT_CERT 0" >> client.ovpn
echo "<ca>;" >> client.ovpn
cat ca.crt | grep -A 100 "BEGIN CERTIFICATE" | grep -B 100 "END CERTIFICATE" >> client.ovpn
echo "</ca>" >> client.ovpn
echo "<cert>" >> client.ovpn
cat client1.crt | grep -A 100 "BEGIN CERTIFICATE" | grep -B 100 "END CERTIFICATE" >> client.ovpn
echo "</cert>" >> client.ovpn
echo "<key>" >> client.ovpn
cat client1.key | grep -A 100 "BEGIN PRIVATE KEY" | grep -B 100 "END PRIVATE KEY" >> client.ovpn
echo "</key>" >> client.ovpn

We got – client.ovpn. Rewiev the file, change: proto to udp, remote – IP address – to our public router address (or name if we have Dynamic DNS service), and enter the IP addresses into server line. Remove any ca cert key references to the files if they exist in imported example. The ready file goes to Android device – import, run connect and there it is!. Do mind that for mobile devices (i.e. Android/iOS- OpenVPN Connect requires the certificates to be embedded in the *.ovpn file!



Domoticz part 1: Relays, magnetic sensors, Wake-on-LAN and Z-Wave – Domoticz with Imperi Home serving your house

Domoticz on PI with relay
Domoticz on PI with relay

This entry was initially about single and simple purpose: how to quickly develop a solution that would open gate or garadge door by simulating the remote’s operation mode – pressing a button – but using smartphone, tablet, web browser, app. Domoticz is such an overkill and also blessing for such project – that it evolved into a lot of small sub-projects – more or less andvanced.

Direct inspiration was another entry by colleague on  MalinowePi Facebook group – Roman Kubat, who showed ImperiHome. This application has great interface but does not support press-type (dorbell) buttons. Hence the whole idea!

Continue reading Domoticz part 1: Relays, magnetic sensors, Wake-on-LAN and Z-Wave – Domoticz with Imperi Home serving your house

Light effect for any device with Video output based on raspbmc or OpenELEC and RaspberryPi

“Good news, everyone!”. If you succeeded in creating light effect using the previous entry:  https://blog.jokielowie.com/en/2014/02/efekt-swietlny-dla-media-playera-operatego-o-raspberty-pi/ you must’ve noticed that it’s limited to the content being played by raspbmc or OpenELEC. Luckily thanks to constant development by hyperion’s creator (tvdzwan) we’ve just got ability to use the effect with other devices that have video output the http://pl.wikipedia.org/wiki/Composite_video. Usually modern STB, Blu-Ray player or amplituners sport a SCART output or the yellow colored ‘Video’ output. What is important – they allow both HDMI and Composite video outputs to be working simultaneously.

What is needed ?

This time we need to get a cheap $10 ‘TV grabber ‘ or Video DVR’ USB dongle. It has Composite Video and Audio (L+P) inputs, and those sticks are used to transfer to new world movies from old VHS cassettes:

Ikona dla Grabbera
Raspberry Pi – with grabber

Hardware and software configuration

So those $10 dongles with different names (i.e. EzCap) are equipped with two chipsets: STK1160 or Fushicai. To check which is in our dongle issue

sudo lsusb

OpenELEC

Both are supported by latest OpenELEC (Ghost 13), but make sure that lsmod contains (but not limited to) this modules when Fushicai chipet is present:

# lsmod
spi-bcm2708
videobuf2_core
videobuf2_vmalloc
usbtv

When STK1160 is present lsmod contains (but not limited to) this modules:

# lsmod
spi-bcm2708
videobuf2_core
videobuf2_vmalloc
stk1160

The working grabber can be checked in many ways, for starters let’s issue:

ls -al /dev/video

Plese do remeber if you’ve identfied video0 – or maybe video1 this is important for configuration.

So we’re ready to test – lets grab our first video frame:

OpenELEC:~ # LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/storage/hyperion/bin /storage/hyperion/bin/hyperion-v4l2 --screenshot
V4L2 width=720 height=576
V4L2 pixel format=YUYV
V4L2 grabber signal threshold set to: {0,0,0}
V4L2 grabber started
V4L2 grabber stopped

The result should be the screenshoot.png file – you can use the Pictures menu in  OpenELEC to view it or transfer via Filezilla to PC and view it there.

If you don’t have input video the picture is going to look like this:

Brak sygnału Composite
No Composite signal

If the composite input signal is present and OK you will get a greenish picture – just like form the first days of television:

Niezły domyślny sygnał Composite
Good quality Composite signal

So it works! Don’t worry about the greenish tint we will get rid of with by decimating the frame – we don’t need such precision. Now the part of the kernel that takes care of the grabbing is called Video 4 Linux 2 or simply V4L2. This is enough to make it running – please add the new section of the configuration to /storage/.config/hyperion.config.json but before last bracket:

 /// Configuration for the embedded V4L2 grabber
    ///  * device          : The V4L2, you would like to use [default="/dev/video0"]
    ///  * input           : Input of the V4L2 you're going to use [default=0]
    ///  * standard        : Standard Video (no-change/PAL/NTSC) [default="no-change"]
    ///  * width                : Width of the frame for V4L2  [default=-1]
    ///  * height               : Height of the frame  [default=-1]
    ///  * frameDecimation      : Decimation factor klatki [default=2]
    ///  * sizeDecimation       : Decimation value [default=8]
    ///  * priority             : Hyperion priority channel [default=800]
    ///  * mode                 : 3D mode to use 2D/3DSBS/3DTAB (note: no autodetection) [default="2D"]
    ///  * cropLeft             : Cropping from the left [default=0]
    ///  * cropRight            : Cropping from the right [default=0]
    ///  * cropTop              : Cropping from the top [default=0]
    ///  * cropBottom           : Cropping from the bottom [default=0]
    ///  * redSignalThreshold   : Signal threshold for the red channel between 0.0 and 1.0 [default=0.0]
    ///  * greenSignalThreshold : Signal threshold for the green channel between 0.0 and 1.0 [default=0.0]
    ///  * blueSignalThreshold  : Signal threshold for the blue channel between 0.0 and 1.0 [default=0.0]
    "grabber-v4l2" :
    {
        "device" : "/dev/video0",
        "input" : 0,
        "standard" : "PAL",
        "width" : 360,
        "height" : 240,
        "frameDecimation" : 2,
        "sizeDecimation" : 8,
        "priority" : 1000,
        "mode" : "2D",
        "cropLeft" : 26,
        "cropRight" : 27,
        "cropTop" : 20,
        "cropBottom" : 20,
        "redSignalThreshold" : 0.1,
        "greenSignalThreshold" : 0.1,
        "blueSignalThreshold" : 0.1
    },

Now lets allow OpenELEC to use remote controll – menu Settings->Services->Remote Control->Allow other programs to control remotely. This is crucial to tell OpenELEC that we’re in menu or we’re palying a movie using OpenELEC – which will use the proper source for LED control.

Let’s test then! The effect should be visible while you’re in  OpenELEC or raspbmc main menu (so right after start), but please check if Hyperion, is grabbing the frames while in menu. To turn of the grabbing – go to   hyperion.config.json – and change –  “grabMenu” : false,:

"xbmcVideoChecker" :
    {
        "xbmcAddress" : "127.0.0.1",
        "xbmcTcpPort" : 9090,
        "grabVideo" : true,
        "grabPictures" : true,
        "grabAudio" : true,
        "grabMenu" : false,
        "grabScreensaver" : false,
        "enable3DDetection" : true
    },


What’s the stress for Rpi 1 ? Using 96 LED,  Raspberry PI B 256MB (the oldest model available) looks like this:

 293     1 root     R     397m106.4   0 63.9 /usr/lib/xbmc/xbmc.bin --standalone -fs --lircdev /run/lirc/lircd
 401   400 root     S    72328 18.9   0 17.1 /storage/hyperion/bin/hyperiond /storage/.config/hyperion.config.json

But what if you no longer have Composite/AV output from your other devices ? The answers is simple – use HDMI splitter and HDMI to AV converter.



Light effect for Raspberry Pi based media player – raspbmc or OpenELEC/LibreELEC

Raspberry Pi can be easily used as media player. We can choose from many variations, but the one I’d like to focus on is Raspbmc based on XBMC. Raspbmc with hyperion has ability to drive RGB LED strip or daisychain using Raspberry Pi. The important part is that it will lit the RGB LEDs accordingly to the screen borders in realtime. It’s used to make the visible part of the picture more vivid and extend it beyond the display, and it’s simply quite cool:

So, the effect is interesting, but … it doesn’t bother you ? Will my eyes hurt, or am I going to be distracted by it ? What about fast changing scenes ? Well in fact – no worries, you’ll forget about it in 5 minutes, mostly becasue it effects only perephrial vision. Try it out yourself! As it turns out it’s very important to use original Pi board, since Piotr (more in comments) had bad experience with clones – there’s no communication with LEDs!

Let’s begin, our system will not require any soldering!

List of material

  • 1 piece of Raspberry Pi 2,3 or 4B: RaspberryPi shield
  • 10 jumper cables that fit PINs in Raspberry Pi, you’ll need two male-female and two female-female ones: P2210009-small
  • 1 power source 5V 2 to 6A based on the length and type of LED strip. It’s going to power both LED strip and Raspberry Pi.
  • 1 socket with ability to fit the power source and attach jumper cables: OLYMPUS DIGITAL CAMERA
  • 1 piece of RGB LED strip with  WS2801 chip or other form of RGB LED WS2801 enabled “x-mass liights”. 2 meters strip will fine for 24-36″ TV, while 3 metrs will last for 42-46. You might also find strips using GRB configuration, as one of the commenter noticed – software can also support such configuration – see the Software section and HyperCon

Connecting

CAUTION:  RGB LED strip with WS2801 chips is powered via 5V power supply (PSU) and usually will need about 7 Watts  per meter, so be very carefull when calculating the PSU! We’re going to connect power for Raspberry Pi using it’s PIN – not via USB. Why ? Most of the problems with strip comes when using separate PSUs and it also allows to use just one powerfull PSU, without worrying about USB cable with thin lines. But the downside is that we will be bypassing the internal fuse in Raspberry Pi, so choose a very stable PSU!

Should you choose strip or xmass lights ? Each have pros and cons, but let’s deal with that later.

Lets start by preparing our power socket for connection to RaspberryPI and the LED strip:
Connecting Power Wires
The important step now is to connect data & clock signals to proper end of the strip (look at the arrow):

  • PIN 23 – connect to CLOCK on the stip (abbrv. CL)
  • PIN 19 – connect to DATA on the strip (abbrv. DO)
  • PIN 2 – the +5V – to PSU
  • PIN 6 – the GND – to PSU

Let’s properly wire the power to Raspberry Pi:
2. Power Pins Connected
Same wiring for the LED strip:
3. Connected LED Strip
And finally – clock and data wires connected to LED strip:
4. Everything is Connected
Now STOP:  Did you connect the data & clock lines on the correct end of the strip?

Our cabling should looke like this:

Chip WS2801

Software

raspbmc

Let’s start with raspbmc distribution – first we need to log in via ssh to raspbmc running on Pi. User/password are the same as in default Rasbian distribution pi and raspberry (as of January 2014).
Now let’s prepare to install ‘hyperion’.

Wait, what is ‘hyperion’ ?

Well it turns out that raspbmc already includes the support for  RGB LED strips – it’s called  ‘boblight’. This is a very well working software, but it happens to be slow – when using more LEDs (i.e. 150 when using 3 meters strip). So let’s replace it by better and faster software – hyperionem.

First of all we need to install proper libraries to build the software:

sudo apt-get update
sudo apt-get install libqtcore4 libqtgui4 libqt4-network libusb-1.0-0 libprotobuf7 ca-certificates

Now let’s turn off boblight:
sudo /sbin/initctl stop boblight

All that’s left is to install hyperion:
wget -N https://raw.github.com/tvdzwan/hyperion/master/bin/install_hyperion.sh
sudo sh ./install_hyperion.sh

Let’s test the connection to the strip – lit some LEDs!
hyperion-remote --priority 50 --color red --duration 5000

Now all you need to do is to configure hyperion according to your LED positions and quantity and enjoy the view!
Hyperion manual gives us Java program (), which looks like this:

hypercon_mainscreen_01

So it’s quite easy to select proper number of LED, their placement, clockwise orientation, etc. It’s also the place to check right configuration of the color coding on the strip – RGB or GRB.
Leave all other to defaults and save the file as hyperion.conf.json

Now using scp or FTP – upload the hyperion.config.json file to Raspberry Pi running RASPBMC. Then, login via ssh onto RASPBMC and issue:

sudo cp hyperion.config.json /etc/hyperion.config.json

That’s it!

OpenELEC

Now, OpenELEC is very XBMC-centric distribution, with little overhead of operating system. This comes down to simpler installation, but still I’d like you to go and read the raspbmc paraghraph, as we will need it for configuration.

Hyperion installation is similar, after logging in via SSH – you’ll get direct root access with root password openelec – you need to issue in console:

curl -L --output install_hyperion.sh --get https://raw.githubusercontent.com/tvdzwan/hyperion/master/bin/install_hyperion.sh
sh ./install_hyperion.sh

Next please store your config file here:

/storage/hyperion/effects

That’s it!

How to uninstall

But what if we would like to return to boblight and uninstall hyperion ?

sudo /sbin/initctl start boblight

and:
sudo /sbin/initctl stop hyperion
rm /etc/init/hyperion.config.json

SDR using Raspberry Pi: Tracking airplanes above our heads – recieving ADS-B data with visualization using DVB-T dongle

Raspberry Pi SDR

 

Note: There’s a newer version of this entry – updated with latest tests, much easier to install: https://blog.jokielowie.com/en/2017/01/sledzimy-samoloty-cz-2-ads-b-i-mlat-najtanszy-odbiornik-dla-flightaware-oraz-flightradar-24/.

Last time I’ve shared with you the Raspberry Pi “computer for a buck”. Unless you’re are learning Python or playing Minecraft – or better – you’re already bored – I have a new thing fro you. It’s a very nice idea that you can use as your graduation or school project – and it’s all about tracking airplanes high in the sky. From hardware point – you will need a DVB-T/DAB USB dongle – quite easy to obtain and also – quite cheap. We will be aiming to build a very cheap platform to send data back to Flight Radar 24 (and for us too), which gives us premium access and increases coverage of this particular service. So what’s it all about ?

  • spotting – it’s basically taking photos of the airplanes (regular airlines) that are travelling at RNAV. To take a photo – you need to know exactly when they are gonna fly-by and you need to know who’s flying – where to, where from etc.
  • receiving radio signal – we need to receive a specially modulated signal that airplanes are sending and interpret it – I’ll get back to this
  • drawing on the map the up to date position of the airplane and sharing this over local network
  • you can also try to listen to classic FM radio
  • as well as CB, TAXI corporations chatter
  • tracking amateur baloons that go up high into stratosphere
  • if you live near the airport you can also listen to chatter just like LiveATC.net does
  • basically listening to any radio from 50MHz to 2GHz, and demodulating digital signals
  • feeding FlightRadar24 with data and getting premium access

Continue reading SDR using Raspberry Pi: Tracking airplanes above our heads – recieving ADS-B data with visualization using DVB-T dongle

RaspberryPI – best & cheapest computer out there for everybody

pi-unbox There’s no doubt that non-profit organization behind http://www.raspberrypi.org/, the $25  computer (well let’s just say $35 + some xtras) had a huge success and is the next big thing for every computer enthusiast. Raspberry Pi is a full blown computer based on the ARM  architecture  that allows to run different sorts of operating systems. Today we have a choice of many Linux distributions, and the one to start and one to stay with is the Rasbian, based on another famous Linux distribution – the Debian. This little computer is designed in a very smart way allowing you to display video both on Composite only monitors (just back when I had Atari ST in the ’90, and every other Atari/Amstrad/Commodore C4 or Amiga) but also HDMI output, that you can optionally configure to work with any screen. Most HDMI->VGA converters do also work just fine. Raspberry Pi is now shipped in few variants – the classic 512MB RAM in model B is only available as used (just as the A model, that lacked Ethernet port), but previously they had – A i B just 256MB RAM. Today as of 2015 we have still B+ and A+ models – redesigned to allow better power management and distribution as well as the factor of outputs. The flagship today is of course the Raspberry Pi 2 and 3 – 1GB RAM, four cores and backward compatibility, while the version 3 has also WiFi and BLuetooth (LE) onboard, and the cores have been updated to A53 1,2GHz, 64-bit.  To continue the sound is also being transferred over  HDMI, which allows for decoding – but also you can choose analog (it’s on the jack along with composite video). For multimedia player enthusiast – this device is AVC hardware-decoding capable – way up to 1080p with sound – but there’s not default MPEG-2 license – you can order it separately. The original A, A+,  B and B+ had a CPU horsepower comparable to old Pentium II 350MHz, but don’t forget that GPU is 1080p capable. Just to start Raspbery Pi offers: Continue reading RaspberryPI – best & cheapest computer out there for everybody