Home automation

Home automation.

Accumulated some information from internet circuitry, controllers, various examples. As a confirmation of my small developing the project, decided to write an article ...
So we have: router dir-320 (with dd-wrt, the installation process described in the previous article about dir-320 + C), the controller ATmega8535, a small control circuit for optodriver, object management (reading lamps, power audio, home water heater), a little wires soldering iron, FREE  time.
What I would like to receive: software power management objects that feed from ~ 220 V.
Proper management of the facility as follows:
Mobile phone-> mobile browser-> wifi (mobile) -> wifi (router dir-320) -> set lightpd-> handler to perl-> uart (router) -> uart (controller mega8535) -> control signal (+5 controller) -> control signal (+5 optodrayvera) -> control signal (~ 220 optodriver) -> object management.
In this scheme can be replaced with «Mobile phone-> mobile browser-> wifi (mobile) ->» on a laptop, work PC and TP.
Also has the ability to manage through the wan port connected to the Inet Network, again by reference to lightpd.
What + this scheme:
  • No need to get out of bed for the inclusion of blackouts, TC management with a laptop or a mobile.
  • There is a possibility to saving by switch off / on the water heater, even if you're not at home — through the Net.
  • Integrated on / off devices (coming to the house, you can pre-enable such light wherever necessary. Including teapot before joining)
  • Often forget to disconnect the device from the network before leaving. This is possible at any time.
What — this scheme:
  • Maybe not quite convenient to get or keep a hand every once in a mobile phone for on/off light. But this is  solved by parallel turning optodriver with switch light.But, again, will not turn off remotely if it enabled.
  • Each device is an additional wire and a device for optodraiver. The wires need to get by secured to the wall or hide in the wall, which entails a change of wallpaper ))))) Optodriver also need to get by hiding in the socket, God forbid, if there is enough room)
  • Exception when MOBILE, laptop does not turn on the computer broke. Solved only parallel connection of a physical means of control.

What are the possible additions of this scheme:

  • Put motion sensors to turn on the light. On the controller to make an adjustable off delay (3-5min of discretion).
  • Wireless control via toshiba chip, but will supplement supply from the circuit ~ 220V.

Well on practice will do the following:

First we need to decide on software router.

Since we have linux on board the dir-320, then work with devices not cause any problems — software is complete, you can set and use.

Our UART interface router with software on the dot of  view is on / dev/tts/0.

It is what we will write the data to be transmitted to the controller.

Generally I'm not a coder and write prog to C to work with devices that IMHO has established tools. For example output redirection in the shell:

> Echo «data»> / dev/tts/0

Actually the team will send a string to the port that we need.

Before using the port must be configured. (Stop bits, flow control, speed)

In this case, will be used dynnh 8 bits, 1 stop bit, 300 baud (and more and we do not have).

> stty-crtscts 300

To control has been developed a simple protocol:

The string starts with !, followed by the word port, then an indication of the port controller (case mega8535 their 4 — A, B, C, D). Then the number of pins (there are 8 pieces, from 0 to 7).  Then B n or f (n — 5, respectively, to submit to a pin, f — do not apply for a pin +5 V)


Porta1n — including a second pin port A on the controller.

! Ports4f — disable the fifth pin port on the controller.

! — Symbol indicates the beginning of the command, the controller will wait for those top teams only with the character!

port — for control of the data, you never know what will navdki (although ochenmaloveroyatno!).

For this was written a little script in Perl like this:

  #!/opt/usr/bin/perl #! / Opt / usr / bin / perl  

 $cmd=$ARGV[0]; $ Cmd = $ ARGV [0];
 `echo !port$cmd > /dev/tts/0`; `Echo! Port $ cmd> / dev/tts/0`; 

Running it done in a format «do.pl a1n», which include the controller pin 1 port A.

The script do.pl the parameters will have to run lighttpd

Put lighttpd:

>ipkg-opt update > Ipkg-opt update

>ipkg-opt install lighttpd > Ipkg-opt install lighttpd

>ipkg-opt install mod-fastcgi > Ipkg-opt install mod-fastcgi

Next, edit the / opt / etc / lighttpd / lighttpd.conf (I'm using vim)

Expose the port configuration — 8080 (choose at their discretion).


# server.event-handler = «freebsd-kqueue» # needed on OS X # Server.event-handler = «freebsd-kqueue» # needed on OS X


server.event-handler = «poll» # needed on OS X server.event-handler = «poll» # needed on OS X

otherwise you will swear at startup.

>/opt/etc/init.d/S80lighttpd start > / Opt/etc/init.d/S80lighttpd start

The root directory is / opt / share / www. Write a script to run on nxn perl script with the parameters. (Hemorrhoids huh? Dapoh, but it works! © approx. Ed.)


$do=$_GET['do']; $ Do = $ _GET ['do'];

if (!isset($do)) { print "no command"; exit(0);} if (! isset ($ do)) {print "no command"; exit (0);}

passthru("/opt/bin/perl /opt/home/do.pl do"); passthru ("/ opt / bin / perl / opt / home / do.pl do");
?> ?>

WARNING! I want to note that the use of these examples — a blow to the security ... IMHO will need to check the variable do.

Now, turning to the address http:// : 8080/test.php? Do = a1n — send data to the controller Vklyuchitstso second pin port A.

Now consider the physical connection to the router controller.

As seen from the picture — uart us at the bottom — 5 extreme conclusions — the transmitter and receiver second middle earth 3.3, but we do not need.
The controller also isplzuyutsya to send / receive 14 and 15 ports.
Accordingly, a router and connect the RX TX controller, and vice versa TX RX router with the controller.
If you feed the controller from the power source router (the router is powered by 5 V), then you need only 2 wires. НО! BUT! But if you feed the controller from the other sources of enerjy, it is necessary to combine the third GND wire!
In the end I got this.
Incidentally, the router from MTS, whit))
As you can see the controller is svrehu, in a cover made 4 holes D = 1mm for wire, 2-uart, 2 — power controller.
Convenient to upgrade the firmware of the controller, controller sockets mk)))
Also, the findings of pins connected to the controller board with an additional shrinkable tube (a blue), from which comes the control current to optodrayvery.
So the controller.

