Vehicle Driver Monitoring and Safety Alert System
by tumipham07 in Circuits > Arduino
43 Views, 1 Favorites, 0 Comments
Vehicle Driver Monitoring and Safety Alert System
This project is a fault-tolerant vehicle safety monitoring system developed using an Arduino Nano 33 IoT, Raspberry Pi, MQTT communication, and Node-RED dashboard monitoring. The system is designed to detect dangerous driving behaviour and nearby obstacles using sensors including an MPU6050 accelerometer/gyroscope and HC-SR04 ultrasonic sensor.
The Arduino continuously monitors vehicle movement data such as tilt, swerving behaviour, and obstacle distance. These sensor values are transmitted in real time to a Raspberry Pi through MQTT communication, where they are visualised on a Node-RED dashboard using charts, gauges, and alerts. The dashboard also supports remote LED (auto mode) warning control, where you can practice by extending to buzzer (auto mode) control.
To improve system reliability, fault tolerance was implemented using local autonomous warning behaviour and offline event buffering. If the network or MQTT broker disconnects, the Arduino continues operating independently and stores warning events locally before automatically retransmitting them when the connection is restored.
Supplies
Hardware Components
- Arduino Nano 33 IoT
- Raspberry Pi
- Monitor
- MPU6050 Accelerometer and Gyroscope Sensor
- HC-SR04 Ultrasonic Distance Sensor
- BH1750 Light Sensor
- LED
- Passive Buzzer
- Breadboard
- Jumper wires
- USB cable
- WiFi connection
Software and Services
- Arduino IDE
- Raspberry Pi OS
- Mosquitto MQTT Broker
- Node-RED
- MQTT protocol
- Gmail SMTP / Node-RED Email node
Libraries Used
- WiFiNINA
- ArduinoMqttClient
- Wire.h
- Adafruit_MPU6050
- Adafruit_Sensor
- BH1750
System Features
- Real-time vehicle movement monitoring
- Obstacle distance detection
- MQTT-based two-way communication
- Node-RED dashboard visualization
- Remote LED and buzzer control
- Email alert notifications
- Fault-tolerant offline event buffering
Understand the System Design
Before building the project, we first plan the overall system architecture. The system uses an Arduino Nano 33 IoT as the on-board sensing unit and a Raspberry Pi as the monitoring server. The Arduino reads data from the MPU6050 motion sensor and HC-SR04 ultrasonic sensor to detect dangerous movement, tilt, and nearby obstacles. It then sends sensor data and alert messages to the Raspberry Pi using MQTT.
The Raspberry Pi runs Mosquitto MQTT Broker and Node-RED. Node-RED displays live charts, manages dashboard controls, and sends email alerts when danger is detected. The dashboard also allows the user to remotely enable or disable the LED and buzzer warnings. This creates two-way communication between the Arduino and Raspberry Pi.
Hardware Connection
The hardware components were connected using a breadboard and jumper wires. The MPU6050 sensor was connected through I2C communication to detect vehicle tilt and acceleration, while the HC-SR04 ultrasonic sensor was used to measure front obstacle distance. A LED and passive buzzer were added as local warning outputs to alert dangerous driving conditions.
The Arduino Nano 33 IoT was used as the main controller because it supports built-in WiFi communication for MQTT networking. The Raspberry Pi was prepared as the central monitoring server to run Mosquitto MQTT Broker and Node-RED dashboard services.
After completing the hardware connections, the sensors and warning devices were tested individually before integrating MQTT communication and dashboard monitoring.
The table below shows the main hardware connections used in the prototype.

Test the Sensors and Local Warning Outputs
Before adding MQTT communication, let's test the hardware locally on the Arduino. This helped confirm that the sensors and warning outputs were working correctly before connecting the system to the Raspberry Pi.
First, let's test the MPU6050 sensor by printing the GyroZ and AccZ values to the Serial Monitor. GyroZ was used to observe sudden swerving movement, while AccZ was used to observe tilt behaviour.
Your Serial Monitor should be like this:

Then, let's test the HC-SR04 ultrasonic sensor by printing the measured distance in centimetres.
The sensor should return different distance values depending on how close the object was placed to the front of the sensor.

