📜 ⬆️ ⬇️

How to make a controlled outlet from a Chinese router, or another project for a smart home and office

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!



image
')
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:


Technical task

The device was given the name "MZ-switch", and it had to do the following things:


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:

code_inject

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:


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:


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:

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
outside
from the inside
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.

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


All Articles