📜 ⬆️ ⬇️

Home automation

Accumulated some information from Ineta on circuitry, controllers, various examples. As confirmation of my small developing project, I decided to write this article ...

So we have:
The dir-320 router (with dd-wrt, the installation process is described on our personal blog, if necessary, I will add links to the blog), ATmega8535 controller, a small control circuit on the opto driver, control object (desk lamp, sound amplifier, home water heater), a little wires, soldering iron, free time.
What I would like to receive: software power management of objects that are powered from a network of ~ 220 V.

On Habré there was a whole cycle of articles about "how to turn on the LPT desk lamp" and so on ... BUT! It offers a more or less advanced, secure and autonomous management system.
')
For the limitless - under the cut of the picture.

The object itself is managed as follows:

Mobile phone-> mobile browser-> wifi (mobile) -> wifi (dir-320 router) -> installed lightpd-> handler for perl-> uart (router) -> uart (mega8535 controller) -> control signal (+ 5v controller) -> control signal (+ 5V optodriver) -> controlled signal (~ 220V optodriver) -> control object.
In this scheme, you can replace “Mobile phone-> mobile browser-> wifi (mobile) ->” with a laptop, a working PC, and so on.

It is also possible to control through the wan port connected to the Internet network, again by referring to lightpd.

Which + of this control scheme:



Which - this control scheme:


What are the possibilities to supplement this control scheme:


Well, actually in practice we will do the following:

First you need to decide on the router software.
Since we have linux on board the dir-320, working with physical ports does not cause any problems - the software is full, you can install and use.
Our UART interface of the router hangs from / dev / tts / 0 from a software point of view.
In it, we will write data that will be transmitted to the controller.
In general, I am not a coder and write programs on the PC for working with devices, IMHO there are regular tools. For example, redirecting output to the shell:

echo «» > /dev/tts/0

The team itself will send the string to the port, which is what we need.
Before using the port, it must be configured. (stop bits, transmission control, speed)
In this case, 8 data bits, 1 stop bit, 300 baud will be used (and we don’t need more).

stty -crtscts 300 < /dev/tts/0

A simple protocol was developed for management:

The line begins with the symbol!, Then the word port goes, then the controller port is indicated (in this case, mega8535 has 4 of them - A, B, C, D). Then the pin number (there are 8 of them, from 0 to 7). Then byte n or f (n - respectively submit + 5v to pin, f - not submit to pin + 5V)

Note:

! porta1n - will turn on the second pin of port A on the controller.
! ports4f - disables the fifth pin of port C on the controller.
! - the symbol means the start of the command, those controller will wait for the start of the command only with the symbol!
port - for “data control”, is it not enough what kind of interference will be (although it is very unlikely).

To do this, a small script was written on the form:

#!/opt/usr/bin/perl
$cmd=$ARGV[0];
`echo !port$cmd > /dev/tts/0`;


It is launched in the format “do.pl a1n”, which will enable pin 1 of port A on the controller.
lighttpd will run the do.pl script

We put lighttpd:

ipkg-opt update
ipkg-opt install lighttpd
ipkg-opt install mod-fastcgi


Next, edit /opt/etc/lighttpd/lighttpd.conf
We set the port setting - 8080 (you can choose at your discretion).
!!! CHANGING
# server.event-handler = "freebsd-kqueue" # needed on OS X
on
server.event-handler = "poll" # needed on OS X
otherwise it will swear at startup.

/opt/etc/init.d/S80lighttpd start

The root directory will be / opt / share / www. We write a script on php to run the perl script with parameters. (It is very resource-intensive, and I would even say “stupid” to use such investments of pearl in php, but the example is still working.)

<?php
$do=$_GET['do'];
if (!isset($do)) { print "no command"; exit(0);}
passthru("/opt/bin/perl /opt/home/do.pl do");
?>


ATTENTION!!! I want to note that using these examples is a blow to security ... IMHO you will need to check the variable do.

Now, referring to the address http: //: 8080 / test.php? Do = a1n - send data to the controller. The second pin of port A. will turn on.

Now consider the physical connection of the router with the controller.



As you can see from the image - Uart is at the bottom - 5 extreme conclusions - the receiver and transmitter are second ground 3.3V, but we do not need it.


In the controller, ports 14 and 15 are used for receiving / transmitting.
Accordingly, we connect the RX router and the TX controller, and vice versa the TX router with the RX controller.
If you power the controller from the power source of the router (the router is powered by 5 V), then you only need 2 wires. BUT! if you power the controller from other sources, you need to connect GND with the third wire !!!
As a result, I got the following.