After confirming that the sensor readings changed correctly, I added the LED and buzzer warning logic. When dangerous swerving, tilt, or a nearby obstacle was detected, the LED and buzzer were activated locally. This step was important because it proved that the Arduino could respond to danger independently before MQTT communication was added.
Test Local Warning Outputs
After testing the sensors individually, the LED and passive buzzer warning outputs were added to create a local safety alert system. The warning devices were programmed to activate whenever dangerous movement, tilt, or nearby obstacles were detected.
Threshold values were introduced to determine when the warning system should activate. During testing, the LED and buzzer responded immediately when the MPU6050 detected strong swerving or tilt movement, or when the ultrasonic sensor detected an object too close to the front of the system.
This step was important because it allowed the Arduino Nano 33 IoT to continue providing local safety warnings even without network or Raspberry Pi communication.



Set Up Raspberry Pi and Install MQTT Broker
Before connecting the Arduino to MQTT, the Raspberry Pi need to prepared to host the Mosquitto MQTT Broker and Node-RED dashboard services.
First, Raspberry Pi OS was installed and the Raspberry Pi was connected to the same WiFi network as the Arduino Nano 33 IoT. After updating the system packages, Mosquitto MQTT Broker and Mosquitto Clients were installed to support MQTT communication between devices.
Node-RED was also installed to provide dashboard monitoring, real-time charts, email alerts, and remote warning control.
Update Raspberry Pi Packages
Open terminal on Raspberry Pi
First, the Raspberry Pi system packages should be updated.
This command refreshes the Raspberry Pi package list.
This command upgrades installed software packages to the latest version.
Install Mosquitto MQTT Broker
Next, installing Mosquitto MQTT Broker and MQTT client tools.
Explanation:
- mosquitto installs the MQTT broker service.
- mosquitto-clients installs MQTT testing tools such as publish and subscribe commands.
- -y automatically confirms installation.
Enable and Start Mosquitto
The broker service is enabled and started using the following commands.
This command allows Mosquitto to automatically start whenever the Raspberry Pi boots.
This command starts the MQTT broker service immediately.
Check MQTT Broker Status
The following command was used to confirm that the broker was running correctly.
If the broker is active, the terminal should display:
Find Raspberry Pi IP Address
The Raspberry Pi IP address is required later in the Arduino MQTT code.
Example output:

This IP address becomes the MQTT broker address inside the Arduino program.
Install Node-RED
Node-RED is installed using the official installation script.
Explanation:
- Downloads and installs Node.js and Node-RED.
- Automatically configures required dependencies.
Enable Node-RED Service
This command allows Node-RED to automatically start when the Raspberry Pi boots.
Start Node-RED
This command starts the Node-RED server.
Open Node-RED Editor
After Node-RED starts, open a browser and enter:
Example:
This opens the Node-RED editor interface where the dashboard and MQTT flows are created.
Install Node-RED Dashboard
The Node-RED dashboard package is installed to create gauges, charts, switches, and dashboard widgets.
In the Raspberry Pi terminal, run:
Then install the dashboard package:
Restart Node-RED:
After this, dashboard nodes such as gauges, charts, switches, and text displays become available in Node-RED.
Open the Node-RED Dashboard Interface
To access the live monitoring dashboard, add /ui to the Node-RED address:
Example:
The dashboard interface displays (after the Node-RED dashboard flow has been fully configured and deployed in the following steps):
- Real-time GyroZ chart
- AccZ monitoring chart
- Obstacle distance chart
- LED warning control switch
- Buzzer warning control switch
- Email alert enable/disable switch
This dashboard allows users to monitor sensor values and remotely control warning systems in real time through MQTT communication.
Configure MQTT Authentication
To make the MQTT broker more secure, a username and password can be created for Mosquitto.
Replace your_username with the username you want to use. After running this command, the terminal will ask you to enter and confirm a password.
After creating the password file, restart Mosquitto:
Test MQTT Publish and Subscribe
Before connecting the Arduino, MQTT communication can be tested directly on the Raspberry Pi.
Open one terminal window and run:
This command subscribes to all MQTT topics that start with car/ and waits for incoming messages.
Open a second terminal window and run:
This command publishes a test message to the car/test topic.
If MQTT is working correctly, the first terminal should display:
Configure MQTT Communication
After the Raspberry Pi and Mosquitto MQTT Broker were prepared, the Arduino Nano 33 IoT need vto be updated to connect to the broker through WiFi. In this step, MQTT isused to send live sensor data from the Arduino to the Raspberry Pi and to send control commands from Node-RED back to the Arduino.
This step creates two-way communication between the two devices. The Arduino publishes GyroZ, AccZ, distance, and alert messages, while also subscribing to LED and buzzer control topics.

