Skip to main content

Smart home with Raspberry PI


My first home automation system was based on Arduino and Raspberry PI .
All the relays I wanted to control, the switches from which I received signals and temperature sensors that I used, were connected to the arduino mega with ethernet shield.
Arduino was responsible for storing the states of individual devices. I had the MQTT server installed on Raspberry PI. Arduino sent all signals to the MQTT  they were downloaded by OpenHAB and made available to the user via a web GUI or a mobile application. 
In addition, OpenHab could send user commands to MQTT. Arduino read messages from MQTT queue and updated the status of home appliances.

The solution had one basic advantage: it worked :)
However, there were a few problems that I mentioned in previous posts. These included:

- complicated architecture - in case of problems there were many places to verify.

- problems with power failures - arduino with ethernet shield has problems with proper boot after power outage. The system could hang on startup and it was necessary to manually disconnect it from power and restart. Sometimes several times. There are descriptions of such cases on the internet. Some advise putting an additional capacitor between reset PIN and GND (if I remember correctly :)), but in my case it did not help much.

- random switching on / off of devices during voltage peaks. I could not find the reason. I tried to connect the device to additional stabilizers, but it did not help. It was not somehow troublesome, because it happened relatively rarely, but it was quite a big scratch on the stability of the whole solution

- uncomfortable connection of devices to arduino - connecting sensors and relays directly to the arduino PINs  is generally a very poor solution. 

- problematic introduction of changes to the system - adding new relay or sensor  besides connecting the hardware required changes in the arduino code, moving the laptop to the garage where my arduino was stored, hooking the USB to arduino, programming the system, testing and in case of error repeating everything several times - unhealthy :)
After more than two years of using such a system, I began to think about how to simplify the entire architecture, so that most of the above problems could be eliminated. Subconsciously, I felt that in this situation arduino is completely unnecessary, that Raspberry PI should be enough for everything. The problem is, however, the limited number of GPIO PINs.

In the previous post RPI and additional GPIO pins on several mcp23017 circuits and a switch on the transistor I described how these problems can be solved. The only thing left was to design a suitable system that would meet all my requirements, provide flexibility and the possibility of further expansion, and a PCB with possibility of easy attach devices to Raspberry. I have defined the following requirements:

- 16 digital inputs (support for up to 16 switches)
- 16 digital outputs (support for 16 devices which I can on / off)
- support for One Wire bus
- LIRC support (infrared remote control)
- easy and durable connection with Raspberry PI
- easy and durable connection of devices and sensors
- access to other GPIO PINs
- the ability to operate devices powered by different voltage
The task was quite time-consuming, but I think that I managed to achieve the required effect. I did a scheme and project at Eagle, and here are the PCB I received:










This is the first prototype, as you can see, not yet fully soldered.
The board is designed for a 40-pin GPIO connector available from Raspberry Pi 2.
In this case, the connection is very convenient and durable - via a 40-core strip to the Raspberry and the PCB.
Nothing, however, stands in the way of connecting it to the older version of RPI where the connector was 26-pin. In this case the connection must be different - for example, female-female cables:




The connection is simple because the connector on the board corresponds to the GPIO port in Raspberry.

The tile contains all the elements about which I wrote in the requirements:

Now a few words of explanation.

The board should be connected to the Raspberry PI GPIO port. In addition, an external DC power source must be connected. 
I decided on this solution because RPI has too little current efficiency to handle 16 external devices that may have different requirements. 
As you can see on the picture we have 16 transistors. 
All outputs are designed as transistor keys. MCP23017 chips activate individual transistors and supply external power to individual outputs. 
So here we have a very interesting feature - depending on what voltage we supply as an external power source, such devices might be controlled on the outputs. 
For example, by plugging the board to + 5V we will be able to control relays that require 5V, connecting the board to + 12V we will be able to connect 12-volt devices, for example LED strips. There is a limitation here - all outputs must operate at the same voltage.

However, another feature of the PCB allows to overcome this limitation. 
As you can see on the board there is an additional GPIO output, which theoretically can be connected to the second same PCB. Then connect first PCB to + 5V and the other to + 12V. Then we have 16 outputs + 5V and 16 outputs + 12V. And so on .... To the second we can connect the third, third to the fourth :). On each board you only need to change the addressing of MCP23017 circuits using jumpers. 
The limit is only the number of addresses - 3 bits, i.e. we can handle 7 MCPs - from the fourth board we would have to take out one MCP chip - they are in sockets, so there is no problem. I have not tested such a solution yet - I do not have so many PCBs :), but the theory says that this solution should work. I have yet to investigate the current limitation of all connected devices - it is still ahead of me. From the preliminary calculations it appears that up to 2A should work without the risk of overheating - this is what I will study.

Another note - only the devices that can be connected via two wires (plus and minus) can be connected to the outputs. This allows, for example, to enable relays, but only one-channel relays. They have 3 pins. We connect the minus to minus, the plus of the board at the same time to the plus and signal on the relay. For me, this solution is enough. I control single-channel relays hidden in wall boxes. Controlling multi-channel relay modules can be problematic because they share power for all channels. I'm already working on the solution.

There are 16 digital inputs on the board. They are ordinary 2-pin connectors. Shorting the pins causes the + 5V voltage to be applied to the appropriate MCP23017 channel and from there we can read the state of the button.

In addition, I placed a 3-pin LIRC connector on the board, which can be directly connected to the TSOP receiver.The connector is connected to 3.3V with RPI so only TSOP circuits that support such voltage will work. I personally checked the TSOP4836 and it works fine.

You can connect 10 OneWire devices directly to the board. Each input has 3 PINs (+ 5V, GND, SIGNAL).Devices are plugged in directly, without an additional resistor, which I have already placed on the PCB.

These are probably all the functions of the board. For me, the PCB has replaced the previous configuration about a month ago and it has been working fine so far. All the limitations of my previous home automation were removed.

And the biggest advantage - how we use the board, what language do we use  - it only depends on us.. You can use python, java, c, or even write scripts in bash. Only the imagination limits us. Everything can be  tested by logging in to RPI via ssh and running programs. You do not need to program arduino every time :)

I decided that I would still stay with OpenHab launched on RPI.

OpenHab 1.x has a "binging" supporting MCP23017 systems. Probably it can be used, but recently i switched  to OpenHab 2.x.
Unfortunately, the binding did not work, so I had to write my own. The code is already merged to official release, so it should work with OpenHab 2.3 or above.

There was also no binding for handling temperature sensors connected to OneWire connectors.  I also had to write my own. The code is also available in OpenHab 2.3 or above

Summarizing:
the whole solution took a lot of time, from the prototype on the contact board, through the diagram and project in eagle, soldering the circuit, writing the software, but it was definitely worth it. The whole system works much more stable than the previous solution, and  it is much more convenient and flexible. I am able to imagine a lot more use cases of such a PCB than just home automation.

Currently I use it with:

- 7 relays for controlling lighting or sockets 
- switches for operating relays
- 7 One Wire DS18B20 temperature sensors
- controling relays with infrared remote control
- reading a state of garage door (closed / open)

All this on one Raspberry PI and the PCB described :)

During the work I prepared three prototypes of the tile. One works productively, one I have for further testing and one left unused. If anyone is interested, I can sell it. Feel free to contact me if interested. It is fully manually soldered. Contact me if You are interested. If there will be more interested persons I'll prepare more of them.

You can also buy MCP23017 chips for example in aliexpres, from this link:
http://ali.pub/2hk68b

This is affiliate link. When you will buy from this link I will get some provision, so You will help in this blog development. Thank You!


Comments

Popular posts from this blog

Control OpenHab using Telegram Messenger

Recently, I've been trying to discover a way to control my OpenHab instance using Google Assistant. It is rather easy to do this using  IFTTT , but only when You use OpenHab in a cloud. I prefer using private instalation of OpenHab in my LAN, so that was not an option for me. I was searching for another solution, but only app supported by IFTTT which might be usefull form me was Telegram messenger. The only problem is, that OpenHab does not support Telegram controling by default. I google'd a little and it turned out that Telegram has a great public API with good Java support, so I decided to write a simple Spring Boot application as a bridge between Telegram Bot and OpenHab instance. You can find source code of my application on my GitHub account: https://github.com/aogorek/openhabtelegrambot In order to run it there are few steps required: 1. Create Bot in Telegram Messenger First You have to create Your own bot to communicate with. You need to contact @BotFather

Angular 5, SpringBoot 2, Oauth2 and IMPLICIT flow - silent token refresh

Recently I've been working on simple web application for managing students and users. Application will be published on the internet, so it needs proper access control. I wanted to learn some new stuff, so I decided to use Angular5 with Oauth2 authentication. I didn't want to use any options as "Login with Facebook", or "Login with Google". I wanted my Spring Boot app to work as the authentication server and the resource server. I read a little abouth Oauth2 and different flows possible, and it turns out, that preffered flow to use with web application is IMPLICIT flow. Implicit flow uses only one token. It doesn't have a refresh token, as it could be overtaken by an attacker. Access token has defined validity period. In other flows, where refresh token exists it is used to get another access token when the first one expires. In theory in implicit flow user should just log again, but forcing a user to log in during active session is not an option.

Raspberry PI and additional GPIO pins on several mcp23017 chips and a transistor switch

Some time ago I came to the conclusion that the home automation control system I made before was too complicated. I've been using a Raspberry PI + arduino + ethernet shield + additional connector board.  Arduino together with ethernet shield sometimes causes problems, especially after power outages. It has problems with restarting and requires manual restart. So this was the element I wanted to get rid of. It seems that the raspberry PI itself should definitely be enough. The only limitation is the number of GPIO pins and current efficiency. To avoid that problem I have already started designing the PCB which, when connected to raspberry, will provide us with additional pins and the ability use more power consumng devices. The easiest way to gain additional pins is to use the mcp23017 chip. It uses I2C bus to connect to the raspberry PI. Each chip will provide us with 16 additional pins. The chips can be connected together in chain, so you can have even more pins. I calculated t