Introduction: Programming the Electrodragon IoT Relay

Picture of Programming the Electrodragon IoT Relay

I recently bought Electrodragon's IoT Relay (http://www.electrodragon.com/product/wifi-iot-rela...) and was surprised at the ease of use and feature available.

Features:

  1. Based on ESP-12F ESP8266 Wifi Board
  2. Supports two relay AC output.
  3. Relays are rated at 250 VAC - 10A, however I have seen concerns from others that the PCB traces may not be thick enough to suppport 10A. Even the site mentions that the R1.1 boards have a "2MM trace width to let 2A current pass through roughly for two relays together, so either 2A for running one channel device or 1A for two devices" They further go on to say that you can add more solder to the existing masks to increase the current capacity. However, I would limit the use of this device to 2A at the most - which is good enough for bulbs, small fans, electronics, etc (~100-200W)
  4. Comes with a plastic case, however the case has to be modified (i.e. cut open) slightly to allow access to both the output relays.

The device comes installed with the AT firmware - which is absolutely of no use, as you are not going to connect this wifi device using a serial adapter all the time. You want to control your device using wifi. The least they could have done was provided a firmware similar to the sonoff module, or at least their demo code. However, with a little bit of hit and trial, I was able to upload a new simple firmware that allows me to control the relay using wifi.

The ElectroDragon site has a wiki for this relay (http://www.electrodragon.com/w/ESP_Relay_Board) - however there are some inaccuracies to the image for the R1.0 board which caused me a few headaches. Also, the demo code and instructions for re-programming assume that you are going to use the LUA language. I haven't even tried to learn LUA, and luckily the ESP8266 boards can be programmed with the Arduino IDE as well - so that's not too big a problem.

Step 1: Setting Up the Software (Arduino IDE)

Picture of Setting Up the Software (Arduino IDE)

The latest version of the Arduino IDE has made it easier to program these boards and you no longer have to go through multiple hoops to get it to work with the ESP8266 boards. The steps are as follows:

  1. Download the latest IDE from https://www.arduino.cc/en/Main/Software
  2. Open the IDE and go to Tools -> Boards -> Boards Manager...
  3. Search for ESP8266 and click install (see image above)

That's it - you're ready to program the IoT Relay

Step 2: Connecting the IoT Relay to Your Computer for Programming

Picture of Connecting the IoT Relay to Your Computer for Programming

This module does not come with a USB interface, so you need to use a USB-TTL module / arduino to handle the USB communication with the computer. You can buy any one of the cheap modules available on ebay (http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&...) - all work the same - only caveat being finding the correct drivers so that your computer detects the module.

This is the step that caused the most headache, as the image on the wiki has errors. I have used the image from the wiki, however have provided comments - so that you get it correct.

The connections are pretty simple (once you know which pins to use):

  1. Connect the GND from USB-TTL to the pin marked GND on the image (first pin on the left bottom)
  2. Connect the 3.3V from the USB-TTL to the pin marked VCC on the image (it's the pin above the GND pin)
  3. Connect the RX from the USB-TTL to the pin marked RX on the image (this is opposite of what you would normally expect wherein you connect the RX to the TX pin and vice-versa)
  4. Connect the TX from the USB-TTL to the pin marked TX on the image (this is opposite of what you would normally expect wherein you connect the TX to the RX pin and vice-versa)

The module is now ready to be programmed. Holding down BTN2 (marked as BTN1 on the wiki image) and then plugging the module into the USB port puts it in the programming mode and you can upload the new firmware (keep the BTN2 pressed till the software shows that the firmware has been fully uploaded).

Please note, I have the v1.0 boards and hence cannot vouch for the correctness of the V1.1 diagram on the wiki.

Step 3: The Firmware

As the IoT Relay is based on the ESP8266, there are a whole host of firmwares available for you to use. Some of these include:

  1. Souliss: http://souliss.net/getting-started/
  2. ESPEasy: http://www.esp8266.nu/index.php/ESPEasy
  3. Blynk: http://www.blynk.cc/
  4. MQTT: https://github.com/tuanpmt/esp_mqtt

And many more. There are many instructables already present and you can see them by following this link: https://www.instructables.com/howto/ESP8266/

However, I went with the simplest possible solution - and that's to run a simple webserver on the IoT relay itself. This is based on the 'WifiWebServer' example that comes with the ESP8266 library.

Things to note:

  1. Change the wifi SSID and password to your setup
  2. The IoT relay has the two relays connected to GPIO13 and GPIO12

Copy and paste the following code to a new "sketch" in the arduino IDE

#include <ESP8266WiFi.h>

const char* ssid = "SSID";
const char* password = "Password";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO13
  pinMode(13, OUTPUT);
  digitalWrite(13, 0);
  // prepare GPIO12
  pinMode(12, OUTPUT);
  digitalWrite(12, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  
  if (req.indexOf("/gpio/1/0") != -1){        // GPIO 13 Low
    digitalWrite(13,0);
    client.flush();
    client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 13 is now Low </html>\n");
  }
  else if (req.indexOf("/gpio/1/1") != -1){        // GPIO 13 High
    digitalWrite(13,1);
    client.flush();
    client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 13 is now High </html>\n");
  }
  else if (req.indexOf("/gpio/2/0") != -1){        // GPIO 12 Low
    digitalWrite(12,0);
    client.flush();
    client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 12 is now Low </html>\n");
  }
  else if (req.indexOf("/gpio/2/1") != -1){        // GPIO 12 High
    digitalWrite(12,1);
    client.flush();
    client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 13 is now High </html>\n");
  }
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  delay(1);
  Serial.println("Client disonnected");

}

Step 4: Uploading the Firmware and Running the Server

Picture of Uploading the Firmware and Running the Server

To upload the firmware, you need to follow these steps:

  1. Press BTN2 and connect the USB-TTL to the USB port on your computer
  2. Choose Board 'NodeMCU 0.9 (ESP-12 Module)'
  3. Choose the correct port (it came up as COM9 on my system)
  4. Click on the right arrow to compile and upload
  5. Ensure that you keep the BTN2 pressed till the firmware is fully uploaded

In most cases this should work, however if you get an error, disconnect the USB and start from step 1 again.

Once the firmware is uploaded, you're ready to connect the module to AC power (BE VERY CAREFUL WHILE HANDLING AC CURRENT - it is extremely dangerous and potentially fatal). The IoT Relay will not connect to the wifi when it is powered through the USB, as the USB-TTL does not provide enough current.

  1. Disconnect the IoT Relay from the USB-TTL module
  2. Connect the input wires and output wires
  3. Check connections again
  4. Close the case
  5. Plugin the input wires to an AC outlet

The sketch I have provided sends out the IP address of the IoT Relay via serial, however I don't think it's a great idea to connect your USB-TTL alongwith the AC power to the IoT Relay. So the best bet is to go to your router's home page and find the IP address of the IoT Relay.

Now you can switch on / off the relays using the following commands:

http://<ip address>/gpio/1/1 - switch on relay 1
http://<ip address>/gpio/1/0 - switch off relay 1
http://<ip address>/gpio/2/1 - switch on relay 2
http://<ip address>/gpio/2/0 - switch off relay 2

Comments

MatiasL12 (author)2017-07-22

Nice post!
I wonder if anyone can use te pin4, pin5 or pin15?
I want to do a digitalRead from two pins, but can´t work it out. I want to do something like this:
pinMode(15, INPUT);

status = digitalRead(15);

It could be 15, 4 or 5 or whatever. But I cant do a digital read from any pin.

I would apreciate any souggestion!

plastkort (author)2017-06-22

I like it, I would like to use this to make a reboot or poweron/off remotely , but the relays keeps stuck on high, and if i try turn off, nothing happens, if i change the pin, the relays turn off, but when i try to set it back, it stuck on the high position. any clues?

firmware issue?, i have a lol1n V3

LászlóS40 (author)2017-02-13

Super and simle. Thank You!

LuisC258 (author)2016-10-04

Great tip!

I am uploading the amazing Homie OTA (https://github.com/marvinroger/homie-esp8266). Hopefully it will enable me to upgrade over the air and integrate with my hub via MQTT.

dushyantahuja (author)LuisC2582016-10-15

That's real nice - I have updated the code to include the OTA library - it's still very simple, but works for me.

sami_owais made it! (author)2016-07-30

Thank you!!!

I wasn't able to load a code to mine until followed the following steps:
1. I disconnect the USB,

2. Pressed and held the BTN2

3. Reconnected the USB. ( here the status led converted from flashing state to solid ON), once program is finished, the status LED turned off.

Source: http://blog.kodeten.com/experimenting-with-a-new-g...

Also, I update the code to include a HTML page to give a nice interface to the user

Here is my code:

#include <ESP8266WiFi.h>

#include <WiFiClient.h>

const char* ssid = "SSID";

const char* password = "PASSWORD";

String readString;

String ipStr ;

String s;

// Create an instance of the server

// specify the port to listen on as an argument

WiFiServer server(80);

void setup() {

Serial.begin(115200);

delay(10);

// prepare GPIO13

pinMode(13, OUTPUT);

digitalWrite(13, 0);

// prepare GPIO12

pinMode(12, OUTPUT);

digitalWrite(12, 0);

// Connect to WiFi network

Serial.println();

Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("WiFi connected");

// Start the server

server.begin();

Serial.println("Server started");

// Print the IP address

Serial.println(WiFi.localIP());

}

void loop() {

// Check if a client has connected

WiFiClient client = server.available();

if (!client) {

return;

}

// Wait until the client sends some data

Serial.println("new client");

while(!client.available()){

delay(1);

}

// Read the first line of the request

String req = client.readStringUntil('\r');

Serial.println(req);

client.flush();

// if (req == "/" ) {

IPAddress ip = WiFi.localIP();

ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);

setS_HTML();

Serial.println(s);

// Match the request

if (req.indexOf("/gpio/1/0") != -1){ // GPIO 13 Low

digitalWrite(13,0);

// client.flush();

// client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 13 is now Low </html>\n");

Serial.println("GPIO 13 Low");

}

else if (req.indexOf("/gpio/1/1") != -1){ // GPIO 13 High

digitalWrite(13,1);

// client.flush();

// client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 13 is now High </html>\n");

Serial.println("GPIO 13 High");

}

else if (req.indexOf("/gpio/2/0") != -1){ // GPIO 12 Low

digitalWrite(12,0);

// client.flush();

// client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 12 is now Low </html>\n");

Serial.println("GPIO 12 Low");

}

else if (req.indexOf("/gpio/2/1") != -1){ // GPIO 12 High

digitalWrite(12,1);

// client.flush();

// client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 12 is now High </html>\n");

Serial.println("GPIO 12 High");

}

else if (req.indexOf("/gpio/12/0") != -1){ // GPIO 12 and 13 Low

digitalWrite(12,0);

digitalWrite(13,0);

// client.flush();

// client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO 12 is now Low </html>\n");

Serial.println("GPIO 12 Low");

Serial.println("GPIO 13 Low");

}

else {

Serial.println("invalid request");

// client.stop();

// return;

}

setS_HTML();

Serial.println(s);

client.print(s);

delay(1);

Serial.println("Client disonnected");

req ="";

}

/*********** Setting up the HTML page *************/

void setS_HTML() {

s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";

s += "<head>";

// Set the refresh rate:

// s += "<meta http-equiv=\"refresh\" content=\"5\">";

// Setting the width of the page:

//https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag

s += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";

s += "</head> \r\n" ;

s += "<BODY>";

s += "Hello from Sami Oweis, PhD. \r\n <b> ESP8266 at ";

s += ipStr;

s += "\r\n \r\n";

s += "<p>\r\n";

s += "<p>\r\n";

// custom buttons

s += "<input type=submit value=\"Switch 1 ON\" style=width:100px;height:45px onClick=location.href='/gpio/2/1' target='" + ipStr + "'>" ;

s += "<input type=submit value=\"Switch 1 OFF\" style=width:100px;height:45px onClick=location.href='/gpio/2/0' target='" + ipStr + "'>" ;

s += "<p>\r\n";

s += (String) GetIO12St();

s += "<p>\r\n";

s += "<input type=submit value=\"Switch 2 ON\" style=width:100px;height:45px onClick=location.href='/gpio/1/1' target='" + ipStr + "'>" ;

s += "<input type=submit value=\"Switch 2 OFF\" style=width:100px;height:45px onClick=location.href='/gpio/1/0' target='" + ipStr + "'>" ;

s += "<p>\r\n";

s += (String) GetIO13St();

s += "<p>\r\n";

s += "<p>\r\n";

s += " <input type=submit value='ALL OFF' style=width:100px;height:45px onClick=location.href='/gpio/12/0;'>";

s += "</BODY>";

s += "</html>\r\n\r\n";

}

/************* GPIOs Status ************/

String GetIO12St(void){

String st = "GPIO 1 is OFF" ;

if (digitalRead(12))

st = "GPIO 1 is ON/High" ;

return st ;

}

String GetIO13St(void){

String st = "GPIO 2 is OFF" ;

if (digitalRead(13))

st = "GPIO 2 is ON/High" ;

return st ;

}

roroid (author)2016-07-26

Nice work.I want to use something similar for my water garden system. Is it possible to ad to the board 2 humidity sensors? I see there btn 1&2. Maybe not very precise readings but at least to get the feeling when sprinklers need to stop. Or is much better to go with a promini and a esp module?

dushyantahuja (author)roroid2016-07-27

The headers provide access to the other GPIOs - so you can easily connect sensors using that.

About This Instructable

7,350views

19favorites

License:

More by dushyantahuja:Programming the Electrodragon IoT RelayWifi CalendarThe Ultimate Child Tracker
Add instructable to: