Category Archives: Linux

Linux

Raspberry Pi Zero – updating the gate opener with water meter OCR

Introduction

My Raspberry Pi 1B+ has been opening garadge and letting me know of the conditions for work nearly 4 years. Recently I’ve decided to replace this 2014 small board computer (SBC) with newer version. I’ve also looked at better (the principle Keep it Small and Simple – KISS) solutions that became available – i.e. ESP Easy on ESP8266 (controlling the relay and reading the contact switch is not too much for this platform, even with addition of environmental sensors), but after some thinking – the option of leaving the Raspberry Pi in place won because of one additional perk that I wanted to handle – a classic water meter. Yes, such an old analogue water meter. Fun!

Raspberry Pi Zero Front
Raspberry Pi Zero Front

Continue reading Raspberry Pi Zero – updating the gate opener with water meter OCR

Tracking gliders, airplanes, helicopters and baloons, part 3 – FLARM

We already know how to track airplanes using DVB-T USB dongle and Orange Pi or Raspberry Pi. Let’s move on to a new system that allows tracking of light aircrafts:

  • gliders
  • helicopters
  • baloons
  • smaller airplanes
Sender/reciever for glider (photo by: Sebastian Lampart)
Sender/reciever for glider (photo by: Sebastian Lampart)

FLARM – is a collision avoidance system for light aircrafts, popularized and approved by EASA, gaining traction, used mainly on gliders in Poland and Europe. We’re not going to use it to avoid collision – but to receive position and track light aircrafts – so we can share them on the OGN procest’s map – OPEN GLIDER. There are few station in Poland – let’s build a more robust network, so that gliders can be safer and their records – recorded.

What will you need?

Hardware:

  • Orange Pi Zero or RPI Zero W” rel=”noopener” target=”_blank”>Raspberry Pi Zero W
  • DVB-T USB Dongle. Look for the upgraded version in blue chassis – AKA “DVB-T RTL2832U+R820T2” – it offeres better sensitivity in interesting frequency – and will give you extra range. The most important part in that equation is the new R820T2 (version ‘2’) and of course the included antenna
  • Micro SD – could a cheap 8G for $4, but better is to use Sandisk Extreme/Ultra – at least 4GB
  • Power supply – at least 1.5 to 2A @5V with micro USB (i.e.: old charger from cellphone)
  • Two small heatsinks (radiators) for CPU and controller – for Orange Pi Zero
  • ALTERNATIVE: Raspberry Pi 2 or 3 – they have enough of CPU power, but triple the price
  • Antenna 868MHz. The one you got with USB dongle is OK for start – gives me around 50km of range without any modification. You can cut it to the lenght of single segment

Software:

    li>Armbian for Orange Pi – the best at this point is to use the Xenial NIghtly, developer/beta version, with 4.9.x kernel from https://www.armbian.com/orange-pi-zero/
  • ogn-decode i ogn-rf – software to be downloaded – ARM CPU version
  • Free ports on the Orange Pi/Raspberry Pi – 8080 and 8081 – if there’s nothing else there installed – they should be free to use.
Cokpit with FLARM  (photo by: Sebastian Lampart)
Cokpit with FLARM  (photo by: Sebastian Lampart)

Installation

First – if not installed already – we need SDR and some system libraries as well as binaries from glidernet.org:

cd /home/pi
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install rtl-sdr
sudo apt-get install libconfig-dev fftw3-dev libjpeg-dev libconfig9 libjpeg8

To allow usage of the raw feed from airplanes we need to turn off the automatic loading of the DVB-T:

nano /etc/modprobe.d/rtl-sdr-blacklist.conf

paste:

blacklist dvb_usb_rtl28xxu
blacklist e4000
blacklist rtl2832
blacklist r820t
blacklist rtl2830
blacklist dvb_usb_rtl28xxu
blacklist dvb_usb_rtl2832u

Reboot the Orange Pi/Raspberry Pi
Now, lets download from glidernet.org the correct file, unpack it:

wget http://download.glidernet.org/arm/rtlsdr-ogn-bin-ARM-latest.tgz
tar xvzf rtlsdr-ogn-bin-ARM-latest.tgz

Create a FIFO to allow communication between both parts of the OGN software:

cd rtlsdr-ogn
mkfifo ogn-rf.fifo

View from glider (photo by: Sebastian Lampart)
View from glider (photo by: Sebastian Lampart)

Next, calibration of the USB dongle – cheap DVB-T USB tuners are not always good at tuning with high precision to given frequency – we need to correct the skew. To do that use the gsm_scan, that will give the ppm and gain correction values:

cd rtlsdr-ogn
./gsm_scan --ppm 50 --gain 20

Change values so that the deviation is around 10ppm (check the best GSM frequency GSM) – write them down.
Create own configuration based on the example:

