ESP32 MQTT dashboard on iPhone and iPad

Updated

Build a dashboard for an ESP32 on your iPhone or iPad with Routix Dash: the board publishes readings over MQTT, the app shows them on a gauge and a chart, and a toggle in the app switches a pin on the board.

You need an ESP32 board, the Arduino IDE, and an MQTT broker both the board and your phone can reach, for example Mosquitto on a Raspberry Pi in your home network.

1. Flash the sketch

Install the PubSubClient library in the Arduino IDE (Sketch → Include Library → Manage Libraries), then upload this sketch. Change the Wi-Fi name, the password and the broker address first.

#include <WiFi.h>
#include <PubSubClient.h>

const char* WIFI_SSID = "your-wifi";
const char* WIFI_PASSWORD = "your-password";
const char* MQTT_HOST = "192.168.1.10";  // your broker
const int MQTT_PORT = 1883;

const char* TOPIC_TELEMETRY = "home/esp32/telemetry";
const char* TOPIC_LED = "home/esp32/led";
const int LED_PIN = 2;

WiFiClient wifi;
PubSubClient mqtt(wifi);
unsigned long lastSent = 0;

// A message on the LED topic: "on" lights the LED, anything else turns it off.
void onMessage(char* topic, byte* payload, unsigned int length) {
  String text;
  for (unsigned int i = 0; i < length; i++) {
    text += (char)payload[i];
  }
  digitalWrite(LED_PIN, text == "on" ? HIGH : LOW);
}

void connectMqtt() {
  while (!mqtt.connected()) {
    if (mqtt.connect("esp32-dashboard-demo")) {
      mqtt.subscribe(TOPIC_LED);
    } else {
      delay(2000);
    }
  }
}

void setup() {
  pinMode(LED_PIN, OUTPUT);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  mqtt.setServer(MQTT_HOST, MQTT_PORT);
  mqtt.setCallback(onMessage);
}

void loop() {
  if (!mqtt.connected()) {
    connectMqtt();
  }
  mqtt.loop();

  // Every 5 seconds: chip temperature, Wi-Fi signal and seconds since start.
  if (millis() - lastSent >= 5000) {
    lastSent = millis();
    char message[96];
    snprintf(message, sizeof(message),
             "{\"temperature\":%.1f,\"rssi\":%d,\"uptime\":%lu}",
             temperatureRead(), WiFi.RSSI(), millis() / 1000);
    mqtt.publish(TOPIC_TELEMETRY, message);
  }
}

Every 5 seconds the board publishes a message like this to home/esp32/telemetry:

{"temperature": 41.7, "rssi": -58, "uptime": 125}

temperatureRead() is the temperature of the chip itself. Replace it with the reading of your own sensor when you have one.

2. Connect Routix Dash to the broker

Open the menu → MQTT connections → Add MQTT connection. Enter a Name, your broker's Host and Port (1883), and a user name and password if the broker needs them. Tap Test, then the save tick.

3. Subscribe to the readings

Open the menu → Data sources → Add data source:

  1. Set Name to ESP32.
  2. Set Data source type to MQTT.
  3. Choose your MQTT connection.
  4. Set Topic filters (subscribe) to home/esp32/telemetry.

Tap the save tick.

4. Show temperature and signal

On a data section, tap Add data piece manually and pick Design your own data piece. Set ESP32 as the root data source.

Tap Add component and choose Dial gauge:

Add a Label for the Wi-Fi signal. Turn Inherited off, then under Data transformers pipe tap Add... → Simple expression and enter value?.rssi.

To see how the temperature changes, add a Chart and give it the same kind of expression, value?.temperature. Each new message adds a point.

Save the data piece. The gauge moves every 5 seconds.

5. Switch the LED from the app

Open the data piece editor again and add a Toggle:

Under Events → On tap, a new Toggle already has Apply value. It flips 1 and 0, so tap it and change Value to {{ value == "on" ? "off" : "on" }}: the toggle then flips between on and off.

Then tap Add... below it and choose MQTT send. Pick your connection, set the topic to home/esp32/led and leave Payload empty, so the toggle sends on or off.

Save and tap the toggle on the dashboard. The LED on the board follows it.

Ideas to go further

Read more