Throughout his adult life, he dreamed of making, if not the whole world, then at least his own dwelling similar to houses from science fiction films. So that, after going home after a hard day's work, I was carefully turned on the light, greeted me politely, reported incidents, and at the exit warned me about the weather overboard and closed the door behind me. So that I do not have to go, stammering in the dark to the bed, but I could lie down and only then turn off the light from my gadget, which is always at hand.
On Habré is already full of articles about the implementation of this, what will be different from mine? I was strangled by a toad. I did not want to buy the finished device, it is expensive and not interesting, it was decided to do everything myself.
At the moment, my smart home is completely finished: the server part, the web interface, the application with Android widgets, controllers, sensors and actuators, algorithms, even my own scripting programming language for them.
')
But for starters, I would like to tell you about the hardware of the "workhorse" at home - the RC-2 (room controller). I have been writing firmware for AVR MK for a long time, so the Atmega 8-16PU, which is in all stores of radio components, will be the brain of our controller. Yes, her older brother is used in the already annoying Arduino Uno, but we will stay away from her.
What do we want?
And now we define the requirements for our horse: the controllers should be in each room and have a connection to the server, work from the supply voltage of 7-40 volts, have the ability to connect temperature and humidity sensors, various sensors and dry contact buttons (reed switches, buttons , motion sensors), the ability to control external devices, the ability to do simple actions without the server (to turn on the light on the button, motion sensor, reed switch).
Now let's go through the requirements and choose the means that can meet these requirements:
- Contact with the server. Ethernet is difficult and a lot of wires (from the switch to each of the controllers), Bluetooth, WiFi and other wireless ones are difficult and expensive. Recall the good old RS-485 bus and we will use it.
- The logic is 5 volts, we need a converter. We will use a simple and very efficient power supply on the LM2576 switching regulator (the scheme is in the datasheet on the first page)
- All sorts of sensors? Atmega8 has everything on board: ADC, discrete inputs, there is even a built-in pull-up resistors. We implement 1-Wire bus support on one of the legs to communicate with the sensors (the same DHT11).
- Controlling external devices? Again, we have discrete outputs and PWMs on the MK.
Shut up - for clever you will fall
There should be one master on the RS-485 bus that will interrogate our smart controllers connected to the bus in turn. At one point in time, one says, the rest are listening. The controllers respond strictly only to the request of the master, by itself no one says anything, otherwise there will be collisions, and no one will understand anything. All strictly.
The data exchange on the bus is half duplex, that is, at one point in time the device can either transmit or receive data.
To work with the bus, we will use the MAX485 chip, plus the Atmega8 UART itself.
Everything is simple with it: on the 2nd and 3rd leg the logical unit is passed, zero is listened. After switching these modes, it is important to wait until the transients are completed, somewhere in half a millisecond, before transmitting / receiving.
What is the result?
The divorced scheme looks like this:
Ready controller:
In the nearest radio store it all costs:
- Atmega8-16PU - 130r
- MAX485 - 60r
- LM2576 - 60r
- Powder (textolite, resistors, diodes, capacitors, quartz, terminals) - 150r
Total = 400r.
It turned out cheap and cheerful. Communication protocol device-server binary, with checksum. The controller pushes the paws at the command of the server, in response to a state request, it sends an array of states of its inputs, outputs, ADCs and what was obtained by 1-Wire. Without a server, it can turn on the light by a button, switch, motion sensor.
If someone wants to see my RC-2, or even use it at home, I will describe in detail the protocol for communicating it with the server and give it the circuits and firmware.