# Mqtt from ESP to Node-red

**URL:** https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041
**Category:** Hardware
**Created:** [14 October 2022 19:53 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041 "2022-10-14T19:53:02Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [14 October 2022 19:53 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/1 "2022-10-14T19:53:02Z")

</div>

hi

i am using EspMQTTClient.h on my ESP for sending mqtt to node-red. From my Mqtt nodes i am sending my data to influxdb node.

Every 5min i send around 12mqtt msg from my ESP.

On Node-red side i have set up a debug node to see all my mqtt payloads.

Problem is that sometimes i receive all messages. Sometimes only few and sometimes nothing.

ESP:

// Optional functionalities of EspMQTTClient  
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output

ESP Serial Monitor:

```auto
21:34:29.540 -> MQTT << [home/weather_station/sensors/tmp-1] 22.13
21:34:29.588 -> MQTT << [home/weather_station/sensors/tmp-2] 22.13
21:34:29.634 -> MQTT << [home/weather_station/sensors/tmp-3] 22.13
21:34:29.822 -> MQTT << [home/weather_station/sensors/moisture-1] 3857
21:34:30.246 -> MQTT << [home/weather_station/sensors/moisture-2] 3840

```

etc..

ESP serial monitors always writes all 12 msq as sent. But how can i be sure ...

Node-Red Debug:  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23deaaa5137c1836fecc0db9326d6986e1389a90.png)  
Every time i should get 12 mqtt msg. But i dont. I seems random. Sometimes there are gaps of 5min to 1h without any msg received.

Dont know where should i check for the problem. Any tips?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [14 October 2022 21:03 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/2 "2022-10-14T21:03:29Z")

</div>

I would start by installing MQTTExplorer on my PC and connecting the broker, and checking what that sees. If it sees the same as node-red then the messages are not getting to the broker.

Which MQTT broker are you using? Is it running on the pi?

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [14 October 2022 21:05 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/3 "2022-10-14T21:05:42Z")

</div>

I am using mosquito and it is running on Pi

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [14 October 2022 21:07 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/4 "2022-10-14T21:07:25Z")

</div>

Mosquitto is good, the problem will not be with mosquitto.  
Try MQTTExplorer. It is a useful tool to have anyway, often one wants to know what topics things are publishing on.

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [14 October 2022 21:32 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/5 "2022-10-14T21:32:55Z")

</div>

i have installed MQTTExplorer

1st cycle  
MQTTExplorer  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65c232a101a112519e97c8483e8f29c0d694908e.png)

ESP  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/5794d375358369911f974732ddda1d407e9851ec.png)

but cant see in 1st cyle MqttExplorer this one:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/45ef2f4ca9cc75a98bb7e18bb526ea4f4d027a80.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6ea393ded136f5184623a6c6ba66cd2850c2d8b1.png)

etc

2nd cycle(after 5min)  
new msg appeared in MQTTExplorer but not all 12 at the same time.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8ef1aa92241d4ef5deca8a148b1295466291dcdc.png)

3rd cycle  
new msg appeared in MQTTExplorer but not all 12 at the same time.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/25be0e9d88063308689e120c2239d31843aa7606.png)

All 12 msg should appear in the 1st cycle and not separately

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [14 October 2022 21:48 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/6 "2022-10-14T21:48:47Z")

</div>

Ok, will focus on ESP coz it seems that it is not publishing or something is wrong there... will try tomorrow

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [14 October 2022 23:03 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/7 "2022-10-14T23:03:10Z")

</div>

I would guess your sending loop on the ESP is the problem - you have probably not completed filling the messages before you exit the loop and send out the messages

Also there is a limited buffer on the ESP (i assume you are using Ardunio IDE for this ?) are you sure you are not overfilling it ?

Lastly have you looked at the excellent Tasmota code ? even if you do not use their precompiled code you can use the source code as the basis for this - check out their sensors stream

Craig

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [15 October 2022 08:22 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/8 "2022-10-15T08:22:00Z")

</div>

> [@craigcurtin](#):
>
> Lastly have you looked at the excellent Tasmota code ? even if you do not use their precompiled code you can use the source code as the basis for this - check out their sensors stream

Alternatively you could try to go with [node-red-mcu](https://github.com/phoddie/node-red-mcu) & my [node-red-mcu-plugin](https://github.com/ralphwetzel/node-red-mcu-plugin). MQTT works like a charm - as @phoddie has shown @ Node-RED Conf 22.

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [15 October 2022 08:51 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/9 "2022-10-15T08:51:05Z")

</div>

My code is something like this:

I also added some time delays 100milis after each client.publish and 500 before deepsleep.

Setup:

```auto
simpleTimer_sensors = timer.setInterval(1000, sensorData_callback);

```

Functions:

```auto
void sensorData_callback() {
  if(WiFi.status() == WL_CONNECTED && client.isMqttConnected() && sensorData_checkOnlyOnce && sensorRain_allowSensors){
    timer.disable(simpleTimer_sensors);
    sensorData_checkOnlyOnce = false;
    readLight();
    readLux();
    readSensorIR();
    readDS18B20();
    readMoisture();
    readBatteryVoltage();

    deepSleep();
  }
}

```

And then each sensor function for example Light:

```auto
void readLight() {
  int sensorLight = analogRead(sensorLight_PIN);

  client.publish("vikend/weather_station/sensors/light", String(sensorLight), true);
  delay(DELAY_AFTER_PUBLISH); // ADDED a delay hoping that would help but it doesnt

}
void readLux() {
  sensorLightLux.begin();
  int sensorLux = sensorLightLux.GetLightIntensity();

  client.publish("vikend/weather_station/sensors/lux", String(sensorLux), true);
  delay(DELAY_AFTER_PUBLISH);
}

etc

```

At the end there is deepsleep call:

```auto
void deepSleep() {

 ...some other code

    delay(500); // ADDED a delay hoping that would help but it doesnt
    esp_deep_sleep_start();

}

```

MQTTExplorer:  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/29b59f603897335ec6a18803ecd40fdb81840d6f.png)  
Each square represents random cycles(5min) of data that i get. Most of the time i get all data. But sometimes i get only red, blue or green.

After adding delays ... any other suggestion?

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [15 October 2022 09:55 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/10 "2022-10-15T09:55:43Z")

</div>

How about only publishing on one topic and not deep sleeping at all

Is that reliable?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [15 October 2022 10:04 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/11 "2022-10-15T10:04:49Z")

</div>

> [@mojgithub](#):
>
> `simpleTimer_sensors = timer.setInterval(1000, sensorData_callback);`

Doesn't that call the function every second? Does that mean that it restarts it before the previous one has finished?

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [15 October 2022 10:14 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/12 "2022-10-15T10:14:19Z")

</div>

no i have a flag there and i have also disabled timer

timer.disable(simpleTimer\_sensors);  
sensorData\_checkOnlyOnce = false;

Now i am going to try without deepsleep shorter interval from 5min to 1min...

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [15 October 2022 10:32 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/13 "2022-10-15T10:32:43Z")

</div>

After disabling deepsleep and after around 30 x 1min cycles i did get everytime my all 9 sensor data without skipping.  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a19d09454dce2823e7acc96c4d6fc41e253bef8e.png)  
(example for last client-publish data voltage-raw; 1min cycles)

So question is why with deepsleep enabled skips randomly?  
Should i put some code before it goes to deepsleep? Some sort of cleaning, disabling connection.. not good at this, any suggestion?

My procedure to go to deep sleep in something like this ...

```auto
void deepSleep() {
  
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

    delay(500); // Added doesnt help
    esp_deep_sleep_start();
  }   

}

```

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [15 October 2022 11:32 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/14 "2022-10-15T11:32:48Z")

</div>

With DeepSleep enabled after 30 x 1min cycles i got 9 skips on voltage-raw sensor which is the last in the series that i publish. 30% loss which is bad.  
Other sensors have different skip numbers.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [15 October 2022 12:09 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/15 "2022-10-15T12:09:08Z")

</div>

Why not create an object with all the readings and just publish one message?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [15 October 2022 17:24 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/16 "2022-10-15T17:24:43Z")

</div>

You have to remember that after a deep sleep, you need time to wake, reconnect to WiFi and then reconnect to MQTT. My guess is that you are not getting enough time to do all of that before you try your next sleep.

Honestly, if you don't have much experience in programming microprocessors, it is likely that you would be better off using a pre-built firmware rather than rolling your own.

My personal choice is ESPHome which gives a nice balance between robustness, being able to simply configure all of your sensors, outputs, timings and being able to drop into C++ code should you need something that the project hasn't already covered (though they have virtually all of the common sensors and controls already pre-configured). It also has the advantage that you can break your config up into reusable parts which is great if, like me, you have a lot of different ESP devices of different types.

If you do want to roll your own, I would advise you to get the basics right without dealing with deep sleep (which is a whole subject area in itself) and then go do some research on the best way to use deep sleep with MQTT.

---

<div class="post-metadata">

### Author: ![PeteKnight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/peteknight/32/18345_2.png) [@PeteKnight](https://discourse.nodered.org/u/PeteKnight)
#### Post date: [16 October 2022 06:28 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/17 "2022-10-16T06:28:43Z")

</div>

> [@mojgithub](#):
>
> `delay(DELAY_AFTER_PUBLISH); // ADDED a delay hoping that would help but it doesnt`

I'd try replacing that delay with a `client.loop();` command, or at the very least explicitly calling `client.loop()` before initiating deepsleep.

Pete.

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [16 October 2022 19:33 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/18 "2022-10-16T19:33:50Z")

</div>

i have done this but without any success.

---

<div class="post-metadata">

### Author: ![mojgithub](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mojgithub/32/63532_2.png) [@mojgithub](https://discourse.nodered.org/u/mojgithub)
#### Post date: [16 October 2022 19:39 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/19 "2022-10-16T19:39:45Z")

</div>

I have created 1 object with multiple sensors readings and only 1 client.publish instead of having 9 seperated sensors readings / 9 client.publish events.

Results are the same. Skipping if using deepsleep. Without deepsleep it works normally

 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04497cbb785e87d97359c6d3805bf19460c7c711.png)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [16 October 2022 21:40 UTC](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041/20 "2022-10-16T21:40:02Z")

</div>

I don’t know if this will help but when you wake from deep sleep your setup code will run. How does that effect your processing?

[Next page](https://discourse.nodered.org/t/mqtt-from-esp-to-node-red/69041.md?page=2)
