#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Backend API configuration - Using ngrok URL
const char* backendUrl = "https://sentinellike-meaghan-gaspingly.ngrok-free.dev";
String sessionId = "d343b7fb-fad8-419c-b499-6d9b95e3632f";
String inputBuffer = "";
void setup() {
Serial.begin(115200);
delay(1000);
connectWiFi();
delay(2000);
startSession("TROLLEY_01");
Serial.println("\n=== Smart Trolley Barcode Scanner ===");
Serial.println("Enter product barcode and press Enter:");
Serial.println("\nSample barcodes:");
Serial.println(" 8901234567890 - Milk (1L) - ₹65");
Serial.println(" 8901234567891 - Bread (White) - ₹45");
Serial.println(" 8901234567892 - Rice (5kg) - ₹350");
Serial.println(" 8901234567893 - Cooking Oil (1L) - ₹180");
Serial.println(" 8901234567894 - Sugar (1kg) - ₹55");
Serial.println("=====================================\n");
}
void loop() {
while (Serial.available()) {
char c = Serial.read();
if (c == '\n' || c == '\r') {
if (inputBuffer.length() > 0) {
Serial.println("\n📦 Scanning barcode: " + inputBuffer);
if (sessionId.length() > 0) {
sendBarcodeToBackend(inputBuffer);
} else {
Serial.println("❌ Error: No active session!");
}
inputBuffer = "";
Serial.println("\nEnter next barcode:");
}
} else {
inputBuffer += c;
}
}
delay(10);
}
void connectWiFi() {
Serial.println("🔌 Connecting to WiFi...");
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\n✅ WiFi connected!");
Serial.print("📡 ESP32 IP: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("\n❌ WiFi connection failed!");
}
}
void startSession(String trolleyId) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("❌ WiFi not connected!");
return;
}
Serial.println("\n🛒 Starting session for " + trolleyId + "...");
HTTPClient http;
String url = String(backendUrl) + "/api/session/start";
Serial.println("📡 Connecting to: " + url);
http.begin(url);
http.addHeader("Content-Type", "application/json");
http.setTimeout(15000);
StaticJsonDocument<100> jsonDoc;
jsonDoc["trolley_id"] = trolleyId;
String jsonString;
serializeJson(jsonDoc, jsonString);
int httpResponseCode = http.POST(jsonString);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("✅ Response code: " + String(httpResponseCode));
StaticJsonDocument<512> responseDoc;
DeserializationError error = deserializeJson(responseDoc, response);
if (!error) {
sessionId = responseDoc["session_id"].as<String>();
Serial.println("🆔 Session ID: " + sessionId);
Serial.println("✅ Session started successfully!\n");
} else {
Serial.println("⚠️ JSON parse error");
Serial.println("Response: " + response);
}
} else {
Serial.println("❌ Connection failed! Code: " + String(httpResponseCode));
Serial.println(" Make sure:");
Serial.println(" 1. Django server is running");
Serial.println(" 2. ngrok tunnel is active");
Serial.println(" 3. URL is correct");
}
http.end();
}
void sendBarcodeToBackend(String barcode) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("❌ WiFi not connected!");
return;
}
HTTPClient http;
String url = String(backendUrl) + "/api/cart/scan";
http.begin(url);
http.addHeader("Content-Type", "application/json");
http.setTimeout(15000);
StaticJsonDocument<200> jsonDoc;
jsonDoc["session_id"] = sessionId;
jsonDoc["barcode"] = barcode;
String jsonString;
serializeJson(jsonDoc, jsonString);
Serial.println("📤 Sending: " + jsonString);
int httpResponseCode = http.POST(jsonString);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("📥 Response code: " + String(httpResponseCode));
StaticJsonDocument<512> responseDoc;
DeserializationError error = deserializeJson(responseDoc, response);
if (!error && httpResponseCode == 200) {
const char* productName = responseDoc["product"]["name"];
const char* price = responseDoc["product"]["price"];
int quantity = responseDoc["quantity"];
const char* subtotal = responseDoc["subtotal"];
const char* action = responseDoc["action"];
Serial.println("\n✅ SUCCESS!");
Serial.println(" 🏷️ Product: " + String(productName));
Serial.println(" 💰 Price: ₹" + String(price));
Serial.println(" 📦 Quantity: " + String(quantity));
Serial.println(" 💵 Subtotal: ₹" + String(subtotal));
Serial.println(" 🎬 Action: " + String(action));
} else {
Serial.println("❌ Response: " + response);
}
} else {
Serial.println("❌ HTTP Error: " + String(httpResponseCode));
}
http.end();
}