Here only firmware. All written in CodeVisionStudio.
Example of firmware on C.
In the code generator turn on all the ports on the output.
In the panel include UART interrupt buffer to 8 characters. 300 baud, 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;} } } if (port=='c') { putchar('C'); if (num=='0') {if ( (PINC.0==0) && (is=='n') ) {putchar('N'); PORTC=PORTC+1;}   if ( (PINC.0==1) && (is=='f') ) {putchar('F'); PORTC=PORTC-1;} } if (num=='1') {if ( (PINC.1==0) && (is=='n') ) {putchar('N'); PORTC=PORTC+2;}   if ( (PINC.1==1) && (is=='f') ) {putchar('F'); PORTC=PORTC-2;} } if (num=='2') {if ( (PINC.2==0) && (is=='n') ) {putchar('N'); PORTC=PORTC+4;}   if ( (PINC.2==1) && (is=='f') ) {putchar('F'); PORTC=PORTC-4;} } if (num=='3') {if ( (PINC.3==0) && (is=='n') ) {putchar('N'); PORTC=PORTC+8;}   if ( (PINC.3==1) && (is=='f') ) {putchar('F'); PORTC=PORTC-8;} } if (num=='4') {if ( (PINC.4==0) && (is=='n') ) {putchar('N'); PORTC=PORTC+16;}   if ( (PINC.4==1) && (is=='f') ) {putchar('F'); PORTC=PORTC-16;} } if (num=='5') {if ( (PINC.5==0) && (is=='n') ) {putchar('N'); PORTC=PORTC+32;}   if ( (PINC.5==1) && (is=='f') ) {putchar('F'); PORTC=PORTC-32;} } if (num=='6') {if ( (PINC.6==0) && (is=='n') ) {putchar('N'); PORTC=PORTC+64;}   if ( (PINC.6==1) && (is=='f') ) {putchar('F'); PORTC=PORTC-64;} } if (num=='7') {if ( (PINC.7==0) && (is=='n') ) {putchar('N'); PORTC=PORTC+128;}   if ( (PINC.7==1) && (is=='f') ) {putchar('F'); PORTC=PORTC-128;} } } return 0; }
In function main:
while (1) while (1) { { k = getchar(); k = getchar (); if (k=='!') if (k =='!') { { for (i=0;i<=6;i++) for (i = 0; i <= 6; i + +) { { buf[i]=getchar(); buf [i] = getchar (); } } if ( (buf[0]=='p') && (buf[1]=='o') && (buf[2]=='r') && (buf[3]=='t') ) if ((buf [0] == 'p') & & (buf [1] == 'o') & & (buf [2] == 'r') & & (buf [3] == 't')) { { port(buf[4],buf[5],buf[6]); port (buf [4], buf [5], buf [6]); } } } } } }
Here example only for ports A and C.
At this step have the ability to control the output through a Web router!
Now we need to somehow control the load of ~ 220 through 5B, which gives us the controller.
Of course one could use a relay for such purposes + transistor TR output current pin controller is not able to control the relay. Again, it would be necessary to use additional power supply for the circuit tranzitor — relay. Yes, and not durable relyushki.
In view of these circumstances had to stop at optodrayvere.
  • Do not need additional power.
  • Contact between the chains 220 and 5 exclusively by light (LED + light sensor)
  • Quiet
  • High current
  • Slightly more expensive than relays
  • Triac warms, we need an additional radiator
  • Manufacturing longer and harder, but not much
So Optodrayver.

We need:
  1. Resistor 380 Ohm
  2. Resistor 330 ohm
  3. Resistor 39 Ohm
  4. Resistor 1 kOhm
  5. Ceramic capacitor is 0.01 pF volts as at 500-600, but not less than 300!
  6. BT139 Triac
  7. MOC3041
The result should like chtoto like these boards.
This scheme maintains the current to 16 amps, which corresponds to 3 kilowatts load.
Given the fact that my water heater — the most consumed by the device in the apartment, and then consumption goes — 2 kilowatts. !!! But at a load higher than a kilowatt, it is necessary to put the radiator on the triac, IMHO heated.
So far so ... later screwed to the sensor controller + is necessary to teach the feedback controller — a router, for some reason the data on the router come, but as ever to catch them does not work ...
PS: who can know what Trouble? Perhaps due to the fact that you are using the system console on  UART port? I will be glad if anyone tell something;)