To update the previous code, we add the WiFiNINA and ArduinoMqttClient libraries, configure the Raspberry Pi broker IP address, created MQTT topics, and added functions to connect to WiFi, connect to MQTT, and publish sensor values.
**(Before uploading the code, update the WiFi name, WiFi password, Raspberry Pi IP address, MQTT username, and MQTT password to match your own setup.)**
After uploading this version, open the Serial Monitor. You should see:

The Arduino now connects to the Raspberry Pi Mosquitto MQTT Broker and publishes live sensor values to different MQTT topics. It also publishes danger alerts when swerving, tilt, or nearby obstacle conditions are detected.
Test Live MQTT Data Communication
Before building the Node-RED dashboard, MQTT communication between the Arduino Nano 33 IoT and Raspberry Pi should be tested to confirm that sensor data could be published and received correctly in real time.
In this step, MQTTX is used as a testing client to monitor live MQTT topics from the Arduino. The Arduino continuously published GyroZ, AccZ, distance, and alert messages to the Mosquitto MQTT Broker running on the Raspberry Pi.
This step confirmed that:
- the Arduino successfully connected to WiFi
- the Arduino successfully connected to the MQTT broker
- sensor values were being published correctly
- the Raspberry Pi MQTT broker was receiving live data
Install MQTTX
Open a browser and download MQTTX from:
Or directly download from GitHub:
Install MQTTX on the Raspberry Pi or another computer connected to the same WiFi network.
Open MQTTX
Method 1: Open from Raspberry Pi desktop
Method 2: Open from terminal
If using AppImage:
Create MQTT Connection
Host: YOUR_RPI_IP
Port: 1883
Username: your_username
Password: your_password
Example:
Host: 192.168.20.211
Port: 1883
Subscribe to all project topics:
This allows MQTTX to display all messages under the car/ topic group.
Verify Live Messages
When the Arduino is running, MQTTX should show live messages such as:
car/gyroZ
car/accZ
car/distance
car/alert
If these values appear and update in real time, the Arduino and Raspberry Pi MQTT communication is working correctly.

Build the Node-RED Dashboard
After confirming that MQTT communication was working correctly, a Node-RED dashboard was created to visualize live sensor data and remotely control the warning systems.
The dashboard uses:
- MQTT input nodes to receive live sensor values
- Dashboard chart nodes to display data
- Dashboard switch nodes for remote control
- MQTT output nodes to send commands back to the Arduino
- Email nodes for danger notifications
Open Node-RED Editor
Open a browser and enter:
Example:
Add MQTT Input Nodes
On the left Node-RED panel, drag four MQTT input nodes into the workspace.
ouble-click the first MQTT node.
Configure:
Server: localhost:1883
Topic: car/gyroZ
Output: a parsed string
Name: GyroZ Data

Click:
Done
Repeat for:
car/accZ
car/distance
car/alert
These nodes subscribe to live MQTT data published by the Arduino Nano 33 IoT.
Configure Dashboard Charts
From the dashboard section, drag three chart nodes into the workspace.
Connect:
car/gyroZ → chart
car/accZ → chart
car/distance → chart
Configure chart names:
Vehicle Swerving Level Over Time
Vehicle Tilt Variation Over Time
Front Distance Over Time
Configure:
X-axis: time
Y-axis: automatic
Click:
Done
Configure LED Control Switch
From the dashboard section on the left panel, drag a ui_switch node into the workspace.
Double-click the switch node and configure:
Group: Vehicle Safety Dashboard
Label: LED AUTO MODE
On Payload: (string) LED_ON
Off Payload: (string) LED_OFF
Topic: car/control/led
Click:
Done
Next, drag an mqtt out node into the workspace.
Connect:
LED AUTO MODE switch → mqtt out
Double-click the MQTT output node and configure:
Server: localhost:1883
Topic: leave blank
QoS: 0
Retain: false
Click:
Done
When the dashboard switch is turned ON or OFF, MQTT commands are sent to the Arduino Nano 33 IoT to enable or disable the LED warning system.
Configure Buzzer Control Switch
Drag another ui_switch node into the workspace.
Double-click the node and configure:
Group: Vehicle Safety Dashboard
Label: BUZZER AUTO MODE
On Payload:(string) BUZZER_ON
Off Payload: (string)BUZZER_OFF
Topic: car/control/buzzer
Click:
Done
Drag another mqtt out node into the workspace.
Connect:
BUZZER AUTO MODE switch → mqtt out
Configure the MQTT output node:
Server: localhost:1883
Topic: leave blank
QoS: 0
Retain: false
When the switch is changed on the dashboard, MQTT commands are sent back to the Arduino to enable or disable the buzzer warning behaviour.
Configure Email Alert Flow
The email alert system sends warning emails whenever danger is detected.
First, drag an mqtt in node into the workspace.
Configure:
Server: localhost:1883
Topic: car/alert
Output: a parsed string
Name: Danger Alerts
Click:
Done
Next, drag a delay node into the workspace.
Connect:
Danger Alerts → delay
Configure the delay node:
Rate limit: 1 message every 10 seconds
Drop intermediate messages: enabled
This helps prevent excessive repeated email notifications.
Configure Email Alert Switch
Drag a ui_switch node into the workspace.
Configure:
Label: EMAIL ALERT
On Payload: true
Off Payload: false
Next, drag a change node into the workspace.
Connect:
EMAIL ALERT switch → change node
Configure the change node:
Set flow.emailEnabled to msg.payload
This stores the dashboard switch state inside Node-RED.
Add Email Filter
Drag a switch node into the workspace.
Connect:
delay → switch
Configure:
Property: flow.emailEnabled
Rule: is true
This allows emails to be sent only when the EMAIL ALERT switch is enabled.
Configure Email Node
Drag an email node into the workspace.
Connect:
switch → email
Configure:
Server: smtp.gmail.com
Port: 465
Userid: your_email@gmail.com
Password: your_app_password
Secure connection: enabled
To: your_email@gmail.com
Subject: Vehicle Safety Alert
Click:
Done
When danger is detected and the EMAIL ALERT switch is enabled, Node-RED sends a warning email notification.
Deploy Dashboard
After all flows are configured, click:
Deploy
in the top-right corner of Node-RED.
If the deployment is successful, Node-RED will display:
Successfully deployed

