ESP32 + DS18B20 Temperature Monitor for V-VAC IoT Dashboard (MQTT) by Vvaciot
by vvaciot in Circuits > Electronics
41 Views, 0 Favorites, 0 Comments
ESP32 + DS18B20 Temperature Monitor for V-VAC IoT Dashboard (MQTT) by Vvaciot
If you’re searching for a simple, accurate, and waterproof temperature monitoring project, the DS18B20 + ESP32 combination is one of the best choices.
This sensor delivers high precision, works on a single-wire interface, and supports long cable installations, making it ideal for:
- Home temperature monitoring
- Aquariums
- Freezers
- Industrial temperature logging
- Outdoor IoT nodes
In this DIY guide, you’ll learn how to wire and program the DS18B20 with an ESP32 using Arduino IDE.
Supplies
- ESP32 Dev Board
- DS18B20 sensor (normal or waterproof probe)
- 4.7kΩ resistor (mandatory pull-up resistor)
- Jumper wires
- Breadboard
- A Wi-Fi connection
- Your V-VAC MQTT topic (given by V-VAC)
Wiring the DS18B20 to ESP32 (Easy Pin Connections)
Connect the Esp32 and Sensor according to the connection Diagram.
Install Required Arduino Libraries
- WiFi by Arduino
- PubSubClient by Nick O'Leary
- OneWire by Paul Stoffregen
- DallasTemperature by Milies Burton
VVAC Cloud Setup
Steps to Connect V-VAC :
- Log in to your V-VAC account. (Create a new account if not signed up already.)
- In the Devices section add a new device.
- Select common device and enter the details of the device.
- Copy the Device Token and paste it in the MQTT section of the code.
- Copy the username and password from the setup in devices section and paste it in the MQTT section of the code.
For further details check out (How to connect to V-VAC).
ESP32 + DS18B20 Code
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define DS18B20_PIN 4 // GPIO pin connected to DS18B20
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
// WiFi Configuration
const char* ssid = "Your_Wifi_SSID"; // eg. ssid = "ssid"
const char* password = "Your_Wifi_Password"; // eg. password = "password"
// MQTT Configuration
const char* mqtt_broker = "data.volkkommen.com";
const int mqtt_port = 1883;
const char* mqtt_username = "Your_MQTT_Username"; // eg. mqtt_username = "mqtt_username"
const char* mqtt_password = "Your_MQTT_Password"; // eg. mqtt_password = "mqtt_password"
const char* device_token = "Your_Device_Token"; // eg. device_token = "6810eb7g27560f46"
WiFiClient espClient;
PubSubClient mqttClient(espClient);
void setupWiFi() {
Serial.println();
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
}
void connectMQTT() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection... ");
if (mqttClient.connect(device_token, mqtt_username, mqtt_password)) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" trying again in 5 seconds");
delay(5000);
}
}
}
String sensor_working() {
sensors.requestTemperatures(); // Read sensor data
float tempC = sensors.getTempCByIndex(0); // Celsius value from DS18B20
if (tempC == DEVICE_DISCONNECTED_C) {
Serial.println("Failed to read from DS18B20 sensor!");
return "{\"temperature\":null}";
}
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" °C");
// Create JSON payload
String payload = "{\"temperature\":" + String(tempC, 2) + "}";
return payload;
}
void publishData() {
String payload = sensor_working();
String topic_p = "data/v1/" + String(device_token);
if (mqttClient.publish(topic_p.c_str(), payload.c_str())) {
Serial.println("Message published: " + payload);
} else {
Serial.println("Failed to publish message");
}
}
void setup() {
Serial.begin(115200);
sensors.begin(); // Start DS18B20
setupWiFi();
mqttClient.setServer(mqtt_broker, mqtt_port);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
setupWiFi();
}
if (!mqttClient.connected()) {
connectMQTT();
}
mqttClient.loop();
static unsigned long lastPublishTime = 0;
if (millis() - lastPublishTime >= 2000) { // Publish every 2 sec
publishData();
lastPublishTime = millis();
}
}
Real-Time Temperature Monitoring on VVAC
- In Dashboard section you can configure widgets for your required data.
- Click “Create Dashboard”.
- Give your dashboard a name (e.g., Door Status Dashboard).
- Click the 3rd icon in the dashboard to open the dashboard.
- Inside the dashboard editor, click “Edit” and click "Add Widget" to add new widgets.
- In the selection menu choose the required widget.
- Link widget to a Device telemetry key (the data your device sends).
- After customizing your widget, click the Show on Dashboard button to display it on the dashboard.
- To save your changes, click the Save button at the top-right corner of the dashboard.
- You can now remotely see the Door status from anywhere around the world.
For more IoT projects and tutorials, visit the official V-VAC Platform - V-VAC.