Comments on: ESP32 MQTT – Publish DS18B20 Temperature Readings (Arduino IDE) https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Sun, 26 Apr 2026 20:25:11 +0000 hourly 1 https://wordpress.org/?v=6.8.5 By: Sara Santos https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1182286 Sun, 26 Apr 2026 20:25:11 +0000 https://randomnerdtutorials.com/?p=95323#comment-1182286 In reply to Dani.

GreaT!
I’m glad the issue is solved.
Regards,
Sara

]]>
By: Dani https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1180292 Sun, 19 Apr 2026 18:53:54 +0000 https://randomnerdtutorials.com/?p=95323#comment-1180292 NVM, I got it!

/*
Rui Santos & Sara Santos – Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
extern “C” {
#include “freertos/FreeRTOS.h”
#include “freertos/timers.h”
}

#include <OneWire.h>
#include <DallasTemperature.h>
#include <AsyncMqttClient.h>
#define WIFI_SSID “D Schus”
#define WIFI_PASSWORD “2432Line34”

// Raspberry Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, 0, 192)
// For a cloud MQTT broker, type the domain name
//#define MQTT_HOST “example.com”
#define MQTT_PORT 1883

// Temperature MQTT Topic
#define MQTT_PUB_1 “esp32/ds18b20/temperature”
#define MQTT_PUB_2 “esp32/ds18b20/temperature1”
#define MQTT_PUB_3 “esp32/ds18b20/temperature2”

// GPIO where the DS18B20 is connected to
#define oneWireBus 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Temperature value
float temp;
float temp1;
float temp2;

DeviceAddress sensor1 = { 0x28, 0x4C, 0x45, 0x96, 0xF0, 0x1, 0x3C, 0x91 };
DeviceAddress sensor2 = { 0x28, 0x19, 0x60, 0x96, 0xF0, 0x1, 0x3C, 0x7E };
DeviceAddress sensor3= { 0x28, 0x9F, 0x41, 0x96, 0xF0, 0x1, 0x3C, 0x13 };

AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // Interval at which to publish sensor readings

void connectToWifi() {
Serial.println(“Connecting to Wi-Fi…”);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() {
Serial.println(“Connecting to MQTT…”);
mqttClient.connect();
}

void WiFiEvent(WiFiEvent_t event) {
Serial.printf(“[WiFi-event] event: %d\n”, event);
switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println(“WiFi lost connection”);
xTimerStop(mqttReconnectTimer, 0); // ensure we don’t reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}

void onMqttConnect(bool sessionPresent) {
Serial.println(“Connected to MQTT.”);
Serial.print(“Session present: “);
Serial.println(sessionPresent);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println(“Disconnected from MQTT.”);
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}

/void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println(“Subscribe acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
Serial.print(” qos: “);
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println(“Unsubscribe acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
}
/

void onMqttPublish(uint16_t packetId) {
Serial.println(“Publish acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
}

void setup() {
// Start the DS18B20 sensor
sensors.begin();

Serial.begin(115200);
Serial.println();
Serial.println();

mqttReconnectTimer = xTimerCreate(“mqttTimer”, pdMS_TO_TICKS(2000), pdFALSE, (void)0, reinterpret_cast(connectToMqtt));
wifiReconnectTimer = xTimerCreate(“wifiTimer”, pdMS_TO_TICKS(2000), pdFALSE, (void
)0, reinterpret_cast(connectToWifi));

WiFi.onEvent(WiFiEvent);

mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
//mqttClient.onSubscribe(onMqttSubscribe);
//mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// If your broker requires authentication (username and password), set them below
//mqttClient.setCredentials(“dani”, “dani”);
connectToWifi();
}

void loop() {
unsigned long currentMillis = millis();
// Every X number of seconds (interval = 10 seconds)
// it publishes a new MQTT message
if (currentMillis – previousMillis >= interval) {
// Save the last time a new reading was published
previousMillis = currentMillis;
// New temperature readings
sensors.requestTemperatures();
// Temperature in Celsius degrees
temp = sensors.getTempC(sensor1);
temp1 = sensors.getTempC(sensor2);
temp2 = sensors.getTempC(sensor3);
// Temperature in Fahrenheit degrees
//temp = sensors.getTempFByIndex(0);

// Publish an MQTT message on topic esp32/ds18b20/temperature
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_1, 1, true, String(temp).c_str());

Serial.printf("Publishing on topic %s at QoS 1, packetId: ", MQTT_PUB_1);
Serial.println(packetIdPub1);
Serial.printf("Message: %.2f /n", sensors.getTempC(sensor1));

uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_2, 1, true, String(temp1).c_str());

Serial.printf("Publishing on topic %s at QoS 1, packetId: ", MQTT_PUB_2);
Serial.println(packetIdPub2);
Serial.printf("Message: %.2f /n", sensors.getTempC(sensor2));

uint16_t packetIdPub3 = mqttClient.publish(MQTT_PUB_3, 1, true, String(temp2).c_str());

Serial.printf("Publishing on topic %s at QoS 1, packetId: ", MQTT_PUB_3);
Serial.println(packetIdPub3);
Serial.printf("Message: %.2f /n", sensors.getTempC(sensor3));

}
}

]]>
By: Dani https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1180287 Sun, 19 Apr 2026 18:09:25 +0000 https://randomnerdtutorials.com/?p=95323#comment-1180287 UPDATE! I finally got the ds18b20 connecting with node-red. (laptop issue still persists, but whatever).
The goal of this project is to rig up some floor heat sensor to identify issues in a floor-heating system for piglets.
The next step is to try to integrate several ds18b20’s on one card, but I am having trouble publishing effectively to the other topic addresses. I hope you would be so kind as top offer advice. I have tried to alter the code several ways with limited success. Here’s what I have so far:

/*
Rui Santos & Sara Santos – Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
extern “C” {
#include “freertos/FreeRTOS.h”
#include “freertos/timers.h”
}

#include <OneWire.h>
#include <DallasTemperature.h>
#include <AsyncMqttClient.h>
#define WIFI_SSID “D Schus”
#define WIFI_PASSWORD “2432Line34”

// Raspberry Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, 0, 192)
// For a cloud MQTT broker, type the domain name
//#define MQTT_HOST “example.com”
#define MQTT_PORT 1883

// Temperature MQTT Topic
#define MQTT_PUB_TEMP “esp32/ds18b20/temperature”
#define MQTT_PUB_TEMP1 “esp32/ds18b20/temperature1”
#define MQTT_PUB_TEMP2 “esp32/ds18b20/temperature2”

// GPIO where the DS18B20 is connected to
#define oneWireBus 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Temperature value
float temp;

DeviceAddress sensor1 = { 0x28, 0x4C, 0x45, 0x96, 0xF0, 0x1, 0x3C, 0x91 };
DeviceAddress sensor2 = { 0x28, 0x19, 0x60, 0x96, 0xF0, 0x1, 0x3C, 0x7E };
DeviceAddress sensor3= { 0x28, 0x9F, 0x41, 0x96, 0xF0, 0x1, 0x3C, 0x13 };

AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // Interval at which to publish sensor readings

void connectToWifi() {
Serial.println(“Connecting to Wi-Fi…”);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() {
Serial.println(“Connecting to MQTT…”);
mqttClient.connect();
}

void WiFiEvent(WiFiEvent_t event) {
Serial.printf(“[WiFi-event] event: %d\n”, event);
switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println(“WiFi lost connection”);
xTimerStop(mqttReconnectTimer, 0); // ensure we don’t reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}

void onMqttConnect(bool sessionPresent) {
Serial.println(“Connected to MQTT.”);
Serial.print(“Session present: “);
Serial.println(sessionPresent);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println(“Disconnected from MQTT.”);
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}

/void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println(“Subscribe acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
Serial.print(” qos: “);
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println(“Unsubscribe acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
}
/

void onMqttPublish(uint16_t packetId) {
Serial.println(“Publish acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
}

void setup() {
// Start the DS18B20 sensor
sensors.begin();

Serial.begin(115200);
Serial.println();
Serial.println();

mqttReconnectTimer = xTimerCreate(“mqttTimer”, pdMS_TO_TICKS(2000), pdFALSE, (void)0, reinterpret_cast(connectToMqtt));
wifiReconnectTimer = xTimerCreate(“wifiTimer”, pdMS_TO_TICKS(2000), pdFALSE, (void
)0, reinterpret_cast(connectToWifi));

WiFi.onEvent(WiFiEvent);

mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
//mqttClient.onSubscribe(onMqttSubscribe);
//mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// If your broker requires authentication (username and password), set them below
//mqttClient.setCredentials(“dani”, “dani”);
connectToWifi();
}

void loop() {
unsigned long currentMillis = millis();
// Every X number of seconds (interval = 10 seconds)
// it publishes a new MQTT message
if (currentMillis – previousMillis >= interval) {
// Save the last time a new reading was published
previousMillis = currentMillis;
// New temperature readings
sensors.requestTemperatures();
// Temperature in Celsius degrees
temp = sensors.getTempCByIndex(0);
// Temperature in Fahrenheit degrees
//temp = sensors.getTempFByIndex(0);

// Publish an MQTT message on topic esp32/ds18b20/temperature
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());

Serial.printf("Publishing on topic %s at QoS 0, packetId: ", MQTT_PUB_TEMP);
Serial.println(packetIdPub1);
Serial.printf("Message: %.2f /n", sensors.getTempC(sensor1));

uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_TEMP1, 1, true, String(temp).c_str());

Serial.printf("Publishing on topic %s at QoS 0, packetId: ", MQTT_PUB_TEMP1);
Serial.println(packetIdPub2);
Serial.printf("Message: %.2f /n", sensors.getTempC(sensor2));

uint16_t packetIdPub3 = mqttClient.publish(MQTT_PUB_TEMP2, 1, true, String(temp).c_str());

Serial.printf("Publishing on topic %s at QoS 0, packetId: ", MQTT_PUB_TEMP2);
Serial.println(packetIdPub3);
Serial.printf("Message: %.2f /n", sensors.getTempC(sensor3));

}
}

All node-red displays will only show the esp32/ds18b20/temperature information, though on node red they are specified esp32/ds18b20/temperature, esp32/ds18b20/temperature1, esp32/ds18b20/temperature2.

Any advice?

]]>
By: Dani https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1174930 Wed, 01 Apr 2026 23:26:17 +0000 https://randomnerdtutorials.com/?p=95323#comment-1174930 So here is the current state of affairs.
New + Old Arduino IDE still allows me to select esp32 boards and ports, but fail to upload code (on laptop).
Raspberry Pi Arduino IDE uploads code correctly to esp32 board.
ODDLY when the board is plugged back into laptop, the serial monitor on both old and new IDE are able to open and have the same readout as the rasp pi IDE.

So current issue relating to project:19:20:53.658 ->
19:20:53.658 -> Connecting to Wi-Fi…
19:20:53.658 -> [WiFi-event] event: 101
19:20:53.734 -> [WiFi-event] event: 110
19:20:55.909 -> [WiFi-event] event: 113
19:20:55.909 -> WiFi lost connection
19:20:56.036 -> [WiFi-event] event: 112
19:20:57.066 -> [WiFi-event] event: 115
19:20:57.066 -> WiFi connected
19:20:57.066 -> IP address:
19:20:57.066 -> 192.168.0.187
19:20:57.066 -> Connecting to MQTT…
19:20:57.066 ->
19:20:57.066 -> assert failed: tcp_alloc /IDF/components/lwip/lwip/src/core/tcp.c:1854 (Required to lock TCPIP core functionality!)
19:20:57.066 ->
19:20:57.066 ->
19:20:57.066 -> Backtrace: 0x4008bad4:0x3ffb56d0 0x4008ba99:0x3ffb56f0 0x40092055:0x3ffb5710 0x400f09d3:0x3ffb5850 0x400f0b45:0x3ffb5870 0x400d8542:0x3ffb5890 0x400d5aa9:0x3ffb58e0 0x400d2807:0x3ffb5920 0x400d285a:0x3ffb5940 0x400d3ee3:0x3ffb5980 0x400d3f41:0x3ffb5a80 0x4008cd8d:0x3ffb5aa0
19:20:57.099 ->
19:20:57.099 ->
19:20:57.099 ->
19:20:57.099 ->
19:20:57.099 -> ELF file SHA256: 7a0d2f56e
19:20:57.099 ->
19:20:57.374 -> Rebooting…

]]>
By: Sara Santos https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1174186 Mon, 30 Mar 2026 10:59:53 +0000 https://randomnerdtutorials.com/?p=95323#comment-1174186 In reply to Dani.

Hi.
The link you were using was broken. This is the correct one: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
However, you no longer need to add the URL. Check this tutorial: https://randomnerdtutorials.com/installing-esp32-arduino-ide-2-0/
Regards,
Sara

]]>
By: Dani https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1173960 Sun, 29 Mar 2026 17:39:09 +0000 https://randomnerdtutorials.com/?p=95323#comment-1173960 correction
the wifi does connect, here is the full serial monitor log till error

connecting to wifi…
[wifi-event] event: 101
[wifi-event] event: 110
[wifi-event] event: 113
wifi connection lost
[wifi-event] event: 112
[wifi-event] event: 115
wifi connected
IP address:
xxx.xxx.x.xxx
connecting to mqtt

assert failed: tcp_alloc /IDF/components/lwip/lwip/src/core/tcp.c:1854 (required to lock tcpip core functionality!)

backtrace: then a bunch of number letter combos

]]>
By: Dani https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1173957 Sun, 29 Mar 2026 17:30:41 +0000 https://randomnerdtutorials.com/?p=95323#comment-1173957 Results: restart, nothing, arduino ide on laptop still wont recognize esp32 boards anymore.
arduino ide and necessary libraries on raspberry pi: code uploads, serial monitor reads:
connecting to wifi…
[wifi-event] event: 101
[wifi-event] event: 110
[wifi-event] event: 113
wifi connection lost
[wifi-event] event: 112
[wifi-event] event: 115

and then repeats

Apologies again

]]>
By: Dani https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1173911 Sun, 29 Mar 2026 14:30:43 +0000 https://randomnerdtutorials.com/?p=95323#comment-1173911 The boards all installed to new new ide pre-antivirus shutoff (some reason all boards did not leave old ide on bootup). Antivirus has been turned off, old ide runs now (before antivirus was turned off). With antivirus off i still receive “Error downloading https://raw.githubusercontent.com/espressif/arduino-esp32/ghpages/package_esp32_index.json” upon opening old ide. Both new and old ide will not recognize esp32 boards/com3. Just for kicks i uploaded a blink code to an arduino rev4, success. I am also just gonna throw arduino ide on the raspberry pi since and restart the laptop.

]]>
By: Dani https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1173906 Sun, 29 Mar 2026 14:00:35 +0000 https://randomnerdtutorials.com/?p=95323#comment-1173906 Technically none currently after deleted the file lol, but downloading 3.3.7 currently, downloaded and attempting to upload code. Error.

In file included from C:\Users\dschu\AppData\Local\Arduino15\packages\esp32\tools\esp32-libs\3.3.7/include/bt/include/esp32/include/esp_bt.h:16,
from C:\Users\dschu\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.3.7\cores\esp32\esp32-hal-bt.c:30:
C:/Users/dschu/AppData/Local/Arduino15/packages/esp32/tools/esp32-libs/3.3.7/include/controller/esp32/esp_bredr_cfg.h:18:9: note: ‘#pragma message: BT: forcing BR/EDR max sync conn eff to 1 (Bluedroid HFP requires SCO/eSCO)’
18 | #pragma message (“BT: forcing BR/EDR max sync conn eff to 1 (Bluedroid HFP requires SCO/eSCO)”)
| ^~~~~~~
In file included from C:\Users\dschu\AppData\Local\Arduino15\packages\esp32\tools\esp32-libs\3.3.7/include/bt/include/esp32/include/esp_bt.h:16,
from C:\Users\dschu\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.3.7\cores\esp32\esp32-hal-misc.c:29:
C:/Users/dschu/AppData/Local/Arduino15/packages/esp32/tools/esp32-libs/3.3.7/include/controller/esp32/esp_bredr_cfg.h:18:9: note: ‘#pragma message: BT: forcing BR/EDR max sync conn eff to 1 (Bluedroid HFP requires SCO/eSCO)’
18 | #pragma message (“BT: forcing BR/EDR max sync conn eff to 1 (Bluedroid HFP requires SCO/eSCO)”)
| ^~~~~~~
Sketch uses 928336 bytes (70%) of program storage space. Maximum is 1310720 bytes.
Global variables use 47080 bytes (14%) of dynamic memory, leaving 280600 bytes for local variables. Maximum is 327680 bytes.

A fatal error occurred: Could not open COM3, the port is busy or doesn’t exist.
(could not open port ‘COM3’: OSError(22, ‘A device which does not exist was specified.’, None, 433))

esptool v5.1.0
Serial port COM3:
Failed uploading: uploading error: exit status 2

]]>
By: Sara Santos https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/#comment-1173898 Sun, 29 Mar 2026 13:16:22 +0000 https://randomnerdtutorials.com/?p=95323#comment-1173898 In reply to Dani.

Hi.
Do you have anti-virus installed?
It may be preventing the Arduino IDE to download the boards and necessary packages to install them.
You may need to temporarily disable your antivirus.
Regards,
Sara

]]>