Open Dashboard UI
Open a web browser and enter:
Example:
The dashboard should now display:
- live sensor charts
- warning alerts
- LED control switch
- buzzer control switch
- email alert switch
The dashboard can now monitor the vehicle safety system and remotely control warning behaviours through MQTT communication.

Test the Complete System
After building the dashboard and configuring MQTT communication, the full system is tested to verify that all components were working together correctly. This step checks real-time monitoring, warning detection, remote control, and email alert functionality.
Test Real-Time Dashboard Monitoring
Move or tilt the MPU6050 sensor and place objects in front of the ultrasonic sensor.
Verify that:
- GyroZ chart updates
- AccZ chart updates
- Distance chart updates
- warning alerts appear
Expected result:
Live values should continuously change and be recorded on the dashboard.
Test LED Warning Control
Turn the dashboard LED switch OFF.
Verify:
- Arduino receives:
- LED no longer activates during danger detection
Turn the switch ON again.
Verify:
- Arduino receives:
- LED warning behaviour returns
Test Buzzer Warning Control
Turn the dashboard buzzer switch OFF.
Verify:
- Arduino receives:
- buzzer no longer activates during danger detection
Turn the switch ON again.
Verify:
- Arduino receives:
- buzzer warning behaviour returns
Test Email Alert System
Enable the EMAIL ALERT switch.
Trigger dangerous movement or obstacle detection.
Verify:
- Node-RED receives:
- warning email is sent successfully
Disable the EMAIL ALERT switch.
Trigger danger again.
Verify:
- dashboard still displays alerts
- no email is sent
Test MQTT Communication
Open MQTTX and subscribe to:
Verify that live messages appear:
This confirms successful two-way MQTT communication between the Arduino and Raspberry Pi.


Implement Fault-Tolerant Behaviour
In this step, the Arduino code was updated to make the system more reliable when MQTT or WiFi communication fails. Instead of completely stopping when the Raspberry Pi or MQTT broker is unavailable, the Arduino continues reading sensor values, detecting danger, and activating the local LED and buzzer warnings.
The main improvement in this step is offline event buffering. When danger is detected but MQTT is disconnected, the Arduino temporarily stores the warning message in a local array. After the MQTT connection is restored, the stored warning events are automatically published back to the Raspberry Pi.
What Was Added
- Automatic WiFi reconnection
- Automatic MQTT reconnection
- Offline warning event buffer
- Store-and-forward behaviour
- Local LED and buzzer warning during communication failure
- Resending saved danger events after reconnection
Update the previous Arduino MQTT code with the following version:
To test this:
Stop Mosquitto on Raspberry Pi
Trigger danger
Tilt the MPU6050 or put an object close to ultrasonic.
Expected:
- LED/buzzer still work
- Serial Monitor shows:
Start Mosquitto again
Expected Serial Monitor:

This article is part of an assignment submitted to Deakin University, School of IT, Unit SIT210/730 - Embedded Systems Development.