Cannot Troubleshoot Code (MQTT)

Hey - I have just about finished my set up for a humidity/temp reader but am not getting readings to my home assistant. I’ve followed tons of instruction and have hit an unforeseen roadblock. Is anyone able to determine what I might be missing here? Thank you tons in advance.

Arduino code for node mcu. I DO get readings to the arduino app so I know the node is functioning at least.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <ArduinoJson.h>

#define MQTT_VERSION MQTT_VERSION_3_1_1

// Wifi: SSID and password
const char* WIFI_SSID = "and Yoko";
const char* WIFI_PASSWORD = "lifeinprison";

// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "DHT22-1";
const PROGMEM char* MQTT_SERVER_IP = "192.168.0.105";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "mqtt_admin";
const PROGMEM char* MQTT_PASSWORD = "beatles";

// MQTT: topic
const PROGMEM char* MQTT_SENSOR_TOPIC = "DHT22-1/sensor1";

// DHT - D1/GPIO5
#define DHTPIN D2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
WiFiClient wifiClient;
PubSubClient client(wifiClient);

long lastMsg = 0;
int sleepSeconds = 10;

// function called to publish the temperature and the humidity
void publishData(float p_temperature, float p_humidity) {
  // create a JSON object
  // doc : https://github.com/bblanchon/ArduinoJson/wiki/API%20Reference
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  // INFO: the data must be converted into a string; a problem occurs when using floats...
  root["temperature"] = (String)p_temperature;
  root["humidity"] = (String)p_humidity;
  root.prettyPrintTo(Serial);
  Serial.println("");
  char data[200];
  root.printTo(data, root.measureLength() + 1);
  client.publish(MQTT_SENSOR_TOPIC, data, true);
  yield();
}

// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("INFO: Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
      Serial.println("INFO: connected");
    } else {
      Serial.print("ERROR: failed, rc=");
      Serial.print(client.state());
      Serial.println("DEBUG: try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  // init the serial
  Serial.begin(115200);

  dht.begin();

  // init the WiFi connection
  Serial.println();
  Serial.println();
  Serial.print("INFO: Connecting to ");
  WiFi.mode(WIFI_STA);
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("INFO: WiFi connected");
  Serial.println("INFO: IP address: ");
  Serial.println(WiFi.localIP());

  // init the MQTT connection
  client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  // Send a message every minute
  if (now - lastMsg > 1000 * sleepSeconds) {
    lastMsg = now;
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();

    if (isnan(h) || isnan(t)) {
      Serial.println("ERROR: Failed to read from DHT sensor!");
      return;
    } else {
      publishData(t, h);
    }
  }
}

MQTT 5:

{
  "logins": [
    {
      "username": "mqtt_admin",
      "password": "beatles"
    }
  ],
  "anonymous": false,
  "customize": {
    "active": true,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}

configuration.yaml:


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:


# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

sensor 1:
  platform: mqtt
  state_topic: 'DHT22-1/sensor1'
  name: 'Temperature'
  unit_of_measurement: '°F'
  value_template: '{{ value_json.temperature }}'

/usr/share/hassio/share/mosquitto/accesscontrollist

user mqtt_admin
topic readwrite # 
user homeassistant
topic readwrite # 

/usr/share/hassio/share/mosquitto/acl.conf

acl_file /share/mosquitto/accesscontrollist

do you have more information from log-file?
Try also indention:

sensor 1:
  - platform: mqtt
    state_topic: 'DHT22-1/sensor1'
    name: 'Temperature'
    unit_of_measurement: '°F'
    value_template: '{{ value_json.temperature }}'

Is the component sensor1 ? or sensor 1 ?

Use mosquitto_sub to see what (if anything) is actually being received.

Thanks for getting back to me. The MQTT log is:

[07:00:36] INFO: Setup mosquitto configuration
[07:00:36] WARNING: SSL not enabled - No valid certs found!
[07:00:36] INFO: Found local users inside config
[07:00:37] INFO: Initialize Hass.io Add-on services
[07:00:37] INFO: Initialize Home Assistant discovery
[07:00:37] INFO: Start Mosquitto daemon
1568804437: Error: Unable to open include_dir '/share/mosquitto'.
1568804437: Error found at /etc/mosquitto.conf:29.

I will have to figure out how to use mosquitto_sub after work. One thing I noticed is the two ACL files DO NOT save permanently. I just hooked up my Pi and Node and when I came back into Home Assistant, the two files were not there. I wonder if I’m saving them to the wrong location? @honikos - I tried the extra indentation and unfortunately it did not solve it.

What operating system and installation methods did you use for ha and mosquitto?