Skip to main content

Connect ESP32 or ESP8266 using MQTT

Version: 1.26.0

ESP32 and ESP8266 are some of the most popular and well established boards for devices by Espressif. ESP32 and ESP8266 (and other types) can be easily linked to an OpenRemote instance using our MQTT Broker. If you have larger numbers of devices connecting you might want to use the auto provisioning flow. This allows you to provision your devices in such a way that they automatically connect your devices to OpenRemote, create an asset of the type you have defined, and link the attributes over the right topics. ESP32 and ESP8266 are perfectly suitable to make use of it.

Here are some practical tips and code samples to get you going.

Publishing and subscribing to topics over MQTT

This a basic MQTT example for connecting to the OpenRemote MQTT Broker. It consists of two files OpenRemoteESP32.ino and secret.h. Take care you adjust yourrealm, ClientID, AttributeName, and AssetID for each topic to match your setup. Also add a valid certificate.

OpenRemoteESP32.ino

#include "secret.h"

#if defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <WiFiClientSecure.h>
#include <PubSubClient.h>

WiFiClientSecure networkClient;
PubSubClient client(networkClient);

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

void reconnect() {
while (!client.connected()) {
if (client.connect(clientId, username, mqttPassword, lastWillTopic, 1, true, "0")) {
client.subscribe(subscribeTopic);
} else {
delay(5000);
}
}
}

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, wifiPassword);
while (WiFi.status() != WL_CONNECTED) delay(500);

networkClient.setCACert(rootCa);
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
}

void loop() {
if (!client.connected()) reconnect();
client.loop();
client.publish(publishTopic, "1");
delay(10000);
}

This code is for an ESP32. In case you use an ESP8266, change the Wi-Fi Library. #include <ESP8266WiFi.h> (see code)

For ESP8266 SSL Connection, you need a fingerprint of your Server Certificate Example: "static const char *fingerprint PROGMEM = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";" and in the setup: askClient.setFingerprint(fingerprint);

This sketch demonstrates the basic capabilities of the library. It connects to the OpenRemote MQTT Broker then:

  • publishes boolean "1" to your topic
  • publishes "Hello" to another topic
  • subscribes to your topic printing out any messages

It will reconnect to the server if the connection is lost using a blocking reconnect function.

secret.h

#pragma once

const char* ssid = "xxxxxxxxxx";
const char* wifiPassword = "xxxxxxxxxx";
const char* mqttServer = "your-installation.example";
const unsigned int mqttPort = 8883;
const char* username = "yourrealm:service-user";
const char* mqttPassword = "xxxxxxxxxx";
const char* clientId = "device-01";
const char* publishTopic = "yourrealm/device-01/writeattributevalue/AttributeName/AssetID";
const char* subscribeTopic = "yourrealm/device-01/attributevalue/AttributeName/#";
const char* lastWillTopic = "yourrealm/device-01/writeattributevalue/AttributeName/AssetID";

static const char rootCa[] = R"EOF(
-----BEGIN CERTIFICATE-----
Replace this line with the PEM-encoded CA certificate.
-----END CERTIFICATE-----
)EOF";

Edit secret.h for your credentials.

In case you connect to the broker nonsecure, change the object "WifiClientSecure" to "WifiClient" and comment out in setup askClient.setCACert(local_root_ca); Also change the MQTT Port in secret.h

Important: Don't forget to add the PubSubClient to your Library https://github.com/knolleary/pubsubclient

Auto provisioning ESP32 based devices

OpenRemote supports auto provisioning which can also be applied with Espressif products. See the User Guide Auto provisioning

Note you need to add a CA certificate and add the code to locate the appropriate CA certificate for the server, making a secure connection possible.

See Also


License notice: OpenRemote documentation attribution