Menu

Arduino IDE Quickstart

The smallest possible publish and subscribe sketches. Confirm your toolchain works, then move on to the SDK.

This is a smoke test, not a starting point. The sketches below use bare WiFi.h + PubSubClient with blocking connects in setup(). They're the fastest way to prove your ESP32 can reach the broker — but they have no reconnect logic, no device ID handling, and no real TLS verification. Once you've seen them work, move to the Minimalist SDK for anything you actually plan to keep running.
Prerequisites: ESP32 board support installed in the Arduino IDE, and the PubSubClient library by Nick O'Leary. If you don't have these, the Getting Started | Arduino IDE page walks through both. You'll also need MQTT credentials from the dashboard Settings page.
1

Drop your credentials in a header

Create a new sketch (File → New Sketch), save it as quickstart_pub, then add a tab called credentials.h (click the small down-arrow in the tab bar →New Tab). Paste in your values from the dashboard:

#define WIFI_SSID   "YourWiFiName"
#define WIFI_PASS   "YourWiFiPassword"

#define MQTT_HOST   "mqtt.ohioiot.com"
#define MQTT_PORT   8883
#define MQTT_USER   "larry"
#define MQTT_PASS   "your_mqtt_password"
If this sketch ever sees git, add credentials.h to .gitignore first.
2

The publish sketch

Switch to the main quickstart_pub.ino tab and paste this in. It connects to WiFi, connects to MQTT, publishes a single value, and sits idle.

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include "credentials.h"

WiFiClientSecure wifi;
PubSubClient mqtt(wifi);

void setup() {

    Serial.begin(115200);
    Serial.println();

    // ----- wifi (blocking) -----
    WiFi.begin(WIFI_SSID, WIFI_PASS);
    Serial.print("wifi");
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }
    Serial.println(" connected");

    // ----- mqtt (blocking) -----
    wifi.setInsecure();              // quickstart only — see warning below
    mqtt.setServer(MQTT_HOST, MQTT_PORT);

    Serial.print("mqtt");
    while (!mqtt.connected()) {
        Serial.print(".");
        if (mqtt.connect("quickstart-pub", MQTT_USER, MQTT_PASS)) break;
        delay(2000);
    }
    Serial.println(" connected");

    // ----- publish one value -----
    String topic = String(MQTT_USER) + "/dev1/numb/temperature/celsius";
    mqtt.publish(topic.c_str(), "23.5");
    Serial.println("published 23.5 to " + topic);
}

void loop() {
    mqtt.loop();
}
wifi.setInsecure() tells the ESP32 to encrypt the connection but skip verifying the broker's certificate. That's fine for a smoke test on a network you trust. It is not fine for production — anyone between you and the broker could pretend to be the broker. The Minimalist SDK ships with the OhioIoT CA cert built in and verifies properly with no extra code.
3

Flash and verify

Plug in your ESP32, pick the right port under Tools → Port, and hit Upload. Open Tools → Serial Monitor at 115200. You should see:

wifi... connected
mqtt.. connected
published 23.5 to larry/dev1/numb/temperature/celsius

Open the dashboard. Under Devices you'll see dev1 with a temperature reading of 23.5.

You're publishing. Your toolchain works, your credentials are right, and the broker is reachable.
4

The subscribe sketch

Make a second sketch (File → New Sketch), save it as quickstart_sub, and add the same credentials.h tab — copy-paste the file from the first sketch.

Paste this into quickstart_sub.ino:

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include "credentials.h"

WiFiClientSecure wifi;
PubSubClient mqtt(wifi);

void onMessage(char* topic, byte* payload, unsigned int length) {
    Serial.print("got [");
    Serial.print(topic);
    Serial.print("] ");
    for (unsigned int i = 0; i < length; i++) Serial.print((char)payload[i]);
    Serial.println();
}

void setup() {

    Serial.begin(115200);
    Serial.println();

    // ----- wifi (blocking) -----
    WiFi.begin(WIFI_SSID, WIFI_PASS);
    Serial.print("wifi");
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }
    Serial.println(" connected");

    // ----- mqtt (blocking) -----
    wifi.setInsecure();
    mqtt.setServer(MQTT_HOST, MQTT_PORT);
    mqtt.setCallback(onMessage);

    Serial.print("mqtt");
    while (!mqtt.connected()) {
        Serial.print(".");
        if (mqtt.connect("quickstart-sub", MQTT_USER, MQTT_PASS)) break;
        delay(2000);
    }
    Serial.println(" connected");

    // ----- subscribe to the same topic the pub sketch wrote to -----
    String topic = String(MQTT_USER) + "/dev1/numb/temperature/celsius";
    mqtt.subscribe(topic.c_str());
    Serial.println("subscribed to " + topic);
}

void loop() {
    mqtt.loop();
}

The two sketches use different client IDs (quickstart-pub vs quickstart-sub) so the broker keeps them as separate connections. If you reused the same ID, the second connect would kick the first off.

5

Flash and watch the round trip

With your ESP32 still plugged in, hit Upload on quickstart_sub. The Serial Monitor will show:

wifi... connected
mqtt.. connected
subscribed to larry/dev1/numb/temperature/celsius

Now publish to that topic. Easiest path: open the dashboard, find dev1, and click the temperature value to send a manual publish. (Or re-flash the publish sketch from Step 2 — it'll publish once on every boot.)

The Serial Monitor on the subscribe sketch should print:

got [larry/dev1/numb/temperature/celsius] 23.5
You're subscribing. The full round trip works: something publishes, your ESP32 sees it.

Now what

These two sketches are the smallest demonstration of MQTT on the OhioIoT platform. They are also approximately the worst code you'd ever ship.

The blocking while (!mqtt.connected()) loop in setup() means your device freezes on boot if WiFi or the broker is briefly unreachable — and never recovers if either drops later. setInsecure() skips certificate verification entirely. The hardcoded dev1 ID means two devices running this code will fight over the same broker connection and kick each other off. There's no system messages, no device metrics, no reconnect handling, no graceful degradation.

The Minimalist SDK handles all of that — and it's only a few more lines of code than what's above. From this point forward, that's what you should build on.