cp Template.conf EPXX_OGN.conf
nano EPXX_OGN.conf

The example file contents based on my tuner: enter CORRECT values for FreqCorr, GSM: CenterFreq Gain – based on gsm_scan:

RF:
{
  FreqCorr = +87.060;          # [ppm]      "big" R820T sticks have 40-80ppm correction factors, measure it with gsm_scan

  GSM:                     # for frequency calibration based on GSM signals
  { CenterFreq  = 938.4;   # [MHz] find the best GSM frequency with gsm_scan
    Gain        =  25.0;   # [dB]  RF input gain (beware that GSM signals are very strong !)
  } ;
} ;

Position:
{ Latitude   =   +51.1514; # [deg] Antenna coordinates
  Longitude  =   +18.4642; # [deg]
  Altitude   =        150; # [m]   Altitude above sea leavel
  GeoidSepar =         48; # [m]   Geoid separation: FLARM transmits GPS altitude, APRS uses means Sea level altitude
} ;

APRS:
{ Call = "EPXX1";     # APRS callsign (max. 9 characters)
                      # Please refer to http://wiki.glidernet.org/receiver-naming-convention
} ;

Finally – start in background two programs using very basic (or other by your choosing method) thanks to cron:
Create two files:

cd /home/pi
nano /home/pi/rtlsdr-ogn/run_ogn-rf

with:

#!/bin/bash
cd /home/pi/rtlsdr-ogn
/home/pi/rtlsdr-ogn/ogn-rf /home/pi/rtlsdr-ogn/EPXX_OGN.conf

Add rights to execute:

chmod 755 /home/pi/rtlsdr-ogn/run_ogn-rf

Second file:
nano /home/pi/rtlsdr-ogn/run_ogn-decode

with:

#!/bin/bash
cd /home/pi/rtlsdr-ogn
/home/pi/rtlsdr-ogn/ogn-decode /home/pi/rtlsdr-ogn/EPXX_OGN.conf

Rights to execute:

chmod 755 /home/pi/rtlsdr-ogn/run_ogn-decode

Now edit your crontab:

crontab -e

Wklejamy:

*/5 * * * * if [ `pgrep ogn-rf | tr '\n' ' '| awk '{print $1}'` ]; then echo "ogn-rf works"; else screen -dmS ognrf /home/pi/rtlsdr-ogn/run_ogn-rf; fi > /tmp/ognrf.log 2>&1
*/5 * * * * if [ `pgrep ogn-decode | tr '\n' ' '| awk '{print $1}'` ]; then echo "ogn-decode works"; else screen -dmS ogndecode /home/pi/rtlsdr-ogn/run_ogn-decode; fi > /tmp/ogndecode.log 2>&1

Of course in our example we’re using username “pi”.
Verify the operation by attaching screen: screecn -r XXX, or enter the URLs in we browsers: http://ADDRESS_OF_PI:8080/ oraz http://ADDRESS_OF_PI:8081/. Your station will be listed at: http://ognrange.onglide.com/, along with the range. Since FLARM changes – check every 6 months for new version.

That’s it!

Glider's tail (photo by: Sebastian Lampart)
Glider’s tail (photo by: Sebastian Lampart)

Raspberry Pi and GSM network- sending SMS, connecting to the Internet (IoT)

This time I’m going to connect to our Raspberry Pi a GSM module. Why? Internet of Things, in short IoT, as it was noted at Krakow’s PLNOG17  by Marcin Aronowski involves and depends on the network which should be easily accessible, and the very essence of IoT is the energy efficiency. Perhaps, he said, 3G will go away but 2G but will still remain. And we do not huge pipes – just enough bandwidth. 2G connection is still the cheapest in the implementation (also due to the cost of the module and a SIM card).

Therefore, I will try to show you how easy it is to connect very cheap GSM module Neoway M590 . It is a simple device that is a GSM modem (2G).

We will learn how to send an SMS and how to connect to the Internet.

Required

Software:

  • software: picocom, gammu, ppp (accessible via standard Raspbian’s repository)
  • turning off the console redirection to serial port on Raspberry Pi

Hardware:

  • Raspberry Pi – 1B, 1A, 2B, 3B, Zero – any model with installed Raspbian (mini version is enough)
  • Neoway M590 GSM module – preferably without extra letters at the end – get a version with everything soldered and 2×7 pins
  • Multimer for voltage and power supply check for GSM module
  • Optional: DC-DC step down converter – at least rated 2A, usually allows using 5-24V on input and keeping stable 3,9V at output – used to power the GSM module

    DC-DC step down module
    DC-DC step down module

Let’s begin!

I chose the Neoway M590 GSM modem module, because it is a bit underestimated – and one of the most important things is to have proper and stable power supply. Module is not as popular as SIM800/SIM900, but – it’s very, very cheap, and after few months of trails on two modules I can say it’s stable.

