loading

This instructable shows the principles involved in making devices to control home automation over the internet. We're going to construct a device (or several of them), that talk to each other over the internet to control lights, motors for curtains/blinds, power sockets etc.

In contrast to devices controlled from a web browser or phone, these are designed to be simple, Arduino based devices that are capable of talking to each other bi-directionally. They may be in the same house or in different buildings and may have one way communication (e.g. a light switch and light) or two way (e.g. an automation controller).

It's possible to have local Device/Relay combinations to control Mains socket power to TVs, Computers etc..

Also you could see this working in building control. Commercial examples that work over local or proprietary interfaces are : Clipsal C-Bus, X10, Bus-SCS.

The Picture shows an example of what we're trying to achieve using the commercial C-Bus devices as an example.
(http://en.wikipedia.org/wiki/File:CBus_Wiring.gif). But in our case the C-Bus is replaced by the Internet or a Local Network.

Practically speaking, it would make most sense to have one device per room with several switches, sensors and possibly low-voltage motors wired into it, and one device at the distribution board controlling relays or dimmers.

A major factor in Energy Waste is having Lights on too brightly during periods when a room is adequately lit from Natural Light. Adding a Light Sensor Device in a Room with a Dimmable Light Device significantly increases Energy Efficiency by controlling the Room Light in response to the sensed light in the room.

==========

By the way. If you like this Instructable, you might also like to read My Blog covering various projects and Tutorials

====

Step 1: You Will Need

For each Device you will need:
* An Arduino (Uno)
* An Ethernet shield (or Wifi)

To prototype:
* A breadboard
* Some LEDs, Wires and Switches

For Mains Control:
* A Mains Relay Arduino Shield (e.g. http://www.dfrobot.com/index.php?route=product/product&product_id=496)

We won't be covering the Mains Control because it's covered in lots of  tutorials and there are a lot of shields available.


Step 2: Prototyping the Arduino Internet Device

We'll be concentrating on the principles of the Device itself.

To do that we'll be prototyping it with LEDs and Switches. Once the Arduinos are talking and controlling LEDs, making it work with Mains Relay shields and motor shields is straight forward.

We're going to construct a device with switches and LEDs, and we'll connect 2 of them over Ethernet. That way we'll be protoyping both the Switch function and the Light Control function at the same time and demonstrating bi-directional comm's.

For many Devices we should connect them via a Router. If we only have 2 devices (as we do here) then we can connect them to each other. The Ethernet IC takes care of the required Crossover.

We need to give some thought to how one device knows the address of the device it wants to communicate with.

Here's how we'll approach it:

Each Device has its own static-IP address. Each Device "knows" which device it wants to talk to. Therefore we can hard-code the Static-IP address of the destination device in the Arduino Code of the Transmitting Device. Not the most elegant solution. But completely practical.

Step 3: Connect the Circuit

Build an example Device as shown in the diagrams. In fact, build two of them. We'll need one to talk to the other.

Each Device consists of:
1. An Arduino (Uno)
2. An Ethernet Shield
3. An LED and Resistor to represent an Output Light.
4. Two Switches to represent an ON/OFF  Switch panel or UP/DN Dimmer (for example).

Once you've built them plug both into your PC USB for programming.

Plug an Ethernet cable from Device 1 to Device 2 (the crossover is taken care of in the Ethernet Shield).

Step 4: UDP Communication

We're going to use UDP for communication over the Internet.

UDP is an Internet Protocol (so is TCP). So our communications will be UDP/IP.
http://en.wikipedia.org/wiki/User_Datagram_Protocol

To Send a UDP packet we use the Arduino Ethernet library as follows:

1.
First we have to define our IP address and the address of the receiving device, as well as the Ports we're sending and receiving on:
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x00, 0x85, 0x46 };
IPAddress ip(192, 168, 2, 177);
IPAddress rem_ip(192, 168, 2, 178);
unsigned int localPort = 8887;  
    // local port to listen on
unsigned int remPort = 8888;      // remote port to send to


2.
We start up the Ethernet and then UDP services (remember it's UDP over Ethernet -UDP/IP)
Ethernet.begin(mac,ip);
Udp.begin(localPort);


3.
When we want to send a message it needs a start, beginning and end:
Udp.beginPacket(rem_ip, remPort);
Udp.write(snd);
Udp.endPacket();


4.
For Receiving we look for a non-zero packet size being received:
int packetSize = Udp.parsePacket();

and read it into the rcv buffer:
Udp.read(&rcv,size);

So what do we send?

Very simply, we could send '0' for Switch 0 Pressed and  '1' for Switch 1 Pressed.

If Devices had sensors attached you may need more data e.g.
{State_sw0, State_sw1, Sensor_val}

Whatever you decide in your application it can be sent as a String over UDP in the same way.

Step 5: Here's the Code

/*
This sketch Sends a UDP message string

Anthony Kelly 07/11/2012

adapted from UDPSendReceive.pde:
21 Aug 2010
by Michael Margolis

This code is in the public domain.
*/

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>      
   // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x00, 0x85, 0x46 };
IPAddress ip(192, 168, 2, 177);
IPAddress rem_ip(192, 168, 2, 178);
unsigned int localPort = 8887;     
// local port to listen on
unsigned int remPort = 8888;      // remote port to send to


char rcv;            // Receive character (make this an Array for String Rx)
char snd;            // Transmit character (make this an Array for String Rx)

// Define the Arduino connections
int swPin0 = A0;
int swPin1 = A1;
int LEDPin = 9;
int sw0, sw1, swd0, swd1;


// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
 
// start the Ethernet and UDP:
  Serial.begin(9600);
  Ethernet.begin(mac,ip);
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  Udp.begin(localPort);
 
  pinMode(LEDPin,OUTPUT);

  // Set the pins to inputs and Enable the pullups so we don't need
  // external pullup resistors for the Switches
  pinMode(swPin0,INPUT);
  pinMode(swPin1,INPUT);
  digitalWrite(swPin0,HIGH); 
// Enable Pullup
  digitalWrite(swPin1,HIGH);  // Enable Pullup
}

void loop() { 
 
  if (RcvPkt()) {

    // We just received a UDP packet so Act on that.
    if (rcv=='1') {
      digitalWrite(LEDPin,HIGH);
      Serial.println("on");
    }
    else if (rcv=='0') {
      digitalWrite(LEDPin,LOW);
      Serial.println("off");
    }
  }
 
 
// Did Someone just Push a Button ?
  swd0 = sw0;                    // Remember the OLD value of the switch
  sw0 = digitalRead(swPin0);     // Get the NEW value
  swd1 = sw1;
  sw1 = digitalRead(swPin1);

  // If the OLD value was NOT pressed (1) and the NEW value is pressed (0)
  // then the key was just pressed.
  if (swd0 && !sw0) snd = '1';  
  else if (swd1 && !sw1) snd = '0';
  else snd = '-';


   // send a packet, to the IP address and port
  if (snd != '-') {
    Udp.beginPacket(rem_ip, remPort);
    Udp.write(snd);
    Udp.endPacket();
  }
 
  delay(100);
}

int RcvPkt() {

    // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());


    // read the packet into packetBufffer
    Udp.read(&rcv,1);
    Serial.println("Contents:");
    Serial.println(rcv);
  }
  return packetSize;
}

