A secure IoT medical monitoring and access control system using two ESP32 microcontrollers with ESP-IDF framework. Real-time vital monitoring with fall detection, posture recognition, and RFID-based access control.
┌──────────────────────────────────────────────────────────┐
│ HiveMQ Cloud MQTT Broker (TLS) │
│ mqtts://*.eu.hivemq.cloud:8883 │
└────────────────────────▲─────────────────────────────────┘
│ MQTT over TLS
│
┌───────────────┴──────────────┐
│ │
┌────▼──────────────┐ ┌───────▼─────────┐
│ Node A │ │ Node B │
│ (Vital Monitor) │ │ (Sentry) │
│ │ │ │
│ - MPU6050 (Fall) │ │ - RC522 RFID │
│ - DHT11 (Temp) │ │ - IR Motion │
│ - HW-827 (Heart) │ │ - Buzzer Alarm │
│ - Button (SOS) │ │ │
└──────────┬────────┘ └────────┬────────┘
│ ESP-NOW P2P │
└───────────────────────┘
(Emergency alerts)
- Microcontroller: ESP32 DevKit V1
- Sensors:
- MPU6050 (I2C: GPIO 21/22) - 6-axis IMU for fall detection + posture
- DHT11 (GPIO 5) - Temperature & humidity (1-wire protocol)
- HW-827 (GPIO 36, ADC) - Heart rate sensor
- Emergency Button (GPIO 4, active LOW) - Manual SOS trigger
- Microcontroller: ESP32 DevKit V1
- Sensors:
- RC522 RFID Reader (SPI: GPIO 23/25/19/22) - Access control
- IR Motion Sensor (GPIO 13) - Intrusion detection
- Buzzer (GPIO 5, NPN driver) - Alarm output
- 3.3V power via AMS1117 regulator
- USB-C or Li-ion battery (3.7-4.2V)
- Breadboards and jumper wires
- Git and ESP-IDF 5.1+
- ESP-IDF v5.1+ - Installation Guide
- Git - For version control
- HiveMQ Cloud Account - Free tier MQTT broker
mkdir -p ~/esp
cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf.git
cd esp-idf
./install.sh esp32
source export.shcd ~
git clone https://github.com/vruga/esp_medmonitor.git
cd esp_medmonitor
git checkout shashvattcp node-a-gateway/main/config.h.template node-a-gateway/main/config.h
cp node-b-sentry/main/config.h.template node-b-sentry/main/config.h
# Edit with your WiFi and HiveMQ credentials
nano node-a-gateway/main/config.h
nano node-b-sentry/main/config.hcd node-a-gateway
idf.py build
idf.py -p /dev/cu.usbserial-XXXXX flash monitorcd ../node-b-sentry
idf.py build
idf.py -p /dev/cu.usbserial-YYYYY flash monitor| Component | GPIO | Protocol | Purpose |
|---|---|---|---|
| MPU6050 SDA | 21 | I2C | Accelerometer/Gyroscope data |
| MPU6050 SCL | 22 | I2C | Accelerometer/Gyroscope clock |
| DHT11 Data | 5 | 1-Wire | Temperature & humidity |
| HW-827 ADC | 36 | ADC1_CH0 | Heart rate sensor |
| Emergency Button | 4 | GPIO Input | SOS button (active LOW) |
| UART TX | 1 | UART | Serial output (115200 baud) |
| UART RX | 3 | UART | Serial input |
| Component | GPIO | Protocol | Purpose |
|---|---|---|---|
| RC522 MOSI | 23 | SPI | RFID data in |
| RC522 MISO | 25 | SPI | RFID data out |
| RC522 SCLK | 19 | SPI | RFID clock |
| RC522 CS | 22 | SPI | RFID chip select |
| IR Sensor | 13 | GPIO Input | Motion detection |
| Buzzer | 5 | GPIO Output | Alarm (NPN driver) |
| UART TX | 1 | UART | Serial output |
| UART RX | 3 | UART | Serial input |
- ✅ Fall Detection: Jerk-based algorithm (200 g/s threshold) with 2s cooldown
- ✅ Posture Recognition: Complementary filter (99% gyro + 1% accel)
- STANDING: pitch ≤ 15°
- SITTING: pitch ≥ 20°
- LYING: pitch & roll ≤ 10°
- ✅ Heart Rate Monitoring: Peak detection on HW-827 sensor
- ✅ Temperature Tracking: DHT11 with retry logic (WiFi interference tolerance)
- ✅ Emergency Button: Manual SOS trigger
- ✅ MQTT Publishing: Real-time sensor data to HiveMQ Cloud
- ✅ ESP-NOW: Emergency messages to Node B (dynamic channel)
- ✅ RFID Access Control: RC522 reader with full SPI protocol
- Request → Anticollision → UID database lookup
- Authorized/Unauthorized card detection
- ✅ IR Motion Detection: PIR sensor with state machine
- ✅ Security State Machine: IDLE → IR_DETECTED → AUTHORIZED/INTRUSION
- ✅ Buzzer Alarm: GPIO-driven NPN transistor
- ✅ MQTT Publishing: Security events to HiveMQ Cloud
- ✅ ESP-NOW Receiver: Listens for emergencies from Node A
- ✅ WiFi: STA mode with auto-reconnect
- ✅ MQTT over TLS: HiveMQ Cloud with certificate verification
- ✅ SNTP: Time synchronization via pool.ntp.org
- ✅ ESP-NOW: Dynamic channel detection (follows WiFi AP channel)
- ✅ DHT11 Retry: Up to 3 retries on checksum errors
Code follows MISRA-C:2012 guidelines:
- ✅ Rule 8.4: Function prototypes in headers
- ✅ Rule 14.4: All if-else chains have final else
- ✅ Rule 17.7: All return values checked
- ✅ Rule 10.3/10.4: Explicit type casts
- ✅ Rule 2.7: Unused parameters marked
(void)param - ✅ Rule 9.1: Variables initialized before use
- ✅ Compiles with
-Wall -Wextra -Werror
# Terminal 1: Node A
cd node-a-gateway && idf.py -p /dev/cu.usbserial-A monitor
# Terminal 2: Node B
cd node-b-sentry && idf.py -p /dev/cu.usbserial-B monitor- Fall Detection: Shake/drop Node A → Watch for "FALL DETECTED"
- Posture: Rotate Node A → Verify posture changes (STANDING/SITTING/LYING)
- Heart Rate: Simulate sensor pulses → Peak count increases
- RFID Authorized: Scan authorized card → Buzzer silent
- RFID Unauthorized: Scan unknown card → Buzzer sounds
- Emergency: Press button on Node A → Node B buzzer sounds
- MQTT: Check HiveMQ console for all messages
ls /dev/cu.usbserial-*
# Hold BOOT button while connecting- External 2.2kΩ pull-up required on GPIO 5
- Keep wire short (<10cm)
- Check 3.3V power
- Both nodes must be on same WiFi channel
- Verify MAC addresses match actual hardware
- Both must be powered on WiFi
- Check WiFi SSID/password in config.h
- Verify HiveMQ credentials
- Ensure TLS certificate is correct
- Check SPI wiring (MOSI/MISO/SCLK)
- Verify RC522 at 3.3V (not 5V)
- Try increasing SPI clock if needed
idf.py fullclean && idf.py build
idf.py -v build # Verbose output
idf.py build -j8 # Parallel build
idf.py menuconfig # Configuration
idf.py monitor --timestamp # Monitor with timestampsesp_medmonitor/
├── node-a-gateway/ # Vital Monitor firmware
│ ├── main/
│ │ ├── config.h.template
│ │ ├── sensors/ # MPU6050, DHT11, HW-827
│ │ ├── tasks/ # Fall detection, sensor, MQTT
│ │ └── utils/
│ └── CMakeLists.txt
├── node-b-sentry/ # Access Control firmware
│ ├── main/
│ │ ├── config.h.template
│ │ ├── modules/ # RFID, IR, Buzzer
│ │ ├── tasks/ # Security, MQTT
│ │ └── utils/
│ └── CMakeLists.txt
├── Hackthon_PCB/ # KiCAD schematics & layouts
│ ├── Hackthon_PCB.kicad_sch
│ ├── Hackthon_PCB.kicad_pcb
│ ├── gerbers/
│ └── kicad_parts/
└── README.md