I don’t have them. My share folder is empty. I’m guessing these need to be created…?
My Arduino code:
#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);
}
}
}
My 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 }}'
sensor 2:
platform: mqtt
state_topic: 'DHT22-1/sensor1'
name: 'Humidity'
unit_of_measurement: '%'
value_template: '{{ value_json.humidity }}'
MQTT Log:
Log
[21:58:59] INFO: Setup mosquitto configuration
[21:58:59] WARNING: SSL not enabled - No valid certs found!
[21:58:59] INFO: No local user available
[21:59:00] INFO: Initialize Hass.io Add-on services
[21:59:00] ERROR: Can't setup Hass.io service mqtt
[21:59:00] INFO: Initialize Home Assistant discovery
[21:59:00] INFO: Start Mosquitto daemon
1568685540: mosquitto version 1.6.3 starting
1568685540: Config loaded from /etc/mosquitto.conf.
1568685540: Loading plugin: /usr/share/mosquitto/auth-plug.so
1568685540: ├── Username/password checking enabled.
1568685540: ├── TLS-PSK checking enabled.
1568685540: └── Extended authentication not enabled.
1568685540: |-- *** auth-plug: startup
1568685540: Opening ipv4 listen socket on port 1883.
1568685540: Opening ipv6 listen socket on port 1883.
1568685540: Opening websockets listen socket on port 1884.
1568685540: Warning: Mosquitto should not be run as root/administrator.
1568685544: New connection from 192.168.0.106 on port 1883.
[INFO] found mqtt_admin on Home Assistant
1568685545: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685583: Client DHT22-1 has exceeded timeout, disconnecting.
1568685590: New connection from 192.168.0.106 on port 1883.
1568685590: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685614: Client DHT22-1 has exceeded timeout, disconnecting.
1568685614: New connection from 192.168.0.106 on port 1883.
1568685614: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685681: Client DHT22-1 has exceeded timeout, disconnecting.
1568685682: New connection from 192.168.0.106 on port 1883.
1568685682: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685723: Client DHT22-1 has exceeded timeout, disconnecting.
1568685724: New connection from 192.168.0.106 on port 1883.
1568685724: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685765: Client DHT22-1 has exceeded timeout, disconnecting.
1568685766: New connection from 192.168.0.106 on port 1883.
1568685766: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685879: New connection from 172.30.32.1 on port 1883.
[INFO] found homeassistant on local database
1568685880: New client connected from 172.30.32.1 as auto-B217936D-C2DD-2850-6C01-1DC467B8CEE9 (p2, c1, k60, u'homeassistant').
1568685880: Client auto-B217936D-C2DD-2850-6C01-1DC467B8CEE9 disconnected.
1568685913: Client DHT22-1 has exceeded timeout, disconnecting.
1568685913: New connection from 192.168.0.106 on port 1883.
[INFO] found mqtt_admin on Home Assistant
1568685913: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685923: New connection from 172.30.32.1 on port 1883.
1568685923: New client connected from 172.30.32.1 as auto-16F91CE4-8773-142D-6660-3FA3B9D597DB (p2, c1, k60, u'homeassistant').
1568685997: Client DHT22-1 has exceeded timeout, disconnecting.
1568685997: New connection from 192.168.0.106 on port 1883.
1568685997: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568686019: Client DHT22-1 has exceeded timeout, disconnecting.
1568686020: New connection from 192.168.0.106 on port 1883.
1568686020: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').