Let's Confer
Home Automation Solution
How to create a complete home automation solution framework using Hass.io?
n this blog, I have explained how using raspberry pi hassio image, nodemcu and 8 channel
relay you can create a home automation solution, beyond sample project, this is an wonderful
I
framework which is using MQTT and websocket and more home automation devices can be
integrated easily with in this framework. Raspberry pi is working as a central hub for all
communications
Components used in projects
1.
Raspberry pi (one)
2.
NodeMCU
3.
3.3V and 5V Power supply module
4.
8 channel Relay
5.
8 Electrical plug points for controlling all eight electrical equipment in a room
6.
Jumper cables
Software required
Install Hassio image in raspberry pi
https://www.home-assistant.io/hassio/installation/
1
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
Configure ESPHome and Mosquito broker (MQTT) and start MQTT in default 8183 port
Now prepare NODEMCU & Relay module and Electrical main circuit for one sample room (in my case
it is bed room)
2
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
Program inside NodeMCU
Create a index.html, style.css and CSS Stylesheet and script.js as main JavaScript for the
NodeMCU WebSocket website
Minify style.css and script.js using node-minify (https://www.npmjs.com/package/node-minify)
Add minified css and js script content within index.html and then minify the html
Using 7z utility create a gzip for minified html (it has created a 3KB file for me after all the process,
where initial html, JavaScript and CSS total size was 13KB
Now convert gzip content to cpp compatible hex code using http://tomeko.net/
Add these Hex code to main .ino program code as indexhtml.h file
Sample Arduino code for NodeMCU to control 8channel relay and communicate using MQTT
with Hassio
Code is same as https://github.com/debsahu/PCF8574_8Relay/ Special thanks to developer Mr.
Debashish Sahu for sharing such a wonderful code with development community Only thing is that I
have not used PCF8574AP, so my connections and code is little different. I have changed following
functions
uint8_t RelayName(uint8_t pinno)
{
uint8_t relaynm;
if (pinno == 0) relaynm = 5;
else if (pinno == 1) relaynm = 4;
else if (pinno == 2) relaynm = 0;
else if (pinno == 3) relaynm = 2;
else if (pinno == 4) relaynm = 14;
else if (pinno == 5) relaynm = 12;
else if (pinno == 6) relaynm = 13;
else if (pinno == 7) relaynm = 15;
/* RELAY1 5 //D1
RELAY2 4 //D2
RELAY3 0 //D3
RELAY4 2 //D4
RELAY5 14 //D5
RELAY6 12 //D6
RELAY7 13 //D7
RELAY8 15 //D8*/ Code for PCF8574AP changed
return relaynm;
}
Also I have given a static IP to NodeMCU , in order to avoid dynamic IP change, Code for that
// Set your Static IP address
IPAddress local_IP(192, 168, 0, 112);
// Set your Gateway IP address
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);
//optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
And add below line
WiFi.config(local_IP,gateway, subnet,primaryDNS,secondaryDNS);
After
WiFi.mode(WIFI_STA); // Make sure you're in station mode
After starting Node MCU with this code for first time, it will create / register following entities in
Home Assistant
3
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
Now use this entities in main page and you are ready to control relays
If you like to use webpage inside NodeMCU instead of Home Assistant URL, then in mobile type
http://192.168.0.112 (my static ip) and you will get this type of page
4
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
Alternative Project
If you do not want a robust MQTT architecture only as simple Mobile driven start / stop, then you do
not need raspberry pi and Hassio, only following components are required.
NodeMCU
3.3V and 5V Power supply module
8 channel Relay
8 Electrical plug points for controlling all eight electrical equipment in a room
Jumper cables
Connection and circuit diagrams will be same, optionally you can use IR vs1838b sensor for
controlling Relays using a remote control
————————————————————————————————————————————
Code for Alternative Project
#include
#include
uint16_t RECV_PIN = D10;
IRrecv irrecv(RECV_PIN);
decode_results results;
//use your wifi ssid and password
const char* ssid = "Vodafone";
const char* password = "*******";
IPAddress local_IP(192, 168, 1, 112);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);
//optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
int LED1 = D1;
int LED2 = D2;
int LED3 = D3;
int LED4 = D4;
5
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
int LED5 = D5;
int LED6 = D6;
int LED7 = D7;
int LED8 = D8;
int swval[] = {0,0,0,0,0,0,0,0,0};
WiFiServer server(80);
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
// Start the receiver
delay(10);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
digitalWrite(LED8, HIGH);
// Connect to WiFi network
WiFi.mode(WIFI_STA); //you can use different modes, also make sure to change
the mode in the rewifi() function
WiFi.begin(ssid, password);
WiFi.config(local_IP,gateway, subnet,primaryDNS,secondaryDNS);
delay(20000); //you can change the delay. Incase of electricy cut-off, when
power returns, the router takes about 15sec to make the wifi functional.
server.begin();
}
void relayswitch(int i,int j){
if(swval[j]==1){
digitalWrite(i, HIGH);
swval[j]=0;
}else{
digitalWrite(i, LOW);
swval[j]=1;
}
}
void rewifi(){
if(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println(WiFi.status());
}
void loop() {
Serial.println("hellow");
6
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
//ir receiveing code, change with your ir codes
if (irrecv.decode(&results)) {
unsigned int ircode = results.value;
if(ircode==284133495){
relayswitch(LED1,1);
}else if(ircode==284117175){
relayswitch(LED2,2);
}else if(ircode==284149815){
relayswitch(LED3,3);
}else if(ircode==284109015){
relayswitch(LED4,4);
}else if(ircode==284141655){
relayswitch(LED5,5);
}else if(ircode==284125335){
relayswitch(LED6,6);
}else if(ircode==284155795){
relayswitch(LED7,7);
}else if(ircode==284104935){
relayswitch(LED8,8);
}else if(ircode==284137575){ //Button in ir remote to reconnect wifi
rewifi();
}
irrecv.resume();
// Receive the next value
}
delay(100);
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
client.flush();
// Set LED according to the request
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("");
client.println("");
client.println("");
client.println("");
//client.println("
");
// Return the response LED1
client.print("Relay 1 is now: ");
// Control LED 1
if (request.indexOf("/LED1=ON") != -1)
{
digitalWrite(LED1, LOW);
swval[1] = 1;
}
if (request.indexOf("/LED1=OFF") != -1)
{
digitalWrite(LED1, HIGH);
swval[1] = 0;
}
if(swval[1]==1){client.print("ON");}
if(swval[1]==0){client.print("OFF");}
//client.println("
7
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
");
client.println("TURN ON ");
client.println("TURN OFF
");
// Return the response LED2
//client.println("
");
client.print("Relay 2 is now: ");
// Control LED 2
if (request.indexOf("/LED2=ON") != -1)
{
digitalWrite(LED2, LOW);
swval[2] = 1;
}
if (request.indexOf("/LED2=OFF") != -1)
{
digitalWrite(LED2, HIGH);
swval[2] = 0;
}
if(swval[2]==1){client.print("ON");}
if(swval[2]==0){client.print("OFF");}
//client.println("
");
client.println("TURN ON ");
client.println("TURN OFF
");
// Return the response LED3
//client.println("
");
client.print("Relay 3 is now: ");
// Control LED 3
if (request.indexOf("/LED3=ON") != -1)
{
digitalWrite(LED3, LOW);
swval[3] = 1;
}
if (request.indexOf("/LED3=OFF") != -1)
{
digitalWrite(LED3, HIGH);
swval[3] = 0;
}
if(swval[3]==1){client.print("ON");}
if(swval[3]==0){client.print("OFF");}
//client.println("
");
client.println("TURN ON ");
client.println("TURN OFF
");
// Return the response LED4
//client.println("
");
client.print("Relay 4 is now: ");
// Control LED 4
if (request.indexOf("/LED4=ON") != -1)
{
digitalWrite(LED4, LOW);
swval[4] = 1;
}
if (request.indexOf("/LED4=OFF") != -1)
{
digitalWrite(LED4, HIGH);
swval[4] = 0;
}
if(swval[4]==1){client.print("ON");}
8
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
if(swval[4]==0){client.print("OFF");}
//client.println("
");
client.println("TURN ON ");
client.println("TURN OFF
");
// Return the response LED5
//client.println("
");
client.print("Relay 5 is now: ");
// Control LED 5
if (request.indexOf("/LED5=ON") != -1)
{
digitalWrite(LED5, LOW);
swval[5] = 1;
}
if (request.indexOf("/LED5=OFF") != -1)
{
digitalWrite(LED5, HIGH);
swval[5] = 0;
}
if(swval[5]==1){client.print("ON");}
if(swval[5]==0){client.print("OFF");}
//client.println("
");
client.println("TURN ON ");
client.println("TURN OFF
");
// Return the response LED6
//client.println("
");
client.print("Relay 6 is now: ");
// Control LED 6
if (request.indexOf("/LED6=ON") != -1)
{
digitalWrite(LED6, LOW);
swval[6] = 1;
}
if (request.indexOf("/LED6=OFF") != -1)
{
digitalWrite(LED6, HIGH);
swval[6] = 0;
}
if(swval[6]==1){client.print("ON");}
if(swval[6]==0){client.print("OFF");}
//client.println("
");
client.println("TURN ON ");
client.println("TURN OFF
");
// Return the response LED7
//client.println("
");
client.print("Relay 7 is now: ");
// Control LED 7
if (request.indexOf("/LED7=ON") != -1)
{
digitalWrite(LED7, LOW);
swval[7] = 1;
}
if (request.indexOf("/LED7=OFF") != -1)
{
9
© 2020 Subhendu Datta Bhowmik,CSCP.
Digital Blogs
digitalWrite(LED7, HIGH);
swval[7] = 0;
}
if(swval[7]==1){client.print("ON");}
if(swval[7]==0){client.print("OFF");}
//client.println("
");
client.println("TURN ON ");
client.println("TURN OFF
");
// Return the response LED8
//client.println("
");
client.print("Relay 8 is now: ");
// Control LED 8
if (request.indexOf("/LED8=ON") != -1)
{
digitalWrite(LED8, LOW);
swval[8] = 1;
}
if (request.indexOf("/LED8=OFF") != -1)
{
digitalWrite(LED8, HIGH);
swval[8] = 0;
}
if(swval[8]==1){client.print("ON");}
if(swval[8]==0){client.print("OFF");}
//client.println("
");
client.println("TURN ON ");
client.println("TURN OFF
");
client.println("");
delay(1);
}
10
© 2020 Subhendu Datta Bhowmik,CSCP.