Please remember, in most cases the correct voltage is 3,7-3,9V, and power rating needs to be minimum 2A.

Connecting to Raspberry Pi

Neoway GSM module has a serial port – this is the standard way to talk – via connections TX, RX. However, the default serial port on Raspberry Pi is occupied by the system console – so if you connect our module to Raspberry’s serial port you will get overflow of text data. Edit the Raspberry Pi file /boot/cmdline.txt and remove the part console=/dev/ttyAMA0,115000. Yo can us it after reboot.
Connect the DC-DC step down power converter – you can also use a battery from an unused smartphone or battery type 18650 – provided that it has a rating of 3.7V – Li Poly or Li Ion battery. If you use are using smilar DC-DC converter as on the picture – with adjustable resistor – use a screwdriver when powered with no load, connect a multimeter and turn as long as the voltage suddenly begin to fall – the set 3,9V. Now connect the GSM module and verify the voltage – sometimes cheap module does not provide stable power supply under load – as a result we have a voltage drop that must be corrected. Do the final adjustment VERY carefully – do not damage the GSM module. Here is the power scheme and the connection to the Raspberry Pi. Diagram shows the version with 2×7 pin connector plates on Neoway M590 facing the SIM card:

Schema: RPI - Neoway M590
Schema: RPI connected to Neoway M590 GSM module
Neoway M590 connection
Neoway M590 connection

As you can see this is not hard to wire, please remember the GND connections. Neoway M590 needs GND connected to BOOT to start, hence the “extra” connection to the GND of the system. Again the connections from the view of the SIM card slot:
After inserting the SIM card powering the module – the first LED should lit red, and the second should bling to indicate the GSM network connections. Here’s the final view:

The Raspberry Pi with GSM M590 module
The Raspberry Pi with GSM M590 module

Sending first SMS

To get to now the module – you will need to install this:

sudo apt-get install picocom

then issue on the console:

pi@rpi ~ $ picocom --baud 115200 /dev/ttyAMA0
picocom v1.7

port is        : /dev/ttyAMA0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        :
omap is        :
emap is        : crcrlf,delbs,

Terminal ready
AT
OK

You can exit picocom by CTR+A then Q.
At the very end, in line 21, I’ve entered the command “AT” and M590 answered “OK”. This means that we have properly (proper TX and RX) connected console. The AT commands with parameters are the natural language of modems – the old “analog” and the new GSM modems. If you’re getting “weird” characters – it’s the bad speed, and if we see what you write – it means that you haven’t deactivated the system console redirection to a serial port yet (in that case – please see above). Let’s see those commands in detail..

Let’s send the first SMS, first by picocom, and later “automatically” via gammu:

pi@rpi ~ $ picocom --baud 115200 /dev/ttyAMA0
picocom v1.7

port is        : /dev/ttyAMA0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        :
omap is        :
emap is        : crcrlf,delbs,

Terminal ready
AT+CMGF=1
OK
AT+CSMS=1
+CSMS: 1,1,1

OK
AT+CSCS="GSM"
OK
AT+CMGS="601xxx555"
> "To is TESTING. A SMS that is!"
>
+CMGS: 46

OK

What happened here ? First we setup the modem to send SMS via GSM, then you need to change the “601xxx555” to a proper number for the recipient of the SMS. Next we will get the > sign – this is where you enter your SMS. To send it – press CTRL+Z. After 3-4 seconds you should get: “+CMGS: 46” and “OK’. Check your phone!

To use it with other software and use it wisely – we can use the gammu, that allows single line SMS:

sudo apt-get install gammu

Then edit the gammu’s config:

sudo pico /etc/gammurc

insert just this:

[gammu]
device = /dev/ttyAMA0
connection = at115200

And now send your SMS like that:

pi@rpi ~ $ gammu sendsms TEXT 601xxx555 -text "To is TESTING. A SMS that is!"

Easy, ain’t it ?

Connecting to the Internet

Our module enables data transmission in GPRS mode. Combining that with the fact that we are @ 115kbps serial link, allows us to use the GPRS. Don’t forget – this is suited the task of running IoT solution, and not to use the browser in graphics mode (yet lynx, links or elinks do work without problem)
Let’s install the program to connect to the Internet. The ‘ppp’ will negotiate connection using a GSM modem, set ppp0 and append DNS entries, as well as the default route:

sudo apt-get install ppp

Create a file:

sudo pico /etc/ppp/peers/m590-gsm-internet

with:

# the script - standard to use
connect "/usr/sbin/chat -v -f /etc/chatscripts/gprs -T ****"
 # Our serial port
/dev/ttyAMA0
# The speed of the serial port.
115200
# Get the IP address from ISP.
noipdefault
# Also get DNS entries
usepeerdns
# Set the route as default to the Internet
defaultroute
# pppd will reconnect after disconnection
persist
# Don't auth the remote site
noauth
# No need to check hardware flow on serial port
nocrtscts
# No need to send control lines for modem
local

