{"id":96329,"date":"2020-05-07T14:24:53","date_gmt":"2020-05-07T14:24:53","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=96329"},"modified":"2020-05-08T09:55:39","modified_gmt":"2020-05-08T09:55:39","slug":"esp8266-nodemcu-mqtt-publish-ds18b20-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-mqtt-publish-ds18b20-arduino\/","title":{"rendered":"ESP8266 NodeMCU MQTT &#8211; Publish DS18B20 Temperature Readings (Arduino IDE)"},"content":{"rendered":"\n<p>Learn how to publish DS18B20 temperature readings via MQTT with the ESP8266 (NodeMCU) to any platform that supports MQTT or any other MQTT client. As an example, we&#8217;ll publish sensor readings to Node-RED Dashboard and the ESP8266 will be programmed using Arduino IDE.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-DS18B20-Temperature-Sensor-MQTT-Publish-Arduino-IDE-Node-RED.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU MQTT - Publish DS18B20 Temperature Readings (Arduino IDE)\" class=\"wp-image-96330\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-DS18B20-Temperature-Sensor-MQTT-Publish-Arduino-IDE-Node-RED.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-DS18B20-Temperature-Sensor-MQTT-Publish-Arduino-IDE-Node-RED.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-DS18B20-Temperature-Sensor-MQTT-Publish-Arduino-IDE-Node-RED.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-DS18B20-Temperature-Sensor-MQTT-Publish-Arduino-IDE-Node-RED.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclgray\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\">What is MQTT and How It Works<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>The following diagram shows a high-level overview of the project we&#8217;ll build.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"857\" height=\"426\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-Publish-DS18B20-Readings-Node-RED.png?resize=857%2C426&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU MQTT Publish DS18B20 Temperature Readings How it works and project overview\" class=\"wp-image-96333\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-Publish-DS18B20-Readings-Node-RED.png?w=857&amp;quality=100&amp;strip=all&amp;ssl=1 857w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-Publish-DS18B20-Readings-Node-RED.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-Publish-DS18B20-Readings-Node-RED.png?resize=768%2C382&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 857px) 100vw, 857px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>The ESP8266 requests temperature readings from the DS18B20 sensor. The readings are published in the&nbsp;<strong>esp\/ds18b20\/temperature<\/strong>&nbsp;topic;<\/li><li>Node-RED is subscribed to the&nbsp;<strong>esp\/ds18b20\/temperature<\/strong>&nbsp;topic. So, it receives the DS18B20 temperature readings and displays the readings in a gauge\/chart;<\/li><li>You can receive the readings in any other platform that supports MQTT and handle the readings as you want.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding with this tutorial, make sure you check the following prerequisites.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Arduino IDE<\/h3>\n\n\n\n<p>We&#8217;ll program the ESP8266 using Arduino IDE, so make sure you have the ESP8266 add-on installed.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Installing ESP8266 Board in Arduino IDE (Windows, Mac OS X, Linux)<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">MQTT Broker<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"200\" height=\"197\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto-broker.png?resize=200%2C197&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Mosquitto MQTT broker Raspberry Pi\" class=\"wp-image-73566\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto-broker.png?w=200&amp;quality=100&amp;strip=all&amp;ssl=1 200w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto-broker.png?resize=80%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 80w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><\/figure><\/div>\n\n\n\n<p>To use MQTT, you need a broker. We\u2019ll be using&nbsp;<a href=\"https:\/\/mosquitto.org\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Mosquitto broker<\/a>&nbsp;installed on a Raspberry Pi. Read&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\" target=\"_blank\">How to Install Mosquitto Broker on Raspberry Pi<\/a>.<\/p>\n\n\n\n<p>You can use any other MQTT broker, including a cloud MQTT broker. We&#8217;ll show you how to do that in the code later on.<\/p>\n\n\n\n<p>If you\u2019re not familiar with MQTT make sure you read our introductory tutorial:&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\" target=\"_blank\">What is MQTT and How It Works<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MQTT Libraries<\/h3>\n\n\n\n<p>To use MQTT with the ESP8266 we&#8217;ll use the <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/marvinroger\/async-mqtt-client\" target=\"_blank\">Async MQTT Client Library<\/a>.<\/p>\n\n\n\n<p><strong>Installing the Async MQTT Client Library<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/marvinroger\/async-mqtt-client\/archive\/master.zip\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Click here to download the Async MQTT client library<\/a>. You should have a .zip folder in your Downloads folder<\/li><li>Unzip the .zip folder and you should get&nbsp;<strong>async-mqtt-client-master<\/strong>&nbsp;folder<\/li><li>Rename your folder from&nbsp;<del><strong>async-mqtt-client-master<\/strong><\/del>&nbsp;to&nbsp;<strong>async_mqtt_client<\/strong><\/li><li>Move the&nbsp;<strong>async_mqtt_client<\/strong>&nbsp;folder to your Arduino IDE installation libraries folder<\/li><li>Finally, re-open your Arduino IDE<\/li><\/ol>\n\n\n\n<p>Alternatively, you can go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Add . ZIP<\/strong> library and select the library you&#8217;ve just downloaded.<\/p>\n\n\n\n<p><strong>Installing the ESPAsync TCP Library<\/strong><\/p>\n\n\n\n<p>To use MQTT with the ESP, you also need the&nbsp;<a href=\"https:\/\/github.com\/me-no-dev\/ESPAsyncTCP\" target=\"_blank\" rel=\"noreferrer noopener\">ESPAsync TCP library<\/a>.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/me-no-dev\/ESPAsyncTCP\/archive\/master.zip\" target=\"_blank\">Click here to download the ESPAsync TCP client library<\/a>. You should have a .zip folder in your Downloads folder<\/li><li>Unzip the .zip folder and you should get&nbsp;<strong>ESPAsyncTCP-master<\/strong>&nbsp;folder<\/li><li>Rename your folder from&nbsp;<del><strong>ESPAsyncTCP-master<\/strong><\/del>&nbsp;to&nbsp;<strong>ESPAsyncTCP<\/strong><\/li><li>Move the&nbsp;<strong>ESPAsyncTCP&nbsp;<\/strong>folder to your Arduino IDE installation libraries folder<\/li><li>Finally, re-open your Arduino IDE<\/li><\/ol>\n\n\n\n<p>Alternatively, you can go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Add . ZIP<\/strong> library and select the library you&#8217;ve just downloaded. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DS18B20 Temperature Sensor Libraries<\/h3>\n\n\n\n<p>To interface with the DS18B20 temperature sensor, you need to install the&nbsp;<a href=\"https:\/\/github.com\/PaulStoffregen\/OneWire\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">One Wire library by Paul Stoffregen<\/a>&nbsp;and the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/milesburton\/Arduino-Temperature-Control-Library\" target=\"_blank\">Dallas Temperature library<\/a>. Follow the next steps to install those libraries.<\/p>\n\n\n\n<p>1. Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>2. Type \u201c<strong>onewire<\/strong>\u201d in the search box and install OneWire library by Paul Stoffregen.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install OneWire library by Paul Stoffregen in Arduino IDE\" class=\"wp-image-86581\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure>\n\n\n\n<p>3. Then, search for \u201c<strong>Dallas<\/strong>\u201d and install DallasTemperature library by Miles Burton.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=787%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install DallasTemperature library by Miles Burton in Arduino IDE\" class=\"wp-image-86582\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?w=787&amp;quality=100&amp;strip=all&amp;ssl=1 787w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\">To learn more about the DS18B20 temperature sensor, read our guide: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/\">ESP8266 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>For this tutorial you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266&nbsp;<\/a>(read&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\">Best ESP8266 development boards comparison<\/a>)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\"DS18B20 Temperature Sensor (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\">DS18B20 Temperature Sensor<\/a> &#8211; <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/\">DS18B20 with ESP8266 Guide<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">4.7k Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/raspberry-pi-board\/\" target=\"_blank\">Raspberry Pi board<\/a>&nbsp;(read&nbsp;<a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-starter-kits\/\" target=\"_blank\">Best Raspberry Pi Starter Kits<\/a>)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/microsd-card-raspberry-pi-16gb-class-10\/\" target=\"_blank\">MicroSD Card \u2013 16GB Class10<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-power-supply\/\" target=\"_blank\">Raspberry Pi Power Supply (5V 2.5A)<\/a> <\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a> <\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic Diagram<\/h2>\n\n\n\n<p>Wire the DS18B20 to the ESP8266 as shown in the following schematic diagram with the DS18B20 data pin connected to <span class=\"rnthl rntcblue\">GPIO 4<\/span> (D2).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"763\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_normal_F.png?resize=559%2C763&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU DS18B20 Temperature sensor connected GPIO 4 schematic circuit diagram\" class=\"wp-image-86567\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_normal_F.png?w=559&amp;quality=100&amp;strip=all&amp;ssl=1 559w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_normal_F.png?resize=220%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 220w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Copy the following code to your Arduino IDE. To make it work for you, you need to insert your network credentials as well as the MQTT broker details.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-mqtt-publish-ds18b20-arduino\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files.\n  \n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n*\/\n\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;Ticker.h&gt;\n#include &lt;AsyncMqttClient.h&gt;\n\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n\n\/\/ Raspberri Pi Mosquitto MQTT Broker\n#define MQTT_HOST IPAddress(192, 168, 1, XXX)\n\/\/ For a cloud MQTT broker, type the domain name\n\/\/#define MQTT_HOST &quot;example.com&quot;\n#define MQTT_PORT 1883\n\n\/\/ Temperature MQTT Topics\n#define MQTT_PUB_TEMP &quot;esp\/ds18b20\/temperature&quot;\n\n\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 4;          \n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\/\/ Temperature value\nfloat temp;\n\nAsyncMqttClient mqttClient;\nTicker mqttReconnectTimer;\n\nWiFiEventHandler wifiConnectHandler;\nWiFiEventHandler wifiDisconnectHandler;\nTicker wifiReconnectTimer;\n\nunsigned long previousMillis = 0;   \/\/ Stores last time temperature was published\nconst long interval = 10000;        \/\/ Interval at which to publish sensor readings\n\nvoid connectToWifi() {\n  Serial.println(&quot;Connecting to Wi-Fi...&quot;);\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n}\n\nvoid onWifiConnect(const WiFiEventStationModeGotIP&amp; event) {\n  Serial.println(&quot;Connected to Wi-Fi.&quot;);\n  connectToMqtt();\n}\n\nvoid onWifiDisconnect(const WiFiEventStationModeDisconnected&amp; event) {\n  Serial.println(&quot;Disconnected from Wi-Fi.&quot;);\n  mqttReconnectTimer.detach(); \/\/ ensure we don't reconnect to MQTT while reconnecting to Wi-Fi\n  wifiReconnectTimer.once(2, connectToWifi);\n}\n\nvoid connectToMqtt() {\n  Serial.println(&quot;Connecting to MQTT...&quot;);\n  mqttClient.connect();\n}\n\nvoid onMqttConnect(bool sessionPresent) {\n  Serial.println(&quot;Connected to MQTT.&quot;);\n  Serial.print(&quot;Session present: &quot;);\n  Serial.println(sessionPresent);\n}\n\nvoid onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {\n  Serial.println(&quot;Disconnected from MQTT.&quot;);\n\n  if (WiFi.isConnected()) {\n    mqttReconnectTimer.once(2, connectToMqtt);\n  }\n}\n\n\/*void onMqttSubscribe(uint16_t packetId, uint8_t qos) {\n  Serial.println(&quot;Subscribe acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n  Serial.print(&quot;  qos: &quot;);\n  Serial.println(qos);\n}\n\nvoid onMqttUnsubscribe(uint16_t packetId) {\n  Serial.println(&quot;Unsubscribe acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n}*\/\n\nvoid onMqttPublish(uint16_t packetId) {\n  Serial.print(&quot;Publish acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n}\n\nvoid setup() {\n  sensors.begin();\n  Serial.begin(115200);\n  Serial.println();\n  \n  wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);\n  wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);\n\n  mqttClient.onConnect(onMqttConnect);\n  mqttClient.onDisconnect(onMqttDisconnect);\n  \/\/mqttClient.onSubscribe(onMqttSubscribe);\n  \/\/mqttClient.onUnsubscribe(onMqttUnsubscribe);\n  mqttClient.onPublish(onMqttPublish);\n  mqttClient.setServer(MQTT_HOST, MQTT_PORT);\n  \/\/ If your broker requires authentication (username and password), set them below\n  \/\/mqttClient.setCredentials(&quot;REPlACE_WITH_YOUR_USER&quot;, &quot;REPLACE_WITH_YOUR_PASSWORD&quot;);\n  \n  connectToWifi();\n}\n\nvoid loop() {\n  unsigned long currentMillis = millis();\n  \/\/ Every X number of seconds (interval = 10 seconds) \n  \/\/ it publishes a new MQTT message\n  if (currentMillis - previousMillis &gt;= interval) {\n    \/\/ Save the last time a new reading was published\n    previousMillis = currentMillis;\n    \/\/ New temperature readings\n    sensors.requestTemperatures(); \n    \/\/ Temperature in Celsius degrees\n    temp = sensors.getTempCByIndex(0);\n    \/\/ Temperature in Fahrenheit degrees\n    \/\/temp = sensors.getTempFByIndex(0);\n    \n    \/\/ Publish an MQTT message on topic esp\/ds18b20\/temperature\n    uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());                            \n    Serial.printf(&quot;Publishing on topic %s at QoS 1, packetId: %i &quot;, MQTT_PUB_TEMP, packetIdPub1);\n    Serial.printf(&quot;Message: %.2f \\n&quot;, temp);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_MQTT\/ESP8266_DS18B20.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>The following section imports all the required libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;OneWire.h>\n#include &lt;DallasTemperature.h>\n#include &lt;ESP8266WiFi.h>\n#include &lt;Ticker.h>\n#include &lt;AsyncMqttClient.h><\/code><\/pre>\n\n\n\n<p>Include your network credentials on the following lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define WIFI_SSID \"REPLACE_WITH_YOUR_SSID\"\n#define WIFI_PASSWORD \"REPLACE_WITH_YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Insert the Raspberry Pi IP address, so that the ESP8266 connects to your broker.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define MQTT_HOST IPAddress(192, 168, 1, 106)<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using a cloud MQTT broker, insert the broker domain name, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define MQTT_HOST \"example.com\"<\/code><\/pre>\n\n\n\n<p>Define the MQTT port.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define MQTT_PORT 1883<\/code><\/pre>\n\n\n\n<p>We&#8217;ll publish the temperature on the <strong>esp\/ds18b20\/temperature<\/strong> topic. If you want to change the topic, change it on the following line.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define MQTT_PUB_TEMP \"esp\/ds18b20\/temperature\"<\/code><\/pre>\n\n\n\n<p>You can create more topics if you want.<\/p>\n\n\n\n<p>Setup your DS18B20 on the following lines. In our case, it is connected to <span class=\"rnthl rntcblue\">GPIO 4<\/span>. You can connect it to any other GPIO.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 4;\n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor\nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">temp<\/span> variable will hold the temperature value from the DS18B20 temperature sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float temp;<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">AsyncMqttClient<\/span> object called <span class=\"rnthl rntliteral\">mqttClient<\/span> to handle the MQTT client and timers to reconnect to your MQTT broker and router when it disconnects.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>AsyncMqttClient mqttClient;\nTicker mqttReconnectTimer;\n\nWiFiEventHandler wifiConnectHandler;\nWiFiEventHandler wifiDisconnectHandler;\nTicker wifiReconnectTimer;<\/code><\/pre>\n\n\n\n<p>Then, create some auxiliary timer variables to publish the readings every 10 seconds. You can change the delay time on the <span class=\"rnthl rntliteral\">interval<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>unsigned long previousMillis = 0;  \/\/ Stores last time temperature was published\nconst long interval = 10000;       \/\/ Interval at which to publish sensor readings<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">MQTT functions: connect to Wi-Fi, connect to MQTT, and Wi-Fi events<\/h3>\n\n\n\n<p>We haven\u2019t added any comments to the functions defined in the next code section. Those functions come with the Async Mqtt Client library. The function\u2019s names are pretty self-explanatory.<\/p>\n\n\n\n<p>For example, the <span class=\"rnthl rntliteral\">connectToWifi()<\/span> connects your ESP8266 to your router:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void connectToWifi() {\n  Serial.println(\"Connecting to Wi-Fi...\");\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n}<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">connectToMqtt()<\/span> connects your ESP8266 to your MQTT broker:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void connectToMqtt() {\n  Serial.println(\"Connecting to MQTT...\");\n  mqttClient.connect();\n}<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">onMqttConnect()<\/span> function runs after starting a session with the broker.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void onMqttConnect(bool sessionPresent) {\n  Serial.println(\"Connected to MQTT.\");\n  Serial.print(\"Session present: \");\n  Serial.println(sessionPresent);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">MQTT functions: disconnect and publish<\/h3>\n\n\n\n<p>If the ESP8266 loses connection with the MQTT broker, calls the <span class=\"rnthl rntliteral\">onMqttDisconnect<\/span> function that prints that message in the serial monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {\n  Serial.println(\"Disconnected from MQTT.\");\n\n  if (WiFi.isConnected()) {\n    mqttReconnectTimer.once(2, connectToMqtt);\n  }\n}<\/code><\/pre>\n\n\n\n<p>When you publish a message to an MQTT topic, the <span class=\"rnthl rntliteral\">onMqttPublish()<\/span> function is called. It prints the packet id in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void onMqttPublish(uint16_t packetId) {\n  Serial.print(\"Publish acknowledged.\");\n  Serial.print(\"  packetId: \");\n  Serial.println(packetId);\n}<\/code><\/pre>\n\n\n\n<p>Basically, all these functions that we\u2019ve just mentioned are callback functions. So, they are executed asynchronously.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>Now, let\u2019s proceed to the <span class=\"rnthl rntliteral\">setup()<\/span>. Initialize the DS18B20 sensor and start the serial communication.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.begin();\nSerial.begin(115200);<\/code><\/pre>\n\n\n\n<p>The next two lines create handlers that will allow both the MQTT broker and Wi-Fi connection to reconnect, in case the connection is lost.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);\nwifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);<\/code><\/pre>\n\n\n\n<p>Finally, assign all the callbacks functions. This means that these functions will be executed automatically when needed. For example, when the ESP8266 connects to the broker, it automatically calls the <span class=\"rnthl rntliteral\">onMqttConnect()<\/span> function, and so on.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mqttClient.onConnect(onMqttConnect);\nmqttClient.onDisconnect(onMqttDisconnect);\n\/\/mqttClient.onSubscribe(onMqttSubscribe);\n\/\/mqttClient.onUnsubscribe(onMqttUnsubscribe);\nmqttClient.onPublish(onMqttPublish);\nmqttClient.setServer(MQTT_HOST, MQTT_PORT);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Broker Authentication<\/h4>\n\n\n\n<p>If your broker requires authentication, uncomment the following line and insert your credentials (username and password).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mqttClient.setCredentials(\"REPlACE_WITH_YOUR_USER\", \"REPLACE_WITH_YOUR_PASSWORD\");<\/code><\/pre>\n\n\n\n<p>Finally, connect to Wi-Fi.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>connectToWifi();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">loop()<\/h2>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, you create a timer that will allow you to publish new temperature readings in the <strong>esp\/d18b20\/temperature<\/strong> topic every 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>unsigned long currentMillis = millis();\n\/\/ Every X number of seconds (interval = 10 seconds) \n\/\/ it publishes a new MQTT message\nif (currentMillis - previousMillis >= interval) {\n  \/\/ Save the last time a new reading was published\n  previousMillis = currentMillis;\n  \/\/ New temperature readings\n  sensors.requestTemperatures(); \n  \/\/ Temperature in Celsius degrees\n  temp = sensors.getTempCByIndex(0);<\/code><\/pre>\n\n\n\n<p>If you prefer the temperature in Fahrenheit, uncomment the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/temp = sensors.getTempFByIndex(0);<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about the DS18B20 temperature sensor: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/\">ESP8266 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Publishing to topics<\/h3>\n\n\n\n<p>To publish a message on an MQTT topic, use the next line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());<\/code><\/pre>\n\n\n\n<p>If you would like to publish more readings on different topics, you can duplicate this previous line the <span class=\"rnthl rntliteral\">loop()<\/span>.<\/p>\n\n\n\n<p>Basically, use the <span class=\"rnthl rntliteral\">publish()<\/span> method on the <span class=\"rnthl rntliteral\">mqttClient<\/span> object to publish data on a topic. The <span class=\"rnthl rntliteral\">publish()<\/span> method accepts the following arguments, in order:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>MQTT topic (const char*)<\/li><li>QoS (uint8_t): quality of service &#8211; it can be 0, 1 or 2<\/li><li>retain flag (bool): retain flag<\/li><li>payload (const char*)<\/li><\/ul>\n\n\n\n<p>The QoS (quality of service) is a way to guarantee that the message is delivered. It can be one of the following levels:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>0<\/strong>: the message will be delivered once or not at all. The message is not acknowledged. There is no possibility of duplicated messages;<\/li><li><strong>1<\/strong>: the message will be delivered at least once, but may be delivered more than once;<\/li><li><strong>2<\/strong>: the message is always delivered exactly once;<\/li><li><a href=\"https:\/\/www.ibm.com\/support\/knowledgecenter\/en\/SSFKSJ_8.0.0\/com.ibm.mq.dev.doc\/q029090_.htm\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Learn about MQTT QoS.<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Uploading the code<\/h2>\n\n\n\n<p>With your Raspberry Pi powered on and running the Mosquitto MQTT broker, upload the code to your ESP8266.<\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200 and you&#8217;ll see that the ESP8266 starts publishing messages.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"828\" height=\"445\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/publish-DS18B20-readings-MQTTT-ESP8266-NodeMCU.png?resize=828%2C445&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU Arduino IDE Serial Monitor MQTT Publish sensor readings debugging demonstration\" class=\"wp-image-96334\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/publish-DS18B20-readings-MQTTT-ESP8266-NodeMCU.png?w=828&amp;quality=100&amp;strip=all&amp;ssl=1 828w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/publish-DS18B20-readings-MQTTT-ESP8266-NodeMCU.png?resize=300%2C161&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/publish-DS18B20-readings-MQTTT-ESP8266-NodeMCU.png?resize=768%2C413&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing Node-RED Dashboard<\/h2>\n\n\n\n<p>The ESP8266 is publishing temperature readings every 10 seconds on the <strong>esp\/ds18b20\/temperature<\/strong> topic. Now, you can use any dashboard that supports MQTT or any other device that supports MQTT to subscribe to that topic and receive the readings.<\/p>\n\n\n\n<p>As an example, we&#8217;ll create a simple flow using Node-RED to subscribe to that topic and display the readings on a gauge or chart.<\/p>\n\n\n\n<p>If you don&#8217;t have Node-RED installed, follow the next tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-node-red-on-raspberry-pi\/\">Getting Started with Node-RED on Raspberry Pi<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-node-red-dashboard\/\">Installing and Getting Started with Node-RED Dashboard<\/a><\/li><\/ul>\n\n\n\n<p>Having Node-RED running on your Raspberry Pi, go to your Raspberry Pi IP address followed by :1880.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;raspberry-pi-ip-address:1880<\/code><\/pre>\n\n\n\n<p>The Node-RED interface should open. Drag an MQTT in node, a chart node and a gauge node to the flow.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"254\" height=\"121\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/0-Node-RED-Drag-Nodes-MQTT-In-Chart-Gauge.png?resize=254%2C121&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Node-RED Drag Nodes MQTT In Chart Gauge\" class=\"wp-image-95359\"\/><\/figure><\/div>\n\n\n\n<p>Click the MQTT node and edit its properties as follows:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/MQTT-In-Node-ESP32-ESP8266-Publish-Temperature-Node-RED-Flow.png?resize=471%2C359&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MQTT In node ESP8266 Publish Temperature Node-RED Flow\" class=\"wp-image-95855\" width=\"471\" height=\"359\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/MQTT-In-Node-ESP32-ESP8266-Publish-Temperature-Node-RED-Flow.png?w=628&amp;quality=100&amp;strip=all&amp;ssl=1 628w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/MQTT-In-Node-ESP32-ESP8266-Publish-Temperature-Node-RED-Flow.png?resize=300%2C229&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 471px) 100vw, 471px\" \/><\/figure><\/div>\n\n\n\n<p>The Server field refers to the MQTT broker. In our case, the MQTT broker is the Raspberry Pi, so it is set to localhost:1883. If you&#8217;re using a Cloud MQTT broker, you should change that field. Insert the topic you want to be subscribed to and the QoS.<\/p>\n\n\n\n<p>Set the following properties for the gauge node.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"658\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/2-Gauge-ESP32-Publish-Temperature-Node-RED-Flow.png?resize=548%2C658&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Gauge Node ESP32 Publish Temperature Node-RED Flow\" class=\"wp-image-95355\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/2-Gauge-ESP32-Publish-Temperature-Node-RED-Flow.png?w=548&amp;quality=100&amp;strip=all&amp;ssl=1 548w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/2-Gauge-ESP32-Publish-Temperature-Node-RED-Flow.png?resize=250%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 250w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/figure><\/div>\n\n\n\n<p>Edit the chart node as follows:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"557\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/3-Chart-ESP32-Publish-Temperature-Node-RED-Flow.png?resize=548%2C557&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Chart Node ESP32 Publish Temperature Node-RED Flow\" class=\"wp-image-95356\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/3-Chart-ESP32-Publish-Temperature-Node-RED-Flow.png?w=548&amp;quality=100&amp;strip=all&amp;ssl=1 548w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/3-Chart-ESP32-Publish-Temperature-Node-RED-Flow.png?resize=295%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 295w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/figure><\/div>\n\n\n\n<p>Wire your nodes as shown below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"123\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-MQTT-Publish-DS18B20-Temperature-Node-RED-Flow.png?resize=570%2C123&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU MQTT Publish Temperature Node-RED Flow\" class=\"wp-image-95853\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-MQTT-Publish-DS18B20-Temperature-Node-RED-Flow.png?w=570&amp;quality=100&amp;strip=all&amp;ssl=1 570w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-MQTT-Publish-DS18B20-Temperature-Node-RED-Flow.png?resize=300%2C65&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/figure><\/div>\n\n\n\n<p>Finally, deploy your flow (press the button on the upper right corner).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"138\" height=\"40\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/11_deploy_button.png?resize=138%2C40&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-40013\"\/><\/figure><\/div>\n\n\n\n<p>Alternatively, you can go to <strong>Menu <\/strong>&gt; <strong>Import <\/strong>and copy the following to your <strong>Clipboard<\/strong> to create your Node-RED flow.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">[{&quot;id&quot;:&quot;3eb4b485.bb948c&quot;,&quot;type&quot;:&quot;mqtt in&quot;,&quot;z&quot;:&quot;b01416d3.f69f38&quot;,&quot;name&quot;:&quot;&quot;,&quot;topic&quot;:&quot;esp\/ds18b20\/temperature&quot;,&quot;qos&quot;:&quot;1&quot;,&quot;datatype&quot;:&quot;auto&quot;,&quot;broker&quot;:&quot;8db3fac0.99dd48&quot;,&quot;x&quot;:930,&quot;y&quot;:120,&quot;wires&quot;:[[&quot;706fecd4.6f91a4&quot;,&quot;47ed6377.491d6c&quot;]]},{&quot;id&quot;:&quot;706fecd4.6f91a4&quot;,&quot;type&quot;:&quot;ui_gauge&quot;,&quot;z&quot;:&quot;b01416d3.f69f38&quot;,&quot;name&quot;:&quot;&quot;,&quot;group&quot;:&quot;37de8fe8.46846&quot;,&quot;order&quot;:2,&quot;width&quot;:0,&quot;height&quot;:0,&quot;gtype&quot;:&quot;gage&quot;,&quot;title&quot;:&quot;Temperature&quot;,&quot;label&quot;:&quot;\u00baC&quot;,&quot;format&quot;:&quot;{{value}}&quot;,&quot;min&quot;:0,&quot;max&quot;:&quot;40&quot;,&quot;colors&quot;:[&quot;#00b500&quot;,&quot;#f7df09&quot;,&quot;#ca3838&quot;],&quot;seg1&quot;:&quot;&quot;,&quot;seg2&quot;:&quot;&quot;,&quot;x&quot;:1190,&quot;y&quot;:100,&quot;wires&quot;:[]},{&quot;id&quot;:&quot;47ed6377.491d6c&quot;,&quot;type&quot;:&quot;ui_chart&quot;,&quot;z&quot;:&quot;b01416d3.f69f38&quot;,&quot;name&quot;:&quot;&quot;,&quot;group&quot;:&quot;2b7ac01b.fc984&quot;,&quot;order&quot;:4,&quot;width&quot;:0,&quot;height&quot;:0,&quot;label&quot;:&quot;Temperature&quot;,&quot;chartType&quot;:&quot;line&quot;,&quot;legend&quot;:&quot;false&quot;,&quot;xformat&quot;:&quot;HH:mm:ss&quot;,&quot;interpolate&quot;:&quot;linear&quot;,&quot;nodata&quot;:&quot;&quot;,&quot;dot&quot;:false,&quot;ymin&quot;:&quot;&quot;,&quot;ymax&quot;:&quot;&quot;,&quot;removeOlder&quot;:1,&quot;removeOlderPoints&quot;:&quot;&quot;,&quot;removeOlderUnit&quot;:&quot;3600&quot;,&quot;cutout&quot;:0,&quot;useOneColor&quot;:false,&quot;colors&quot;:[&quot;#1f77b4&quot;,&quot;#aec7e8&quot;,&quot;#ff7f0e&quot;,&quot;#2ca02c&quot;,&quot;#98df8a&quot;,&quot;#d62728&quot;,&quot;#ff9896&quot;,&quot;#9467bd&quot;,&quot;#c5b0d5&quot;],&quot;useOldStyle&quot;:false,&quot;outputs&quot;:1,&quot;x&quot;:1190,&quot;y&quot;:160,&quot;wires&quot;:[[]]},{&quot;id&quot;:&quot;8db3fac0.99dd48&quot;,&quot;type&quot;:&quot;mqtt-broker&quot;,&quot;z&quot;:&quot;&quot;,&quot;name&quot;:&quot;&quot;,&quot;broker&quot;:&quot;localhost&quot;,&quot;port&quot;:&quot;1883&quot;,&quot;clientid&quot;:&quot;&quot;,&quot;usetls&quot;:false,&quot;compatmode&quot;:false,&quot;keepalive&quot;:&quot;60&quot;,&quot;cleansession&quot;:true,&quot;birthTopic&quot;:&quot;&quot;,&quot;birthQos&quot;:&quot;0&quot;,&quot;birthPayload&quot;:&quot;&quot;,&quot;closeTopic&quot;:&quot;&quot;,&quot;closeQos&quot;:&quot;0&quot;,&quot;closePayload&quot;:&quot;&quot;,&quot;willTopic&quot;:&quot;&quot;,&quot;willQos&quot;:&quot;0&quot;,&quot;willPayload&quot;:&quot;&quot;},{&quot;id&quot;:&quot;37de8fe8.46846&quot;,&quot;type&quot;:&quot;ui_group&quot;,&quot;z&quot;:&quot;&quot;,&quot;name&quot;:&quot;DS18B20&quot;,&quot;tab&quot;:&quot;53b8c8f9.cfbe48&quot;,&quot;order&quot;:1,&quot;disp&quot;:true,&quot;width&quot;:&quot;6&quot;,&quot;collapse&quot;:false},{&quot;id&quot;:&quot;2b7ac01b.fc984&quot;,&quot;type&quot;:&quot;ui_group&quot;,&quot;z&quot;:&quot;&quot;,&quot;name&quot;:&quot;SENSORS&quot;,&quot;tab&quot;:&quot;99ab8dc5.f435c&quot;,&quot;disp&quot;:true,&quot;width&quot;:&quot;6&quot;,&quot;collapse&quot;:false},{&quot;id&quot;:&quot;53b8c8f9.cfbe48&quot;,&quot;type&quot;:&quot;ui_tab&quot;,&quot;z&quot;:&quot;&quot;,&quot;name&quot;:&quot;Home&quot;,&quot;icon&quot;:&quot;dashboard&quot;,&quot;order&quot;:2,&quot;disabled&quot;:false,&quot;hidden&quot;:false},{&quot;id&quot;:&quot;99ab8dc5.f435c&quot;,&quot;type&quot;:&quot;ui_tab&quot;,&quot;z&quot;:&quot;&quot;,&quot;name&quot;:&quot;HTTP&quot;,&quot;icon&quot;:&quot;dashboard&quot;,&quot;order&quot;:1,&quot;disabled&quot;:false,&quot;hidden&quot;:false}]\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/MQTT\/esp_ds18b20_mqtt_flow.txt\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Go to your Raspberry Pi IP address followed by <em>:1880\/ui<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;raspberry-pi-ip-address:1880\/ui<\/code><\/pre>\n\n\n\n<p>You should get access to the current sensor readings on the Dashboard (gauge and chart).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"634\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/5-ESP32-MQTT-Publish-Temperature-Node-RED-Dashboard-Gauge-Chart.png?resize=629%2C634&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 MQTT Publish Temperature Node-RED Dashboard Gauge Chart\" class=\"wp-image-95358\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/5-ESP32-MQTT-Publish-Temperature-Node-RED-Dashboard-Gauge-Chart.png?w=629&amp;quality=100&amp;strip=all&amp;ssl=1 629w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/5-ESP32-MQTT-Publish-Temperature-Node-RED-Dashboard-Gauge-Chart.png?resize=298%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 298w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/5-ESP32-MQTT-Publish-Temperature-Node-RED-Dashboard-Gauge-Chart.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w\" sizes=\"(max-width: 629px) 100vw, 629px\" \/><\/figure><\/div>\n\n\n\n<p>That&#8217;s it! You have your ESP8266 board publishing sensor readings to Node-RED via MQTT.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>MQTT is a great communication protocol to exchange small amounts of data between devices. In this tutorial you&#8217;ve learned how to publish DS18B20 temperature readings with the ESP8266 on an MQTT topic. Then, you can use any device or home automation platform to subscribe to that topic and receive the readings.<\/p>\n\n\n\n<p>Instead of a DS18B20, you can use any a different sensor:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-mqtt-publish-bme280-arduino\/\">ESP8266 MQTT \u2013 Publish <strong>BME280<\/strong> Temperature, Humidity and Pressure Readings<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-mqtt-publish-dht11-dht22-arduino\/\">ESP8266 MQTT \u2013 Publish <strong>DHT11\/DHT22<\/strong> Temperature and Humidity Readings<\/a><\/li><\/ul>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. If you want to learn more about the ESP8266, take a look at our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li><li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 NodeMCU Projects&#8230;<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to publish DS18B20 temperature readings via MQTT with the ESP8266 (NodeMCU) to any platform that supports MQTT or any other MQTT client. As an example, we&#8217;ll publish sensor &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU MQTT &#8211; Publish DS18B20 Temperature Readings (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-mqtt-publish-ds18b20-arduino\/#more-96329\" aria-label=\"Read more about ESP8266 NodeMCU MQTT &#8211; Publish DS18B20 Temperature Readings (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":96330,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[265,214,300,240,264],"tags":[],"class_list":["post-96329","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-project","category-esp8266","category-0-esp8266","category-esp8266-projects","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP8266-NodeMCU-DS18B20-Temperature-Sensor-MQTT-Publish-Arduino-IDE-Node-RED.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/96329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=96329"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/96329\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/96330"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=96329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=96329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=96329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}