Hi Guys,
I’m new to OpenHab, i switched from home assistant to here. But i have a problem, and it’s an awfull one.
I’m trying to get the MQTT broker to work, i’m using OpenHabian for this. It is installed, and working fine i think. But when i try to get the ESP8266 to connect to it i keep getting an error RC=5 (MQTT_CONNECT_UNAUTHORIZED ). What can i do to solve this?
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ArduinoJson.h>
/************ WIFI and MQTT INFORMATION (CHANGE THESE FOR YOUR SETUP) ******************/
#define wifi_ssid "###" //type your WIFI information inside the quotes
#define wifi_password "###"
#define mqtt_server "###"
#define mqtt_user "openhabian"
#define mqtt_password "###!"
#define mqtt_port 1883
/**************************** PIN DEFINITIONS ********************************************/
#define DHTPIN D7
#define DHTTYPE DHT22
long previousMillis = 0; // Timer loop from http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
long interval = 60000; //
String temp_str; //see last code block below use these to convert the float that you get back from DHT to a string =str
String hum_str;
char temp[50];
char hum[50];
long lastMsg = 0;
char msg[50];
int value = 0;
WiFiClient OpenHab;
PubSubClient client(OpenHab);
DHT dht(DHTPIN, DHTTYPE);
/********************************** START SETUP WIFI*****************************************/
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/****reset***/
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
Serial.print("resetting");
ESP.reset();
}
/********************************** START SETUP*****************************************/
void setup() {
Serial.begin(9600);
pinMode(DHTPIN, INPUT);
Serial.begin(9600);
delay(10);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
/**************98769879879******************** START SEND STATE*****************************************/
/*
Calculate Heat Index value AKA "Real Feel"
NOAA heat index calculations taken from
http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
float calculateHeatIndex(float humidity, float temp) {
float heatIndex= 0;
if (temp >= 80) {
heatIndex = -42.379 + 2.04901523*temp + 10.14333127*humidity;
heatIndex = heatIndex - .22475541*temp*humidity - .00683783*temp*temp;
heatIndex = heatIndex - .05481717*humidity*humidity + .00122874*temp*temp*humidity;
heatIndex = heatIndex + .00085282*temp*humidity*humidity - .00000199*temp*temp*humidity*humidity;
} else {
heatIndex = 0.5 * (temp + 61.0 + ((temp - 68.0)*1.2) + (humidity * 0.094));
}
if (humidity < 13 && 80 <= temp <= 112) {
float adjustment = ((13-humidity)/4) * sqrt((17-abs(temp-95.))/17);
heatIndex = heatIndex - adjustment;
}
return heatIndex;
}
*/
/**** Reconnect ***/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("OpenHab")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/********************************** START MAIN LOOP***************************************/
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
delay(2000);
//this is where you get the data from the sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
//Using Serial print to debug
Serial.print("\n\n");
Serial.print("Luchtvochtigheid in %");
Serial.print(humidity, 1);
Serial.print("\t\t");
Serial.print("Temperatuur in C");
Serial.print(temperature, 1);
Serial.print("\n");
//counter for the messages, see if I am missing any on the Mqtt broker
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
//Preparing for mqtt send
temp_str = String(temperature); //converting ftemp (the float variable above) to a string
temp_str.toCharArray(temp, temp_str.length() + 1); //packaging up the data to publish to mqtt whoa...
hum_str = String(humidity); //converting Humidity (the float variable above) to a string
hum_str.toCharArray(hum, hum_str.length() + 1); //packaging up the data to publish to mqtt whoa...
Serial.print("Publish message: ");//all of these Serial prints are to help with debuging
Serial.print("\n");
client.publish("Algemeen/Temperatuur", temp); //money shot
client.publish("Algemeen/Luchtvochtigheid", hum);
}
}
This is my code for the ESP. it worked with Home Assistant, but just not with this one.
My mqtt.cfg is here.
#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#
# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
OpenHab.url=tcp://localhost:1883
# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.
OpenHab.clientId=OpenHab
# Optional. User id to authenticate with the broker.
OpenHab.user=openhabian
# Optional. Password to authenticate with the broker.
OpenHab.pwd=###
Thx for the help guys