Connect ESP32 to Open Remote with MQTT

Hi all,

I’m trying to use an ESP32 board as client to send data with MQTT to a locally hosted Open Remote environment.

I used these wiki topics to get to where I now am:
Quick Start
Connect ESP32 or ESP8266 using MQTT
Connect Your MQTT Client

First I tried to use port 1883, which didn’t work, but then i found this topic on the forum:

After this I tried to connect to Open Remote with MQTT by using MQTTX and this worked (could publish in OR) with the following settings:

I use the Arduino IDE and my ESP32 code is the following:

// Wifi 
const char* ssid = "AndroidAP"; // Wifi SSID
const char* password = "prupke124"; // Wifi Password

//MQTT Broker
const char* mqtt_server = "mqtt://localhost";
unsigned int mqtt_port = 8883; //SSL 8883 NoneSSL 1883
const char* username = "master:mqttuser"; // Service User Realm:Serviceuser
const char* mqttpass = "gyCS6uEDWnt4FJohamMeteO2oGQDqnnu"; // Service User Secret
const char* ClientID = "client01";
//LastWill
const char* lastwill = "master/client01/writeattributevalue/writeAttribute/6Ndo7qsOqF6UfgAJMScwDK";
const char* lastwillmsg = "0";


//subscribing Topic
const char *topic = "master/client01/attribute/+/#"; //see Subscribing Topics in Documentation https://github.com/openremote/openremote/wiki/User-Guide%3A-Manager-APIs#mqtt-api-mqtt-broker


//Local CA

const char* local_root_ca = \
                            "-----BEGIN CERTIFICATE-----\n" \
                            "MIIDnzCCAoegAwIBAgIUE3jYzxKpepVM0CSLZd9GNv6BHj8wDQYJKoZIhvcNAQEL\n" \
                            "BQAwUDELMAkGA1UEBhMCR0IxHTAbBgNVBAMMFE9wZW5SZW1vdGUgRGVtbyBDZXJ0\n" \
                            "MRMwEQYDVQQKDApPcGVuUmVtb3RlMQ0wCwYDVQQLDAREZW1vMCAXDTIwMDYwODE5\n" \
                            "MTc1MVoYDzIwNTAwNjAxMTkxNzUxWjBQMQswCQYDVQQGEwJHQjEdMBsGA1UEAwwU\n" \
                            "T3BlblJlbW90ZSBEZW1vIENlcnQxEzARBgNVBAoMCk9wZW5SZW1vdGUxDTALBgNV\n" \
                            "BAsMBERlbW8wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCrKEk77HcJ\n" \
                            "B5SqvoN2UbRsDh9d0ECN8tOU5hC2poih+6XBJgikQ8gdy7ptt477KRh3ZIiw3ZTX\n" \
                            "Hg0//Ju71D/4EDBYwHxoSK9WehP5Kz/LrBHhtArXK3RYH8pFS13CDOPjXnm6LMN5\n" \
                            "2mRGwm2gCwKwRTbfm+D9hjpVuwt0sfHaXVETlUc4JystlfYVurMcfsox9tsbRuzl\n" \
                            "EakyK9Cr1V7bgaLMosHDX3NSuEyzb9DQZ3PBK3JjJhSeYkGNuP/NocMrWy/JHd2v\n" \
                            "2Wev9W+D1Pv46Sqfrvd6K7oP00FL0CdODkMRBVTlb1wq/6uJdRbnVUM0PGA9enrQ\n" \
                            "vMB11fFglHa3AgMBAAGjbzBtMB0GA1UdDgQWBBT0ixs03BOrns+E2+xSU+nfP9KX\n" \
                            "iTAfBgNVHSMEGDAWgBT0ixs03BOrns+E2+xSU+nfP9KXiTAPBgNVHRMBAf8EBTAD\n" \
                            "AQH/MBoGA1UdEQQTMBGCCWxvY2FsaG9zdIcEfwAAATANBgkqhkiG9w0BAQsFAAOC\n" \
                            "AQEAawmLoD7bzFTM0Z58PR6jQR3ypD6IAyei6xiBI7wvxbjyxqQrk1i0rK2Aexjk\n" \
                            "v2ZsAUmtrm5k5pWpBsokNuRddPV1K2OZjTj9HPc9AxqjyHKyqRXmVKWkzbWQDLVS\n" \
                            "lGRk7yviUFS8nRuY0vLfqZzF7e2HeasThILJibY8rUVLuq+iMS35RDwQ9usIOiYz\n" \
                            "dF4CO3HFZ6NtDheM1mPAy4Q76H1P1fINuA8mp/by9J8heexqjgpBKYexiQhjb1A7\n" \
                            "NBdWbJPXoNJplGXjGIbj8KxW61ih1wDRE2ZseOflRstO9/Txm7+Cuqo+WBOK39cU\n" \
                            "CXPKre2pqmkIu65wJ6VcTKeSqw==" \
                            "-----END CERTIFICATE-----";



