📜 ⬆️ ⬇️

My home automation system

Hi, Habr!

The purpose of this article (cycle) is an introductory story about my home automation system, of course I did not invent it and created everything from scratch, but only collected technologies and projects that I considered most suitable for this purpose and added a little from myself. The level is amateur, but as a result everything works, people use me and do not run after me with a cudgel. He did everything himself. Constructive criticism is welcome, it will be interesting to know any opinion.

The system includes equipment and a set of programs: the automation system itself for communication of equipment and visualization, communication and telemetry for remote monitoring and updating, voice assistant from Yandex. Everything (almost everything) is open and laid out on Github .

Equipment level


The main and necessary part is a server based on the Raspberry Pi, but it can easily work on a PC with Debian or Ubuntu. Raspbian operating system. The system should work 24/7/365 so a high-quality power supply unit is needed, for example, a bp for the iPad is perfect.
')


For Remote Control, I also need the skill for a voice assistant. Yandex needs a server with a “white” IP and domain. The server is running an MQTT server, using SSL / TLS for security.

KNX support via BAOS 771-774 gateway. BAOS is the interface between the KNX bus and the LAN. Allows you to access the addresses on the bus via a web service in JSON format.

My equipment:

Projects for Arduino IDE

Wifi Controller based on Wemos D1 mini



230 volt mains supply, it is also possible without a power supply from a common 5 volt bus to power Wemos or from the equipment it controls, such as a curtain drive. It is possible to install in the installation and terminal boxes of sufficient depth, for example behind a switch.

It can be used instead of radio control with different curtain drives.

Scheme


Pinout pin driven:
L1, L2 - Power outputs from the BT-137S triacs, controlled by wemos D0, D5 outputs through an MOC 3063S optocoupler with control of phase transition through zero. Switching the load on and off will take place without interfering with the network.
p1, p2 - pwm output for dimmers or motorized curtain drives or additional buttons, depends on the settings. correspond to the conclusions of wemos D6, D7.
A0 analog input for connecting various sensors, such as light or an additional button.
ds - The connection of the DS18B20 temperature sensor corresponds to pin D1.
DHT - Connecting the DHT22 temperature sensor corresponds to pin D2.
b1, b2 - Buttons, short and long press, with the function of the counter of clicks, can be connected to the pulse outputs of electricity meters, water, etc.

Demonstration of work


The controller program is created in the Arduino IDE. Suitable for all ESP-8266 based boards. Network configuration, control and operation of the controller is carried out by MQTT. For convenient viewing of settings, there is a Web-interface, previously there was a possibility to control via http, but then I found it unnecessary and removed it.

Web interface


Web interface can be disabled. For the convenience of initial configuration, a script winit.sh and instructions are provided. To reset the controller to the default settings, either enter the “default 1” command or simultaneously press the b1, b2 buttons for 20 seconds. There is also a simplified version of the program for controlled outlets Sonoff.

Arduino Mega based controller



It consists of the megabyte itself and the shield of the W5100 network card, the board divides the outputs for dimmers and the inputs for sensors and buttons, the relay outputs must be connected separately using a cable to the 2-row connector located on the mega board on the opposite side of the power and USB ports. The controller is rated for the D6MG package.

Pinout:

D2-9, D11-13 - PWM outputs for dimmers, PWM frequency is increased from the standard.
D14-21 - DS18B20 and DHT22 temperature sensors.
D22-49 - relay outputs, outputs D22-29 can be configured for louver drives, gates, curtains.
D10,50-53 - uses a W5100 network card.
A0-16 - inputs for buttons, short and long press. A0-A6 can be configured for analog sensors.

Network settings for mega are set in the program before the firmware. IP address is fixed.

Web interface


Dimmer on a simistor BT137-600E



Input 220 volts, PWM control 0-5 / 3.3 volts, set by jumper setting. 0-5 volts to work with Arduino Mega or 0-3.3 volts in the case of wemos. The PWM signal is fed to the analog input A0 Arduino Pro where it is converted into a delay for the opening of the triac, a connector is provided for flashing the arduine in place. There is a place for the radiator. On the arduino side, there is an arduine control and power supply connector (PWM, -, +), on the side of the SMIstor there is a 4-pin power connector - power and output to the load, if necessary, you can also install a varistor or snubber on it. The dimmer board is designed for the D2MG case.

