Menu

Connect Your ESP32 — PlatformIO

Get a device talking to OhioIoT over a secure TLS connection. No certificate wrangling.

Using the Arduino IDE instead? Switch to the Arduino IDE version of this page.
Prerequisites: A completed Create Your Account setup (account, subscription, MQTT credentials), an ESP32 dev board, and PlatformIO installed in VS Code.
1

Download the Minimalist SDK

Download the SDK and unzip it. You'll get three folders: device_id, wifi_tools, and mqtt. That's the whole SDK.

2

Create your project

Create a new folder and set it up like this:

ohioiot-connect/
├── platformio.ini
├── config/
│   └── config/
│       └── credentials.h
├── minimalist/
│   ├── device_id/
│   ├── wifi_tools/
│   └── mqtt/
└── src/
    └── main.cpp

Drop the three SDK folders into minimalist/.

3

platformio.ini

Create platformio.ini in the project root:

[platformio]
default_envs = dev

[env:dev]
platform      = espressif32
board         = esp32dev
framework     = arduino
monitor_speed = 115200

lib_deps =
    knolleary/PubSubClient@^2.8

lib_extra_dirs =
    config
    minimalist

If your board isn't esp32dev, change that one line. Everything else stays the same.

4

Credentials

Create config/config/credentials.h with your values from the Settings page in 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"

Port 8883 is the TLS port. Don't change it.

Add credentials.h to your .gitignore now, before you forget.
5

The sketch

Open src/main.cpp and paste this:

#include <Arduino.h>
#include "credentials.h"
#include "device_id.h"
#include "wifi_tools.h"
#include "mqtt.h"

char DEVICE_ID[9];

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();
    } else {
        wifi_tools.reconnect();
        mqtt.report_disconnect();
    }
}
6

Flash and watch

Click Upload in PlatformIO. Open the Serial Monitor at 115200 baud. You should see:

  booting...

	existing deviceID: a8f3k2m1

	wifi connecting...
	wifi connected...

	mqtt connecting...
	mqtt connected...
You're connected. Your ESP32 has a TLS-encrypted MQTT connection to OhioIoT.

What just happened

device_id.get_or_set(DEVICE_ID) checked the ESP32's non-volatile storage for a saved device ID. Since this is the first boot, it generated a random 8-character string, stored it, and returned it. Next boot, it returns the same ID.

wifi_tools.begin() connected to WiFi and set up an event handler that tracks connection state automatically.

mqtt.setup() configured the MQTT client with TLS. The SDK ships with the OhioIoT broker's CA certificate built in — you didn't need to configure it. That's why the connection just worked over port 8883.

mqtt.maintain() handles everything in the loop: reconnecting if dropped, processing incoming messages, keeping the heartbeat alive.

What's handling TLS?

The SDK ships with the CA certificate for Let's Encrypt's ISRG Root X1 — the certificate authority that signs the OhioIoT broker's TLS certificate. When your ESP32 connects to port 8883, it uses this CA cert to verify that it's talking to the real mqtt.ohioiot.com.

If you ever connect to a different broker — your own Mosquitto instance, AWS IoT, HiveMQ — create a file called ca_cert.h in your config/config/ folder and define your broker's root certificate:

// config/config/ca_cert.h
#define CA_CERT "-----BEGIN CERTIFICATE-----\n" \
    "...your broker's root CA cert...\n" \
    "-----END CERTIFICATE-----\n"

The SDK will use your certificate instead of the built-in one. No other changes needed.

Troubleshooting

"failed to connect" in the Serial Monitor — double-check your MQTT username and password in credentials.h. They must match the Settings page in the dashboard exactly.

WiFi connects but MQTT doesn't — some networks block port 8883. Try a mobile hotspot. If it works there, your network is blocking outbound TLS-MQTT.

Device keeps reconnecting — two devices with the same device ID will kick each other off the broker. Unplug one and see if the other stabilizes.