B4R Question Esp32 communicating over wifi (mqtt)


New Member
Hello togehter,

i have following applecation that send data from an ESP32 board over Wifi to a router (local net without internet access).

The router is connected to a pc via ethernet and should read the data sent to the router from the ESP32 board via ethernet.

The software development tool is Arduino IDE. I have decided to use Mqtt to send the data. I am using mqtt explorer and mosquitto software.

Unfortunattly i am not getting the board connected as you can see in the Serialmonitor.

Can anyone have a look through my code and tell me what i am missing?

Here is my code:

#include <WiFi.h>
#include <MQTT.h>

#define BROKER_IP    "" //The Broker IP address that it's using on the Mqtt Explorer
#define DEV_NAME     "mqttdevice"
#define MQTT_USER    "" //Please enter a valid username for your connection
#define MQTT_PW      "" //Please fill the void with your password to make the connection more secure

//////////////////////////// Access Point credentials////////////////////////////////////////////////////////
const char ssid[] = "NETGEAR61"; //Access Point Name
const char pass[] = "classyflute500"; //Access Point Password
////////////////////////////Global variables declaration////////////////////////////////////////////////////////
WiFiClient net;
MQTTClient client;

char buff [50] = "Here are the sensor data";

unsigned long lastMillis = 0; //Keeping track of time

void connect() {

  Serial.print("Wifi Scan Check ... ");
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print("\nConnecting to the access point ...");

  while (!client.connect(DEV_NAME, MQTT_USER, MQTT_PW)) {

  Serial.println("\nConnected successfuly!"); //ESP32 Board connected successfuly
  client.subscribe("/sensor_data"); //Subscribe to the MQTT topic under the name of "Sensor_Data"

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

  // Note: Do not use the client in the callback to publish, subscribe or
  // unsubscribe as it may cause deadlocks when other things arrive while
  // sending and receiving acknowledgments. Instead, change a global variable,
  // or push to a queue and handle it in the loop after calling `client.loop()`.

void setup() {
  Serial.begin(115200); //Initializing the serial monitor on a 115200 Bauderate
  WiFi.begin(ssid, pass); // Init the Wifi Connection

  client.begin(BROKER_IP, 1883, net); //Connect to the broker (Windows Computer)
  client.onMessage(messageReceived); //A special methode of the MQTT library


void loop(void) {
  // put your main code here, to run repeatedly:
  delay(10);  // <- fixes some issues with WiFi stability

  if (!client.connected()) {

  // publish a message roughly every second.
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    client.publish("/sensor_data", buff);

Thank you in advance.


  • Applecation.PNG
    106.4 KB · Views: 94
  • Mqtt_explorer.PNG
    52.7 KB · Views: 95
  • serialmoitor.PNG
    21.6 KB · Views: 95