ESP32 Integration Guide
Overview
The REward kiosk uses ESP32 microcontrollers to detect recycled items and communicate with the API.
Hardware Components
| Component | Model | Purpose |
|---|---|---|
| Main Controller | ESP32-S (38-pin) | Primary processing |
| Camera | ESP32-CAM (OV2640) | Dedicated QR Scanner |
| Display | OLED (ST7789) | User interface (135x240) |
| Keypad | 4x4 Matrix | Manual ID input |
| Status LED | 5mm LED | Visual activity (GPIO 16) |
| Buzzer | Active Buzzer | Audio feedback (GPIO 17) |
| Servo | MG995 | Sorting mechanism |
Wiring Diagram (Core Pins)
ESP32 Pin Component
--------- ---------
GPIO 16 Status LED
GPIO 17 Audio Buzzer
GPIO 13 Servo PWM
GPIO 26 Inductive Sensor (Metal)
GPIO 25 IR Sensor (Plastic)
GPIO 3, 1 Serial (QR Data from CAM)
GPIO 32-14 Keypad Matrix
API Integration
Authentication
Store API key in ESP32:
const char* API_KEY = "your-secret-api-key";
const char* API_URL = "https://yourdomain.com/api";
Update Status on Boot
void updateKioskStatus(String status) {
HTTPClient http;
http.begin(API_URL + "/kiosk/" + KIOSK_ID + "/status");
http.addHeader("Content-Type", "application/json");
http.addHeader("X-API-Key", API_KEY);
String json = "{\"status\":\"" + status + "\"}";
int code = http.POST(json);
http.end();
}
Submit Transaction
When user completes recycling:
void submitTransaction(String userId, int plastic, int metal) {
HTTPClient http;
http.begin(API_URL + "/kiosk/transaction");
http.addHeader("Content-Type", "application/json");
http.addHeader("X-API-Key", API_KEY);
String json = "{";
json += "\"kioskId\":\"" + KIOSK_ID + "\",";
json += "\"userId\":\"" + userId + "\",";
json += "\"plasticCount\":" + String(plastic) + ",";
json += "\"metalCount\":" + String(metal);
json += "}";
int code = http.POST(json);
if (code == 200) {
// Success - display confirmation
DynamicJsonDocument doc(1024);
deserializeJson(doc, http.getString());
int points = doc["transaction"]["pointsEarned"];
displaySuccess(points);
}
http.end();
}
User Flow
- User scans QR code from app at kiosk
- Kiosk extracts userId from QR code
- User deposits items one by one
- Sensors detect item type (plastic/metal)
- Servo sorts item to correct bin
- Count displayed on OLED
- User presses "Done" button
- ESP32 calls API to submit transaction
- User receives push notification with points
QR Code Format
The app generates QR codes containing:
{
"userId": "abc123",
"timestamp": 1703019600
}
Sensor Logic
Metal Detection (Inductive)
if (digitalRead(METAL_SENSOR_PIN) == LOW) {
// Metal detected
metalCount++;
activateSorter(METAL_BIN);
}
Plastic Detection (IR)
if (digitalRead(PLASTIC_SENSOR_PIN) == LOW &&
digitalRead(METAL_SENSOR_PIN) == HIGH) {
// Plastic detected (not metal)
plasticCount++;
activateSorter(PLASTIC_BIN);
}
Safety Considerations
- Logic Level Shifters - Sensors output 12V, ESP32 is 3.3V
- Flyback Diodes - Protect against servo motor back-EMF
- Decoupling Capacitors - Stabilize power for sensors
- Watchdog Timer - Reset ESP32 if it hangs
Libraries
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <ESP32Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>