Hey, what's up, Guys! Akarsh here from CETech.
In the first part of this tutorial, we have covered the basics of MQTT, How it works, its advantages, and insight into the Reyax MQTT broker. In this tutorial, we are going to apply our gained knowledge and create a home automation board which is like a board that is inside a smart plug. We will be using ESP8266 with a relay board and for the time being, we will use an LED in place of any appliance. When the board is powered ON and connected to the broker, we will be able to control the onboard LED over the internet through an app installed on our mobiles or tablets. Sound Fun isn't it? Another great thing is that we are going to mount all these on a PCB so there is no mess due to wires and also we can modify it to have 12 relays and control 12 different appliances through a single board. You are definitely going to like it so let's not waste any time and move straight to the business end.
Get PCBs for Your Projects Manufactured
You must check out PCBWAY for ordering PCBs online for cheap!
You get 10 good-quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto PCBWAY to get them manufactured with good quality and quick turnaround time. Check out their online Gerber viewer function. PCBGOGO takes care of its customers a lot that's why they offer reward points with every purchase that you make from them these reward points can be redeemed for various useful items from their gift shop.
What is a Relay
The Relay Module is a convenient board that can be used to control high voltage, high current loads such as lamps and AC loads. A power relay module is an electrical switch that is operated by an electromagnet. The electromagnet is activated by a separate low-power signal from a microcontroller. When activated, the electromagnet pulls to either open or close an electrical circuit It is designed to be easily interfaced with microcontrollers such as Arduino, ESP8266, etc. The relay modules generally have the following terminals:
NC(Normally closed)= The normally closed configuration is used when you want the relay to be closed by default, meaning the current is flowing unless you send a signal from the Arduino to the relay module to open the circuit and stop the current
NO(Normally Open)= The normally open configuration works the other way around: the relay is always open, so the circuit is broken unless you send a signal from the Arduino to close the circuit.
Common pin= It is generally in the middle
Input pins= These are the pins through which the relay is connected to the controlling device.
If not mentioned on the module we can find out the NC pin using a multimeter in continuity setting by connecting the common with the other terminal through multimeter probes if it creates a beep sound then the pin is NC.
Reyax MQTT Broker
RYC1001 from Reyax is a cloud platform suitable for low-data-volume and power-saving devices. Using the MQTT protocol, you can use simple commands for your applications, monitor end devices, and easily establish your IoT connection. It can be used as long as the device supports the MQTT protocol. The compatibility test of all mainstream IoT communication modules has been completed. It can be used on Android, iOS, Windows, and Linux. The purpose is to allow enterprises to enter the cloud platform at a low cost. REYAX RYC1001 is built on a stable AWS service, we can use simple instructions to integrate the IoT cloud platform quickly as well. Some Important Features of the Reyax RYC1001 MQTT Broker are:-
Built on a stable AWS service
Use MQTT protocol suitable for low-data-volume and power-saving mode
Devices that support the MQTT protocol can be used.
Compatibility testing of all mainstream IoT communication modules is completed
Support Android, iOS, Windows, Linux
Low-cost access to the cloud platform
Use simple instructions to quickly integrate the IoT cloud platform
To get some more details about the Reyax RYC1001 MQTT Broker you can check the datasheet of the product from here.
About the Project
The main components of the board that we are going to make today are ESP8266, Relay, Pushbuttons, USB to Serial Converter, Resistors, etc. What our final product will do is that it will be able to connect to the Internet using the onboard ESP8266 chip to be specific it will be connected to the MQTT Broker on the Internet that we have received from Reyax. On the other side, we will have a dashboard created on an application that is also connected to the same broker over the Internet. The dashboard that we have created will be configured in such a manner that it would be able to send ON and OFF commands to the board connected to our broker. From the broker, the board will receive that command and according to that command the relay will either close the circuit or open the circuit eventually turning ON or OFF the onboard LED. We need not create a new board every time we want to control a new device but we can modify this board as well and connect 12 relays to it making us capable of controlling 12 different appliances. The name of the app that we are going to use in this project is "IoT OnOff". You can download the app from here and after creating a dashboard for the same we can control the board from any place where the internet is available. We have made a custom PCB for this project whose Gerber file can be accessed from the Github repository of the project which you can check out from here. In the Github repository, The file named "gerber.zip" is the Gerber file for the project PCB. You can modify the file as well and add as many as 12 relays to the board. This project can also be made using an ESP8266 based NodeMCU development board by externally connecting a relay but I do suggest that if you are getting into home automation you should definitely try the PCB thing. Once the PCB is ready you can follow the instructions in the next step to make your project work.
Setting Up the PCB
After populating our custom PCB with the required components, we can move on to the next step that is programming and configuring the PCB to work. For programming our PCB we need a USB to FTDI Converter. Now we need to follow the steps given below to complete the setup:-
First, we need to connect the USB to the FTDI converter to the PCB. For that, we need to connect the Rx pin of the FTDI converter to the Tx pin of the ESP8266, the Tx pin of the FTDI converter to the Rx pin of the ESP8266, and the Vcc and GND pins of the ESP8266 to that of the FTDI converter.
Now as we are done with the connections we need to make the code ready for uploading. For that, first, we need to access the Github repository of the project from here. In the repository, you will see a file named "code. ino". Open that file copy the code from there and paste that into your Arduino IDE.
In the Arduino IDE, you need to do some small changes in the code. You need to enter the SSID and Password of your Wifi at the places where they are required. After that, you need to enter the MQTT Server as "iot.reyax.com". I have already added that to the code. After that, you need to enter the MQTT Username and MQTT Password which you will receive after purchasing the RYC1001 MQTT Broker from Reyax. You can get your RYC1001 MQTT Broker from here.
Once all these modifications are done in the code. We need to select the correct Board i.e. NodeMCU1.0(ESP-12E Module) and the correct COM port and then before uploading the code we need to head over to the board.
Now when we connect the board to our computer, at that time our board is not ready to receive any code for that we need to make the board enter the boot mode. For that, we have two buttons present on our PCB which are the GPIO 0 and Reset buttons. What we need to do with these buttons is that we first need to press the GPIO 0 button and with that button pressed we need to press the reset button then we need to release the reset button and at the end, we need to release the GPIO 0 button. This will make the board enter the boot mode and now the board is ready to receive the code.
After following all the steps above. We can upload the code to our board. Once the code is uploaded we can unplug the board from our computer.
Now we need to set up the app in our phone/table which will act as another client and will be used to send the commands through the MQTT broker. For that, we need to install the app named "IoT OnOff". You can get that from here. In that app, you need to create a simple dashboard as I did and configure that which we are going to do next.
When you are done with the creation of the dashboard you need to go to the Settings menu. Ther you need to enter the MQTT Broker section. There you have to enter the Host as "iot.reyax.com", the port as "1883". After that on the same page, you will see a tab named authentication. You need to open that and there you need to enter the Username and Password that you received from Reyax on the purchase of the MQTT Broker and then click the Ready button two times once in the Authentication section and the second time in the Broker settings.
After doing this, you need to switch the Connect button you will see that the status will show connecting and after that, it will show connected which means that our dashboard is connected to our broker as a client.
As our app is connected to the broker, we need to go back to the dashboard here we have the button widget that we added to the dashboard. We need to enter the settings for the widget. In the settings, we need to enter the Subscribe tab. Here you will see an entity named Topic filter here we need to enter the topic name to which our dashboard will subscribe to transfer data. The topic name should be kept the same as the topic name in the code so that the data transfer can take place.
Similarly in the Publish settings, we need to make sure that the Topic String is the same as the one in the code. After that, we need to switch On the Allow publish setting and then Turn Off the Prefix device name. We also have the QoS setting available here for this project we have kept that setting to "At Most Once".
After doing this we need to set up the publish and subscribe values i.e. the values that will be sent when a True or False is pressed and we are done. We just need to press the ready button and we are good to go.
In this way, we have programmed our board and we have also completed the configuration of our dashboard and we are ready to operate this. We can also connect several different sensors to our dashboard and receive data from them on our dashboard we also have a variety of widgets available which we can use to make our dashboard more interactive and good-looking.
Testing Our Board
Now as we are done with the setup of the dashboard and PCB. We are ready to move on to check if our Board is working fine or not. For that, we need to power up our PCB. We can do that by using a Powerbank as well by connecting it through a suitable adapter. Once the board is powered, we can move to our phone or laptop and open the IoT OnOff app. In the app, we need to open the dashboard that we created, and then we need to press the button on the dashboard to turn the LED ON. If everything is working fine, then you will see that the LED on the board will turn ON as the Switch on the dashboard is toggled to ON position. The relay will also make a click sound on every ON/OFF switching.
We have connected the LED on the board to the GPIO 13 pin of the ESP8266. If we connect any other appliance to that pin like an LED bulb or something else then we would be able to control that as well with our dashboard. So in this way we are able to control appliances easily over the Internet with the help of MQTT. We can also scale up this and connect multiple relays to it by modifying the board design.
In the end, I would like to say that when you are using this kind of board for controlling your home appliances then you need to be very careful as a small mistake can lead to big damage. With this, we end this tutorial here. We will be back soon with another fun tutorial like this until then keep enjoying our other projects.