Checkout the gprs file from chatscripts – it should be exactly like this:

# You can use this script unmodified to connect to cellular networks.
# The APN is specified in the peers file as the argument of the -T command
# line option of chat(8).

# For details about the AT commands involved please consult the relevant
# standard: 3GPP TS 27.007 - AT command set for User Equipment (UE).
# (http://www.3gpp.org/ftp/Specs/html-info/27007.htm)

ABORT           BUSY
ABORT           VOICE
ABORT           "NO CARRIER"
ABORT           "NO DIALTONE"
ABORT           "NO DIAL TONE"
ABORT           "NO ANSWER"
ABORT           "DELAYED"
ABORT           "ERROR"

# cease if the modem is not attached to the network yet
ABORT           "+CGATT: 0"

""              AT
TIMEOUT         12
OK              ATH
OK              ATE1

# +CPIN provides the SIM card PIN
#OK             "AT+CPIN=1234"

# +CFUN may allow to configure the handset to limit operations to
# GPRS/EDGE/UMTS/etc to save power, but the arguments are not standard
# except for 1 which means "full functionality".
#OK             AT+CFUN=1

OK              AT+CGDCONT=1,"IP","\T","",0,0
OK              ATD*99#
TIMEOUT         22
CONNECT         ""

That’s it. Connect to the internet using:

sudo pon m590-gsm-internet

Check the logs in separate console (i.e.: tail -f /var/log/syslog)

Oct  8 23:23:13 rpi pppd[17390]: Connect: ppp0  /dev/ttyAMA0
Oct  8 23:23:13 rpi ifplugd(ppp0)[17403]: ifplugd 0.28 initializing.
Oct  8 23:23:13 rpi ifplugd(ppp0)[17403]: Using interface ppp0/00:00:00:00:00:00
Oct  8 23:23:13 rpi ifplugd(ppp0)[17403]: Using detection mode: IFF_RUNNING
Oct  8 23:23:13 rpi ifplugd(ppp0)[17403]: Initialization complete, link beat detected.
Oct  8 23:23:14 rpi ifplugd(ppp0)[17403]: Executing '/etc/ifplugd/ifplugd.action ppp0 up'.
Oct  8 23:23:14 rpi ifplugd(ppp0)[17403]: client: Ignoring unknown interface ppp0=ppp0.
Oct  8 23:23:14 rpi ifplugd(ppp0)[17403]: Program executed successfully.
Oct  8 23:23:14 rpi kernel: [701282.272157] PPP BSD Compression module registered
Oct  8 23:23:14 rpi kernel: [701282.284205] PPP Deflate Compression module registered
Oct  8 23:23:16 rpi pppd[17390]: local  IP address 10.75.12.27
Oct  8 23:23:16 rpi pppd[17390]: remote IP address 10.75.12.27
Oct  8 23:23:16 rpi pppd[17390]: primary   DNS address 89.108.202.21
Oct  8 23:23:16 rpi pppd[17390]: secondary DNS address 89.108.195.21
Oct  8 23:23:17 rpi ntpd[779]: Listen normally on 11 ppp0 10.75.52.17 UDP 123
Oct  8 23:23:17 rpi ntpd[779]: peers refreshed
Oct  8 23:23:18 rpi dnsmasq[6117]: reading /var/run/dnsmasq/resolv.conf
Oct  8 23:23:18 rpi dnsmasq[6117]: using name server 8.8.4.4#53
Oct  8 23:23:18 rpi dnsmasq[6117]: using name server  8.8.8.8#53
Oct  8 23:23:18 rpi dnsmasq[6117]: using name server  89.108.202.21#53
Oct  8 23:23:18 rpi dnsmasq[6117]: using name server  89.108.195.21#53

It works Check the ifconfig output:

ppp0      Link encap:Point-to-Point Protocol
          inet addr:10.75.12.27  P-t-P:10.75.12.27  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:116 (116.0 B)  TX bytes:191 (191.0 B)

Turning off is also easy:

sudo poff m590-gsm-internet

Logs will show:

Oct  8 23:26:40 rpi avahi-daemon[539]: Withdrawing workstation service for ppp0.
Oct  8 23:26:41 rpi ntpd[779]: Deleting interface #11 ppp0, 10.75.12.37#123, interface stats: received=0, sent=0, dropped=0, active_time=204
 secs
Oct  8 23:26:41 rpi ntpd[779]: peers refreshed

That’s it – please remember that data plan can be costly. Check your data plan!

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:

pi@raspberrypi ~/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 <vowstar@nodemcu.com>
-- 2015/02/14 sza2 <sza2trash@gmail.com> 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