ImpactX is an emergency response system that combines an edge device (ESP32 + sensors + GPS) with a FastAPI backend and web dashboard.
The ESP32 sends telemetry to the backend, and the backend handles severity decisions, logging, and optional Twilio notifications.
- ESP32 + FastAPI architecture for live incident telemetry.
- Real-time severity classification:
SAFE,ALERT,EMERGENCY. - 20-second confirmation window before escalation.
- ESP32 sends high-impact sensor/GPS telemetry for cloud decisioning.
- Local emergency indication on device (red LED + buzzer) with green LED always on.
- Dashboard for live status, activity feed, and event logs.
- Optional Twilio integration for cloud SMS/call workflows.
- Online learning from cancelled incidents (false alarms) adjusts thresholds.
- Reads telemetry and computes local severity.
- If backend is unavailable, keeps operating standalone with local cancel/alert logic.
- If backend is available, sends only high-impact events (
impact > 20) with telemetry. - Starts physical cancel window.
- Polls backend command channel and executes remote
ALERT/EMERGENCY/CANCELactions. - Triggers local emergency outputs when required.
- Validates and normalizes incoming events.
- Computes severity and manages event state transitions.
- Resolves nearest hospital from a demo dataset.
- Sends communication actions (simulated or Twilio-backed).
- Persists event records for audit/history.
- Hardware mode: ESP32 sends sensor payload to
POST /event. - Demo mode: dashboard buttons submit predefined
SAFE,ALERT, orEMERGENCYpayloads toPOST /event.
- Backend normalizes telemetry and computes severity score using:
impact_score = min(100, impact * 6.5)speed_score = min(100, speed / 140 * 100)tilt_score = min(100, tilt / 90 * 100)risk_score = impact_score*0.55 + speed_score*0.30 + tilt_score*0.15- low-speed suppression (
speed < 12 && impact < 7) appliesrisk_score *= 0.65
- Classification (adaptive):
SAFEifrisk_score < (20 + shift)ALERTif20 + shift <= risk_score <= 50 + shiftEMERGENCYifrisk_score > 50 + shift
shiftis learned online from false-alarm ratio (cancelled incidents).
ALERTandEMERGENCYfirst enterPENDING_CONFIRMATION.- System waits 20 seconds for manual cancel via
POST /event/{event_id}/cancel.
- If cancelled: status becomes
CANCELLED. - If not cancelled:
ALERT→ hospital lookup + SMS flow.EMERGENCY→ hospital lookup + SMS + voice call flow.
- Default behavior is simulated communication (safe for local demos).
- Real delivery is enabled only when
ENABLE_REAL_COMMUNICATION=trueand Twilio environment variables are configured.
- Finalized events are appended to
events_log.jsonl. - Adaptive learning profile is persisted in
learning_profile.json. - Dashboard polls:
GET /statusfor latest state and recent activity.GET /logsfor full event history (UI filters to ALERT/EMERGENCY rows).
main.py— FastAPI backend and agent workflow.static/index.html— dashboard UI.iot/esp32_sender.ino— ESP32 firmware logic.events_log.jsonl— runtime event log output.
POST /event— ingest telemetry event (JSON).POST /event/{event_id}/cancel— cancel pending event.POST /device/report— device heartbeat + hardware false-alarm cancellation report.GET /device/{device_id}/command— fetch latest backend command for ESP32.POST /device/{device_id}/command/ack— acknowledge command execution.GET /status— latest status and recent activity.GET /logs— event history.GET /health— service health check.GET /— dashboard page.
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
uvicorn main:app --reloadDashboard: http://127.0.0.1:8000
-
Run backend on a reachable network interface
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
-
Find your laptop/PC LAN IP
- Example:
192.168.1.23 - ESP32 and laptop must be on the same Wi-Fi network.
- Example:
-
Update ESP32 firmware config in
iot/esp32_sender.inoconst char* WIFI_SSID = "YOUR_WIFI"; const char* WIFI_PASS = "YOUR_PASS"; const char* API_URL = "http://192.168.1.23:8000/event/camera";
Use your actual LAN IP in
API_URL(not127.0.0.1). -
Flash ESP32 and open Serial Monitor (115200)
- On successful posts you should see:
POST /event try X => 200. - If backend is unreachable, device retries and logs POST failures in Serial Monitor.
- On successful posts you should see:
-
Verify data in backend dashboard
- Open:
http://<your-laptop-ip>:8000(orhttp://127.0.0.1:8000on same machine). - Check
GET /statusandGET /logsfor incoming events.
- Open:
POST ... => -1or timeout: wrongAPI_URL, network mismatch, or firewall blocking port8000.- No Wi-Fi connect on ESP32: wrong SSID/password or unsupported band (use 2.4 GHz).
- Dashboard opens but no events: ensure sensor score crosses threshold so firmware sends
/event. - Firmware sends only high-impact candidates (
impact > 20) to reduce low-impact network noise.
The dashboard supports manual simulation buttons:
Demo SAFEDemo ALERTDemo EMERGENCY
You can also submit a location-based sample event using Send Demo Event (My Location).
By default, communication actions are simulated.
Set these environment variables in the same terminal/session where you start the FastAPI server (uvicorn main:app --reload), or configure them in your deployment platform's environment settings.
Enable real Twilio delivery:
export ENABLE_REAL_COMMUNICATION=true
export TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export TWILIO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxx
export TWILIO_FROM_NUMBER=+1XXXXXXXXXX
export EMERGENCY_TO_NUMBER=+1YYYYYYYYYY
export TWILIO_TWIML_URL=http://demo.twilio.com/docs/voice.xmlSAFE:- No Twilio SMS or call is sent.
ALERT:- Sends one SMS to
EMERGENCY_TO_NUMBER.
- Sends one SMS to
EMERGENCY:- Sends one SMS to
EMERGENCY_TO_NUMBER. - Places one voice call to
EMERGENCY_TO_NUMBER.
- Sends one SMS to
Current SMS body format:
Accident detected! Status: <ALERT|EMERGENCY>. Location: https://maps.google.com/?q=<lat>,<lon>
Current voice call behavior:
- Uses Twilio
CallsAPI fromTWILIO_FROM_NUMBERtoEMERGENCY_TO_NUMBER. - Plays TwiML from
TWILIO_TWIML_URL(defaults to Twilio demo URL if not set).
Delivery behavior:
ALERT→ SMSEMERGENCY→ SMS + voice call