ESP32 MQTT - Send data


To be able to receive data from an ESP32, I have prepared an MQTT broker as a Docker container . The container can be integrated into Home-Assistant and thus forward the data from the ESP32 to Home-Assistant via MQTT. On the part of ESP32, I tested sending with the following sketch and later integrated the relevant parts into another sketch.

Note: As an alternative to the Arduino IDE and MQTT, ESP-Home can be used for Home Assistant. ESP-Home takes care of the connection and data exchange to Home Assistant.
see: Home Assistant + DIY Microcontroller + ESP Home (Docker).

MQTT - ESP32 - WiFi


const char* ssid = "home";
const char* password = "???";
const char* mqttServer = "";
const int mqttPort = 1883;
const char* mqttUser = "mqtt";
const char* mqttPassword = "???";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");
  Serial.println("Connected to the WiFi network");
  client.setServer(mqttServer, mqttPort);
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
    } else {
      Serial.print("failed with state ");
  client.publish("esp32/test", "Hello from ESP32");

void loop() {


When using the Eclipse Mosquitto MQTT Docker image, the connection setup can be examined in the logs, see: MQTT Viewing Connections: Logs

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments

By continuing to browse the site, you agree to our use of cookies. More Details