Hi all,
Has anyone actually ever successfully connected an ESP8266 to Open Remote? All the posts I’ve read on this forum are about people facing challenges but never actually getting it to work the way it’s supposed to.
I am new to open remote but have a decent background in electronics. I have been trying to connect my ESP8266 device to the OpenRemote mqtt broker with no success. I have followed the wiki guide and made some adjustments as per the posts on this forum but I keep getting the Error/return code -4.
// Wifi
char* ssid = "Finfoot"; //"Muchira Portal"; // Wifi SSID
const char* password = "H(g{Cw8trahV";// "nobaconhere5"; // Wifi Password
//MQTT Broker
const char* mqtt_server = "192.168.100.173"; //
unsigned int mqtt_port = 1883; //SSL 8883 NoneSSL 1883
const char* username = "master:mqttuser"; // Service User Realm:Serviceuser
const char* mqttpass = "68q0WccufHszcJoJc94dDEYy5MIR5K7U"; // Service User Secret
const char* ClientID = "client123";
//LastWill
const char* lastwill = "master/client123/writeattributevalue/AttributeName/7JiY3cLJTO14FE2GoowKGP";
const char* lastwillmsg = "0";
//subscribing Topic
const char *topic = "master/client123/attribute/subscribeAttribute/7JiY3cLJTO14FE2GoowKGP"; //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" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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, and add comment at #include <WiFi.h>
//#include <WiFi.h>
#include <PubSubClient.h>
//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);
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);
//reconnect();
client.setCallback(callback);
reconnect();
}
void loop() {
//Publish Boolean format:
client.publish("master/client123/writeattributevalue/writeAttribute/7JiY3cLJTO14FE2GoowKGP", "23");
//To publish Strings:
client.publish("master/client123/writeattributevalue/msg/7JiY3cLJTO14FE2GoowKGP", String("Hello").c_str());
delay(10000);
}
//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 also observed that when I change the hostname from localhost to my computer’s IP address I am sometimes unable to access the open remote manager from my browser as the openremote/proxy container keeps restarting on it’s own. I am beginning to think this may be a windows issue. Does anyone have a solution?
The only changes I made to the docker compos.yml was to add the 1883 port as shown below:
ports:
- "80:80"
- "${OR_SSL_PORT:-443}:443"
- "8883:8883"
- "1883:1883"