#include <WiFi.h>
#include <HTTPClient.h>
// Wi-Fi credentials
const char* ssid = "Mathurs_4G"; // Your Wi-Fi network SSID
const char* password = "Vijay@302020"; // Your Wi-Fi network password
// API endpoint URL
const char* apiUrl = "https://httpbin.org/post"; // For testing POST requests
void setup() {
// Start Serial Monitor for debugging and reading barcode data
Serial.begin(115200);
// Wait for the Serial Monitor to initialize
while (!Serial) {}
// Connect to Wi-Fi
WiFi.begin("Wokwi-GUEST", "", 6);
Serial.println("Connecting 2 WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (Serial.available() > 0) {
String barcode = "";
while (Serial.available()) {
barcode += (char)Serial.read(); // Read the data from Serial Monitor
}
// Print the barcode data to the Serial Monitor
Serial.println("Scanned Barcode: " + barcode);
// Send the barcode data to an API
sendBarcodeToAPI(barcode);
}
delay(1000);
}
void sendBarcodeToAPI(String barcode) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(apiUrl); // Start the HTTP request to the API
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare the data to send (barcode)
String payload = "barcode=" + barcode;
// Send POST request
int httpResponseCode = http.POST(payload);
// Check the response from the API
if (httpResponseCode > 0) {
Serial.println("HTTP Response Code: " + String(httpResponseCode));
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.println("Error in HTTP request");
}
// Close the HTTP connection
http.end();
} else {
Serial.println("WiFi not connected");
}
}