Part 6 - Buttons (switches)

In the Pi-Home project, you can use any standard buttons and switches for 230 V or special ones for 24 V or 12V. In smart homes, instead of switches (classic off/on positions) the buttons are preferred (return to original position after pressing) . The reason is obvious, we can attach more different functions to the button (repeated pressing, holding, etc). In our realizations we have a very good experience with classic 230V buttons from Hager/Berker, but of course it is possible to use any other brands.

What we'll need

Part 2 - OpenHAB na Raspberry Pi

230V buttons - design is up to you, Hager/Berker recommended

Arduino Mega with Ethernet Shield (DIN holder)

Krone LSA terminals


Power and scheme

We recommend to wire an UTP cable to the buttons in smart homes. CAT5 or CAT6 are enough. Every UTP/FTP has 8 wires. For one button we need only 2 wires (DATA and GND), for two buttons 3 wires, for four buttons 5 wires. You can use one UTP cable and connect more buttons, but be sure to keep a reserve, you never know when will be needed. The GND is always connected together and all GNDs are connected to the grounding KRONE terminal - in the picture the red terminal (it has all pins connected). Second contacts of the buttons are connected to the classic white KRONE terminal and individual pins are then connected from here to the pins of the Arduino Mega board. It goes through the KRONE terminal because of the possibilities of changes and better overview of cabling. Of course you can lead UTP wires directly to the Arduino pin, but it can become rigind and messy. More than 50 buttons or 25 two-buttons can be connected to one Arduino Mega board etc.

Pi-Home - zapojení vypínačů OpenHAB


The aim is to send us the information via the MQTT protocol that the press of the button has occurred (connection of DATA and GND). If we hold the button, the information about the press will be repeated after the selected time delay, e.g. after half a second. Sketch of the model house buttons can be found below. The information about the press is captured via MQTT in OpenHAB where the update of the given button in the list according to the specific message of MQTT takes place. In OpenHAB the expiration (automatic shutdown) is set to 1s. The last step is then to set up rule (Rules) that perform the action when the value of the given button is updated - what action to take.

Sample sketch

In this simple sketch we are using libraries SPI.h, Ethernet2.h and PubSubClient.h. In the directory for Arduino there must be a Makefile (filename) with the contents below. Here is what libraries we will load and where they are located. You can search for libraries on the internet and download them. Furthermore, in Makefile there is an specification of Arduino, I use a specific name, as Arduino reports on the path /dev/serial/by-id/usb-Arduinoxxxx . Simply go to the folder and try to plug in/disconnect the given Arduino and record what name will appear here. Copy the device name to Makefile. In sketch there are declared variables (pins) on which individual buttons are attached. Pressing the button causes the connection of the Arduino DATA pin with GND and sending MQTT with the information that the button has been pressed. The Item of the given button is updated in OpenHAB and the rule is called if it is defined. The Arduino sketch pattern and an example of the rule for OpenHAB can be found below:

Example "Makefile"

ARDUINO_DIR = /usr/share/arduino
BOARD_TAG    = mega2560
ARDUINO_PORT = /dev/serial/by-id/usb-Arduino__www.arduino.cc__0042_85531303630351119291-if00 (replace with your ID)
USER_LIB_PATH = /usr/share/arduino/libraries
ARDUINO_LIBS = Ethernet2 SPI pubsubclient
include /usr/share/arduino/

Example sketch "ArduinoPA3.ino"

 Arduino 3 (PA03)
  - connects to an MQTT server
  - publishes "hello world" to the topic "pihome"
  - subscribes to the specific topic pihome/xxx
  - sends a specific "on"/"off" to the specific topic when a specific button is pressed
  - multiple arduino's with same generic sketch can run parallel to each other
  - multiple arduino's need each to have a unique ip-addres, unique mac address and unique MQTT client-ID
  - tested on arduino-mega with W5500 ethernet shield


#include <SPI.h>
#include <Ethernet2.h>
#include <PubSubClient.h>

// Set relay variables to Arduino digital pins

