Extension - Control power circuit

Switching power circuits - sockets, lights, blinds, pumps and other devices under 110-230V is one of the essential functions of an intelligent house. At Pi-Home, we are using Arduino Mega boards with Ethernet Shield, which control Arduino's compatible SSR relays connected with high power relays of the switchboard.

What we'll need

Basic - OpenHAB na Raspberry Pi

Arduino Mega with Ethernet Shield (DIN holder)

Arduino SSR relay 8x2A/230V (DIN holder)

Installation relay ie EATON Z-R230/S or multiphase EATON Z-R230/4S

DC Power Source 5V 40W (DIN)

 

Power and scheme

The Arduino Mega is connected to the RPi via USB and via LAN to the internal network. An 8x2A/230V relay is attached to the Arduino pins. Arduin relay is powered from an external source of DC 5V 40W. Based on the number of controlled outputs, you can place the necessary amount of these relays on the DIN bar and link them to Arduino. This arduino relays controll the installation relay in the power part of the switchboard. Use multi-phase relays for multi-phase circuits/appliances. High power part of the switchboard should be projected by certified engineer with revision! This is the same as classic home electric appliance.

Pi-Home - ovládání silových obvodů

Software

To control the Arduino relay, we provided a simple sketch that subscribe the MQTT communications. In the case of a specific topic and value is subscribed, a relay is switched on and close the circuit. If there are more than one of these relay boards, I recommend having them on a separate Arduino and not combining with sensors or PIRs on one Arduino.

Example sketch

In this simple sketch SPI.h, Ethernet.h and PubSubClient.h libraries are used. The Arduino directory must contain a Makefile (filename) with the content below. Makefile contains what libraries we will load and where they are located. You can search libraries online and download. Also, there is a unique Arduino specification in Makefile. We are using a specific name under path /dev/serial/by-id/. You can find there something like usb-Arduinoxxxx Simply go to the folder and try to plugin/disconnect given Arduino and record what name appears here. Copy the device name to Makefile below. In the sketch there are declared variables (pins) for relays and the MQTT subscribe logic, some reconnect, etc.

Example of "Makefile"

ARDUINO_DIR = /usr/share/arduino
BOARD_TAG    = mega2560
ARDUINO_PORT = /dev/serial/by-id/usb-Arduino__www.arduino.cc__0042_85531303630351119291-if00 (substitute by your ID!)
USER_LIB_PATH = /usr/share/arduino/libraries
ARDUINO_LIBS = Ethernet SPI pubsubclient
include /usr/share/arduino/Arduino.mk

