In this post we will learn how to make our own homeautomation system using our own MQTT cloud broker.
In this homeautomation System , we are able to control our home-appliances by an smartphone and also we can control it with manual switches 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, Iam 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, the will guide you.
RYC1001 purchase link: http://amzn.to/3hAY5zp
REYAX: http://reyax.com/product/
Reyax Support – sales@reyax.com
App Configuration
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 PCB.
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 Black color.
Once you’re ready, you can order the PCBs by clicking “Save to Cart” and complete your order.
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.
Connection of bulb & switches.
Connect all the bulbs and switches as shown in schematic below.
Code
Copy this code and modify the necessary details, and upload the code to your ESP32 after selecting right board and COM port.
/* Long message ESP8266 MQTT example This sketch demonstrates sending arbitrarily large messages in combination with the ESP8266 board/library. It connects to an MQTT server then: - publishes "hello world" to the topic "outTopic" - subscribes to the topic "greenBottles/#", printing out any messages it receives. NB - it assumes the received payloads are strings not binary - If the sub-topic is a number, it publishes a "greenBottles/lyrics" message with a payload consisting of the lyrics to "10 green bottles", replacing 10 with the number given in the sub-topic. It will reconnect to the server if the connection is lost using a blocking reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to achieve the same result without blocking the main loop. To install the ESP8266 board, (using Arduino 1.6.4+): - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": http://arduino.esp8266.com/stable/package_esp8266com_index.json - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" - Select your ESP8266 in "Tools -> Board" */ #include <WiFi.h> #include <PubSubClient.h> const int Relay1 = 15; const int Relay2 = 2; const int Relay3 = 4; const int Relay4 = 22; #define switch1 32 #define switch2 35 #define switch3 34 #define switch4 39 //WiFi Status LED #define wifiLed1 26 #define wifiLed2 25 #define wifiLed3 27 int switch_ON_Flag1_previous_I = 0; int switch_ON_Flag2_previous_I = 0; int switch_ON_Flag3_previous_I = 0; int switch_ON_Flag4_previous_I = 0; // Update these with values suitable for your network. //WIFI SETUP const char* ssid = "XXXXXXXXXX"; //WiFI Name const char* password = "XXXXXXXXX"; //WiFi Password const char* mqtt_server = "iot.reyax.com"; //MQTT SETUP const char* username = "XXXXXXXX"; //Reyax Useename const char* pass = "XXXXXXXX"; //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 pub1 "switch1_status" #define pub2 "switch2_status" #define pub3 "switch3_status" #define pub4 "switch4_status" 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) { digitalWrite(wifiLed1, HIGH); delay(250); Serial.print("."); digitalWrite(wifiLed1, LOW); delay(250); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); digitalWrite(wifiLed1, HIGH); Serial.println(WiFi.localIP()); } void reconnect() { while (!client.connected()) { if (client.connect(clientID, username, pass)) { Serial.println("MQTT connected"); digitalWrite(wifiLed2, HIGH); client.subscribe(sub1); client.subscribe(sub2); client.subscribe(sub3); client.subscribe(sub4); //Serial.println(sub1); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in few seconds");digitalWrite(wifiLed1, LOW);digitalWrite(wifiLed2, LOW); delay(200); digitalWrite(wifiLed1, HIGH); delay(200);// wait few sec and retry //manual control when internet is not connected digitalWrite(Relay1, digitalRead(switch1)); digitalWrite(Relay2, digitalRead(switch2)); digitalWrite(Relay3, digitalRead(switch3)); digitalWrite(Relay4, digitalRead(switch4)); } } } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived in topic: "); digitalWrite(wifiLed3, HIGH);delay(50); 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("-----------------------"); digitalWrite(wifiLed3, LOW); 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 { Serial.println("unsubscribed topic"); } } void manual_control(){ if (digitalRead(switch1) == LOW) { if (switch_ON_Flag1_previous_I == 0 ) { digitalWrite(Relay1, LOW); client.publish(pub1, "0"); Serial.println("Relay1- ON"); switch_ON_Flag1_previous_I = 1; } } if (digitalRead(switch1) == HIGH ) { if (switch_ON_Flag1_previous_I == 1) { digitalWrite(Relay1, HIGH); client.publish(pub1, "1"); Serial.println("Relay1 OFF"); switch_ON_Flag1_previous_I = 0; } } if (digitalRead(switch2) == LOW) { if (switch_ON_Flag2_previous_I == 0 ) { digitalWrite(Relay2, LOW); client.publish(pub2, "0"); Serial.println("Relay1- ON"); switch_ON_Flag2_previous_I = 1; } } if (digitalRead(switch2) == HIGH) { if (switch_ON_Flag2_previous_I == 1) { digitalWrite(Relay2, HIGH); client.publish(pub2, "1"); Serial.println("Relay1 OFF"); switch_ON_Flag2_previous_I = 0; } } if (digitalRead(switch3) == LOW) { if (switch_ON_Flag3_previous_I == 0 ) { digitalWrite(Relay3, LOW); client.publish(pub3, "0"); Serial.println("Relay1- ON"); switch_ON_Flag3_previous_I = 1; } } if (digitalRead(switch3) == HIGH ) { if (switch_ON_Flag3_previous_I == 1) { digitalWrite(Relay3, HIGH); client.publish(pub3, "1"); Serial.println("Relay1 OFF"); switch_ON_Flag3_previous_I = 0; } } if (digitalRead(switch4) == LOW) { if (switch_ON_Flag4_previous_I == 0 ) { digitalWrite(Relay4, LOW); client.publish(pub4, "0"); Serial.println("Relay1- ON"); switch_ON_Flag4_previous_I = 1; } } if (digitalRead(switch4) == HIGH ) { if (switch_ON_Flag4_previous_I == 1) { digitalWrite(Relay4, HIGH); client.publish(pub4, "1"); Serial.println("Relay1 OFF"); switch_ON_Flag4_previous_I = 0; } } delay(100); } void setup() { pinMode(Relay1, OUTPUT); pinMode(Relay2, OUTPUT); pinMode(Relay3, OUTPUT); pinMode(Relay4, OUTPUT); pinMode(switch1, INPUT); pinMode(switch2, INPUT); pinMode(switch3, INPUT); pinMode(switch4, INPUT); pinMode(wifiLed1, OUTPUT); pinMode(wifiLed2, OUTPUT); pinMode(wifiLed3, OUTPUT); digitalWrite(wifiLed1, HIGH); delay(100); digitalWrite(wifiLed2, HIGH); delay(100); digitalWrite(wifiLed3, HIGH); delay(100); digitalWrite(wifiLed1, LOW); digitalWrite(wifiLed2, LOW); digitalWrite(wifiLed3, LOW); delay(500); Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); } void loop() { if (!client.connected()) { reconnect(); } else{ manual_control(); } client.loop(); delay(50); }