Introduction: 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)

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

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

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