Skip to content

MikeHodges-IT/SugarCube

Repository files navigation

SUGAR CUBE

Your Glucose. Glowing. Always Visible.

Sugar Cube Color Range Display

Imagine a glowing cube on your nightstand that tells you your glucose at a glance. No phone. No app. Just COLOR.


What Is This Magic?

Sugar Cube is a glowing cube that shows your real-time glucose level through color. It pulls data from your Dexcom or Libre CGM every 5 minutes and lights up in the color that matches your current glucose:

🔵 BLUE = Low (You need sugar!)
🟢 GREEN = Perfect (You're crushing it!)
🟠 ORANGE = Getting High (Keep an eye on it)
🔴 RED = High (Time to take action)
🟣 PURPLE = Very High (Definitely take action)

No squinting at phones. No opening apps. Just look at the cube.


The Hardware: One Tiny Board. That's It.

Package Contents

You literally need ONE thing: The Waveshare ESP32-C3 Zero board. It has the RGB LED BUILT RIGHT IN.

  • No soldering
  • No wiring
  • No electronics knowledge needed
  • Just plug in USB-C and you're done

Want it to look amazing? Print the frosted cube enclosure (file included). Don't have a 3D printer? The bare board still looks cyberpunk as hell.


Setup in 60 Seconds

1. Plug It In

USB-C cable. Any charger. Done.

2. Connect to Its WiFi

Your cube creates a network called SugarCube-Setup-XXXX. Connect from your phone - it automatically opens the captive portal (like hotel WiFi).

WiFi Setup Screen

3. Three Quick Steps in Your Browser

Configuration Portal

  • Pick your home WiFi
  • Enter your Dexcom or Libre login
  • Adjust the colors if you want (or leave them - defaults are perfect)

4. IT'S ALIVE

Your cube starts glowing with your current glucose. Setup complete. Forever.


Why This Exists

Because checking your phone 50 times a day sucks.

Sugar Cube sits anywhere you want instant glucose awareness:

  • Nightstand - See your glucose without waking up your partner
  • Desk - Peripheral vision glucose monitoring while working
  • Kitchen - Know if you need to treat before you eat
  • Living Room - Ambient awareness for the whole family

The Tech Details (for nerds)

What You Get

/sugarcube.ino          # The brain
/portal_html.h          # Captive portal interface
/hardware/cad/          
  └── sugar cube.stl    # 3D printable frosted cube
/images/                # Documentation images

The Board

Waveshare ESP32-C3 Zero - A $10 board with everything built in:

  • ESP32-C3 processor
  • WiFi
  • RGB LED
  • USB-C power

Get it from: Waveshare, Amazon, AliExpress (search "ESP32-C3 Zero")

The Code

  • Connects to Dexcom Share or LibreView
  • Updates every 5 minutes
  • Stores credentials securely on device
  • Auto-recovers from network issues
  • Zero maintenance after setup

Build Your Own

You Need:

  1. Waveshare ESP32-C3 Zero board (~$10)
  2. Any USB-C cable
  3. Arduino IDE (free)
  4. 5 minutes

Flash It:

  1. Download this repo
  2. Open sugarcube.ino in Arduino IDE
  3. Install two libraries: ArduinoJson and FastLED
  4. Select board: ESP32C3 Dev Module
  5. Click Upload
  6. You now have a Sugar Cube

Optional: Print the Cube

  • File: /hardware/cad/sugar cube.stl
  • Material: White or translucent PETG/PLA
  • Settings: 0.2mm layers, 20% infill, no supports
  • Result: A gorgeous frosted cube that glows beautifully

Customization

Change Your Colors

Don't like the defaults? Set your own ranges in the web portal:

  • Maybe you want green up to 140
  • Maybe purple should start at 250
  • It's YOUR cube

Change Update Speed

Want faster updates? Edit one line in sugarcube_config.h:

const unsigned long CGM_FETCH_INTERVAL_MS = 300000; // 5 minutes
// Change to 60000 for 1-minute updates

Status Light Decoder

The cube tells you what it's doing:

Color What's Happening
White Need to run setup
Yellow (blink) Connecting to WiFi
Magenta Connected, waiting
Cyan (flash) Getting your glucose
Your glucose color WORKING PERFECTLY
Red (solid, not glucose) Check your CGM login

Real Talk

This Is NOT Medical Equipment

  • Don't make medical decisions based on the cube
  • Use your real CGM app for dosing insulin
  • This is for ambient awareness, not treatment

Your Data

  • Credentials stored locally on the device in encrypted flash
  • Uses the same APIs as Dexcom Follow / LibreLinkUp
  • No cloud service, no subscriptions, no tracking

Troubleshooting

Cube won't connect? → Only works with 2.4GHz WiFi
Stays red? → Re-enter your CGM password
Not updating? → Check if your CGM app is uploading
Can't find setup network? → Power cycle and try again


How The Code Works

The Magic Behind Sugar Cube

The code is surprisingly simple for what it does. Here's how it all works:

Captive Portal Setup Mode

When Sugar Cube boots up (or can't connect to WiFi), it creates its own WiFi network and runs a captive portal - that's the popup that automatically appears on your phone when you connect (like at hotels or airports). The portal is a self-contained web server running on the ESP32.

The portal HTML/CSS/JavaScript is stored directly in portal_html.h as a single string - no external files needed. When you connect, it:

  • Scans for nearby WiFi networks and shows signal strength
  • Tests your WiFi credentials before saving them
  • Verifies your CGM login works before committing
  • Saves everything to the ESP32's non-volatile memory

Main Loop - Two Modes

Setup Mode:

  • Creates WiFi access point: SugarCube-Setup-XXXX
  • Runs DNS server that redirects all requests to the portal
  • Serves the configuration web interface
  • Auto-exits after 60 seconds if unused (or 10 minutes if active)

Normal Mode:

  • Connects to your home WiFi
  • Every 5 minutes:
    • Calls Dexcom or Libre API
    • Gets your latest glucose value
    • Sets the LED to the appropriate color
  • Handles network drops and reconnects automatically

API Integration

Dexcom Share:

1. Authenticate with username/password → Get Account ID
2. Login with Account ID → Get Session ID  
3. Request glucose with Session ID → Get glucose value

LibreView:

1. Login once → Get auth token (saved for reuse)
2. Request connections endpoint with token → Get glucose value
3. Token refreshes automatically when expired

Color Logic

The setGlucoseLedColor() function takes the glucose value and compares it against your custom ranges:

if (glucose <= rangeBlue)        { leds[0] = CRGB::Blue;   }
else if (glucose <= rangeGreen)  { leds[0] = CRGB::Green;  }
else if (glucose <= rangeOrange) { leds[0] = CRGB::Orange; }
else if (glucose <= rangeRed)    { leds[0] = CRGB::Red;    }
else                              { leds[0] = CRGB::Purple; }

Security

  • Credentials stored in ESP32's Preferences (encrypted flash storage)
  • Portal only accessible via direct WiFi connection (not from internet)
  • One user at a time during setup (locks to first connected device)
  • SHA256 hashing for Libre authentication

Error Handling

  • WiFi drops: Auto-reconnect with visual feedback (yellow blink)
  • API failures: Red LED + retry on next cycle
  • Invalid credentials: Clear stored tokens to force re-login
  • No configuration: White LED until setup

The Files Explained

  • sugarcube.ino - Main program logic, setup/loop functions
  • portal_html.h - Complete web interface in a single HTML string
  • led_controller.cpp/h - LED color functions and patterns
  • sha256_utils.h - Hashing library for Libre authentication
  • sugarcube_config.h - All timing and API constants in one place

The entire codebase is under 1000 lines - clean, readable, and hackable.


The Bottom Line

For the price of two fancy coffees, you can build a device that shows your glucose as a glowing color 24/7.

No subscriptions. No apps running. No battery to charge. Just a cube that glows your glucose.


License: MIT
Created for the diabetes community

About

Visual glucose monitor using ESP32 and RGB LED. Displays CGM data from Dexcom/Libre as ambient colored light. No app needed.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors