Lately, there are more and more articles on Habré on the topic “Smart Home”, “Make it Yourself”, and this makes me happy. I will also make my modest contribution to the development of civilization. Welcome to the world of smart things!

')
Brief prehistory
At first there was chaos ... Let's skip the stages of creation of the world and the era of dinosaurs and go straight to the 21st century. Around wireless technology, electronics everywhere, spacecraft are flying to Mars ...
Having discovered the Chinese online stores of various goods, I bought all the good. In particular, kits were purchased for remote control of light bulbs: a remote control and several boxes for it. As a result, I made 4 controlled outlets at home and now, lying on the sofa in the evening and reading Habr, you can turn on or turn off the lights in the room and in the hallway, without getting up from the sofa. Conveniently.
Also absolutely wonderful small cheap WiFi routers were
purchased . One of them was used as a "extension" for wireless Internet in the nursery, and the other remained in reserve. By the way, I recommend very good specifications for less than 600 rubles.
There were many different good things in the “bins of the Motherland”, and there were great New Year's holidays. And the project was born ...
Purpose and means
It was decided that an absolutely necessary thing in my “smart home” is an electrical outlet that can be controlled via the Internet. For this, the necessary details were collected:
- router
- AtMega16 microcontroller board from an old project
- solid-state relays
- 2-line LCD display
- body
- wires and other consumables
Technical task
The device was given the name "MZ-switch", and it had to do the following things:
- receive commands from the host via the Internet
- work as a load control relay and as a time relay
- work as a "pingovalka" and be able to reboot the router and disconnect the load in the absence of ping
- program the parameters of work from the command line and save them in your memory
- show status on display
- please the master with his existence
Code injection into the router
When I received the
router , then, of course, immediately got inside. At first I got there not mechanically, but metaphysically, through telnet.
It
turned out that he has Linux
neon inside, and you can even write and run your own script if you wish. You can even save it in the memory of the router, but how to make the script automatically run at startup?
A few days were killed to find the ability to run the script. The main idea was in the "code injection" in the launch of some kind of startup script, and such a script was found! The injection looks like this:

Now, when my router starts, it extracts the scripts stored there from its memory and runs them. Therefore, you can implement your own algorithm of interaction with the outside world.
Having achieved the launch of the scripts from the router, I got into it mechanically. Inside there was a serial port through which you can access the console. Having soldered 3 pins to the right points of the board, I was able to control the router via the serial port using a microcontroller.
Microcontroller and control program
The microcontroller must be able to communicate through the serial port with the router, manage the load, display the results of the work and generally do all the dirty work. From the previous project, there was a motherboard with an AtMega16 microcircuit having the necessary functionality. She went to work.
The basic algorithm of the microcontroller is as follows: we get a string with a command, analyze, execute, return a string with the answer. It reminded me of the logic of analog modems:
S-registers with data and
AT- control
commands .
Respecting copyrights and generally for diversity, the algorithm operates with the concepts of
R- registers and
MZ- commands. Registers store numbers and strings, and commands allow you to change registers and control the logic of work.
An example of the display contrast adjustment command: "
MZ + R0 = 200 "
Clever algorithm
The algorithm of the microcontroller consists of several separate tasks:
- data transmission from the transmission buffer
- receiving data and storing them in the receive buffer
- analysis of received data and determination of commands
- read and write internal R-registers
- load management and logic
- indication of operating modes
- countdown time intervals
All this managed to be placed in the amount of 8K code and 512 bytes of data, using only half of the
resources of the microcontroller . So you can still come up with something and program. For example, show the time, weather, play music and move around the apartment.
The control program was written and debugged on a common terminal. No device was harmed.
The result is the following device functionality:
- pause after power on: allows you to wait for the router to load and not respond to the “garbage” that it gives to the console
- if authorization is required, then enter the login and password in response to a request from the router
- running a script that allows you to configure the router to work with the device (init)
- running the main script that will be executed by the router during operation (loop)
- receiving commands from the router and sending response messages
- watchdog timer to restart the router when there is no activity
- running a script reloading the router (halt)
- load switching on and off using an opto-isolated relay
- the ability to customize all parameters and store them in the memory of the microcontroller
Shaytan machine
For some reason, very often the implementation of the functionality of some simple thing reminds me of the task from the
Incredible machine game. Well, how can the “turn on the light bulb” purpose consist of such intricate things and actions? That's how:
- So that the router could execute our scripts, we will do “code injection” into one of its own scripts.
- The injected code creates several service scripts in the / tmp folder, which we will use later.
- The router does not allow creating files with the attribute "executable" and, thanks to caring Chinese, it does not have the chmod command.
- But we elegantly bypass this trouble: first, copy some executable script under the name we need into the / tmp folder, and then overwrite its contents!
- The router has a WEB-interface to manage, but you cannot change / add your page: everything is read-only
- But we elegantly bypass this trouble: it turns out there is a page for launching any command, but there are no direct links to it. We type the address of this page manually, and we can run our scripts! Although not a beautiful button, but an old school command line.
- In order for the device to receive a command from the user, we use the “save-and-pass” method: one script is called from the command entry form and saves the user input to the file. Another script constantly checks for the presence of this file, and when it finds it, it sends it via a serial port to the microcontroller.
- Having received user input, the microcontroller selects a command from it and executes it. For example, the command "turn on the load."
Everything, the problem is solved!
Assembly in the case
Many projects do not live to see the release due to the fact that laziness or nothing to do with a normal body. In this case, the goal was originally set: to make an “industrial design” so that it looked adult, and not as usual.
The hull was spacious enough to accommodate everything there without crowding. A couple of days off with a drill and a file, yo-ho-ho and a bottle of rum, and now the device is assembled. All fasteners were found in the closet, where the remnants of furniture assembly and
trash in general household items are kept. Do not be surprised with dowel fasteners: there were no screws with nuts for the required diameter.
An indispensable material during assembly is the "Chinese snot" - plastic tubes for glue guns. In the absence of such a pistol, all the work was done with the tip of an ordinary soldering iron.
Here's what happened in the end:
outside

from the inside

Why was it necessary
I will not say that a unique device has been created, which has no analogs yet. Although why not? I will! Any new product in marketing materials is referred to as “unique” and “unparalleled”. In the extreme case, "several times better than normal ...". Therefore, I say: it turned out really
unique device, more than
9,000 times smarter than a
regular outlet .
You can connect an electric kettle and turn it on with a laptop via Wi-Fi. And you can turn on the ship siren, if the Internet is gone in the house. And you can connect the
Main Server to the device and restart it while at home.
In fact, this project was implemented as a “warm-up for the mind”, so as not to lose the skills in programming microcontrollers and owning a soldering iron.
Project materials are allowed to copy and modify. It is desirable with reference to the author of the project.