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:
- Customer opens website on phone
- Selects quantity and pays online
- Receives unique QR code
- Scans QR at machine
- Machine dispenses milk automatically
Hardware Components
- ESP32 development board – Wi-Fi connectivity and control logic
- QR code scanner module – Reads customer codes
- LED (prototype phase) – Placeholder for solenoid pump
- Relay module – Controls high-power devices
- Power supply – 12V for pump (future), 5V for ESP32
- Breadboard & wires – Prototyping connections
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)
- HTML/CSS: Clean, mobile-friendly payment interface
- JavaScript: Handle payments, generate QR codes
- QR Code Library: qrcode.js for generating unique codes
Firebase Backend
- Firestore Database: Store payment records, QR codes, dispensing status
- Real-time sync: ESP32 listens for new valid codes
- Security rules: Prevent unauthorized access
ESP32 Firmware
- Connect to Wi-Fi
- Listen to QR scanner
- Query Firebase for code validity
- Activate relay/LED if code is valid
- Mark code as used in database
Development Flow
Phase 1: Web Interface
Built payment page where users:
- Enter phone number
- Select quantity (500ml, 1L, 2L)
- Make payment (UPI/card integration planned)
- Receive QR code displayed and sent via SMS
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
- Real pump: Peristaltic or solenoid pump for actual dispensing
- Flow sensor: Verify correct quantity dispensed
- Level sensor: Monitor milk tank level, alert when low
- Display: OLED showing status, instructions, errors
- Enclosure: Weather-proof cabinet with cooling
Software
- Payment gateway: Razorpay/Stripe integration for real payments
- Admin dashboard: Monitor sales, refills, machine status
- SMS notifications: Receipt, low milk alerts
- Analytics: Track popular times, quantities, revenue
Business Features
- Subscription model: Monthly milk delivery at discounted rate
- Multi-product: Different milk types, other beverages
- Loyalty program: Points for frequent customers
- Referral system: Discounts for bringing new customers
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
- Food-grade materials for milk contact
- Regular cleaning protocols
- Refrigeration to maintain milk quality
- Tamper-proof design
Maintenance
- Daily milk refills and quality checks
- Weekly cleaning of dispensing system
- Monthly hardware inspection
- Remote monitoring for issues
Legal & Compliance
- FSSAI license for food handling
- GST registration for business
- Local permits for vending machines
- Insurance for equipment and liability
Market Potential
Automated milk vending addresses real problems:
- Convenience: 24/7 availability
- Hygiene: Contactless dispensing
- Quality: Directly from dairy, no intermediaries
- Cost: Lower overhead than staffed shops
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.