// pin 0 RX
// pin 1 TX
// pin 4 (SS for SD Card)
//B111 has underscore because B111 variable already exist in some of the library :D and PIN 5 is not usable as well
int B111_ = 39;
int B112 = 6;
int B121 = 7;
int B122 = 8;
int B131 = 9;
// pin 10 (SS for Ethernet)
int B132 = 11;
int B133 = 12;
int B134 = 13;

int B141 = 2;
int B142 = 3;
int B151 = 14;
int B152 = 15;
int B153 = 16;
int B154 = 17;
int B155 = 18;
int B156 = 19;
// pin 20 SDA
// pin 21 SCL

int B157 = 22;
int B158 = 24;
int B159 = 26;
int B160 = 28;
int B161 = 30;
int B162 = 32;
int B201 = 34;
int B202 = 36;
int B211 = 23;
int B212 = 25;
int B213 = 27;
int B214 = 29;
int B221 = 31;
int B222 = 33;
int B223 = 35;
int B224 = 37;          
int B231 = 38;
int B232 = 40;
int B233 = 42;
int B234 = 44;
int B241 = 46;
int B242 = 47;
int B243 = 48;
int B244 = 49;
//int rez = 39;
//int rez = 41;
//int rez = 43;
//int rez = 45;

// pin 50 (SS for Ethernet)
// pin 51 (MOSI)
// pin 52 (MISO)
// pin 53 (SCK)

//A0 (D54)
// int Rez = 54;
// int Rez = 55;
// int Rez = 56;
// int Rez = 57;
// int Rez = 58;
// int Rez = 59;
// pin 60 inaccesibble
// int Rez = 61;

//int rez = 63;
//int rez = 64;
//int rez = 65;
//int rez = 66;
//int rez = 67;
//int rez = 68;
//int rez = 69;
//A15 (D69)

// Set variables to act as virtual switches
// Set variable values initially to LOW (and not HIGH)
// HIGH or LOW as defautl is based on Relay Board               


// Arduino MAC address must be unique for every node in same network
// To make a new unique address change last letter
// Arduino 3
byte mac[]    = { 0xCC, 0xFA, 0x0B, 0xCC, 0x19, 0x01 };  

// Unique static IP address of this Arduino 1
IPAddress ip(10,0,0,173);

// IP Address of your MQTT broker (OpenHAB server)
byte server[] = { 10, 0, 0, 24 };

// Handle and convert incoming MQTT messages ----------------------------------------

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived (no messages expected though)

// Initiate instances -----------------------------------

EthernetClient arduino3;
PubSubClient client(server, 1883, callback, arduino3);

