Menu

Your First Sensor

Extend your connected sketch to publish a simulated temperature reading, and see it appear on a live graph. (⏱ ~10 min)

Prerequisites: You should have a working sketch from Connect Your ESP32 that reaches mqtt connected... in the Serial Monitor. This page picks up from there.
1

Add a publish loop

Open your sketch from the previous page. You're going to add two things — a timestamp variable at the top, and a publish block inside the loop. The full updated code looks like this:

#include "credentials.h"
#include "device_id.h"
#include "wifi_tools.h"
#include "mqtt.h"

char DEVICE_ID[9];
unsigned long last_publish = 0;

void setup() {

    Serial.begin(115200);
    Serial.println("\n\n  booting...\n");

    device_id.get_or_set(DEVICE_ID);

    wifi_tools.begin(WIFI_SSID, WIFI_PASS);

    mqtt.setup(MQTT_HOST, MQTT_PORT, DEVICE_ID, MQTT_USER, MQTT_PASS);
}


void loop() {

    if (wifi_tools.is_connected) {

        mqtt.maintain();

        if (mqtt.is_connected && millis() - last_publish > 5000) {

            float temperature = 68.0 + random(-30, 30) / 10.0;

            mqtt.publish("~/~/numb/temperature/temp", temperature);

            last_publish = millis();
        }

    } else {

        wifi_tools.reconnect();
        mqtt.report_disconnect();

    }
}

Arduino IDE users: the SDK includes use angle brackets (<mqtt.h>) rather than quotes — same as in your original sketch.

2

Understand the publish line

The whole point of this page is one line:

mqtt.publish("~/~/numb/temperature/temp", temperature);

The ~/~/ prefix is a shorthand the SDK rewrites to username/deviceID/ before sending, so the actual topic on the broker is larry/a8f3k2m1/numb/temperature/temp.

The platform sees numb in the third segment, so it knows this is a number to graph. It creates a graph called "temperature" with a series called "temp" — no schema registration required.

Skipping TLS for local development? If you're publishing to a local broker without TLS, you can enable plaintext MQTT by defining ALLOW_INSECURE_MQTT as a build flag. On PlatformIO, uncomment the matching line in platformio.ini. On the Arduino IDE, add #define ALLOW_INSECURE_MQTT at the very top of your sketch, before any SDK includes. Then change MQTT_PORT to 1883. Don't ship this to production.
3

Flash and verify

Upload the sketch and open the Serial Monitor at 115200 baud. You should see:

  booting...

	existing deviceID: a8f3k2m1

	wifi connecting...
	wifi connected...

	mqtt connecting...
	mqtt connected...

	sending: larry/a8f3k2m1/numb/temperature/temp    68.30
	sending: larry/a8f3k2m1/numb/temperature/temp    67.50
	sending: larry/a8f3k2m1/numb/temperature/temp    69.10
✓ If you see sending: lines appearing every 5 seconds, your device is connected and publishing data to the OhioIoT broker.
4

See it on the dashboard

Open app.ohioiot.com and go to the Data screen. Within a few seconds you should see a graph labeled "temperature" with a line called "temp" that updates in real time.

Go to the Devices screen. You'll see a device card with your 8-character device ID. You can click on it to see details.

That's it. You have a connected ESP32 publishing data to a live graph on your OhioIoT dashboard.

What just happened

Your ESP32 published a message to the MQTT broker on the topic larry/a8f3k2m1/numb/temperature/temp with a numeric payload. The platform's processor saw the numb type in the third segment, recognized it as a number to aggregate, and routed it through the data pipeline. A few seconds later, the aggregated value was pushed to the dashboard via a WebSocket, and your graph updated.

You didn't register the device. You didn't create a graph. You didn't define a schema. You published to a topic in the right shape, and the platform did the rest.

Try next

Now that you have a working device, try these:

Add a second series to the same graph. Add another publish line — something like mqtt.publish("~/~/numb/temperature/humidity", 45.0). The "temperature" graph will now have two series: "temp" and "humidity".

Add a boolean. Publish mqtt.publish("~/~/bool/climate/heating", "true") to create a boolean graph that tracks on/off state.

Report device status. Publish mqtt.publish("~/~/status/firmware", "v1.0.0") and check the Devices screen — the firmware field will appear on your device card.

Subscribe to a topic. Read the Subscribing guide to listen for incoming messages and react to commands.