Menu

Your First Sensor

Your device is now connected. Take it a step further and begin to publish mock temperature data, and see it appear on a live graph.

Prerequisites: You should have a working sketch from either Connect — Arduino IDE or Connect — PlatformIO that reaches mqtt connected... in the Serial Monitor. This page picks up from there.
A quick understanding of the publish function:

In the OhioIoT system, if you would like to publish to a graph named 'Temperature', and you are sending a data point named 'temp1', your device should publish data to the following topic:

~/~/numb/temperature/temp1

The breakdown: Your topic should begin with your MQTT username, and then the device ID of the sending device. If you use any of the provided SDKs, the firmware already knows your MQTT username and device ID, so these can be injected for you my starting your topic with ~/~/. The firmware reads those wildcard symbols and converts them to MQTT username and device ID for you.

Next, you tell the system that you are requesting a numerical graph with numb/.

The fourth element in the topic is the graph name, which we said is temperature/. That name will be capitalized for you automatically.

Lastly, you provide the data point name, which is temp1.

With your topic complete, simply provide your float data point variable as the payload, and your data point will go to an automatically generated graph in the OhioIoT app.

1

Start where you left off

In the previous lesson, you pulled down the SDK and connect your device. Let's start with that code base.

2

Add a timer mechanism to your loop

Look at your src/main.cpp and fine the loop() function.

void loop() {

    controller.loop();

    if (mqtt.is_connected) {

        // do something
        
    }

}

Modify your loop to run some code every 8 seconds (and maybe delete some of those extra lines):

unsigned long timer = millis();
            
void loop() {
    controller.loop();
    if (mqtt.is_connected) {
        if (millis() - timer > 8000) {
            // do something
        }
    }
}
3

Generate a mock temperature value

Replace // do something with

float temperature = 68.0 + random(-30, 30) / 10.0;
mqtt.publish("~/~/numb/temperature/temp1", temperature);
timer = millis();
4

Confirm that your device is sending

Upload your code, and check the serial monitor. You should see something like this:

  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 8 seconds, your device is connected and publishing data to the OhioIoT broker.
5

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 "temp1" that updates in real time.

When you have several data points publishing to a graph, it can be slightly chaotic to see the different data points arriving at different times. To smooth out the visualization, numerical data points, by default, are aggregated and published together on a 10-second interval. So, you will notice that, although you are sending data every 8 seconds, the graph is updating every 10, showing the last value that was published.

If you would like to change or turn off this aggregation interval, go to the Settings tab, and open "My Data". There, you can change the aggregation interval for both numerical data and boolean data.

✓ 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. On the default 10-second interval, the latest value is pushed to your browser window, 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. This is a deliberate streamlining of the process that you will find when using OhioIoT.