Trouble connecting ESP8266 to OH2

Hi,
i recently installed openhab on my raspberry pi 3 b+ and i programed a node mcu with the sonof mqtt code and connected it to a relay. I installed mqtt on my server using some guide on the web and I installed the mqtt-binding in openhab. But i`m having trouble connecting the esp to openhab. It connects to the wi-fi but when it goes to connect to the mqtt server i get an error saying:
Attempting MQTT connection…failed, rc=-2 try again in 5 seconds
I have entered the ip of the raspberry pi and also the port in the code. I also know that mqtt is runing on my server from the status command. But i cant seem to get it to connect

have you downloaded mosquitto?

I used this information here:

Ok and have you set up you mqtt config file?

Which is the config file? What do i need to edit there?

for us to better assist you, I suggest you give us more details on what you have done so far (config files, rules, items, even your esp8266 sektches). Then we can all get deeper to see which parts are working and which ones are not

Well after installing Mosquito from the guide shown in my previous answer and installing OH2 from repository installation, nothing else. I only edited mqtt.cfg
mqtt:broker.url=tcp://localhost:1883
mqtt:broker.clientId=openhab
mqtt:broker.retain=true
mqtt:broker.async=false

and added two switches in switch.items which do not work because my esp8266 wont connect to my server. Nothing else. In my sketch i only added the wi-fi credentials, the server ip (i tried ading the port also but no luck) and i edited the pins for the relay.

So did you add mqtt libraries, credentials and code to your arduino sketch? From memory it’s pubsub or similar

Yes I have PubSubClient.h

And you have add the mqtt server credentials into your arduino sketch? From memory a lot of tutorials out on setting up mqtt from an arduino have it output stuff to the serial monitor to see that it’s connecting

FWIW here’s my rough (redacted) sketch that I use to get inverter data to OH2 from my wemos via mqtt. It’s a work in progress that I haven’t worked on for some time now, and includes stuff that does nothing yet (eg the OTA stuff).

But it may give you some clues. Sorry if it renders oddly, working from my phone:

/*
TODO: #include <Time.h>
/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
const char
ssid = "";
const char* password = "
************";
const char
mqtt_server = “192.168.0.123”;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int OTAactive = 0;
const char
outTopic = “digitalPinValue”;
const char
inTopic = “digitalReadPin”;
const char
firmware = “1.0.23”;
int din_1 = D3;
int din_2 = D4;
int din_3 = D5;
int din_4 = D6;
int led_pin = D9;
int analog_pin = A0;
//ADC_MODE(ADC_VCC);

void callback(char* topic, byte* payload, unsigned int length) {
char chpayload[length];
for (int i = 0; i < length; i++) chpayload[i] = payload[i];
if (chpayload[0] == ‘V’) {
float vccf = 0.00f;
vccf = ESP.getVcc();
char espvcc[7];
itoa((ESP.getVcc() / 20), espvcc, 10);
client.publish(outTopic, espvcc);
}
if (chpayload[0] == ‘R’) {
char esprssi[7];
itoa(WiFi.RSSI(), esprssi, 10);
client.publish(outTopic, esprssi);
}
if (chpayload[0] == ‘D’) {
if (chpayload[1] == ‘9’) {
if (chpayload[3] == ‘0’) digitalWrite(led_pin, HIGH);
if (chpayload[3] == ‘1’) digitalWrite(led_pin, LOW);
}
if (chpayload[1] == ‘3’) {
char d3[3];
itoa(digitalRead(din_1), d3, 3);
client.publish(outTopic, d3);
}
if (chpayload[1] == ‘4’) {
char d3[3];
itoa(digitalRead(din_2), d3, 3);
client.publish(outTopic, d3);
}
if (chpayload[1] == ‘5’) {
char d3[3];
itoa(digitalRead(din_3), d3, 3);
client.publish(outTopic, d3);
}
if (chpayload[1] == ‘6’) {
char d3[3];
itoa(digitalRead(din_4), d3, 3);
client.publish(outTopic, d3);
}
}
if (chpayload[0] == ‘A’) {
if (chpayload[1] == ‘0’) {
char chA0[8];
itoa((analogRead(analog_pin) / 12), chA0, 8);
client.publish(outTopic, chA0);
}
}
/*
client.publish(outTopic, chpayload);

if (chpayload[0]=='0') digitalWrite(led_pin,HIGH);
if (chpayload[0]=='1') digitalWrite(led_pin,LOW);

/
}
void setup_wifi() {
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
}
void reconnect() {
while (!client.connected()) {
if (client.connect(“ESP8266Client”, "
", "**********")) {
client.publish(“espannounce”, String(ESP.getChipId()).c_str());
client.publish(“espannounce”, firmware);
client.publish(“espannounce”, String(WiFi.macAddress()).c_str());
//client.publish(outTopic, “D1R1 ready”);
client.subscribe(inTopic);
digitalWrite(led_pin, HIGH);
delay(200);
digitalWrite(led_pin, LOW);
delay(200);
digitalWrite(led_pin, HIGH);
delay(200);
digitalWrite(led_pin, LOW);
delay(200);
digitalWrite(led_pin, HIGH);
} else delay(5000);
}
}
void setup() {
pinMode(led_pin, OUTPUT);
pinMode(analog_pin, INPUT);
pinMode(din_1, INPUT);
pinMode(din_2, INPUT);
pinMode(din_3, INPUT);
pinMode(din_4, INPUT);
ArduinoOTA.setPassword((const char *)"********");
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
ArduinoOTA.onStart( {
Serial.println(“Start”);
OTAactive = 1;
client.disconnect();
});
ArduinoOTA.onEnd( {
Serial.println("\nEnd");
OTAactive = 0;
});
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle();
if (OTAactive == 0) {
if (!client.connected()) reconnect();
client.loop();
}
}

Yes I have added all the cresentials in my sketch. I output on the serial monitor and i get an error saying:

Attempting MQTT connection…failed, rc=-2 try again in 5 seconds

Do you have a user and password setup on your mqtt broker? It may seem odd, but it may connect to the mqtt broker better if it needs to provide a username and password

This tries to connect to the broker with a username and password
Your mqtt.cfg doesn’t have a username and password

Did you set up you broker with a username and password?
Did you allow anonymous access?

I would recommend that you setup a username and password for the broker
and use the same for the arduino code and the mqtt.cfg
You will also need to enter the username and password in your mqtt monitor (I use mqtt.fx but there are others)

Heh think you confused my post/sketch with the person asking the question. Mine uses username and password, as does my broker and mqtt.cfg

Yes,
I though that was @vkirov code

Sorry mate. You need to set up your mqtt broker in the .config file. Have you downloaded the mqtt binding yet? There should be a config file in the services folder. Thats how you connect to mqtt to Openhab

I have downloaded the binding but where is the cfg file and what do i need to config there?

I`m using raspbian stretch. How can i install mqtt on there? When i go and start the service i get an error that there is no mosquitto.service available.

I think first you should find out where the error is:

Is your MQTT Broker up and running? Check with MQTT-Spy and see if it connects propperly.
If that is working you can look on the ESP8266 side and debug it.
Then last point is OH and the MQTT Binding.

Holger