Using Node-RED to Display Data from ESP32 and DS18B20 Sensor

  ·   3 min read

Node-RED is an open-source flow-based development tool for visual programming, originally developed by IBM for wiring together hardware devices, APIs, and online services as part of the Internet of Things. In this article, we will explore how to use Node-RED to display temperature data from an ESP32 microcontroller connected to a DS18B20 temperature sensor.

Prerequisites

Before we start, ensure you have the following:

  1. ESP32 Development Board: A microcontroller with Wi-Fi and Bluetooth capabilities.
  2. DS18B20 Temperature Sensor: A digital temperature sensor with a 1-Wire interface.
  3. Node-RED Installed: You can install Node-RED on your local machine or a server. Follow the official installation guide if you haven’t installed it yet.
  4. MQTT Broker: We will use MQTT to send data from the ESP32 to Node-RED. You can use a local broker like Mosquitto or a cloud-based service like HiveMQ.

Setting Up the ESP32 and DS18B20

Wiring

Connect the DS18B20 sensor to the ESP32 as follows:

  • VCC to 3.3V on the ESP32
  • GND to GND on the ESP32
  • Data to a digital pin on the ESP32 (e.g., GPIO 4)

Don’t forget to add a 4.7kΩ pull-up resistor between the data line and VCC.

Programming the ESP32

Use the Arduino IDE or PlatformIO to program the ESP32. Below is a simple sketch to read temperature data from the DS18B20 and publish it to an MQTT topic.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <PubSubClient.h>

// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// MQTT Broker
const char* mqtt_server = "broker.hivemq.com";

WiFiClient espClient;
PubSubClient client(espClient);

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  sensors.begin();
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  sensors.requestTemperatures();
  float temperatureC = sensors.getTempCByIndex(0);

  Serial.print("Temperature: ");
  Serial.println(temperatureC);

  char tempString[8];
  dtostrf(temperatureC, 1, 2, tempString);
  client.publish("esp32/temperature", tempString);

  delay(5000);
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("ESP32Client")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

Configuring Node-RED

  1. Install Node-RED Dashboard: To visualize the data, install the Node-RED Dashboard nodes. You can do this by navigating to the Node-RED interface, clicking on the menu, and selecting “Manage palette” -> “Install” -> search for node-red-dashboard and install it.

  2. Create a Flow:

    • Drag an MQTT input node to the workspace and configure it to connect to your MQTT broker and subscribe to the esp32/temperature topic.
    • Connect the MQTT node to a debug node to verify that data is being received.
    • Add a chart node from the dashboard section and connect it to the MQTT node to visualize the temperature data.
  3. Deploy and View: Deploy your flow and navigate to the Node-RED Dashboard to view the temperature data in real-time.

Conclusion

By following these steps, you can successfully use Node-RED to display temperature data from an ESP32 and DS18B20 sensor. This setup can be expanded to include more sensors and actuators, making it a powerful tool for home automation and IoT projects.

References