//#include "secret.h"
//#include <ESP8266WiFi.h> // remove comment for ESP8266, and add comment at #include <WiFi.h> 
#include <WiFi.h>      
#include <WiFiClientSecure.h>    
#include <PubSubClient.h>


#define BUTTON_PIN 21 // GIOP21 pin connected to button
// Variables will change:
int lastState = HIGH; // the previous state from the input pin
int currentState;     // the current reading from the input pin


//Objects
WiFiClientSecure askClient; //SSL Client
//WiFiClient askClient; //Non-SSL Client, also remove the comments for askClient.setCACert(local_root_ca);

PubSubClient client(askClient);

void setup() {
  Serial.begin(115200);
  Serial.println(ssid);

  // initialize the pushbutton pin as an pull-up input
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);

  }

  Serial.println(WiFi.localIP());
  askClient.setCACert(local_root_ca); //If you use non SSL then comment out
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);

}

void loop() {
  Serial.println("Currently in state loop");

  // read the state of the switch/button:
  currentState = digitalRead(BUTTON_PIN);
  if(lastState == LOW && currentState == HIGH)
    Serial.println("The state changed from LOW to HIGH");
  // save the last state
  lastState = currentState;

  
  //Publish Boolean format:
  //yourrealm/ClientID/writeattributevalue/AttributeName/AsssetID
  
  client.publish("master/client01/writeattributevalue/publish/6Ndo7qsOqF6UfgAJMScwDK", "1");
  //To publish Strings:
  //client.publish("yourrealm/ClientID/writeattributevalue/AttributeName/2DDreaYR3vApZH5yRTSS2E", "1");
  delay(5000);

}

//MQTT callback
void callback(char* topic, byte * payload, unsigned int length) {

  for (int i = 0; i < length; i++) {
    Serial.println(topic);
    Serial.print(" has send ");
    Serial.print((char)payload[i]);
  }

}

//MQTT reconnect
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("********** Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(ClientID, username, mqttpass, lastwill, 1, 1, lastwillmsg)) {
      Serial.println("-> MQTT client connected");
      client.subscribe(topic);
      Serial.print("Subscribed to: ");
      Serial.println(topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println("-> try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

I think there now must be a problem in my code because i can’t create a connection between the ESP32 and Open Remote.
Hopefully someone can help me!
I would like to hear when you need more clarifying questions.
Thanks!

1 Like

Hi! I had the same problem, I think the problem is your SSL certs. Check your Root CA again or try port 1883 (without SSL). Btw, I can’t fix it either lol.

Hi @beautifulcarrot12
maybe it’s a stupid question but have you tried with

const char* mqtt_server = "localhost";

I checked some random sites for esp32 mqtt and everyone use only the IP/fqdn

And if that doesn’t work you could try with the s like you have in your MQTTX screenshot: “mqtts://localhost”; :upside_down_face:

Hi,

what i observed that , pls follow below steps

create realm ----> under realm create service user try to connect
Capture

or i am able to connect through master/user
Capture

Thanks for your response! Port 1883 doesn’t work while using MQTTX or ESP32. I also think the problem is with the certificate. I now copied the certificate from: proxy/01-selfsigned at main · openremote/proxy · GitHub. Are there more steps I should preform when using this type of certificate or are there alternatives?

Thanks! Tried this but it didn’t work, but there could be multiple issues causing the problem of course :wink:

Thanks Don! Also no succes when changing this issue

Thanks for your suggestion, but I don’t really understand what the use would be of doing that. I already created a service user called “mqttuser” and use the realm “master”. This works when using MQTTX, but not when trying to connect with a ESP32

I found this forum thread but don’t really understand it: Cannot connect to MQTT and SSL certificates

Could this be usefull for me?

hi by following this step have you able to connect esp32 to openremote

hi community i am trying to connect esp32 to openremote but not able to connect can any one try it and able to connect if yes can you help me out.
4

I used this library : GitHub - 256dpi/arduino-mqtt: MQTT library for Arduino
There is my code:
https://github.com/swalker2000/OpenRemoteEsp/blob/main/OpenRemoteEsp.ino
Everything worked

#include <WiFiClientSecure.h>
#include <MQTT.h>

const char ssid[] = "";
const char pass[] = "";
const char* mqtt_server = "";
const char *subscribeTopic = "";
const char *publicTopic = "";
const char* username = "master:mqttuser"; 
const char* mqttpass = ""; 
const char* clientID = "client123";

WiFiClientSecure net;
MQTTClient client;

unsigned long lastMillis = 0;

void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.print("\nconnecting...");
  net.setInsecure();
  while (!client.connect(clientID, username, mqttpass)) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nconnected!");
  client.subscribe(subscribeTopic);
}

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  client.begin(mqtt_server, 8883, net);
  client.onMessage(messageReceived);
  pinMode(25, INPUT);
  connect();
}

void loop() {
  client.loop();
  delay(10);  // <- fixes some issues with WiFi stability

  if (!client.connected()) {
    connect();
  }
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    int value = digitalRead(25);
    String valueStr = String(value);
    Serial.print("Value : "); Serial.println(value);
    client.publish(publicTopic, valueStr.c_str());
  }
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.