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

[+]
#include 
#include 

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

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    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 )) {
      Serial.println("connected");
    } else {
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
    }
  }
  client.publish("esp32/test", "Hello from ESP32");
}

void loop() {
  client.loop();
}

Source: www.esp32learning.com/code/publishing-messages-to-mqtt-topic-using-an-esp32.php

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!

Updated: 2023-08-19 von Bernhard | Übersetzung Deutsch |🔔 | Comments:0

ESP32 WiFi example | ESP32 | ESP32 Flowmeter and RS485 Modbus
MQTT - Broker in Docker

Top articles in this section


ESP32 Flowmeter and RS485 Modbus

As described on the article ESP32 programming, Arduino - install requirements, my first goal was to read out a TUF-2000M Ultrasonic Flow Meter via an ESP32. I found an example for an ESP8266 on the internet: Reading a TUF-2000M Ultrasonic Flow Meter with an Arduino or ESP8266 and https://forum.arduino.cc/t/comunicacion-rs485/698786/2. I described the setup of the TUF-2000M in the following article: Field Report: Ultrasonic Flow Meter TUF-2000M. To be able to read out the TUF-2000M via RS485, it...


DS18B20 - Temperature sensors in ESP-Home

Complementary to the article: DS18B20 Temperature Sensors ESP32, MQTT and WiFi - HowTo, I have meanwhile replaced the Arduino project with ESP-Home. Simple projects can be implemented much easier in ESPHome. As an example, in ESPHome these 2 lines are enough to address the temperature sensors:


add a Relay Board ESP32 - ESPHome

To be able to switch certain relays in Home Assistant via an ESP32, I tested a relay board and integrated it via ESP-Home.

Questions / Comments


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