void setup(){
  Ethernet.begin(mac, ip);
  if (client.connect("arduino3", "openhabian", "openhabian")) {
    client.publish("pihome", "Hello world - here Arduino PA03 with IP");
      pinMode(B111_,   INPUT);
 digitalWrite(B111_,   HIGH);
      pinMode(B112,   INPUT);
 digitalWrite(B112,   HIGH);
      pinMode(B121,   INPUT);
 digitalWrite(B121,   HIGH);
      pinMode(B122,   INPUT);
 digitalWrite(B122,   HIGH);
      pinMode(B131,   INPUT);
 digitalWrite(B131,   HIGH);
      pinMode(B132,   INPUT);
 digitalWrite(B132,   HIGH);
      pinMode(B133,   INPUT);
 digitalWrite(B133,   HIGH);
      pinMode(B134,   INPUT);
 digitalWrite(B134,   HIGH);
      pinMode(B141,   INPUT);
 digitalWrite(B141,   HIGH);
      pinMode(B142,   INPUT);
 digitalWrite(B142,   HIGH);
      pinMode(B151,   INPUT);
 digitalWrite(B151,   HIGH);
      pinMode(B152,   INPUT);
 digitalWrite(B152,   HIGH);
      pinMode(B153,   INPUT);
 digitalWrite(B153,   HIGH);
      pinMode(B154,   INPUT);
 digitalWrite(B154,   HIGH);
      pinMode(B155,   INPUT);
 digitalWrite(B155,   HIGH);
      pinMode(B156,   INPUT);
 digitalWrite(B156,   HIGH);
      pinMode(B157,   INPUT);
 digitalWrite(B157,   HIGH);
      pinMode(B158,   INPUT);
 digitalWrite(B158,   HIGH);
      pinMode(B159,   INPUT);
 digitalWrite(B159,   HIGH);
      pinMode(B160,   INPUT);
 digitalWrite(B160,   HIGH);
      pinMode(B161,   INPUT);
 digitalWrite(B161,   HIGH);
      pinMode(B162,   INPUT);
 digitalWrite(B162,   HIGH);
      pinMode(B201,   INPUT);
 digitalWrite(B201,   HIGH);
      pinMode(B202,   INPUT);
 digitalWrite(B202,   HIGH);
      pinMode(B211,   INPUT);
 digitalWrite(B211,   HIGH);
      pinMode(B212,   INPUT);
 digitalWrite(B212,   HIGH);
      pinMode(B213,   INPUT);
 digitalWrite(B213,   HIGH);
      pinMode(B214,   INPUT);
 digitalWrite(B214,   HIGH);
      pinMode(B221,   INPUT);
 digitalWrite(B221,   HIGH);
      pinMode(B222,   INPUT);
 digitalWrite(B222,   HIGH);
      pinMode(B223,   INPUT);
 digitalWrite(B223,   HIGH);
      pinMode(B224,   INPUT);
 digitalWrite(B224,   HIGH);
      pinMode(B231,   INPUT);
 digitalWrite(B231,   HIGH);
      pinMode(B232,   INPUT);
 digitalWrite(B232,   HIGH);
      pinMode(B233,   INPUT);
 digitalWrite(B233,   HIGH);
      pinMode(B234,   INPUT);
 digitalWrite(B234,   HIGH);
      pinMode(B241,   INPUT);
 digitalWrite(B241,   HIGH);
      pinMode(B242,   INPUT);
 digitalWrite(B242,   HIGH);
      pinMode(B243,   INPUT);
 digitalWrite(B243,   HIGH);
      pinMode(B244,   INPUT);
 digitalWrite(B244,   HIGH);

long lastReconnectAttempt = 0;

boolean reconnect() {
  if (client.connect("arduino3", "openhabian", "openhabian")) {
    // Once connected, publish an announcement...
    client.publish("pihome","Arduino 3 - reconnected");
  return client.connected();

//Priprava tlacitek
void loop()

// read the pushbutton input pin:
if (digitalRead(B111_)==LOW){
if (digitalRead(B112)==LOW){
if (digitalRead(B121)==LOW){
if (digitalRead(B122)==LOW){
if (digitalRead(B131)==LOW){
if (digitalRead(B132)==LOW){
if (digitalRead(B133)==LOW){
if (digitalRead(B134)==LOW){
if (digitalRead(B141)==LOW){
if (digitalRead(B142)==LOW){
if (digitalRead(B151)==LOW){
if (digitalRead(B152)==LOW){
if (digitalRead(B153)==LOW){
if (digitalRead(B154)==LOW){
if (digitalRead(B155)==LOW){
if (digitalRead(B156)==LOW){
if (digitalRead(B157)==LOW){
if (digitalRead(B158)==LOW){
if (digitalRead(B159)==LOW){
if (digitalRead(B160)==LOW){
if (digitalRead(B161)==LOW){
if (digitalRead(B162)==LOW){
if (digitalRead(B201)==LOW){
if (digitalRead(B202)==LOW){
if (digitalRead(B211)==LOW){
if (digitalRead(B212)==LOW){
if (digitalRead(B213)==LOW){
if (digitalRead(B214)==LOW){
if (digitalRead(B221)==LOW){
if (digitalRead(B222)==LOW){
if (digitalRead(B223)==LOW){
if (digitalRead(B224)==LOW){
if (digitalRead(B231)==LOW){
if (digitalRead(B232)==LOW){
if (digitalRead(B233)==LOW){
if (digitalRead(B234)==LOW){
if (digitalRead(B241)==LOW){
if (digitalRead(B242)==LOW){
if (digitalRead(B243)==LOW){
if (digitalRead(B244)==LOW){
  //Wait 50 ms and repeat
  if (!client.connected()) {
    long now = millis();
    if (now - lastReconnectAttempt > 5000) {
      lastReconnectAttempt = now;
      // Attempt to reconnect
      if (reconnect()) {
        lastReconnectAttempt = 0;
  } else {
    // Client connected

// End of sketch ---------------------------------


In OpenHAB you should already have the buttons ready according to the tutorial. Try adding a rule. Below is a model rule for the B111 button and the L111 light on/off.

configuration: {}
  - id: "2"
      itemName: B111
      state: ON
      previousState: OFF
    type: core.ItemStateChangeTrigger
conditions: []
  - inputs: {}
    id: "1"
      type: application/vnd.openhab.dsl.rule
      script: |2
         if(L111.state == ON){   
         sendCommand(L111, OFF)
         sendCommand(L111, ON)
    type: script.ScriptAction

That's it! Now you can test some buttons. This solution can be extended indefinitely. We have experience with seamless installation of up to 200 buttons in a family home.

Add comment

Nejlepší články z blogu

Nilan VP18 vs OpenHABRating: 

We provide tutorial for the connection of the active Nilan recuperation unit, in our case the VP18 K WT type with OpenHAB 3. By communicating via Modbus it is possible to read the whole range of values, but mainly also to control the unit through OpenHAB. This allows us to remotely or programmatically change the performance and ventilation settings depending on the mode (night, cooking, etc.)

PiHome - Chytry dum mereni spotreby elektriny
Energy meter in smart homeRating: 

We are providing a simple step-by-step tutorial how to install home energy meter. We did not find any MQTT device, so we buy Aeotec Home Energy Meter as we are already have a Z-Wave controlller in our smart home.

Centrální tablet pro chytrý dům
Wall tablet to controll your smart homeRating: 

Wall tablet or older smartphone is perfectly suited as a central indicator of the state of the home, as well as controlling it. It usually replaces the video doorbell and as example, you can play your favorite music or read recipes on it during cooking in your kitchen. In the article, we describe a functional example of such a "wall" tablet and its settings in so-called kiosk mode so, display doesn't run at night and only reacts when someone is nearby.

Jablotron and smarthome synchronization
Connecting Jablotron with OpenHABRating: 

In this article we will show you how to install Jablotron binding in OpenHAB. This binding helps us to synchronize states from Jablotron alarm to OpenHAB. Typically, you can force to switch off lights, close water pipe and circulation, switch off sockets or whatever you want to do when you leave home and set alarm.

Advanced disk cloningRating: 

The classic disk cloning tool in linux is the dd command. This command creates a image of disk or extracts the contents from the image to disk. The dd tool needs to be handled carefully and you need to understand its use. We'll show a more advanced technique of creating a disc image that contains unallocated space. A classic example is when I have a Raspbian operating system installed on a 250 GB disc, but there's only a tiny boot partition (e.g. sda1) and my own operating system partition (sda2) which together have 15 GB. Instead of saving a 250 GB image, I would like to save only two partitions with 15 GB, so that the image is smaller and can be expanded to 16 or 32 GB flash.

iOS OpenVPN Connection SettingsRating: 

In this article you will find detailed instructions on how to connect to OpenVPN from iOS devices.

Android OpenVPN Connection SettingsRating: 

In this article you will find detailed instructions on how to connect to OpenVPN on Android.

OpenVPN on MikroTik routersRating: 

If you have a smart-home, NAS, etc., it's a good idea to think about safe access to your network when you are not at home. Currently I recommend commercial router like Turris or a cheaper option of MikroTik routers. In the other hand, Raspberry Pi can be used as OpenVPN server too. In this article, we will show how to set up secure access to your home network from the internet using OpenVPN on MikroTik routers.

Video intercom Doorbird in OpenHABRating: 

Native support for OpenHAB is provided by video intercom by Doorbird company. The solution is powered and connected by a UTP cable and has its own IP address to make it easier for you to install at the gate. OpenHAB is then able to import images from the intercom, visit history, manage gate opening on the button and by the phone, do not disturb feature during night, alarm feature etc. In the instructions we guide you how to use OpenHAB with Doorbird D101.