Menu

Connect Your ESP32 | Arduino IDE

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

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

Add ESP32 board support

If you've never flashed an ESP32 from the Arduino IDE before, you need to add Espressif's board package first.

In the Arduino IDE, open File → Preferences and add this URL to the Additional Board Manager URLs field:

https://espressif.github.io/arduino-esp32/package_esp32_index.json

Then open Tools → Board → Boards Manager, search for esp32, and install the esp32 package by Espressif Systems.

Once it finishes, select your board under Tools → Board → esp32. If you're not sure, ESP32 Dev Module is a safe default.

2

Install PubSubClient

Open Tools → Manage Libraries, search for PubSubClient, and install the one by Nick O'Leary (version 2.8 or later).

3

Install the Minimalist SDK

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

Drop all three folders into your Arduino libraries directory:

macOS / Linux:  ~/Documents/Arduino/libraries/
Windows:        Documents\Arduino\libraries\

When you're done it should look like this:

Arduino/
└── libraries/
    ├── device_id/
    │   ├── device_id.h
    │   └── device_id.cpp
    ├── wifi_tools/
    │   ├── wifi_tools.h
    │   └── wifi_tools.cpp
    └── mqtt/
        ├── mqtt.h
        └── mqtt.cpp

Restart the Arduino IDE so it picks up the new libraries.

4

Create your sketch

In the Arduino IDE, File → New Sketch, then File → Save As — name it ohioiot_connect. Arduino will create a folder with a matching .ino file inside.

Now add a second file to the sketch. In the tab bar, click the small down-arrow and choose New Tab. Name it credentials.h. Paste these 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.

If you check this sketch into git, add credentials.h to your .gitignore now, before you forget.
5

The sketch

Switch back to the main ohioiot_connect.ino tab and paste this:

#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();
    }
}

The SDK headers use angle brackets (<mqtt.h>) because they live in the Arduino libraries folder. credentials.h uses quotes because it's part of your sketch.

6

Flash and watch

Plug in your ESP32, pick the correct port under Tools → Port, and click the Upload button (→ arrow) in the toolbar.

When it finishes uploading, open Tools → Serial Monitor and set the baud rate to 115200. 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 — add a new tab to your sketch called ca_cert.h (same way you added credentials.h) and define your broker's root certificate:

// 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

Libraries aren't found when compiling — make sure you restarted the Arduino IDE after copying the SDK folders into libraries/. The IDE only scans that directory at startup.

"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.

No port shows up under Tools → Port — you may need a USB-to-UART driver (CP210x or CH340, depending on your board). Unplug, install the driver, and plug back in.

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.