ESP8266 MQTT -> LED control

Consider an example that will allow you to control the microcontroller LED through the «Switches» widget and plot the Wi-Fi signal level.

Required materials:

  • ESP8266 Wemos d1 mini microcontroller
  • Arduino IDE development framework (you can look for settings here)

Device adding and setting:

  1. Add a new device under the name of «LED control test» in VizIoT
  2. Configure device settings. Add three parameters:
    1. rssi key of «Signal strength, dBm» type;
    2. led key of «On / Off, 0-1» type.
  3. Copy pass key and access password in «Basic settings» of the device.

Firmware ESP8266:

  1. Create a new project in Arduino IDE.
  2. Copy the example below to the created project.
    #include <ESP8266WiFi.h>
    #include <Ticker.h>
    #include <VizIoTMqttClient.h>
    
    //Pin address with LED
    #define LED_ESP 2
    
    //ssid and access password for connection to WI-FI
    const char* ssid = ".........";
    const char* password = "...........";
    
    //VizIoT access key and password Devices (can be found in device settings)
    String VizIoT_Device_key = "..............";
    String VizIoT_Device_pass = "..................";
    
    WiFiClient espClient;
    PubSubClient clientMQTT(espClient);
    VizIoTMqttClient clientVizIoT(clientMQTT);
    long lastMsg = 0;
    char msg[100];
    byte statusLed = 0;
    
    
    /*----------Sending data Ticker----------------*/
    Ticker sender;
    bool isSendDataToServer;
    void SendDataToServer() {isSendDataToServer = true;} 
    #define INTERVAL_SEND_DATA 60 
    
    
    
    void setup()
    { 
      //allow to control the LED
      pinMode(LED_ESP, OUTPUT);
      digitalWrite(LED_ESP, HIGH);
      
      //Turn on the output of information in the Serial Monitor
      Serial.begin(9600);
      
      //We connect to WI-FI
      setup_wifi();
    
      //Set the address of the MQTT broker
      clientVizIoT.config(VizIoT_Device_key, VizIoT_Device_pass);
      //set callbeck function
      clientVizIoT.listenCommand(callback);
      sender.attach(INTERVAL_SEND_DATA, SendDataToServer); // Create an event for sending data every INTERVAL_SEND_DATA sec
    }
    
    //Handling the Data Received Event
    void callback(String parameter, byte value) {
       Serial.print("Posting a message: parameter");
       Serial.print(parameter);
       Serial.print("value ");
        Serial.println(value);
      if (parameter.compareTo("led") == 0) {
        if (value == 1) {
          statusLed = 1;
          digitalWrite(LED_ESP, LOW);
        } else {
          statusLed = 0;
          digitalWrite(LED_ESP, HIGH);
        }
    
        snprintf(msg, sizeof(msg), "{\"led\":\"%c\"}", (statusLed) ? '1' : '0');
        Serial.print("Posting a message: ");
        Serial.println(msg);
    
        clientVizIoT.sendJsonString(String(msg));
      }
    }
    
    //Wi-Fi connection function
    void setup_wifi() {
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        //To be connected to WI-FI
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
    }
    
    
    
    void loop()
    {
      //required to process incoming messages and maintain connection to the Broker
      clientVizIoT.loop();
      
      //Sending the WI-FI signal level and LED status to the server every minute
      if (isSendDataToServer) {
        isSendDataToServer = false; 
        snprintf (msg, sizeof(msg), "{\"rssi\":\"%i\",\"led\":\"%c\"}", WiFi.RSSI(), (statusLed) ? '1' : '0');
        Serial.print("Posting a message: ");
        Serial.println(msg);
        clientVizIoT.sendJsonString(String(msg));
      }
    }
    
  3. Install the library «PubSubClient and VizIoTMqttClient» for the MQTT client to work.
  4. Edit the parameter
    #define MQTT_MAX_PACKET_SIZE 1024
    in the «PubSubClient» library, which is usually located in «C:\Users\{Username}\Documents\arduino\libraries\PubSubClient\src\PubSubClient.h»
  5. Set values of the variables:
    1. ssid – your Wi-Fi point name
    2. password – your Wi-Fi point password
    3. VizIoT_Device_key – copied device access key
    4. VizIoT_Device_pass – copied device access password
  6. Connect ESP8266 to computer and send firmware to microcontroller.

Adding widgets:

  • Create a new dashboard under the name «LED control test»
  • Add widgets:
    • Signal strength
      • type: «Chart»;
      • device: «LED control test»;
      • parameter: rssi;
    • LED control
      • type: «Switches»;
      • device: «LED control test»;
      • parameter: led;

Now you have everything configured and it remains to wait for your device to connect to the server and you can control the LED by clicking on the switch in the Dashboard.