Step 6: Next Steps

 

The main ideas have been presented. Some next steps to try:

* Make the LED Dimmable under control of the remote Switch

* Make a Dimmable LED achieve constant brightness in the presence of Sensed Light Levels in the room
- Hint: When the Light Sensor senses increased brightness, the LED Dims and vice.versa
- Note : This is a MAJOR factor in energy efficiency. Having Room Lights too bright during periods of increased Natural Light wastes a lot of energy.

* Use Port Forwarding on your Router to make this work in different locations.

* Make the Message protocol more advanced to allow many different devices on the Network:
e.g.
{State_sw0, State_sw1, Sensor_val}
or
{"SWITCH", sw_number, sw_state}
{"LIGHT SENSOR", sensor_number, sensor_value}
etc...

* Make a Handshaking Protocol so Receiver has to send an ACKnowledge back to the Transmitter.

* Devise a Dynamic IP Addressing Scheme (Advanced).
<p>Grate project.</p><p>Check out my android app to add wifi and low bandwidth functionality.</p><p><a href="https://www.instructables.com/id/Home-Automation-Using-MQTT-and-NodeMcu-or-Arduino/" rel="nofollow">https://www.instructables.com/id/Home-Automation-Us...</a></p>
<p>what type of modem you use? for this application</p>
Great! =D <br>Congratulations!
nice.. hope to see the next steps soon.

About This Instructable

110,372views

912favorites

License:

Bio: Technologist, Electronic Engineer, sometime Coderdojo mentor.
More by akellyirl:Arduino Guitar Tuner Using DSP Techniques Add Amazon Echo Voice Control to You Smart Device Smart Home Thermostat 
Add instructable to: