Introduction: Controlling Relay Module From Anywhere in the World With Amazing UI Using Losant

About: Programming, running and pumping iron

Learn how to control relay module from anywhere in the world with Amazing UI using Losant, this is part 6 of IoT World Series

Step 1: Needed Components

Step 2: Creating Device and Setting Up Dashboard

Log in your account, in case of not having one, sing up, it’s really fast and easy

Create a device with no Device attributes set, my recipe is available

Create a dashboard (mine is named Relay dashboard) with 2 Button Trigger

Turn on button with Command Name – on

Turn off button with Command Name – off

In case you can’t press the buttons, go to the Settings icon in upper right on the block and select Unlock

Step 3: Wiring the ESP8266 and the Relay Module

Connect Relay Module to ESP8266

S to D0

+ to VIN

to GND

Step 4: Flashing the Code on the ESP8266

Create a file inside ProjectName/src/ called main.cpp and inside put

#include <ESP8266WiFi.h>

#include <Losant.h>

// WiFi credentials.
const char *WIFI_SSID = "wifi-name";
const char *WIFI_PASS = "wifi-password";</p><p>// Losant credentials.
const char *LOSANT_DEVICE_ID = "device-id"; // Device id can be found under
                                            // Applications > Devices > Your
                                            // device > Right corner
const char *LOSANT_ACCESS_KEY = "access-key";
    "access-secret"; // To generate Key & Secret go to Applications > Security >
                     // Right corder + Add access key</p><p>WiFiClientSecure wifiClient;</p><p>// For an unsecure connection to Losant.
// WiFiClient wifiClient;</p><p>#define RELAY_PIN D0</p><p>LosantDevice device(LOSANT_DEVICE_ID);</p><p>// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
  Serial.print("Command received: ");
  Serial.println(command->name);</p><p>  if (strcmp(command->name, "on") == 0) {
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("Relay on");
  } else if (strcmp(command->name, "off") == 0) {
    digitalWrite(RELAY_PIN, LOW);
    Serial.println("Relay off");
}</p><p>void connect() {
  // Connect to Wifi.
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);</p><p>  WiFi.begin(WIFI_SSID, WIFI_PASS);</p><p>  while (WiFi.status() != WL_CONNECTED) {
  }</p><p>  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());</p><p>  // Connect to Losant.
  Serial.print("Connecting to Losant...");</p><p>  device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);</p><p>  // For an unsecure connection.
  // device.connect(wifiClient, ACCESS_KEY, ACCESS_SECRET);</p><p>  while (!device.connected()) {
  }</p><p>  Serial.println("Connected successfully");
}</p><p>void setup() {
  while (!Serial) {
    // wait until Serial is initialized
  }</p><p>  // map the pin as output
  pinMode(RELAY_PIN, OUTPUT);</p><p>  // default turned off
  digitalWrite(RELAY_PIN, LOW);</p><p>  // Register the command handler to be called when a command is received
  // from the Losant platform.
  device.onCommand(&handleCommand);</p><p>  // connects to WiFi
}</p><p>void loop() {
  bool toReconnect = false;</p><p>  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected from WiFi");
    toReconnect = true;
  }</p><p>  if (!device.connected()) {
    Serial.println("Disconnected from Losant");
    toReconnect = true;
  }</p><p>  if (toReconnect) {
  }</p><p>  device.loop();

Project is available in GitHub

git clone <a href=""></a>

Connected to your ESP8266, on the right in Platform IO there is an arrow button > with tooltip Upload, press it, the upload should take few seconds

In case you get similar message

/dev/ttyUSB0: Access denied 

You need to give rights to your current user to read ttyUSB0 port

Controlling relay module from anywhere in the world with Amazing UI using Losant is part 6 of IoT World series, you can check other in my blog

First Time Author Contest

Participated in the
First Time Author Contest