Hi, i am new in this field, and i'm trying to build a qrcode scanner project using ESP32-CAM to scan the qrcode and then upload it to MySQL database to do comparison with the existing data. If the data is same, it will print verify, if not, it will print not verify. My problem right now is the code is functioning but the camera unable to scan the qrcode and I am trying to use MySQL connector to upload the data to MySQL, but it keep showing "Connection failed". I am not sure what I had done wrong. I need help. Thank you very much.
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ESP32QRCodeReader.h>
ESP32QRCodeReader reader(CAMERA_MODEL_AI_THINKER);
const char* ssid = "Jie";
const char* password = "987654321";
void onQrCodeTask(void *pvParameters)
{
struct QRCodeData qrCodeData;
while (true)
{
if (reader.receiveQrCode(&qrCodeData, 100))
{
Serial.println("Found QRCode");
if (qrCodeData.valid)
{
Serial.print("Payload: ");
Serial.println((const char *)qrCodeData.payload);
// Send QR code payload to the Node-RED server
WiFiClient client;
HTTPClient http;
if (http.begin(client, serverURL))
{
http.addHeader("Content-Type", "application/json");
// Create a JSON payload with the QR code payload
String jsonPayload = "{\"payload\": \"" + String((const char *)qrCodeData.payload) + "\"}";
int httpResponseCode = http.POST(jsonPayload);
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else
{
Serial.println("Error sending request");
}
http.end();
}
else
{
Serial.println("Error connecting to server");
}
}
else
{
Serial.print("Invalid: ");
Serial.println((const char *)qrCodeData.payload);
}
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected");
reader.setup();
Serial.println("Setup QRCode Reader");
reader.beginOnCore(1);
Serial.println("Begin on Core 1");
xTaskCreate(onQrCodeTask, "onQrCode", 4 * 1024, NULL, 4, NULL);
}
void loop()
{
delay(100);
}