How I roamed through the ioBroker barriers in the field

I will tell you my experience with ioBroker for managing barriers on a farm and integrating them into voice assistants Alice and Siri. And also, I will introduce the JetHome D1 controller and the peculiarities of its use for the ioBroker.

I will not talk in great detail, but pointing out the methods of solution, so that many who are fond of automation will be able to repeat this at home. If you are interested in the details or do not understand how a specific functionality is implemented, ask here or look for me in Telegram.

Problematic

In the agricultural park, on the territory of which my wife organized her own farm, installed barriers around the perimeter. Some.

It turned out that many cars will move without a remote control, for which these barriers need to be opened. The barriers are located far from the buildings and I really didn’t want a person with a remote control to constantly drive them to open them.

Of course, you can think of a lot of solutions. I suggested using the Telegram bot to open the barrier (in addition to the remotes). Those employees to whom the bot was issued will be able to open the barrier at the entrance, and for the rest of the arriving / leaving the barrier can be opened remotely.

Connection to the barrier

Fortunately, they connected to the solution of the issue in advance – cameras were installed at the barriers, which means there is an opportunity to gain access to the general network. The distances were long and “optics” go to the barriers, there are switches with free ethernet connectors in the cabinet.

The barriers were set in a mode when pressing a button on the remote control opened it and after a while it closed itself if there was no interference. The barriers use the CAME ZL38 control board. According to the instructions, to open the barrier, it is enough to arrange the closure of a pair of contacts on the control board (contacts 2 and 7). A dry contact relay can handle this.

Connection diagram
Connection diagram
After connecting
After connecting

I chose ethernet relay, which can receive commands via several protocols: MQTT, Modbus, TCP, UDP, etc. Additionally, it allows you to connect an input to, for example, monitor the opening of a barrier. To do this, a voltage sensor must be connected instead of the indicator lamp on the control board (pins E and 10). The relay can also be powered from the barrier control unit by connecting to 24v (pins 10 and 11).

Ethernet relay
Ethernet relay
Voltage sensor
Voltage sensor

JetHome D1 controller

To implement the Telegram bot, I planned to add a small controller to the general network and launch a bot on it that will manage barriers.

A year ago, I had a good candidate for this role – the JetHome D1 controller. http://jethome.ru/jethub-d1

JetHome D1 controller
JetHome D1 controller

This is such a server on Armbian with a ready-made harness, Ethernet, Wifi, Bluetooth, Zigbee, RS-485, 1-Wire interfaces. It also has 4 dry inputs and 3 relay outputs. It is convenient for placement in a cabinet on a DIN rail. My version had 1GB of RAM and 8GB on the card.

There are plenty of resources to control barriers, and it will also be possible to further expand the functionality to control other devices – the interfaces allow.

We put ioBroker

There were a lot of software options, but since I often use ioBroker, I decided to install it.

Minimum set of adapters for a farm
Minimum set of adapters for a farm

In order to control the network relays, it is necessary to raise the mqtt broker – therefore we install the mqtt adapter and run it in the “Server / Broker” mode.

Simple MQTT adapter settings
Simple MQTT adapter settings

The address and details of this service are specified in the relay settings and it appears in the objects:

A set of states after connecting a relay via MQTT
A set of states after connecting a relay via MQTT

To open the barrier, you need to short-circuit the relay, in my case r1. Send the command ON to topic / in / r1 and in a second we send the shutdown command.

When the barrier is opened, we will display a sign at the i1 input and this is a signal that the barrier is opening.

// подпишемся на ловлю открытия
const openhandler = on({id: "mqtt.0.dingtian.relay4207.out.i1", ack: true, val: 'ON'}, function(){
    // уведомим что открывается
    sendToTelegram("Восточный шлагбаум открывается");
    // отпишемся
    unsubscribe(openhandler);
});
setState("mqtt.0.dingtian.relay4207.in.r1", "ON");
setStateDelayed("mqtt.0.dingtian.relay4207.in.r1", "OFF", 1000);

To implement the Telegram bot, we install the Telegram adapter. And for writing the bot logic and other scripts, we add a javascript adapter.

A simple menu for opening and notification of the opening of the barrier is written in JS.

Farm bot interface
Farm bot interface

With cameras, you can do the following: start recording a short video when the barrier began to open – in order to catch someone who is near the barrier or drives up to it. Then, send the recorded video to “interested” Telegram users, but not to everyone.

Improvement (or complication)

For myself, I decided to make a slightly improved version of barriers management – to connect them to my voice assistants Alice and Siri. To do this, it was necessary to “forward” the control of devices from the farm to the dacha, where I already had assistants connected.

I solved this question by means of a VPN network to which I connect my controllers. Thus, they begin to see each other and can interact.

This can be organized in different ways:

I really liked the JetHome D1 controller and decided to install it in my country house as well, instead of the old Atom (although it was more powerful and there was 4GB of memory).

And here’s an important note! The controller only has 1GB, “and you need a lot of strength for games and growth.” By default, the ioBroker controller works in the classic mode for it: the controller itself and each adapter instance are launched in a separate isolated process. With a large number of adapters, 1GB of memory is not enough and everything will collapse.

For these purposes, ioBroker implements a new mode of launching the controller and adapters – Compact. It allows you to combine all or several adapters into a compact group and run it as a single process, thereby saving resources. But if one of the adapters of the group falls, the whole group will fall. This is how it differs from a separate launch.

Enabling Compact startup mode must be done on the controller and on adapters that support this startup mode.

Switching on compact mode on the controller:

iob compact on

Enabling compact mode on an adapter instance (after on, you can specify the number of the group into which the instances will be combined):

iob compact zigbee.0 on 0

I had to do some magic over the groups and a set of adapters to run everything I wanted to give on this controller. As a result, I transferred almost all the operating logic to it:

  • telegram bot

  • voice assistants

  • sonoff devices on tasmota firmware

  • zigbee devices, to the cc2538 module built into the controller

  • boiler control via OpenTherm

  • temperature sensors and graphing

Set of adapters for giving
Set of adapters for giving

To access the controller on the farm, you just need to connect to the same mqtt broker as the relay. We have a shared network and therefore we put the mqtt adapter in client mode, specify the address from the vpn network and settings for transferring topics.

MQTT client connection
MQTT client connection
Setting up topic broadcast
Setting up topic broadcast

Voila, all relay parameters are now available on this controller as well. You can add them to your bot or to your assistants.

Add devices for Alice

Now that the topics for managing barriers are available, let’s add their management to Alice.

For each barrier, create states that are responsible for the “open” / “closed” state.

All created states, both for Alice and Siri
All created states, both for Alice and Siri

Additionally, we will write scripts that change these states when the relay topic changes. And vice versa, we will send a command to the topic when the state changes. Here it is important not to be mistaken with the ack-confirmation, so as not to loop the changes.

on({id: "mqtt.0.dingtian.relay4207.in.r1", change: 'any'}, function(obj){
    log(`выставим состояние Шлагбаум_восточный ${obj.state.val} по топику`);
    setState("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный", (obj.state.val == 'ON'), true);
});
on({id: "0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный", ack: false, change: 'any'}, function(obj){
    log(`пожелали переключить Шлагбаум_восточный ${obj.state.val}`);
    // если попросили открыть
    if (obj.state.val) {
        setState("mqtt.0.dingtian.relay4207.in.r1", "ON");
        setStateDelayed("mqtt.0.dingtian.relay4207.in.r1", "OFF", 1000);
    } else {
        setState("mqtt.0.dingtian.relay4207.in.r1", "OFF");
    }
});

Now, in the “Devices” adapter, add devices with the “gate” type and indicate the corresponding created states for them.

Device List
Device List
Configuring device compliance and ioBroker state
Configuring device compliance and ioBroker state

And in the settings of the iot adapter for devices, matches will be found and Alice’s devices will be created, it will be enough to rename them. About connecting Alice to ioBroker you can read here or here

Devices for forwarding to Alice
Devices for forwarding to Alice

Now the devices will appear in the ioBroker skill connected to Alice:

Devices in Alice
Devices in Alice

And you can open barriers from any device where Alice is.

Add devices for Siri

To work through Homekit, we will also create auxiliary states. In this case, the states should be split by current (the current state, added an ending to the name of the state ‘_with’) and target (target state, added ‘_t’).

We will also write scripts to make the appropriate changes, when changing the topics, we set our states to the same values, and when the target-states change, we send control commands to the topics.

on({id: "mqtt.0.dingtian.relay4207.in.r1", change: 'any'}, function(obj){
    log(`выставим состояние Шлагбаум_восточный ${obj.state.val} по топику`);
    setState("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_c", (obj.state.val == 'ON') ? 0 : 1, true);
    setState("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_t", (obj.state.val == 'ON') ? 0 : 1, true);
});

on({id: "0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_t", ack: false, change: 'any'}, function(obj){
    log(`пожелали переключить Шлагбаум_восточный ${obj.state.val}`);
    // если попросили открыть
    if (obj.state.val == 0) {
        setState("mqtt.0.dingtian.relay4207.in.r1", "ON");
        setStateDelayed("mqtt.0.dingtian.relay4207.in.r1", "OFF", 1000);
        setState("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_c", 1, true);
        setState("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_t", 0, true);
        setStateDelayed("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_c", 0, true, 1000);
        setStateDelayed("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_t", 0, true, 1000);
    } else {
        setState("mqtt.0.dingtian.relay4207.in.r1", "OFF");
        setState("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_c", 1, true);
        setState("0_userdata.0.Шлагбаумы_и_ворота.Шлагбаум_восточный_t", 1, true);
    }
});

And accordingly, we add devices in the “yahka” adapter.

Configuring compliance of homekit devices and ioBroker states
Configuring compliance of homekit devices and ioBroker states

After that, we connect our virtual gateway in the Home application on the iphone and leave the configured Apple TV in the country in order to be able to control it remotely. Now, my wife asks Siri to open the desired barrier without opening Telegram and looking for the desired chat with the bot.

Similar Posts

Leave a Reply