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:

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 <VizIoTMqttClient.h>
    #include <ArduinoJson.h>
    #include <Ticker.h>
    
    // SSID and password to connect to Wi-Fi
    const char* WIFI_SSID = "your_wifi_ssid";
    const char* WIFI_PASSWORD = "your_wifi_password";
    
    // Device key and password
    const char* VIZIOT_DEVICE_KEY = "your_16_char_device_key";
    const char* VIZIOT_DEVICE_PASS = "your_20_char_device_pass";
    
    // Create MQTT client
    VizIoTMqttClient mqttClient(VIZIOT_DEVICE_KEY, VIZIOT_DEVICE_PASS);
    
    // LED pin
    #define LED_ESP D4
    // LED state
    bool ledState = false;
    
    // Timer to send data to the VizIoT MQTT broker
    Ticker sender;
    bool isSendDataToServer = false;
    void SendDataToServer() {
      isSendDataToServer = true;
    }
    #define INTERVAL_SEND_DATA 300  // Send data every 5 minutes (5*60=300)
    
    // Function to send data to VizIoT MQTT broker
    void sendPacketToVizIoT() {
      DynamicJsonDocument doc(256);
      JsonObject obj = doc.to<JsonObject>();
      obj["led"] = ledState ? 1 : 0;
      obj["rssi"] = WiFi.RSSI();
    
      String jsonStr;
      serializeJson(doc, jsonStr);
    
      if (mqttClient.publishJson(jsonStr.c_str())) {
        Serial.println("Published JSON: " + jsonStr);
      } else {
        Serial.println("Failed to publish JSON");
      }
    }
    
    // Callback for parameters received from the VizIoT MQTT broker
    void onParameterReceived(const char* paramName, const char* value) {
      Serial.print("Received parameter: ");
      Serial.print(paramName);
      Serial.print(" = ");
      Serial.println(value);
    
      if (strcmp(paramName, "led") == 0) {
        bool newState = (strcmp(value, "1") == 0);
        ledState = newState;
        digitalWrite(LED_ESP, ledState ? LOW : HIGH);  // Turn ON = LOW
        Serial.print("LED state updated to: ");
        Serial.println(ledState ? "ON" : "OFF");
        sendPacketToVizIoT();
      }
    }
    
    // Function to connect to Wi-Fi
    void setup_wifi() {
      WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
      Serial.print("Connecting to Wi-Fi...");
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
      }
      Serial.println("Connected to Wi-Fi");
    }
    
    void setup() {
      // Enable output to Serial Monitor
      Serial.begin(115200);
    
      // Allow control of the LED
      pinMode(LED_ESP, OUTPUT);
      digitalWrite(LED_ESP, HIGH);  // LED off
    
      // Connect to Wi-Fi
      setup_wifi();
    
      // Start MQTT client and set callback for incoming messages
      mqttClient.begin();
      mqttClient.setParameterCallback(onParameterReceived);
    
      // Create a data sending event every INTERVAL_SEND_DATA seconds
      sender.attach(INTERVAL_SEND_DATA, SendDataToServer);
    
      // Send data as soon as we connect to the broker
      SendDataToServer();
    }
    
    void loop() {
      // Check Wi-Fi connection
      if (WiFi.status() != WL_CONNECTED) {
        WiFi.reconnect();
        delay(1000);
        return;
      }
    
      // Handle incoming messages and maintain MQTT connection
      mqttClient.poll();
      if (!mqttClient.isConnected()) {
        mqttClient.reconnect();
      }
    
      // Send data to the server every 5 minutes
      if (isSendDataToServer) {
        isSendDataToServer = false;
        sendPacketToVizIoT();
      }
    
      delay(100);
    }
    
  3. Install the librarys «VizIoTMqttClient» and «ArduinoJson» and «Ticker» for the MQTT client to work.
  4. Set values of the variables:
    1. WIFI_SSID – your Wi-Fi point name
    2. WIFI_PASSWORD – your Wi-Fi point password
    3. VIZIOT_DEVICE_KEY – copied device access key
    4. VIZIOT_DEVICE_PASS – copied device access password
  5. 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.