I hve been trying to connect a arduino mqtt client to OpenRemote. I have created the service user with read and write roles, also the asset with the writeAttribute. I am not able to establish the connection. It always fails with a return code -2.
Let me have a look at your Code.
I guess the reason is the SSL.
Could you use pastebin.com please?
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include "DHT.h"
#define WIFI_AP "********"
#define WIFI_PASSWORD "*******"
#define TOKEN "ESP32_TEST"
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
char server[] = "192.168.178.53";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
unsigned long lastSend;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dht.begin();
delay(10);
InitWiFi();
lastSend = 0;
client.setServer(server, 1883);
}
void loop() {
if ( !client.connected() ) {
reconnect();
}
if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
getAndSendTemperatureAndHumidityData();
lastSend = millis();
}
client.loop();
}
void getAndSendTemperatureAndHumidityData()
{
char strBuf[50];
Serial.println("Collecting temperature data.");
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
float gasSensorValue = analogRead(35);
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println("Sending data to ThingsBoard:");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C ");
sprintf(strBuf, "%f", temperature);
Serial.println("Gas Sensor Value: ");
Serial.print(gasSensorValue);
Serial.println("Sending current GPIO status ...");
client.publish("master/EspDev/writeattributevalue/temperature/2ZFBXQmUUnYW1fbQDWYbkA", strBuf);
/*tb.sendTelemetryFloat("LPG Concentration in ppm", gasSensorValue);
tb.sendTelemetryFloat("Temperature in C", temperature);
tb.sendTelemetryFloat("Humidity in %", humidity);*/
}
void InitWiFi() {
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
// Attempt to connect (clientId, username, password)
if ( client.connect("EspDev", "master:mqttuser1", "9gNT89Yow4fpwi33zog7lqxcUEb83NOp") ) {
Serial.println( "[DONE]" );
// Sending current GPIO status
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}
This is the Arduino code I have written.
Hi,
so there are two ways to get your device connected:
to use the Secure client:
Instead of
WiFiClient wifiClient;
you should use:
WiFiClientSecure askClient;
in Setup, before you setServer
askClient.setCACert(local_root_ca);
local_root_ca should look like:
const char* local_root_ca = \
"-----BEGIN CERTIFICATE-----\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
"-----END CERTIFICATE-----";
then it should work.
but there is also an easy way, but not that secure:
you can add the 1883 port to the manager:
in the docker-compose.yml you can add
ports:
- "1883:1883"
it should look like:
manager:
# privileged: true
restart: always
image: openremote/manager:${MANAGER_VERSION:-latest}
depends_on:
keycloak:
condition: service_healthy
ports:
- "1883:1883"
environment:
hope, this will help you
Just to mentioned:
it’s not an arduino, it’s an ESP right?
cause it’s not the same.
Thank you for your quick response.
I have to use this secure client to establish the connection, right?
And yes, it is ESP32. I was programming it in Arduino IDE.
Yes, but also the other codes, what I‘ve provided you.
I tried both the solutions you provided… no luck yet
Please provide the docker-compose.yml
And how do you start the docker compose?
Does it run locally, did you tried another mqtt client like mqtt explorer?
The docker-compose file is as below:
volumes:
proxy-data:
temp-data:
postgresql-data:
# btmesh-data:
services:
proxy:
image: openremote/proxy:${PROXY_VERSION:-latest}
restart: always
depends_on:
manager:
condition: service_healthy
ports:
- "80:80"
- "${OR_SSL_PORT:-443}:443"
- "8883:8883"
volumes:
- proxy-data:/deployment
environment:
LE_EMAIL: ${OR_EMAIL_ADMIN:-}
DOMAINNAME: ${OR_HOSTNAME:-localhost}
DOMAINNAMES: ${OR_ADDITIONAL_HOSTNAMES:-}
# USE A CUSTOM PROXY CONFIG - COPY FROM https://raw.githubusercontent.com/openremote/proxy/main/haproxy.cfg
#HAPROXY_CONFIG: '/data/proxy/haproxy.cfg'
postgresql:
restart: always
image: openremote/postgresql:${POSTGRESQL_VERSION:-latest}
volumes:
- postgresql-data:/var/lib/postgresql/data
- temp-data:/tmp
keycloak:
restart: always
image: openremote/keycloak:${KEYCLOAK_VERSION:-latest}
depends_on:
postgresql:
condition: service_healthy
volumes:
- ./deployment:/deployment
environment:
KEYCLOAK_ADMIN_PASSWORD: ${OR_ADMIN_PASSWORD:-secret}
KC_HOSTNAME: ${OR_HOSTNAME:-localhost}
KC_HOSTNAME_PORT: ${OR_SSL_PORT:--1}
manager:
# privileged: true
restart: always
image: openremote/manager:${MANAGER_VERSION:-latest}
depends_on:
keycloak:
condition: service_healthy
ports:
- "1883:1883"
environment:
OR_SETUP_TYPE:
OR_ADMIN_PASSWORD:
OR_SETUP_RUN_ON_RESTART:
OR_EMAIL_HOST:
OR_EMAIL_USER:
OR_EMAIL_PASSWORD:
OR_EMAIL_X_HEADERS:
OR_EMAIL_FROM:
OR_EMAIL_ADMIN:
OR_HOSTNAME: ${OR_HOSTNAME:-localhost}
OR_ADDITIONAL_HOSTNAMES: ${OR_ADDITIONAL_HOSTNAMES:-}
OR_SSL_PORT: ${OR_SSL_PORT:--1}
OR_DEV_MODE: ${OR_DEV_MODE:-false}
# The following variables will configure the demo
OR_FORECAST_SOLAR_API_KEY:
OR_OPEN_WEATHER_API_APP_ID:
OR_SETUP_IMPORT_DEMO_AGENT_KNX:
OR_SETUP_IMPORT_DEMO_AGENT_VELBUS:
volumes:
- temp-data:/tmp
# - /var/run/dbus:/var/run/dbus
# # Bluetooth mesh volume
# - btmesh-data:/btmesh
# devices:
# - /dev/ttyACM0:/dev/ttyS0
I start the docker compose with command- sudo OR_HOSTNAME=192.168.178.53 docker-compose -p openremote up -d
It does not work with mqqt explorer as well
Is it locally on your pc? A Server or VM?
It is working now. In the code for ESP32, I was using the secure client after changing the docker compose file, I changed it to WifiClient. It works now.
It is installed locally on a linux machine
Fine you got it to work. Have fun with openremote.