ESP32CAM QR Code Serial Output syntax question!

Hello All!

My name is Connor, I am a hobbyist electrician slowly making me way into deeper waters as I prepare for pursuing my bachelors in Electrical Engineering. I have a basic understanding of Arduino C, but I am having trouble navigating some of the syntax. On to my situation! I am utilizing an ESP32CAM with a QRCodeReader library (code posted below). Once it receives any valid QRcode, it outputs the "payload" (the string that makes up the QRcode) to the serial monitor and then turns on the LED at Pin 12. My question is: Can I use the serial monitor's output to make the LED at Pin 12 only activate when a certain code or "payload" is received? I.E. ESP reads "1234", Serial Monitor outputs "1234", Pin 12 activates, but if ESP reads "4321" and Serial Monitor outputs "4321" Pin 12 does not activate. The goal is eventually to have 3 separate codes that activate their own LEDs. Thank you in advance for any help!

ESP32QRCodeReader reader(CAMERA_MODEL_AI_THINKER);


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);
       digitalWrite(12, HIGH);
     }
     else
     {
       Serial.print("Invalid: ");
       Serial.println((const char *)qrCodeData.payload);
     }
   }
   vTaskDelay(100 / portTICK_PERIOD_MS);
 }
}


void setup()
{
 Serial.begin(115200);
 Serial.println();
 pinMode(12,OUTPUT);


 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);
}

Hello

You can't use the Serial monitor (what's there is there...) but you can use what is in the qrCodeData.payload variable (what you printed out to the Serial monitor) which seems to be a cString (a null terminated char array) so you can use cStrings functions to do stuff like strcmp(). Have a look at other functions in stdlib.h and string.h.

for example

if (reader.receiveQrCode(&qrCodeData, 100))
   {
     Serial.println("Found QRCode");
     if (qrCodeData.valid)
     {
       Serial.print("Payload: ");
       Serial.println((const char *) qrCodeData.payload);
       if (! strcmp((const char *) qrCodeData.payload, "1234")) { // only turn on LED if we got 1234
         digitalWrite(12, HIGH);
       }
     }
     else
     {
       Serial.print("Invalid: ");
       Serial.println((const char *)qrCodeData.payload);
     }
   }

Thank you so much! This was exactly what I needed and now it works perfectly! I looked at the libraries you referenced and I'm sure I can use it for some other projects on the drawing board.

Glad it helped. Have fun!

Bonjour à tous . S'il vous plait j'ai un problème pour lire le code qr avec mon ESP32CAM model Ai Thinker. En j'utilise la bibliothèque QrCodeReader et lorsque je televerse le code j'ouvre le moniteur série et on m'affiche une érreur disant que "camera not suported"

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.