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!