Menu

Talk to the System with Code

Your platform is standard MQTT. Anything that speaks MQTT can talk to your devices.

This page builds on Talk To Your Device. Your ESP32 should be publishing temperature data and listening for commands on larry/device1/command.

The idea

On the previous page, the dashboard sent a command to your device. But the dashboard is just an MQTT client — it published a message to a topic. Any code that can connect to an MQTT broker can do the same thing.

In this example, you'll write a short Node.js script that subscribes to your device's temperature data and sends it a blink command. Same broker, same topics, same credentials.

Setup

mkdir ohioiot-listener
cd ohioiot-listener
npm init -y
npm install mqtt

The script

Create index.js:

const mqtt = require('mqtt');

const USERNAME  = 'larry';
const PASSWORD  = 'your_mqtt_password';
const DEVICE_ID = 'device1';

const client = mqtt.connect('mqtts://mqtt.ohioiot.com:8883', {
    username: USERNAME,
    password: PASSWORD,
});

client.on('connect', () => {
    console.log('connected to OhioIoT');

    // listen for temperature data from the ESP32
    client.subscribe(USERNAME + '/' + DEVICE_ID + '/numb/temperature/temp');
    console.log('subscribed to temperature data');

    // send a blink command every 10 seconds
    setInterval(() => {
        client.publish(USERNAME + '/' + DEVICE_ID + '/command', 'blink');
        console.log('sent blink');
    }, 10000);
});

client.on('message', (topic, payload) => {
    console.log(topic + '  →  ' + payload.toString());
});
Replace larry, your_mqtt_password, and device1 with your actual values.

Run it:

node index.js

You should see:

connected to OhioIoT
subscribed to temperature data
larry/device1/numb/temperature/temp  →  68.30
larry/device1/numb/temperature/temp  →  67.50
sent blink
larry/device1/numb/temperature/temp  →  69.10
✓ Your Node.js script is receiving live sensor data from your ESP32 and sending it commands. Same broker, same topics, no SDK required.

What's happening

The Node.js script is just another MQTT client — exactly like your ESP32, exactly like the dashboard. It connects with the same credentials, subscribes to the same topics, and publishes to the same topics. The broker doesn't know or care whether a message came from a microcontroller, a script, or a browser.

This is why OhioIoT is MQTT-native. You're never locked into a proprietary SDK or a specific language. If it can open a TCP connection, it can talk to your devices.

Freeform topics

Up to now, every topic has followed the dashboard's conventions —username/deviceID/numb/... and so on. But you don't have to. Any topic that starts with your username is valid.

Try this. Add a line to your Node.js script:

client.publish('larry/hello', 'world');

And on your ESP32, subscribe to it:

static const char * my_topics[] = {
    "~/~/command",     // → larry/device1/command
    "~/hello",         // → larry/hello
    nullptr
};

~/~/ expands to username/deviceID/ (device-scoped), and ~/ expands to just username/ (account-wide). You could also write the topics out in full — both forms reach the same broker topic.

Your ESP32 will receive the message. No graph will appear, no device card will update — because larry/hello doesn't match the dashboard's topic patterns. But the message still arrives. It's standard MQTT.

The rule is simple: every topic must start with your username. The broker enforces this. Beyond that, the topic is yours. Certain shapes tell the dashboard to do useful things. Everything else is freeform.

Where to go from here

You've completed Getting Started. You have a device that publishes data, receives commands, and talks to external code — all over encrypted MQTT.

Here's where to go next, depending on what you need: