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:
Firmware ESP8266:
#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);
}
Adding widgets:
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.