How to actually use mqtt with add-on and discovery?

Hi!

I have tried to make use of mqtt with a sensor that I have made. https://community.home-assistant.io/t/diy-low-powered-esp8266-window-sensor/224687

But I am having trouble getting “things” to work properly, and the sea of old and new information is a bit overwhelming so I wanted to see if anyone could help me understand what is actually needed of configuration in home assistant and the c++ code on the esp8266.

I have installed the add-on Mosquitto broker (version 5.1), and the current config is like this:

mqtt configuration
logins:
  - username: mqtt_user
    password: mqtt_password
anonymous: true
customize:
  active: false
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

After the installation I added the integration in home assistant and enabled discovery.

The current code I have on the esp8266 is like this, I am using the pubsubclient (version 2.8):

esp8266 code
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <PubSubClient.h>
#include <secrets.h>

String ssid = SECRET_SSID;
String password = SECRET_PASS;
String hostName = "esp12_mailboxsensor01";
//IPAddress ip(192,168,1,154);
//IPAddress gw(192,168,1,1);
//IPAddress dns(192,168,1,2);
//IPAddress subnet(255,255,255,0);
#define MQTT_SERVER "192.168.1.20"

int sensorPin = 12;
int enablePin = 5;
int sensor;
int code;
String temp = "homeassistant/" + hostName + "/opening";
const char* stateTopic = temp.c_str();

//Enable getvcc
ADC_MODE(ADC_VCC);

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

  //Enable power signal as soon as possible
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);

  pinMode(sensorPin,INPUT);

  Serial.begin(115200);
  Serial.println();

  //Connect to WIFI
  WiFi.mode(WIFI_STA);
  WiFi.hostname(hostName);
  //WiFi.config(ip, subnet, gw, dns);
  WiFi.begin(ssid, password);

  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    WiFi.begin(ssid, password);
    Serial.println("WiFi failed, retrying.");
  }

  client.setServer(MQTT_SERVER, 1883);
  
  while (!client.connected()) {
    // Attempt to connect
    if (client.connect(hostName.c_str())) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }

  //Get the battery voltage and send to HA
  float vcc = ESP.getVcc();
  vcc = vcc / 1000;
  String svcc = String(vcc,3);
  Serial.println(svcc);
//  Serial.println(code);
//  Serial.println();
  


  sensor = digitalRead(sensorPin);
  
  //Send new data to HA
  if (sensor == 0){
    client.publish(stateTopic, "open");
    Serial.println("Published open.");
  }
  else if (sensor == 1) {
    client.publish(stateTopic, "closed");
    Serial.println("Published closed.");
  }
  
  //Turn of the latching circuit
  digitalWrite(enablePin, LOW);
  
}

void loop() {
  //Don't want to loop anything
}

In the logs on the core_mosquitto I get these messages:

1600258898: New connection from 192.168.1.203 on port 1883.
1600258898: New client connected from 192.168.1.203 as esp12_mailboxsensor01 (p2, c1, k15).
1600258920: Client esp12_mailboxsensor01 has exceeded timeout, disconnecting.

And I can’t find any information information in home assistant that it has received anything.
There are neither any devices showing up in the MQTT integration.

Some questions I have that don’t need to be answered if they don’t contribute to the solution.

  1. I have read on https://www.home-assistant.io/docs/mqtt/discovery/ that it requires configuration written in the yaml file. Is this true when enabling the discovery when enabling the integration?
    And if not, what is the discovery_prefix?
  2. The example in https://www.home-assistant.io/docs/mqtt/discovery/#motion-detection-binary-sensor shows that the sensor should be created manually before turning it on, maybe this is what I am missing? If so, how could I write that in the c++ code?
  3. Is there anything else I am missing?