Welcome to HealthGuard, a powerful and secure IoT data monitoring and notification system. This project is designed to seamlessly retrieve sensor data connected to a Raspberry Pi using the MQTT protocol, ensuring efficient communication and real-time updates.
-
MQTT Protocol: Utilizing the MQTT protocol, our system establishes a reliable and lightweight communication channel between the Raspberry Pi and connected devices. This enables the seamless transmission of sensor data.
-
OpenSSL Security: We prioritize the security of your data. OpenSSL is integrated into our system to ensure end-to-end encryption, safeguarding the confidentiality and integrity of the transmitted information.
-
Node.js Backend: The project incorporates a robust Node.js backend, acting as the central hub for data processing and management. It efficiently handles incoming sensor data, performs necessary computations, and securely stores relevant information in a MySQL database.
-
MySQL Database: All notifications and critical information are stored in a MySQL database, providing a structured and organized repository for historical and real-time data. This allows for easy retrieval and analysis of sensor readings and system events.
-
React Native Front End: HealthGuard comes with a feature-rich React Native front-end application. Leverage the power of React Native to create a user-friendly and responsive interface for monitoring and interacting with your IoT data. The React Native app ensures a seamless experience for users on various devices, offering real-time updates and a smooth interface.
-
Raspberry Pi Integration: On the Raspberry Pi side, we use Python scripts to retrieve and publish data to and from the server. This integration enables HealthGuard to tap into the potential of Raspberry Pi's sensors and bring real-world data into the system. The Python code facilitates a robust connection between the physical sensors and the centralized server, ensuring a reliable flow of information.
POST /auth/register
{
message: `user added with id: 10`,
status: 200
}
POST /auth/login
On successful login
{
user: user,
token: token,
status: 200
}
On unsuccessful login
{
message: "Invalid email or password",
status: 400,
}
GET /notification
[
{
"sensor_id": 413,
"userId": 5,
"location": "living_room",
"type": "temperature",
"sensor_reading": "28",
"date": "2023-12-03T10:11:36.000Z",
"warning": "Too cold 10 degree celcious!"
},
{
"sensor_id": 414,
"userId": 5,
"location": "living_room",
"type": "temperature",
"sensor_reading": "28",
"date": "2023-12-03T10:12:31.000Z",
"warning": "Too hot 32 degree celcious!"
}
]
GET /notification
If no notification found
{
message: `No notification found`,
status: 409
}
POST /notification
{
"message": "Notification with id 1 added.",
"status": 200
}
DELETE /notification/:id
If notification not found
{
"message": "Notification with ID 1 not found",
"status": 404
}
On successful notification deletion
{
"message": "Notification with ID 413 deleted successfully",
"status": 200
}
On successful deletion of all notfication
{
"message": "10 notifications deleted successfully",
"status": 200
}
When deleting all notification, if notification is empty
{
"message": "No notifications found to delete",
"status": 404
}
MQTT response example to application via server
{
"temperature": 22.0,
"humidity": 20.0,
"sensor": "temperature",
"location": "living_room"
}
App response example to MQTT via server
{
"temperature": "23",
"location": "living_room",
"action":"decrease"
}
Before you start working with the React Native application, Node.js backend, and Raspberry Pi, ensure that your development environments are set up correctly. Follow these steps to install the necessary tools:
React Native requires Node.js and npm (Node Package Manager). If you haven't installed them yet, download and install from nodejs.org.
Install the React Native Command Line Interface (CLI) globally using npm command in terminal:
npm install -g react-native-cli
Install Expo Go from Play Store or App Store.
DHT11 Temperature and Humidity sensor, breadboard, wires, OLED display & camera module (webcam).
Python should be installed by default. Ensure Python is installed on your Raspberry Pi. You can check the version using this command in terminal :
python --version
For Openssl and MQTT installation execute these command in Raspberry Pi terminal.
sudo apt-get install openssl
sudo apt-get install mosquitto
Start MQTT:
sudo service mosquitto start
Enable MQTT on startup:
sudo service mosquitto enable
Check MQTT on status:
sudo service mosquitto status
- @react-native-async-storage/async-storage: Asynchronous storage for React Native applications.
- @react-navigation/native: Navigation library for React Native applications.
- @react-navigation/stack: Stack navigator for React Navigation.
- expo: Framework and tools for building React Native applications.
- expo-status-bar: Expo component for managing the status bar.
- react: React library.
- react-hook-form: Library for flexible and efficient form validation in React.
- react-native: React Native framework.
- react-native-circular-progress: Circular progress component for React Native.
- react-native-radio-buttons-group: Radio buttons group component for React Native.
- react-native-responsive-screen: Library for creating responsive designs in React Native.
- react-native-svg: Library for using SVG in React Native.
- react-native-toast-message: Toast messages for React Native applications.
- react-native-video: Video component for React Native.
- socket.io-client: Client library for Socket.IO.
- @babel/core: Core of the Babel compiler.
- bcrypt: Library for hashing passwords.
- cors: Middleware for enabling Cross-Origin Resource Sharing in Express.
- dotenv: Module for loading environment variables from a .env file.
- express: Web framework for Node.js.
- express-validator: Middleware for request validation in Express.
- jsonwebtoken: Library for generating JSON Web Tokens (JWT).
- mqtt: MQTT library for Node.js.
- mysql2: MySQL library for Node.js.
- passport: Authentication middleware for Node.js.
- passport-jwt: Passport strategy for handling JWT-based authentication.
- passport-local: Passport strategy for handling local (username and password) authentication.
- sharp: High-performance image processing library for Node.js.
- socket.io: Library for real-time web applications with bidirectional communication.
- ws: WebSocket library for Node.js.
The Python code running on your Raspberry Pi utilizes the following dependencies:
- Adafruit-DHT: Python library for DHT temperature and humidity sensors.
- paho-mqtt: MQTT library for Python.
- adafruit-blinka: CircuitPython (Blinka) compatibility library for single-board computers.
- Pillow: Python Imaging Library (PIL) fork for working with images.
- adafruit-circuitpython-ssd1306: CircuitPython library for SSD1306-based OLED displays.
git clone [email protected]:Nischhal3/Health-Guard.git
cd client
npm install
cd ../server
npm install
Copy folder named ras-pi-4 and into you Rasberry Pi environment. Generate openssl certificates ca.crt server.crt & server.key. You can follow commands from OpenSSL guide. Those certificates should be generated inside ras-pi-4 or you manually need to change path for those certificates in mqtt.py.
Provide these permission -rw-r--r-- for current user to these files ca.crt server.crt & server.key. Permission example for user iot
- -rw-r--r-- 1 iot iot 1704 Dec 2 15:29 server.key
- -rw-r--r-- 1 iot iot 1350 Dec 2 15:29 server.crt
- -rw-r--r-- 1 iot iot 1342 Dec 2 15:29 ca.crt
Copy ca.crt server.crt & server.key in location /etc/mosquitto/certs and provide these permissions for mosquitto
- -rw-r--r-- 1 mosquitto mosquitto 1342 Dec 2 15:29 ca.crt
- -rw-r--r-- 1 mosquitto mosquitto 1350 Dec 2 15:29 server.crt
- -rw-r----- 1 mosquitto mosquitto 1704 Dec 2 15:29 server.key
Make a directory called certs under folder server and copy these files ca.crt server.crt & server.key inside that folder.
To setup MQTT username & password execute following command:
sudo mosquitto_passwd -c /etc/mosquitto/passwd
And add username follwed by password. This will set MQTT for username and password authetication.
Make a file name config.json inside folder ras-pi-4 and add following things:
{
"mqttBrokerAddress": "your_rasberry_pi_ip_address",
"mqttUsername": "your_username",
"mqttPassword": "your_password",
"mqttPort": 8883
}
To enable MQTT for login & openssl authentication execute following command:
sudo nano /etc/mosquitto/mosquitto.conf
Add following content in that file
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 8883
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
require_certificate true
use_identity_as_username true
allow_anonymous false
password_file /etc/mosquitto/passwd
To test mqtt as subscriber execute this command:
sudo /usr/bin/mosquitto_sub -p 8883 --cafile /etc/mosquitto/certs/ca.crt --cert /etc/mosquitto/certs/server.crt --key /etc/mosquitto/certs/server.key -h mqtt_ip_address -t world -u username -P password
To test mqtt as publisher execute this command:
sudo /usr/bin/mosquitto_pub -p 8883 --cafile /etc/mosquitto/certs/ca.crt --cert /etc/mosquitto/certs/server.crt --key /etc/mosquitto/certs/server.key -h mqtt_ip_address -m hello -t world -u username -P password
Replace mqtt_ip_address, username and password with your configured ip address, username and password. If everything configured correctly then you will see 'hello' message poped up in subscriber terminal.
If it's not working as intended then check MQTT log with command:
sudo cat /var/log/mosquitto/mosquitto.log
To set up the Python virtual environment on your Raspberry Pi, execute these commands:
- Create a virtual environment:
python3 -m venv path/to/venv
- Activate the virtual environment:
source path/to/venv/bin/activate
- Install the required Python packages:
pip install Adafruit-DHT
pip install paho-mqtt
pip install adafruit-blinka
pip install Pillow
pip install adafruit-circuitpython-ssd1306
Create MySql database in any server of your choice and Execute following queries in your database
CREATE TABLE user (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE notification (
sensor_id INT AUTO_INCREMENT PRIMARY KEY,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
location VARCHAR(255),
sensor_reading VARCHAR(255),
type VARCHAR(255),
warning TEXT,
userId INT,
FOREIGN KEY (userId) REFERENCES user(id) ON DELETE CASCADE,
);
Create file .env inside server folder and add following content to it:
DB_HOST='your_db_host'
DB_USER='your_db_user'
DB_PASS='your_db_password'
DB_NAME='your_db_name'
JWT_SECRET='your_randomly_generated_chars'
mqttUsername='mqtt_user'
mqttPassword = 'mqtt_password'
mqttBrokerAddress = 'your_rasberry_pi_ip_address'
mqttBrokerPort = 8883
protocol = 'mqtts'
In file client\utils\Variables.js edit this url :export const baseUrl = "http://your_pc_ip_address:3000";
After everything is configured. Go to folder HealthGuard [Main repositiry].Open two teminals execute following commands:
cd client
npm start
cd server
npm start