Remote relay switch

We are creating a simple system that can switch a relay from everywhere, based on a WEMOS D1 mini and a relay shield.

With cloud integration the switch of the relay controlled by the IoT Guru Cloud library, where we will use a simple HTML & JavaScript page with two buttons.


WEMOS D1 mini or any other ESP based MCU

WEMOS relay shield or relay

Step 1

Solder and stack the WEMOS D1 mini and the relay shield together like this:

Alternatively you can use any ESP based MCU and relay.

Step 2

If you don’t have already, you need to create a device, a node and a field in our cloud, follow this articles:

If you haven’t installed it yet, install our official Arduino library:

Step 3

To connect with our cloud, you need to gather six identifier:

  • userShortId: the short identifier of you
  • deviceShortId: the short identifier of your device
  • deviceKey: the secret key of your device
  • nodeShortId: the short identifier of your node
  • nodeKey: the secret key of your node
  • fieldName: the name of the field

Step 4

Create a new Arduino sketch, copy-paste the following code and replace the ssid, the password, the userShortId, the deviceShortId, the deviceKey , the nodeShortId and the fieldName with your Wifi credentials and with your gathered values:

#include <IoTGuru.h>

#ifdef ESP8266
  #include <ESP8266WiFi.h>
#ifdef ESP32
  #include <WiFi.h>

#define RELAY_PIN         5

 * WiFi parameters.
const char* ssid        = "ssid";
const char* password    = "password";
WiFiClient client;

 * Initialize the connection with the cloud.
String userShortId      = "xxxxxxxxxxxxxxxxxxxxxx";
String deviceShortId    = "yyyyyyyyyyyyyyyyyyyyyy";
String deviceKey        = "zzzzzzzzzzzzzzzzzzzzzz";
IoTGuru iotGuru         = IoTGuru(userShortId, deviceShortId, deviceKey);

 * Constants of the MQTT channel check.
String nodeShortId      = "nnnnnnnnnnnnnnnnnnnnnn";
String fieldName        = "relay";

void setup() {

    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {

     * Set the relay pin.
    pinMode(RELAY_PIN, OUTPUT);


void loop() {

void callback(const char* cbNodeShortId, const char* cbFieldName, const char* message) {
    Serial.print(cbNodeShortId);Serial.print(" - ");Serial.print(cbFieldName);Serial.print(": ");Serial.println(message);

    if (strcmp(cbNodeShortId, nodeShortId.c_str()) == 0) {
        if (strcmp(cbFieldName, fieldName.c_str()) == 0) {
            if (strcmp(message, "0") == 0) {
                Serial.println("Switch relay to LOW");
                digitalWrite(RELAY_PIN, LOW);
            } else {
                Serial.println("Switch relay to HIGH");
                digitalWrite(RELAY_PIN, HIGH);

Step 5

Plug your WEMOS D1 mini to your computer with an USB cable, compile the code you’ve edited above and upload it to your WEMOS D1 mini. Check the Serial console about the connection messages.

Step 6

Create a HTML page on your computer or your webserver and copy-paste the following snippet into the body tag; replace the nnn..nnn with your nodeKey value:

  <button id="mqttOn" name="mqttOn" value="ON">ON</button>
  <button id="mqttOff" name="mqttOff" value="OFF">OFF</button>

<script src=""></script>

        $.ajax({url: ""});
        $.ajax({url: ""});

If you have reached this point, pressing the ON and OFF buttons will turn the relay on and off.

Leave a Reply

Your email address will not be published.

The reCAPTCHA verification period has expired. Please reload the page.