Dimmer project:

PCB from PCBWay

Demonstration of work


There is also a block of triacs for 28 channels in the D9MG package.

I apologize for the quality of the picture


The equipment is connected via the LAN, the protocol for MQTT communication. I am using the MQTT Mosquitto server.

Software part


For convenience, you first need to prepare an image of the operating system with all the necessary programs, I use Raspbian Stretch Lite. You will need to install nodejs, python-pip, python3-pip, supervisor, mosquitto, mosquitto-clients, sqlite3. And pip packages: paho-mqtt and psutil. You can also install Node-red, perfect for all kinds of experiments.

After the release of Apple Homekit in 2016, it turned out that all the manufacturers and developers for the entire existence of the Smart Home systems could not do anything close in terms of convenience and functionality compared to homky, this is similar to the situation with the release of the first iPhone, when it turned out that the whole zoo phones, smartphones, communicators turned into a bunch of unnecessary trash. In any case, it is always pleasant to use a quality and finished product.

I chose the homekit2mqtt project as the main visualization system. Of course, you can use OpenHUB or Homeassistant, these systems also work with MQTT.

hjmqtt
Homekit2mqtt creates Homekit Bridge, it can be found in the program "Home" on Apple devices. Accessories (lighting, sensors, thermostats, etc.) need to be written in a special file in JSON format. For the creation of this file, the scripts filegen.sh and install.sh are responsible. In filegen.sh, you need to register accessories, install.sh adds homekit2mqtt to startup and starts. The most important thing happens in the hjmqtt.py file - the connection of the accessories with the equipment, in the file also manually, you need to register the accessories and the equipment parameters, the addresses for the KNX.
Functionality for accessories is registered in the files accessory.py and accessoryknx.py. Device statuses are stored in the sqlite database, for database operations, the statdb.py library is used.

hjconnect
The following project is needed for remote telemetry. This is the use of memory, disks, load and temperature, for this the psutil package is used. Now the open access version of hjconnect is available only for monitoring and without encryption and with a separate project a program for linking files to the same MQTT file-transfer-via-mqtt . Settings are right in the main hjconnect.py file. If you run it with the "-l" parameter, you can test it on the local MQTT server, the settings for the remote server are on line 160

rclient.connect("test.mosquitto.org", 1883, 60) 

server test.mosquitto.org can be replaced with your domain or IP. To identify the Raspberry Pi, the processor serial number is included in the topic, if the program is run on another computer instead of the serial number, the string “SN” will be displayed. Message interval is set in line 96

 th = threading.Timer(9, my_stat) # interval 

where 9 is seconds.

An example of what monitoring looks like


sima
Now the only speaker that speaks Russian is only Yandex. Creating a skill for Alice is easy enough. In contrast to the Siri given complete freedom of action. And access on any devices. Only, unfortunately, while there is no sane way to start the skill, it is inconvenient to constantly start the skill.

Demo


This is a working example of skill. To get started, the skill will require an SSL certificate, you can generate it using openssl. In the sima.py file in line 14 there is an example of certificate generation

 openssl req -new -keyout crt.pem -out crt.pem -x509 -days 365 -nodes -subj '/CN=site.com/O=user/C=RU' 

site.com and user need to be replaced with their data.

When a skill is launched on a new device, its ID is recorded. In order to associate an identifier and an automation system, you first need to create a new csv file in the clients directory, following the sample my.csv file. A serial number is written in the file according to the template, this is the same serial number from the hjconnect program, and the management objects are written: name, type, topic. A topic is a significant part of the MQTT accessory topic from a JSON file for homekit2mqtt. Then, using sn.py, you need to convert the serial number to a password and call this password on the device from which you will manage. The connection is provided by the hjconnect program, and the MQTT monitoring server is the same computer on which the skill is running.

Thanks, bye.

Source: https://habr.com/ru/post/442600/


All Articles