As I was building more and more home automation projects using the ESP8266 WiFi chip, I was looking for a simple solution to control my ESP8266 projects remotely. I wanted to be able to control my projects not only from my local WiFi network, but from anywhere in the world, simply by using a web browser.

I found many solutions, but none of them were really suited to my requirements. As you might know, I developed my own library called aREST to control most of the wireless projects I make. Therefore, I decided to modify this library to be able to control projects from outside of a local WiFi network.

In this article, I will share those new features of aREST with you, and show you how to control a simple relay from anywhere. You will then be able to adapt the project to control any home appliance from anywhere in the world. Let’s dive in!

Hardware & Software Requirements

Let’s first see which hardware we are going to use for this project. There are many board available for the ESP8266 chip (see the guide that I wrote on the topic), but here I will use an Adafruit ESP8266 board which is really easy to use. Here is the picture of the board:


To program the board, you will also need a 5V FTDI USB converter. I used a 5V converter (the ESP8266 uses 3.3V) because a 3.3V converter was not powerful enough to power the Adafruit ESP8266 board (which is 5V compatible anyway).

For the relay, I used a standard relay module from Polulu, but any other module would work as well. Finally, I also used a classic breadboard and some jumper wires.

This is the list of the components you will need for this project:

As for the software, you will need the latest version of the Arduino IDE that you can get from:

https://www.arduino.cc/en/Main/Software

You will also need to install the ESP8266 boards definitions into the Arduino IDE. You can do that by following the process from:

https://github.com/esp8266/Arduino

You will also need the latest version of the aREST library, as well as the PubSub client for Arduino. To install an Arduino library, simply use the library manager by going to Sketch>Include Librarty>Manage Libraries.

Hardware Configuration

It’s now time to assemble the project. Thanks to the components that we chose, it will be an easy operation. Just plug the ESP8266 board on the breadboard, and then connect the USB FTDI converter on the ESP8266 board.

Then, connect the relay to the ESP8266: VCC goes to the ESP8266 3V pin, GND to GND, and the EN or SIG pin of the relay goes to ESP8266 pin 5 (or any other pin available).

This is a picture of the assembled project, not showing the USB FTDI converter:main_pic

Configuring Your ESP8266

Let’s now configure our ESP8266 board, so it can connect to the cloud & be accessible from anywhere. This sketch is relatively simple, as I really wanted to make the whole connection to the cloud as simple as possible using the aREST library. Here is the complete sketch for this project:

// Control ESP8266 anywhere

// Import required libraries
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

// Clients
WiFiClient espClient;
PubSubClient client(espClient);

// Create aREST instance
aREST rest = aREST(client);

// Unique ID to identify the device for cloud.arest.io
char* device_id = "e49n2di";

// WiFi parameters
const char* ssid = "yourWiFiNetwork";
const char* password = "yourWiFipassword";

// Functions
void callback(char* topic, byte* payload, unsigned int length);

void setup(void)
{
  // Start Serial
  Serial.begin(115200);

  // Set callback
  client.setCallback(callback);
  
  // Give name and ID to device
  rest.set_id(device_id);
  rest.set_name("relay_anywhere");

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Set output topic
  char* out_topic = rest.get_topic();

}

void loop() {

  // Connect to the cloud
  rest.loop(client);

}

// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {

  rest.handle_callback(client, topic, payload, length);

}

The sketch starts by importing the correct libraries:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

You will also need to declare a WiFiClient & PubSubClient, that will communicate with the aREST server on the cloud:

WiFiClient espClient;
PubSubClient client(espClient);

We also initialise the aREST library with the server we defined earlier:

aREST rest = aREST(client);

Then, the most important part in the code: the ID of the device. This is what you will use to access it online. I suggest using a 6 character unique name (for example using for a example a generator like https://strongpasswordgenerator.com/):

char* device_id = "unique-id";

You also need to set the name of your WiFi network, and the password:

const char* ssid = "yourWiFiNetwork";
const char* password = "yourWiFiNetwork";

After that, in the setup() part of the sketch, we link the client to the callback that we’ll define later:

client.setCallback(callback);

We also connect the board to the WiFi network:

// Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

Then, inside the loop() part of the sketch, you simply need to call a single function from the aREST library:

rest.loop(client);

It is the same for the callback: the aREST library takes care of everything. You just need to call aREST from within the callback function:

void callback(char* topic, byte* payload, unsigned int length) {

  rest.handle_callback(client, topic, payload, length);

}

Note that you can find all the code on the GitHub repository of the project:

https://github.com/marcoschwartz/relay-anywhere-esp8266

It’s now time to configure the ESP8266 board with this code. Make sure that you modified the WiFi name & password in the code, and that you gave the device an unique ID. Now, put your ESP8266 board in bootloader mode. On the Adafruit ESP8266, it’s as simple as pressing on the GPIO0 & Reset button at the same time, and then releasing the Reset button first.

Then, upload the code to the board, and open the Serial monitor. You should see that the board is connecting to your WiFi, and then to aREST.io. This is the message you are looking for:


Controlling the Relay from Any Web Browser

Now that the ESP8266 board is connected to aREST.io, it’s time to test the project and actually see if we can control it remotely. Let’s start by set pin 5 to an output. Go to your favourite web browser, and type the following command (by changing the unique ID of your device):

http://cloud.arest.io/e49n2di/mode/5/o

You should immediately see the confirmation message in your browser:

{“message”: “Pin D5 set to output”, “id”: “e49n2di”, “name”: “relay_anywhere”, “connected”: true}

Simple, right? Now let’s turn the relay on. Simply type:

http://cloud.arest.io/e49n2di/digital/5/1

You should hear and see the relay turning on immediately, and also get the confirmation in your browser:

{“message”: “Pin D5 set to 1”, “id”: “e49n2di”, “name”: “relay_anywhere”, “connected”: true}

You can now also turn it back off with:

http://cloud.arest.io/3g83df/digital/5/0

Thos commands can be made from any web browser, and not only from within your local WiFi network. Congratulations, you can now control your ESP8266 board from anywhere!

Note that all those commands can perfectly be made from your own web applications, and the response read by a web application as well as it’s pure JSON format.

How to Go Further

I hope that this tutorial managed to show you how you can easily control your ESP8266 board from anywhere. You can of course adapt this tutorial for your own needs, for example by reading out data from the board.

I plan to include dashboards for aREST in the future, so you can control your project from anywhere using cloud-based buttons & indicators. You can also visit http://arest.io/ to find more about the project & about all the commands that you can use for your projects.

Of course, I am always looking for feedback on the aREST platform, so if you have any comment or suggestions about this tutorial or aREST let me know in the comments!