ESP32 Internet Controlled 10Ch HomeAutomation System Using Reyax MQTT Cloud.

ESP32 Internet Controlled 10Ch HomeAutomation System Using Reyax MQTT Cloud.

  • In this Article, we will learn how to make our own homeautomation system using our own MQTT cloud broker & we will also learn about mqtt protocol.
  • In this homeautomation System , we are able to control our home-appliances by an smartphone and monitor the real time status on the smartphone. 
  • This project will work from anywhere in the world, because this is a cloud based MQTT broker.

Reyax MQTT Cloud broker.

For making of this homeautomation project, I am going to use Reyax RYC1001 MQTT Iot cloud plateform. Which you can easily purchase from amazon.com. If in any case it is not available in your country or you are facing any problem while purchasing this Iot cloud plateform , then just contact sales team of reyax, they will guide you.

RYC1001 purchase link: http://amzn.to/3hAY5zp

REYAXhttp://reyax.com/product/

Reyax Support – sales@reyax.com

App Configuration.

To control the homeappliances through smartphone, we also need a MQTT Client in our smartphone, for that we will use iotonoff mobile appliaction, this app is available for both Android as well as for IOS ; You can easily download it from APP Store or from playstore.

What is MQTT?

MQTT is a lightweight, publish-subscribe network protocol that transports messages between devices. The protocol usually runs over TCP/IP, however, any network protocol that provides ordered, lossless, bi-directional connections can support MQTT. It is designed for connections with remote locations where resource constraints exist or the network bandwidth is limited. The protocol is an open OASIS standard and an ISO recommendation (ISO/IEC 20922).

Pic credit: mqqt.org

Components Required.

Designing the PCB.

To design the circuit and PCB, we used EasyEDA which is a browser based software to design PCBs.

Designing the circuit works like in any other circuit software tool, you place some components and you wire them together. Then, you assign each component to a footprint.

Having the parts assigned, place each component. When you’re happy with the layout, make all the connections and route your PC.

Save your project and export the Gerber files.

Ordering the PCBs at PCBWay.

This project is sponsored by PCBWay. PCBWay is a full feature Printed Circuit Board manufacturing service.

Turn your DIY breadboard circuits into professional PCBs – get 10 boards for approximately $5 + shipping (which will vary depending on your country).

Once you have your Gerber files, you can order the PCB. Follow the next steps.

1. Download the Gerber files – click here to download the .zip file.

2. Go to PCBWay website and open the PCB Instant Quote page. 

3. PCBWay can grab all the PCB details and automatically fills them for you. Use the “Quick-order PCB (Autofill parameters)”.

4. Press the “+ Add Gerber file” button to upload the provided Gerber files.

And that’s it. You can also use the OnlineGerberViewer to check if your PCB is looking as it should.

Now select the shipping method , the one you prefer and has cost efficient.

You can increase your PCB order quantity and change the solder mask color. I’ve ordered the green color.

Once you’re ready, you can order the PCBs by clicking “Save to Cart” and complete your order.

PCBWay has lots of other staggering solder mask, Now they can produce pink, orange, grey, even the transparent solder mask. 

Apart from this they also provide Black core PCB.

After approximately one week using the DHL shipping method, I received the PCBs at my place.

As usual, everything comes well packed, and the PCBs are really high-quality. 

The letters on the silkscreen are really well-printed and easy to read. Additionally, the solder sticks easily to the pads.

Let's Make it...!

Now grab all the components whose list is mention above, and soldered on the PCB.

After soldiering rest of components PCB look like this neat, clean and well arranged.

Connections.

Connect All the Bulb (any device) as shown in the schematic below.

Make all the connections as per the above circuit diagram.

Code.

Download the below code and open in your arduino ide, you need to make few modifications in this code which is mentiond below.

#include <WiFi.h>
#include <PubSubClient.h>

const int Relay1 = 26;
const int Relay2 = 27;
const int Relay3 = 14;
const int Relay4 = 12;
const int Relay5 = 13;
const int Relay6 = 21;
const int Relay7 = 2;
const int Relay8 = 4;
const int Relay9 = 18;
const int Relay10 = 19;





// Update these with values suitable for your network.
//WIFI SETUP
const char* ssid = "XXXXXXXXXXXXXXX"; //WiFI Name
const char* password = "XXXXXXXXXX"; //WiFi Password
const char* mqtt_server = "iot.reyax.com";

//MQTT SETUP
const char* username = "XXXXXXX"; //Reyax Useename
const char* pass = "XXXXXXXXX"; //Reyax Password
const char* topic = "api/request";
const char* clientID = "ESP8266Client-"; // client id
//String msgStr = "";      // MQTT message buffer

#define sub1 "switch1"
#define sub2 "switch2"
#define sub3 "switch3"
#define sub4 "switch4"
#define sub5 "switch5"
#define sub6 "switch6"
#define sub7 "switch7"
#define sub8 "switch8"
#define sub9 "switch9"
#define sub10 "switch10"




WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (300)
char msg[MSG_BUFFER_SIZE];
int value = 0;

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    
    delay(500);
    Serial.print(".");
    
  }
  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
 
  Serial.println(WiFi.localIP());
}

void reconnect() {
  while (!client.connected()) {
    if (client.connect(clientID, username, pass)) {
      Serial.println("MQTT connected");
      client.subscribe(sub1);
      client.subscribe(sub2);
      client.subscribe(sub3);
      client.subscribe(sub4);
      client.subscribe(sub5);
      client.subscribe(sub6);
      client.subscribe(sub7);
      client.subscribe(sub8);
      client.subscribe(sub9);
      client.subscribe(sub10);
         
      //Serial.println(sub1);
      
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in few seconds");
      delay(500);
      


   }
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived in topic: ");
  
  Serial.println(topic);
  Serial.print("Message:");
  for (int i = 0; i< length; i++) {
  Serial.print((char)payload[i]);
  }
  Serial.println();  
  Serial.print("Message size :");
  Serial.println(length);
  Serial.println();
  Serial.println("-----------------------");
 
  
  if (strstr(topic, sub1))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay1, LOW);   // Turn the LED on (Note that LOW is the voltage level
      Serial.print("Relay1 LOW");
    } else {
      digitalWrite(Relay1, HIGH);  // Turn the LED off by making the voltage HIGH
      Serial.print("Relay1 HIGH");
    }
  }

  else if ( strstr(topic, sub2))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay2, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay2, HIGH);  // Turn the LED off by making the voltage HIGH
      
    }
  }
  else if ( strstr(topic, sub3))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay3, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay3, HIGH);  // Turn the LED off by making the voltage HIGH
      
    }
  }
  else if ( strstr(topic, sub4))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay4, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay4, HIGH);  // Turn the LED off by making the voltage HIGH
     
    }
  }
  else if ( strstr(topic, sub5))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay5, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay5, HIGH);  // Turn the LED off by making the voltage HIGH
      
    }
  }else if ( strstr(topic, sub6))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay6, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay6, HIGH);  // Turn the LED off by making the voltage HIGH
      
    }
  }else if ( strstr(topic, sub3))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay7, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay7, HIGH);  // Turn the LED off by making the voltage HIGH
      
    }
  }else if ( strstr(topic, sub8))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay8, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay8, HIGH);  // Turn the LED off by making the voltage HIGH
      
    }
  }else if ( strstr(topic, sub9))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay9, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay9, HIGH);  // Turn the LED off by making the voltage HIGH
      
    }
  }
  else if ( strstr(topic, sub10))
  {
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '0') {
      digitalWrite(Relay10, LOW);   // Turn the LED on (Note that LOW is the voltage level
      
    } else {
      digitalWrite(Relay10, HIGH);  // Turn the LED off by making the voltage HIGH
      
    }
  }
  else
  {
    Serial.println("unsubscribed topic");
  }
}



void setup() {
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  pinMode(Relay5, OUTPUT);
  pinMode(Relay6, OUTPUT);
  pinMode(Relay7, OUTPUT);
  pinMode(Relay8, OUTPUT);
  pinMode(Relay9, OUTPUT);
  pinMode(Relay10, OUTPUT);
  
   Serial.begin(115200);

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
    
    }
    
   
  client.loop();
  delay(50);

 
}

Replace the ‘X’s with the SSID and password of your router.

In this section replace the ‘X’s with the Reyax username and password.

You will get the username and password in your registered email id.

After doing these modifications, upload the code in your esp32 board after selecting right board and COM port.

Now your homeautomation system is ready to work.

If you have any question or queries regarding this project please comment below.


Thank you so much for reading. 

Leave a Reply

Your email address will not be published.