Example sketch " Relay.ino"

    /*
     Example MQTT-switch-relay script

      - connects to an MQTT server
      - publishes "hello world" to the topic "relay"
      - subscribes to the topic "relay"
      - controls as many relays as defined
      - turns on/off a specific led when it receives a specific "on"/"off" from the "relay" topic
      - multiple arduino's with same generic sketch can run parallel to each other
      - multiple arduino's need each to have a unique ip-addres, unique mac address and unique MQTT client-ID
      - tested on arduino-mega with W5100 ethernet shield
    */

    //------------------------------------------------------------------------------

    #include <SPI.h>
    #include <Ethernet.h>
    #include <PubSubClient.h>

    // Set relay variables to Arduino digital pins

    //Rele board X1
    int Rel_X1_1 = 22;
    int Rel_X1_2 = 23;
    int Rel_X1_3 = 24;
    int Rel_X1_4 = 25;
    int Rel_X1_5 = 26;
    int Rel_X1_6 = 27;
    int Rel_X1_7 = 28;
    int Rel_X1_8 = 29;

    // Set variables to act as virtual switches
    // Set variable values initially to LOW (and not HIGH)

     //Rele board X1
    int ValueX1_1 = LOW;
    int ValueX1_2 = LOW;
    int ValueX1_3 = LOW;
    int ValueX1_4 = LOW;
    int ValueX1_5 = LOW;
    int ValueX1_6 = LOW;
    int ValueX1_7 = LOW;
    int ValueX1_8 = LOW;


    //---------------------------------------------------------------------------

    // Arduino MAC address must be unique for every node in same network
    // To make a new unique address change last letter
    // Arduino 0
    byte mac[]    = { 0xCC, 0xFA, 0x06, 0xCB, 0x19, 0x00 };  

    // Unique static IP address of this Arduino 0
    IPAddress ip(192,168,4,30);


    // IP Address of your MQTT broker (OpenHAB server)
    byte server[] = { 192,168,4,40 };

    // Handle and convert incoming MQTT messages ----------------------------------------

    void callback(char* topic, byte* payload, unsigned int length) {
      // handle message arrived
      String content="";
      char character;
      for (int num=0;num<length;num++) {
          character = payload[num];
          content.concat(character);
      }   

    // Set specific virtual switches on basis of specific incoming messages ----------------------------
      //REL1
      if (content == "X1_1on") {    ValueX1_1 =  HIGH; }
      if (content == "X1_1off"){    ValueX1_1 =  LOW;  }
      if (content == "X1_2on") {    ValueX1_2 =  HIGH; }
      if (content == "X1_2off"){    ValueX1_2 =  LOW;  }
      if (content == "X1_3on") {    ValueX1_3 =  HIGH; }
      if (content == "X1_3off"){    ValueX1_3 =  LOW;  }
      if (content == "X1_4on") {    ValueX1_4 =  HIGH; }
      if (content == "X1_4off"){    ValueX1_4 =  LOW;  }
      if (content == "X1_5on") {    ValueX1_5 =  HIGH; }
      if (content == "X1_5off"){    ValueX1_5 =  LOW;  }
      if (content == "X1_6on") {    ValueX1_6 =  HIGH; }
      if (content == "X1_6off"){    ValueX1_6 =  LOW;  }
      if (content == "X1_7on") {    ValueX1_7 =  HIGH; }
      if (content == "X1_7off"){    ValueX1_7 =  LOW;  }
      if (content == "X1_8on") {    ValueX1_8 =  HIGH; }
      if (content == "X1_8off"){    ValueX1_8 =  LOW;  }
      


      // Set digital pin states according to virtual switch settings
      digitalWrite(Rel_X1_1, ValueX1_1);  
      digitalWrite(Rel_X1_2, ValueX1_2);
      digitalWrite(Rel_X1_3, ValueX1_3);
      digitalWrite(Rel_X1_4, ValueX1_4);
      digitalWrite(Rel_X1_5, ValueX1_5);
      digitalWrite(Rel_X1_6, ValueX1_6);
      digitalWrite(Rel_X1_7, ValueX1_7);
      digitalWrite(Rel_X1_8, ValueX1_8);
      

    }

    // Initiate instances -----------------------------------
    EthernetClient arduino0;
    PubSubClient client(server, 1883, callback, arduino0);
    //-------------------------------------------------------


    void setup()
    {
      digitalWrite(Rel_X1_1, HIGH);
           pinMode(Rel_X1_1,OUTPUT);
      digitalWrite(Rel_X1_2, HIGH);
           pinMode(Rel_X1_2,OUTPUT);
      digitalWrite(Rel_X1_3, HIGH);
           pinMode(Rel_X1_3,OUTPUT);
      digitalWrite(Rel_X1_4, HIGH);
           pinMode(Rel_X1_4,OUTPUT);
      digitalWrite(Rel_X1_5, HIGH);
           pinMode(Rel_X1_5,OUTPUT);
      digitalWrite(Rel_X1_6, HIGH);
           pinMode(Rel_X1_6,OUTPUT);
      digitalWrite(Rel_X1_7, HIGH);
           pinMode(Rel_X1_7,OUTPUT);
      digitalWrite(Rel_X1_8, HIGH);
           pinMode(Rel_X1_8,OUTPUT);

      // Setup ethernet connection to MQTT broker
      Ethernet.begin(mac, ip);
      if (client.connect("arduino0", "openhabian", "HESLO K MQTT BROKERU")) {                                  // change as desired - clientname must be unique for MQTT broker
        client.publish("relay","hello world - here arduino0 ip 192.168.4.40");
        Serial.println("connected");
        client.subscribe("relay");                                        // subscribe to topic "relay"
      }
    }


    //-----------------------------------------------
    long lastReconnectAttempt = 0;

    boolean reconnect() {
      if (client.connect("arduino0", "openhabian", "HESLO K MQTT BROKERU")) {
        // Once connected, publish an announcement...
        client.publish("relay","reconnected");
        // ... and resubscribe
        client.subscribe("relay");
      }
      return client.connected();
    }
    //----------------------------------------------

    void loop()
    {

      if (!client.connected()) {
        long now = millis();
        if (now - lastReconnectAttempt > 5000) {
          lastReconnectAttempt = now;
          // Attempt to reconnect
          if (reconnect()) {
            lastReconnectAttempt = 0;
          }
        }
      } else {
        // Client connected

        client.loop();
    }

    }

    // End of sketch ---------------------------------

OpenHAB

In OpenHAB, create a relay.items file that lists the switches (type switch) and assigns the name, group, and especially the pettern of the MQTT message to which Arduino will respond. As you can see in sketch, the shape "X1_7on" is for ON and "X1_7off" is for OFF action.

