How I Built My Milk Vending Machine Prototype

Building an automated milk vending machine combined hardware (ESP32, QR scanner, relay), web technologies (HTML/CSS/JS), and cloud services (Firebase). While still a prototype, it demonstrated the potential of IoT in retail automation.

Concept

User flow:

  1. Customer opens website on phone
  2. Selects quantity and pays online
  3. Receives unique QR code
  4. Scans QR at machine
  5. Machine dispenses milk automatically

Hardware Components

Why LED Instead of Pump?

For proof-of-concept, LED represents pump activation. Validates logic without messy liquid handling during development.

Software Architecture

Web Interface (Frontend)

Firebase Backend

ESP32 Firmware

Development Flow

Phase 1: Web Interface

Built payment page where users:

Phase 2: Firebase Integration

Database structure:

transactions/
  └── {transaction_id}/
      ├── qr_code: "unique_string"
      ├── quantity: 1000 (ml)
      ├── paid: true
      ├── used: false
      ├── timestamp: 1234567890
      └── phone: "+91XXXXXXXXXX"

Phase 3: ESP32 Hardware

ESP32 code flow:

1. Connect to Wi-Fi
2. Initialize Firebase connection
3. Loop:
   a. Read QR scanner
   b. If code detected:
      - Query Firebase for this code
      - If valid and unused:
        * Activate LED/pump
        * Mark code as used
        * Log dispensing event
      - Else: Show error LED

Challenges Faced

Challenge 1: Firebase Delays

Problem: Firebase queries took 2-3 seconds, creating awkward wait time after scanning.

Attempted fix: Local caching of recent codes on ESP32. Improved to ~1 second.

Better solution: Use Firebase Cloud Functions to pre-validate and cache active codes.

Challenge 2: QR Scanner Reliability

Problem: Scanner sometimes failed to read codes in bright sunlight or at angles.

Solution: Added LED indicator showing when scanner is ready. Designed holder to position phone at optimal angle.

Challenge 3: Wi-Fi Connectivity

Problem: ESP32 would occasionally lose Wi-Fi connection, halting operation.

Solution: Implemented automatic reconnection logic with exponential backoff. Added offline mode that logs transactions locally and syncs when connection returns.

Challenge 4: Preventing Code Reuse

Problem: Race condition: code could be used twice if scanned rapidly.

Solution: Firebase transaction to atomically check and mark code as used. Only one request succeeds even if scanned simultaneously.

Future Improvements

Hardware

Software

Business Features

Cost Analysis (Prototype)

ESP32 board:           ₹500
QR scanner module:     ₹800
Relay module:          ₹150
Power supply:          ₹200
LEDs & misc:           ₹100
-------------------------------
Total hardware:        ₹1,750

Firebase:              Free tier
Domain/hosting:        ₹500/year

Production version with pump, enclosure, sensors: ₹8,000-₹12,000

Lessons Learned

1. Start Simple, Iterate

Using LED instead of pump for prototype saved time and mess. Validated logic before adding complexity.

2. Network Reliability is Critical

For IoT devices, assume network will fail. Plan for offline operation and graceful degradation.

3. Security From Day One

Preventing code reuse wasn't afterthought—built into architecture from start. Security is easier to build in than bolt on.

4. User Experience Matters

2-3 second delays felt like eternity to users. Optimize for perceived performance (loading indicators, progress bars).

5. Documentation While Building

Documenting wiring, code, and logic as I built made debugging infinitely easier.

Real-World Deployment Considerations

Hygiene & Safety

Maintenance

Legal & Compliance

Market Potential

Automated milk vending addresses real problems:

Potential locations: apartment complexes, office buildings, colleges, residential areas.

Conclusion

The milk vending machine prototype demonstrated that combining IoT, web technologies, and cloud services can create practical automation solutions. While challenges remain (Firebase delays, scanner reliability, Wi-Fi stability), the core concept works.

This project taught me about integrating multiple technologies, handling real-time data, building user interfaces, and thinking through business logic. From LED placeholder to production-ready pump is an engineering challenge, but the foundation is solid.

IoT isn't just about connecting things—it's about solving real problems with technology. One QR scan, one dispense at a time.