Project Info
Inspiration
Tuna, one of our Team members, is someone who suffers from chronic bruxism, a condition of continuous teeth clenching during the day and night, that causes extreme jaw pain, and sleep inconveniences. This firsthand experience of a real problem formed the basis of our project SleepSense, to solve this broader pain point of lack of good sleep, which is especially common amongst people from all groups these days. We found the sentiment of lack of good sleep was repeatedly echoed throughout the hackathon, be it amongst the engineers, or the founder and the VCs, all of them emphasized the less sleep all these people get. So we were driven by this mission to build a project that caters to these amazing people, and make their every hour of sleep count, by helping them have a restful sleep.
What it does
Our project is an end-to-end solution that uses real-time sensing on patients to obtain their vital health parameters, such as cardiac and breathing, along with specific health data, like muscular activity, acquired using our custom-developed device that clips easily onto people's night masks and helps them improve their sleep. The device is not only able to acquire high-frequency data and transmit it securely to the cloud, but it is also of a very small form factor, which is ergonomically convenient for a user to put on during their sleep and monitor any form of abnormalities that disrupt their sleep. This multimodal data is then used by our model, which uses existing clinical data to accurately diagnose the root cause of their sleep disruptions, like bruxism, chronic stress, genetic factors like hormone imbalance, etc. Based on the preliminary diagnosis, our agentic platform can then generate reports about their sleep patterns, acute and chronic conditions, and then recommend specific suggestions about the nearest healthcare specialists relating to the problem, and also provide them with detailed reports of the patient's health data to consult and advise them proper plan of action.
How we built it
Hardware: We designed a compact clip-on module built around an ESP32 microcontroller and an EMG muscle sensor that attaches to a standard sleep mask. The ESP32 reads raw 12-bit ADC values from the EMG sensor at high frequency, capturing jaw muscle activity in real time. For heart rate, we use an off-the-shelf wearable monitor that communicates over Wi-Fi. The ESP32 handles data capture and streams readings wirelessly to the cloud. We 3D-printed a custom enclosure (designed in CAD) to keep the electronics small and comfortable enough to wear during sleep. We also initially faced hiccups in sourcing the hardware and sensors we needed. While we were limited by our access to hardware, we tried to make the best use of what we had available to design this. Software: Data Pipeline: The wearable POSTs heart rate to a Flask server, while the ESP32 writes EMG data to Google Sheets (easiest way to bridge the hardware to our backend). Flask polls both sources, combines them at 10 Hz, and streams the result to the frontend over Server-Sent Events. Dashboard: A Next.js 14 app connects to that SSE stream and renders live Recharts visualizations, heart rate, jaw activity level, and raw EMG, updating at 5 Hz. NextAuth handles login with Google OAuth or a zero-config demo mode. Report Engine: reportLogic.ts classifies jaw activity from EMG thresholds, detects clenching events, and checks whether each one was preceded by a heart-rate spike. If yes → arousal-linked (stress response). If no → isolated (habitual bruxism). These feed into a sleep quality score. AI Chatbot: GPT-4o gets the full sensor dump and event log as context, so it can reason over the actual session data. We gave it two function-calling tools: search_clinics (Google Places API) and confirm_booking, so users can go from data analysis to booking a specialist without leaving the chat.
Challenges we ran into
Hardware: Getting reliable EMG readings from a sensor mounted on a sleep mask was tricky; small shifts in placement caused big swings in signal quality. We also ran into power and heat issues with the ESP32 running continuous high-frequency ADC reads over Wi-Fi, and had to tune the sampling rate to balance data quality with battery life, latency, and stability. Software: Syncing two async data sources: Heart rate arrives via HTTP POST and EMG comes from polling Google Sheets, two completely different timing models. Getting them aligned into a single 10 Hz stream without drift or stale readings took a lot of trial and error with Flask's threading. SSE connection drops: The browser's EventSource would silently disconnect after a few minutes of inactivity or on network hiccups. We had to add reconnection logic and buffer management on the frontend to avoid gaps in the chart data.
Accomplishments we're proud of
We had a lot of fun in the process of making this, and we are really proud of all the friends we made along the way. We were also really happy that we were able to consult real patients through our interactions with other hackers, mentors, judges, and involved parties, like dentists, for understanding the needs of the market space, and we are shocked to realize that this was a far more common issue than we initially thought it to be. Finally, we built something that is a genuinely hard engineering problem, to acquire critical data, and then leverage agentic AI tools that can use existing and new clinical data, and improve people's sleep, and that's what makes us really proud and happy.
What we learned
Working across hardware and software taught us how messy real-time sensor data really is - what looks clean on a breadboard behaves very differently when someone's actually wearing it. We learned how to stitch together multiple async data sources into a reliable streaming pipeline, and how much SSE simplifies things when you only need one-way data flow. On the AI side, function calling turned out to be the unlock - it's what took our chatbot from "here's some generic advice" to actually finding clinics and booking appointments. Talking to dentists and fellow hackers also opened our eyes to how widespread and underserved sleep disruption problems really are.
What's next
One of the key improvements that we would like to incorporate into SleepSense is adding real-time biofeedback to the users, which can help patients suffering from bruxism, which is one of the major sleep disruptors for many people today. We would like to incorporate other important health parameters using our device and leverage on exisitng unpublished clinical data through partnerships to help both doctors, healthcare specialists, and patients. We would like to possibly explore how this project could be taken beyond the hackathon to build a genuinely impactful product.
SleepSense
Real-time bruxism and jaw-clenching monitoring dashboard - built at TreeHacks 2026.
SleepSense connects a smartwatch heart-rate sensor and an muscle sensor to a live analytics dashboard. Sensor data streams through a Flask data hub into a Next.js frontend that visualizes jaw activity, detects clenching events, classifies them by their cardiac–muscular relationship, and generates clinical-grade reports. An embedded GPT-4o chatbot can reason over the patient's data and book a specialist through Google Places.
Table of Contents
Project Structure
sleepsense/
├── app/ # Next.js App Router
│ ├── page.tsx # Landing page — sign-in (Google OAuth or demo)
│ ├── layout.tsx # Root layout with dark theme + SessionProvider
│ ├── providers.tsx # NextAuth SessionProvider wrapper
│ ├── globals.css # Tailwind base + chatbot widget styles
│ ├── dashboard/
│ │ └── page.tsx # Auth-protected dashboard entry point
│ └── api/
│ ├── auth/[...nextauth]/
│ │ └── route.ts # NextAuth handler (Google + mock credentials)
│ ├── sessions/
│ │ ├── route.ts # GET list / POST create sessions
│ │ └── [id]/route.ts # GET / PUT individual session
│ ├── reports/
│ │ └── route.ts # GET by sessionId / POST generate report
│ ├── bookings/
│ │ └── route.ts # POST create booking record
│ └── places/
│ └── route.ts # Proxy to Google Places Text Search API
│
├── components/
│ ├── Dashboard.tsx # Main 3-section layout + session state machine
│ ├── ChatBot.tsx # SleepSense AI chatbot — GPT-4o with function calling
│ ├── ReportBox.tsx # Expandable bullet-point report card
│ ├── SignInButton.tsx # Google OAuth + demo sign-in form
│ ├── StatusBadge.tsx # Connection status indicator
│ └── charts/
│ ├── HeartRateChart.tsx # BPM area chart (Recharts)
│ ├── JawActivityChart.tsx # 3-level step chart (Relaxed / Talking / Clenching)
│ ├── EMGChart.tsx # Raw EMG waveform line chart
│ ├── HRChart.tsx # Simple HR line chart
│ └── MainChart.tsx # Combined 3-signal overview chart
│
├── lib/
│ ├── auth.ts # NextAuth config (Google provider + mock)
│ ├── storage.ts # JSON file read/write for sessions, reports, bookings
│ ├── mockSensor.ts # Seeded PRNG sensor data generator
│ ├── reportLogic.ts # Clench detection, event classification, report scoring
│ └── bruxismAgent.ts # SleepSense AI — GPT-4o agent with search_clinics + confirm_booking tools
│
├── types/
│ └── index.ts # TypeScript interfaces (SensorPoint, SessionRecord, etc.)
│
├── credentials/
│ └── service-account.json # Google service account for Sheets API (EMG polling)
│
├── data/
│ └── db.json # Auto-created local JSON database
├── design/CAD_enclosure # design files for mask
├── hardware/esp32_serial_blocking # data capture and data stream over wifi through esp32
├── test.py # Flask data hub (SleepSense Data Hub) — unifies HR + EMG into 10 Hz SSE stream
├── requirements.txt # Python dependencies (flask, gspread, google-auth)
├── package.json # Node dependencies and scripts
├── tailwind.config.ts # Tailwind CSS configuration
├── tsconfig.json # TypeScript configuration
├── next.config.mjs # Next.js configuration
└── postcss.config.mjs # PostCSS plugins (Tailwind + Autoprefixer)
Tech Stack
| Layer | Technology |
|---|---|
| Frontend | Next.js 14 (App Router) + React 18 + TypeScript |
| Styling | Tailwind CSS |
| Charts | Recharts (AreaChart, LineChart, ReferenceArea) |
| Auth | NextAuth v4 — Google OAuth + zero-config mock credentials |
| AI Chatbot | SleepSense AI — GPT-4o via OpenAI API with function calling (search_clinics, confirm_booking) |
| Clinic Search | Google Places Text Search API |
| Data Hub | SleepSense Data Hub — Flask (Python), combines HR + EMG into a 10 Hz SSE stream |
| EMG Sensor | ESP32 → Google Sheets → Flask polls via gspread |
| HR Sensor | Wearable POSTs BPM to Flask /data endpoint |
| Storage | Local JSON file (data/db.json) via Node fs in API routes |
Architecture Overview
Data flow:
- Heart rate — A wearable device POSTs BPM readings to Flask at
/data. - EMG — An ESP32 writes raw 12-bit ADC values to a Google Sheet. Flask polls the sheet every second via
gspread. - Flask combiner — A background thread reads the latest HR + EMG at 10 Hz and pushes combined JSON events over SSE (
/stream). - Next.js dashboard — Opens an
EventSourceto Flask, buffers incoming data points, and refreshes charts at 5 Hz. - Report engine (
reportLogic.ts) — Classifies jaw activity into Relaxed / Talking / Clenching using ADC thresholds, detects bruxating events, correlates them with heart-rate arousal, and scores sleep quality. - AI chatbot (
bruxismAgent.ts) — Sends the full sensor data dump + event log as GPT-4o system context. The model analyzes patterns, identifies root causes, and can callsearch_clinics(Google Places) andconfirm_bookingto schedule a specialist visit.
Setup
Prerequisites
- Node.js ≥ 18
- Python ≥ 3.9 (for the Flask data hub)
- npm
1. Install Node dependencies
cd sleepsense
npm install
2. Install Python dependencies
pip install -r requirements.txt
3. Configure environment variables
Create a .env.local file in the project root:
# NextAuth
NEXTAUTH_SECRET=your-random-secret
NEXTAUTH_URL=http://localhost:3000
# Google OAuth (optional — demo sign-in works without it)
GOOGLE_CLIENT_ID=...
GOOGLE_CLIENT_SECRET=...
# Google Places API (required for clinic search in chatbot)
GOOGLE_PLACES_API_KEY=...
# OpenAI (optional — can also be entered in the chatbot UI at runtime)
NEXT_PUBLIC_OPENAI_API_KEY=...
Note: The demo sign-in mode works with no environment variables at all. Google OAuth, Places, and OpenAI keys are only needed for their respective features.
4. Start the Flask data hub
python test.py
This starts the SleepSense Data Hub on port 5001. It will:
- Accept heart-rate POSTs from the wearable at
/data - Poll Google Sheets for ESP32 EMG data
- Stream combined data at 10 Hz via SSE at
/stream
5. Start the Next.js dev server
npm run dev
Open http://localhost:3000.
Usage
Sign In
- Demo mode — Click "Use demo account" on the landing page. No OAuth setup needed.
- Google OAuth — Configure
GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRETin.env.local, addhttp://localhost:3000/api/auth/callback/googleas an authorized redirect URI in Google Cloud Console.
Monitor a Session
- Click Connect Device — the dashboard opens an SSE connection to the Flask data hub and starts buffering sensor data.
- Live charts update at 5 Hz showing Heart Rate (BPM area chart) and Jaw Activity (3-level step chart: Relaxed → Talking → Clenching).
- The Live Analysis panel displays running metrics: clenching events, sleep quality score, current jaw state, and average heart rate.
- Click Save Report at any time to snapshot the current analysis. The report engine classifies each bruxating event as arousal-linked (HR spike preceded the clench) or isolated (habitual pattern).
- Click Disconnect to stop the session.
AI Chatbot
- Click the chat bubble in the bottom-right corner.
- If no OpenAI API key is configured, paste one when prompted (stored in
sessionStorageonly). - Ask questions about your session data — GPT-4o has the full sensor dump and event log as context.
- When ready, the chatbot offers to find a specialist. It calls the Google Places API via function calling, presents clinics, and can confirm a booking that includes the sensor report and chat thread.
Past Sessions
Use the Past Sessions dropdown in the top bar to reload any previously saved session's report and chart data.
Demo Video
▶️ Watch the full demo: https://www.youtube.com/watch?v=acdo23eFIlc
Analysis
View
Metric
- 15
- 2
- 1
Figures cover GitHub contributors during the hackathon window. A co-authored commit counts in full for each author, so per-member totals add up to more than the whole-team figures.
Technology
- CSSIn code
- FlaskIn code
- Next.jsIn code
- PythonIn code
- ReactIn code
- Tailwind CSSIn code
- TypeScriptIn code
- OpenAIClaimed
7 of 8 appear in the indexed code. 1 claimed on Devpost could not be matched to code, which may simply mean the tool leaves no trace in the repository.
AI coding agents
No AI coding agent signals were found in this repository.
Detected from committed agent config files and commit authorship. Absence of a signal is not proof an agent was unused.
Codebase size
Source size
128 KB
Source files
30
Counts recognized source files only; vendored directories, binaries and lockfiles are excluded, so this is smaller than the repository on disk.
Repository
raj-chinagundi/treehacks-26
44 files · 7.5 MB · @ e56a42c
Structure
Interface
10 files · 23%Screens, components and styles rendered to the user.
API & routing
6 files · 14%Request entry points: routes, handlers and controllers.
Application logic
12 files · 27%Domain rules, services and shared utilities.
Supporting
Layers are inferred from where files sit in the tree, not from reading the code. A project that names its directories unconventionally will read oddly here — open the file browser to check anything the diagram implies.
Languages
- TypeScript78%
- Python8%
- Markdown7%
- CSS6%
Share of indexed source by file size. Binary and vendored files are excluded.
Dependencies
package.json
npm · 16- next
- next-auth
- react
- react-dom
- recharts
- uuid
- +10 more
requirements.txt
pypi · 3- flask
- google-auth
- gspread
Declared in the repository’s manifests at the indexed commit. A declared package is not proof it is used, and runtime dependencies are listed first.
This project’s features have not been analysed yet.
Export this project's context (description, README, evidence, key source files) to chat with an AI agent elsewhere.