Today, with the advancement of technology, controlling electronic devices has become easier than in the past, you can easily control all kinds of old devices such as toasters, coffee makers, TVs, lamps, fans, cooling systems, etc. remotely by your mobile phone or computer. In this tutorial, we introduce a useful and cheap tool for this work.
Introducing RelayFiRelayFi is an open source 4 channel smart relay board powered by ESP32-WROOM
that has the ability to safely control up to 4 appliances or devices on a load of 110-250 VAC/7A, 30 VDC/10A, and photo-coupling isolation Optocoupler for safety. Users can control their appliances using the third party app on their electronic device thanks to RelayFi. It is designed to work under low power consumption i.e. 60milli-Amphere in ideal state. The board also comes with CH340C USB to TTL chip for programming, an I2C header for expansion, and some jumpers for relay selection.
- Wireless module – Espressif Systems’ ESP32-WROOM-32D module with ESP32 dual-core Tensilica microcontroller, 32Mbit SPI flash, Wi-Fi 802.11 b/g/n up to 150 Mbps, Bluetooth 4.2 LE, and PCB antenna
- 4x relays up to 250VAC/7A or 30VDC/10A
- 4x EL817C optocouplers for safety
- 4x relay status LEDs
- Screw terminals
- Relay selection jumper
- USB – 1x micro USB port for power and programming via CH340C USB to TTL chip
- Expansion – I2C header
- Misc – Power LED reset button
- 5V via micro USB port
- Header with 5V, 3.3V, and GND
- Dimensions – 84.3 x 75.1 mm
Due to the use of ESP32 core in RelayFi design, a wide range of equipment can be used to control RelayFi. For example, it can be controlled via Bluetooth, Wi-Fi, or common smart speakers in the market such as Amazon Alexa, Google Assistant, SIRI and other smart speakers.
The video below shows how to control RelayFi with Amazon Alexa:
It is also compatible with Arduino, Raspberry Pi, BeagleBone, and other development boards. For example, you could power an Arduino board through the 5V/3.3V/GND header on the RelayFi, and the Arduino can enable/disable relays as needed instead of using jumpers.
Keep in mind that you can also program and control relays through Tasmota.
Connection reference guide for AC appliancesHow to connect AC devices is shown in the picture below, Please follow the safety precautions.
For easier access in programming languages, the list of pins used by RelayFi is in the table below, please note that all types of sensors can be connected to RelayFi through i2c pins.
To start the company shares the PDF schematic and a very simple Arduino sketch to control the relays on Github. To start programming with Arduino IDE, the necessary prerequisites need to be installed first. For install the ESP32 Boards in arduino, refer to the following link:
https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html
After installation, Now navigate to Tools ➤ Boards ➤ ESP32 Arduino, and select board as ESP32 Dev Module.
At this point, I have two programs ready to upload to RelayFi. In the first attempt, we upload the following codes to the board. Just note that you need to change your Wi-Fi network settings in the related lines.
//
// The original code was written by Rui Santos and the changes for RelayFi were made by Ramin Sangesari.
//
// Import required libraries
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
// Set to true to define Relay as Normally Open (NO)
#define RELAY_NO false
// Set number of relays
#define NUM_RELAYS 4
// Assign each GPIO to a relay
int relayGPIOs[NUM_RELAYS] = {18, 19, 23, 25};
// Replace with your network credentials
const char* ssid = "USERNAME";
const char* password = "PASSWORD";
const char* PARAM_INPUT_1 = "relay";
const char* PARAM_INPUT_2 = "state";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {font-family: Arial; display: inline-block; text-align: center;}
h2 {font-size: 3.0rem;}
p {font-size: 3.0rem;}
body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
.switch {position: relative; display: inline-block; width: 120px; height: 68px}
.switch input {display: none}
.slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
.slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
input:checked+.slider {background-color: #2196F3}
input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
</style>
</head>
<body>
<h2>RelayFi</h2>
%BUTTONPLACEHOLDER%
<script>function toggleCheckbox(element) {
var xhr = new XMLHttpRequest();
if(element.checked){ xhr.open("GET", "/update?relay="+element.id+"&state=1", true); }
else { xhr.open("GET", "/update?relay="+element.id+"&state=0", true); }
xhr.send();
}</script>
</body>
</html>
)rawliteral";
// Replaces placeholder with button section in your web page
String processor(const String& var){
//Serial.println(var);
if(var == "BUTTONPLACEHOLDER"){
String buttons ="";
for(int i=1; i<=NUM_RELAYS; i++){
String relayStateValue = relayState(i);
buttons+= "<h4>Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
}
return buttons;
}
return String();
}
String relayState(int numRelay){
if(RELAY_NO){
if(digitalRead(relayGPIOs[numRelay-1])){
return "";
}
else {
return "checked";
}
}
else {
if(digitalRead(relayGPIOs[numRelay-1])){
return "checked";
}
else {
return "";
}
}
return "";
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
// Set all relays to off when the program starts - if set to Normally Open (NO), the relay is off when you set the relay to HIGH
for(int i=1; i<=NUM_RELAYS; i++){
pinMode(relayGPIOs[i-1], OUTPUT);
if(RELAY_NO){
digitalWrite(relayGPIOs[i-1], HIGH);
}
else{
digitalWrite(relayGPIOs[i-1], LOW);
}
}
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
// Send a GET request to <ESP_IP>/update?relay=<inputMessage>&state=<inputMessage2>
server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
String inputParam;
String inputMessage2;
String inputParam2;
// GET input1 value on <ESP_IP>/update?relay=<inputMessage>
if (request->hasParam(PARAM_INPUT_1) & request->hasParam(PARAM_INPUT_2)) {
inputMessage = request->getParam(PARAM_INPUT_1)->value();
inputParam = PARAM_INPUT_1;
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
inputParam2 = PARAM_INPUT_2;
if(RELAY_NO){
Serial.print("NO ");
digitalWrite(relayGPIOs[inputMessage.toInt()-1], !inputMessage2.toInt());
}
else{
Serial.print("NC ");
digitalWrite(relayGPIOs[inputMessage.toInt()-1], inputMessage2.toInt());
}
}
else {
inputMessage = "No message sent";
inputParam = "none";
}
Serial.println(inputMessage + inputMessage2);
request->send(200, "text/plain", "OK");
});
// Start server
server.begin();
}
void loop() {
}
After uploading, we obtain the IP assigned to RelayFi from the serial monitor and enter the RelayFi control page through the browser (as shown in the image below). On this page, you can control each relay separately.
In the next example, use the ready codes available in this link. This is a great project for timing and controlling any relay. Relays can be turned on or off at specific times! it is interesting. After uploading, go to the relays control page as in the previous example. [Edit your Wi-Fi network settings and time zone in this line]
As you can see, a wide range of applications can be used to control RelayFi. It is even possible to control RelayFi through a personal server and remotely from anywhere in the world.
If you are interested in buying this board, visit their Kickstarter page.
Comments
Please log in or sign up to comment.