Kategorie
Extension - Control power circuit
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.
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