Introduction: DIY Magnetic Reed Switch Door Monitoring System Using ESP32 and VVAC Cloud Dashboard

About: Supercharged No-Code IIoT Platform Create, connect, and scale IoT applications in days, not months. V-VAC delivers real time monitoring, device management, and data visualization from one browser window. Wheth…

In this simple yet powerful IoT project, you will learn how to use a Magnetic Reed Switch with an ESP32 to build your own Smart Door Monitoring System, complete with real-time status updates on the VVAC IoT Dashboard.

A Reed Switch is a small, low-cost magnetic sensor commonly used for:

  1. Home & office security systems
  2. Door/Window monitoring
  3. Smart cabinets
  4. Attendance/entry logging
  5. Intrusion detection
  6. Industrial automation

This project is beginner-friendly, affordable, and perfect for IoT beginners who want a full security-based cloud project.

Supplies

  1. ESP32 Dev Board
  2. Magnetic Reed Switch
  3. Jumper wires
  4. Breadboard
  5. A Wi-Fi connection
  6. V-VAC Account

Step 1: Wiring the Magnetic Reed Switch

Connect the Esp32 and Sensor according to the connection Diagram.

polarity doesn't matter.

Step 2: Installing Libraries

Open the Arduino IDE, navigate to the Library Manager, and install the required libraries listed below.

  1. WiFi by Arduino // This library is usually built-in, but verify that it is installed.
  2. PubSubClient by Nick O'Leary

Step 3: VVAC Cloud Setup

Steps to Connect V-VAC :

  1. Log in to your V-VAC account. (Create a new account if not signed up already.)
  2. In the Devices section add a new device.
  3. Select common device and enter the details of the device.
  4. Copy the Device Token and paste it in the MQTT section of the code.
  5. 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).

Step 4: Upload the Code

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

#define Sensor_pin 13 // Change according to the pin you want

// 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("");
Serial.println("WiFi 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() {
String Door_status;
bool Switch_Status = digitalRead(13);
if (Switch_Status) {
Door_status = "Open";
} else {
Door_status = "Close";
}

String payload = "{\"Door_status\":\"" + Door_status + "\"}";
return payload;
}

void publishData() {
String payload = sensor_working(); // <-- Get payload from function

String topic_p = "data/v1/" + String(device_token);
if (mqttClient.publish(topic_p.c_str(), payload.c_str())) {
Serial.println("Message published successfully " + payload);
} else {
Serial.println("Failed to publish message");
}
}

/////////////////////////////////////////////////////////////////////

void setup() {
Serial.begin(115200); // <-- Start serial communication

setupWiFi();
mqttClient.setServer(mqtt_broker, mqtt_port);

pinMode(Sensor_pin, INPUT_PULLUP); // <-- Set pin 13 as input
}

void loop() {
if (WiFi.status() != WL_CONNECTED) { // Maintain WiFi connection
setupWiFi();
}

if (!mqttClient.connected()) { // Maintain MQTT connection
connectMQTT();
}
mqttClient.loop();

static unsigned long lastPublishTime = 0; // Publish data every 2 seconds
if (millis() - lastPublishTime >= 500) {
publishData();
lastPublishTime = millis();
}
}

Step 5: View Live Data in V-VAC

  1. In Dashboard section you can configure widgets for your required data.
  2. Click “Create Dashboard”.
  3. Give your dashboard a name (e.g., Door Status Dashboard).
  4. Click the 3rd icon in the dashboard to open the dashboard.
  5. Inside the dashboard editor, click “Edit” and click "Add Widget" to add new widgets.
  6. In the selection menu choose the required widget.
  7. Link widget to a Device telemetry key (the data your device sends).
  8. After customizing your widget, click the Show on Dashboard button to display it on the dashboard.
  9. To save your changes, click the Save button at the top-right corner of the dashboard.
  10. 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.