Example relay.items

  /* Here are the lights controlled by relays */
    
    /*SV1.1*/ Switch Light_Gar_1     "Light garden"   {mqtt=">[mymosquitto:relay:command:ON:X1_1on],>[mymosquitto:relay:command:OFF:X1_1off]"}
    /*SV1.2*/ Switch Light_Gar_2     "Light terase"    {mqtt=">[mymosquitto:relay:command:ON:X1_2on],>[mymosquitto:relay:command:OFF:X1_2off]"}
    /*SV1.3*/ Switch Light_Room_3    "Light saloon"     {mqtt=">[mymosquitto:relay:command:ON:X1_3on],>[mymosquitto:relay:command:OFF:X1_3off]"}
    /*SV1.4*/ Switch Light_Room_4    "Light toilet"   {mqtt=">[mymosquitto:relay:command:ON:X1_4on],>[mymosquitto:relay:command:OFF:X1_4off]"}
    /*SV1.5*/ Switch Light_Room_5    "Light bar"       {mqtt=">[mymosquitto:relay:command:ON:X1_5on],>[mymosquitto:relay:command:OFF:X1_5off]"}
    /*SV1.6*/ Switch Light_Room_6    "Light pantry"      {mqtt=">[mymosquitto:relay:command:ON:X1_6on],>[mymosquitto:relay:command:OFF:X1_6off]"}
    /*SV1.7*/ Switch Light_Room_7    "Light entrance"     {mqtt=">[mymosquitto:relay:command:ON:X1_7on],>[mymosquitto:relay:command:OFF:X1_7off]"}
    /*SV1.8*/ Switch Light_Room_8    "Light kitchen"   {mqtt=">[mymosquitto:relay:command:ON:X1_8on],>[mymosquitto:relay:command:OFF:X1_8off]"}

   
  

And that's it! Try going to your OpenHAB in the browser (http://192.168.4.40:8080 in the example) and select BasicUI. You should see 8 switches and you can turn them on/off over the appliction. This solution can be extended indefinitely. We have experience with seamless installation with up to 200 relays.

Tip: One Arduino Mega board with Ethernet shield you can connect up to 6 pieces of 8x SSR relay board which is up to 48 lights/sockets/pumps etc.

Add comment

Nejlepší články z blogu

Centrální tablet pro chytrý dům
Wall tablet to controll your smart homeRating: 
100%

Wall tablet or older smartphone is perfectly suited as a central indicator of the state of the home, as well as controlling it. It usually replaces the video doorbell and as example, you can play your favorite music or read recipes on it during cooking in your kitchen. In the article, we describe a functional example of such a "wall" tablet and its settings in so-called kiosk mode so, display doesn't run at night and only reacts when someone is nearby.

Jablotron and smarthome synchronization
Connecting Jablotron with OpenHABRating: 
0%

In this article we will show you how to install Jablotron binding in OpenHAB. This binding helps us to synchronize states from Jablotron alarm to OpenHAB. Typically, you can force to switch off lights, close water pipe and circulation, switch off sockets or whatever you want to do when you leave home and set alarm.

Advanced disk cloningRating: 
0%

The classic disk cloning tool in linux is the dd command. This command creates a image of disk or extracts the contents from the image to disk. The dd tool needs to be handled carefully and you need to understand its use. We'll show a more advanced technique of creating a disc image that contains unallocated space. A classic example is when I have a Raspbian operating system installed on a 250 GB disc, but there's only a tiny boot partition (e.g. sda1) and my own operating system partition (sda2) which together have 15 GB. Instead of saving a 250 GB image, I would like to save only two partitions with 15 GB, so that the image is smaller and can be expanded to 16 or 32 GB flash.

iOS OpenVPN Connection SettingsRating: 
0%

In this article you will find detailed instructions on how to connect to OpenVPN from iOS devices.

Android OpenVPN Connection SettingsRating: 
40%

In this article you will find detailed instructions on how to connect to OpenVPN on Android.

OpenVPN on MikroTik routersRating: 
100%

If you have a smart-home, NAS, etc., it's a good idea to think about safe access to your network when you are not at home. Currently I recommend commercial router like Turris or a cheaper option of MikroTik routers. In the other hand, Raspberry Pi can be used as OpenVPN server too. In this article, we will show how to set up secure access to your home network from the internet using OpenVPN on MikroTik routers.

Video intercom Doorbird in OpenHABRating: 
0%

Native support for OpenHAB is provided by video intercom by Doorbird company. The solution is powered and connected by a UTP cable and has its own IP address to make it easier for you to install at the gate. OpenHAB is then able to import images from the intercom, visit history, manage gate opening on the button and by the phone, do not disturb feature during night, alarm feature etc. In the instructions we guide you how to use OpenHAB with Doorbird D101.

Advanced PIR sensors features in OpenHABRating: 
0%

The HC-SR501 PIR sensor contains mechanical adjustement for detect range and time. But in an intelligent house, we want to be able change time how long the light will be active and change whether the light will be controlled by PIR or not. Below it's shown how to program the PIR sensor to enable this features.

Measure tank level water OpenHAB
Measuring the water volume in reservoirRating: 
100%

In this article, you can find how to solve the measuring the water level in the reservoir (e.g. rainwater reservoir). The values are converted into the volume in liters. This enables you to fill the volume tank when needed or predict any other situations.