← Back to Garden
budding ·
mqtt hivemq iot cloud esp32

HiveMQ Cloud

HiveMQ Cloud is a managed MQTT broker service. We use it because the ESP32 (on mobile hotspot) can't reach our home Mac Mini directly due to NAT.

Why Cloud MQTT?

Problem:
  ESP32 → Phone Hotspot → Internet → ??? → Home Router (NAT) → Mac Mini
                                      ↑
                          Can't reach home from outside!

Solution:
  ESP32 → Internet → HiveMQ Cloud ← Internet ← Mac Mini
                     (both connect to cloud)

Setup

  1. Go to https://www.hivemq.com/cloud/
  2. Sign up (free tier available)
  3. Create a "Serverless" cluster (free forever, 10GB/month)
  4. Note your cluster URL: xxxxx.s1.eu.hivemq.cloud
  5. Create credentials in "Access Management"

Free Tier Limits

  • 10 GB data transfer per month
  • 100 concurrent connections
  • Unlimited messages
  • TLS encryption included

For telemetry at ~1 message/second with ~200 bytes each:

  • 200 bytes × 60 sec × 60 min = 720 KB/hour
  • 720 KB × 24 hours = 17 MB/day
  • 17 MB × 30 days = 510 MB/month

Plenty of headroom!

Connection Details

Setting Value
Host xxxxx.s1.eu.hivemq.cloud
Port 8883 (TLS)
Protocol MQTT over TLS
Username Your created username
Password Your created password

Testing with mosquitto_pub

mosquitto_pub \
  -h xxxxx.s1.eu.hivemq.cloud \
  -p 8883 \
  -u username \
  -P 'password' \
  --capath /etc/ssl/certs/ \
  -t "car/telemetry" \
  -m '{"rpm":3000,"speed":100}'

Note: Use single quotes around passwords with special characters to avoid shell interpretation.

Python Client

import ssl
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.username_pw_set("username", "password")
client.tls_set(tls_version=ssl.PROTOCOL_TLS)

client.connect("xxxxx.s1.eu.hivemq.cloud", 8883, 60)
client.subscribe("car/telemetry")
client.loop_forever()

ESP32 Client (Arduino)

#include <WiFiClientSecure.h>
#include <PubSubClient.h>

const char* mqtt_server = "xxxxx.s1.eu.hivemq.cloud";
const int mqtt_port = 8883;
const char* mqtt_user = "username";
const char* mqtt_pass = "password";

WiFiClientSecure espClient;
PubSubClient client(espClient);

void setup() {
  espClient.setInsecure(); // Skip cert validation (for testing)
  client.setServer(mqtt_server, mqtt_port);
  client.connect("esp32-car", mqtt_user, mqtt_pass);
}

void loop() {
  client.publish("car/telemetry", "{\"rpm\":2500}");
  delay(1000);
}

Topic Structure

We use a simple topic structure:

Topic Purpose
car/telemetry Real-time metrics (JSON)
car/status Online/offline status
car/errors Error messages

Message Format

JSON payload for car/telemetry:

{
  "rpm": 2500,
  "speed": 80,
  "coolant": 90,
  "intake_temp": 35,
  "throttle": 45,
  "engine_load": 60,
  "map_kpa": 101,
  "fuel_level": 75,
  "timing_adv": 12,
  "battery_voltage": 14.2
}

Web Client

HiveMQ provides a web-based MQTT client for testing:

  1. Go to your cluster dashboard
  2. Click "Web Client"
  3. Connect and publish/subscribe to topics

Security Notes

  • Always use TLS (port 8883)
  • Create separate credentials for ESP32 and bridge
  • Rotate passwords periodically
  • Don't commit credentials to git

Resources