By the way, the router from the MTS, white))
As you can see, the controller is on top, 4 holes D = 1mm for wires, 2-uart, 2-controller power are made in the cover. (5V router power, who prevents to take us power for the controller directly from the router port :))
Convenient for upgrading the controller firmware, the controller on the socket.
Also, the pins of the controller pins are connected to an additional board with terminal blocks (blue ones), from where the control current to the opto drivers comes from.

So the controller.

Actually there is only firmware. Everything was written in CodeVisionStudio.
An example of firmware on Syah.
In the code generator, we include all the ports for output.
In the UART panel we include an interrupt, a buffer of 8 characters. 300 baud rate, 8 data bits, 1 stop.

Part of the program:

int port(char port,char num,char is)
{

if (port=='a')
{
putchar('A');
if (num=='0') {if ( (PINA.0==0) && (is=='n') ) {putchar('N'); PORTA=PORTA+1;} if ( (PINA.0==1) && (is=='f') ) {putchar('F'); PORTA=PORTA-1;} }
if (num=='1') {if ( (PINA.1==0) && (is=='n') ) {putchar('N'); PORTA=PORTA+2;} if ( (PINA.1==1) && (is=='f') ) {putchar('F'); PORTA=PORTA-2;} }
if (num=='2') {if ( (PINA.2==0) && (is=='n') ) {putchar('N'); PORTA=PORTA+4;} if ( (PINA.2==1) && (is=='f') ) {putchar('F'); PORTA=PORTA-4;} }
if (num=='3') {if ( (PINA.3==0) && (is=='n') ) {putchar('N'); PORTA=PORTA+8;} if ( (PINA.3==1) && (is=='f') ) {putchar('F'); PORTA=PORTA-8;} }
if (num=='4') {if ( (PINA.4==0) && (is=='n') ) {putchar('N'); PORTA=PORTA+16;} if ( (PINA.4==1) && (is=='f') ) {putchar('F'); PORTA=PORTA-16;} }
if (num=='5') {if ( (PINA.5==0) && (is=='n') ) {putchar('N'); PORTA=PORTA+32;} if ( (PINA.5==1) && (is=='f') ) {putchar('F'); PORTA=PORTA-32;} }
if (num=='6') {if ( (PINA.6==0) && (is=='n') ) {putchar('N'); PORTA=PORTA+64;} if ( (PINA.6==1) && (is=='f') ) {putchar('F'); PORTA=PORTA-64;} }
if (num=='7') {if ( (PINA.7==0) && (is=='n') ) {putchar('N'); PORTA=PORTA+128;} if ( (PINA.7==1) && (is=='f') ) {putchar('F'); PORTA=PORTA-128;} }
}

return 0;
}


For other ports by analogy.

In the main function:

while (1)
{
k = getchar();
if (k=='!')
{
for (i=0;i<=6;i++)
{
buf[i]=getchar();
}
if ( (buf[0]=='p') && (buf[1]=='o') && (buf[2]=='r') && (buf[3]=='t') )
{
port(buf[4],buf[5],buf[6]);
}
}
}


At this stage we have the ability to manage the findings through the web of the router !!!
Now we need to somehow manage the load ~ 220 V by means of 5V, which the controller gives us.
Of course, it would be possible to use a relay for such purposes + transistor, since the output current of the pins of the controller is not able to control the relay. Again, it would be necessary to use an additional power source for the transistor – relay circuit. Yes and relyushki durable.
Due to these circumstances, we had to stop at the opto driver.

Benefits:



Disadvantages:



So Optodriver.



We will need:



As a result, it should turn out something like these boards.



This circuit can withstand current up to 16 amps, which corresponds to 3 kilowatts of load.
Considering the fact that my water heater is the most consumed device in an apartment, and that consumption is going on - 2 kilowatts. !!! But with a load above a kilowatt, you need to put the radiator on the triac, as it is heated.

In general, in fact, you can use any device in which there is a UART, and it is in almost all types of routers. The only difference is how to program it.

ps: Questions, suggestions?
pps: people, do not write comments like "here if it were not hands from the experience ..." and the like, it’s all the easier way;)

ppps: I read the comments, I want to make a small economic review in view of comparing the cost with finished products such as Arduin.

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


All Articles