we are making a project that uses this type of scanner to scan QR codes. The MH-ET live scanner only scans/operates when the button beside it pressed. How can we make it constantly on, so that a staff can scan the QR/Barcode, without pressing the button one at a time?
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "1234";
const char* password = "*********";
String apiEndpoint = "http://domain/api/read_qr_kds";
//const unsigned char Passive_buzzer = 2;
String raw_data = "";
bool new_data_available = false;
void setup() {
Serial.begin(9600); // Make sure this matches the baud rate of your QR scanner
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// pinMode(Passive_buzzer, OUTPUT);
}
void loop() {
while(WiFi.status() == WL_CONNECTED){
// Read serial data and construct the raw_data string
while (Serial.available()) {
char input = Serial.read();
// Check if the character is printable before adding it to raw_data
if (isPrintable(input) || input == 'n') {
raw_data += input;
}
// If we receive a newline character, set the flag
if (input == 'n' && raw_data.length() > 1) {
new_data_available = true;
}
}
// If new data is available, process it
if (new_data_available) {
Serial.println(raw_data);
// Encode the raw_data before sending
String encodedData = urlencode(raw_data);
sendQRDataToAPI(encodedData);
raw_data = ""; // Reset raw_data after sending
new_data_available = false; // Reset the flag
}
}
}
void sendQRDataToAPI(String data) {
HTTPClient http;
WiFiClient client;
String link = apiEndpoint + "?raw_data=" + data;
Serial.println(link);
http.begin(client, link);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
Serial.println(httpCode);
String response = http.getString();
Serial.println("API Response: " + response);
// Parse the JSON response
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, response);
if (!error) {
int orderId = doc["qrData"]["OrderID"];
Serial.print("OrderID: ");
Serial.println(orderId);
const char* status = doc["status"];
Serial.print("Status: ");
Serial.println(status);
} else {
Serial.print("JSON deserialization failed: ");
Serial.println(error.c_str());
}
} else {
Serial.print("HTTP Error code: ");
Serial.println(httpCode);
}
http.end();
}
// Encoding the raw_data before sending it to the API
String urlencode(String str) {
String encodedString = "";
char c;
char code0;
char code1;
for (unsigned int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == ' ') {
encodedString += '+';
} else if (isalnum(c)) {
encodedString += c;
} else {
code1 = (c & 0xf) + '0';
if ((c & 0xf) > 9) {
code1 = (c & 0xf) - 10 + 'A';
}
c = (c >> 4) & 0xf;
code0 = c + '0';
if (c > 9) {
code0 = c - 10 + 'A';
}
encodedString += '%';
encodedString += code0;
encodedString += code1;
}
yield();
}
return encodedString;
}
What can I do to my device and waht can I add to my code so it can scan automatically?
New contributor
C Program is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.