Im trying to send qr code data to my local host using python need help

HERE IS MY ESP32CAM CODE

#include <Arduino.h>
#include <ESP32QRCodeReader.h>

ESP32QRCodeReader reader(CAMERA_MODEL_AI_THINKER);

void onQrCodeTask(void *pvParameters){
  struct QRCodeData qrCodeData;
  while (true){
    if (reader.receiveQrCode(&qrCodeData, 100)){
      if (qrCodeData.valid){
        Serial.println((const char *)qrCodeData.payload);
      }
    }
    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}

void setup(){
  Serial.begin(115200);
  reader.setup();
  reader.beginOnCore(1);
  xTaskCreate(onQrCodeTask, "onQrCode", 4 * 1024, NULL, 4, NULL);
}

void loop(){
  delay(100);
}

HERE IS MY PYTHON CODE

import serial
import pymysql
import time

connection = pymysql.connect(host='localhost', user='root', password='', database='sims_db')
cursor = connection.cursor()

ser = serial.Serial('COM5', 11500)
time.sleep(2)

data = []
for i in range(50):
    b = ser.readline()
    string_n = b.decode()  
    string = string_n.rstrip()
    print(string)
    data.append(string)

    sql = 'INSERT INTO qrcodes (code, date, time) VALUES(%s, now(), now())'
    
    cursor.execute(sql, b)
    connection.commit()

    time.sleep(0.1)

ser.close()

for line in data:
    print(line)

I Moved your post to a more suitable place.

——

Before even digging into the code - tell us If you just have the serial monitor - do you see what’s needed coming out of the ESP32?

If you do then it’s a Python question…

where did you move it though send link,, and yes my serial monitor is outputting must be an error with python

seems you found it as you could reply :wink:

Here clearly, else you wouldn't have been able to ask that question :wink:

have your python code print what it gets and compare to what you expect

Also why do you have this 50 thingy?

for i in range(50):

suppose to be an array

can you share with us the typical output from the Arduino's Serial line?

The arduino serial monitor only displays what it has read from the QR Code via this line
`Serial.println((const char *)qrCodeData.payload);
but when I close the monitor and open my python IDLE it does not display anything, could it be that I'm using the wrong libraries or maybe it has something to do with the esp32cam module?

so the qrCodeData.payload is just an URL and you terminate the output by a CR LF through println() as a end marker?

yes thats pretty much what i did, on the serial monitor I see the qrcode data, but on IDLE console I do not see anything

I use this kind of Python code to receive command lines from my Arduino

#!/usr/bin/python3

# ============================================
#  KEEP THIS INFORMATION IF YOU USE THIS CODE
#
#  code is placed under the MIT license
#  Copyright (c) 2019 J-M-L For the Arduino Forum
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.
#  ===============================================

import sys, threading, queue, serial

arduinoPort = '/dev/cu.usbserial-0001'
baudRate = 115200

arduino = serial.Serial(arduinoPort, baudrate=baudRate, timeout=.1)
arduinoQueue = queue.Queue()


def listenToArduino(commandQueue):
    message = b''
    while True:
        incoming = arduino.read()
        if (incoming == b'\n'):
            arduinoQueue.put(message.decode("utf-8").strip().upper())
            message = b''
        else:
            if (incoming != b''):
                 message += incoming


def configure():
    arduinoThread = threading.Thread(target=listenToArduino, args=(arduinoQueue,))
    arduinoThread.daemon = True
    arduinoThread.start()


# ---- MAIN CODE -----

configure()
print("Ready to receive input from " + arduinoPort)
currentCommand = ""

while True:
    if not arduinoQueue.empty():
        currentCommand = arduinoQueue.get()
        print("Got [" + currentCommand + "]")

you need to adjust

arduinoPort = '/dev/cu.usbserial-0001'

to match your settings

you can try the code with this Arduino code

unsigned long count = 0;

void setup() {
  Serial.begin(115200); Serial.println();
}

void loop() {
  Serial.print("count=");
  Serial.println(count++);
  delay(random(500, 2001));
}

the idea is to receive the lines in a dedicated thread and store the messages in a queue. the main code will get the messages out of the queue and handle them (here just printing). This way if the handling part is long, you keep listening in the background to what's coming from the Arduino Side and buffer it into the queue

PS/ remove the .upper() if you want to get the original string as it was sent and not all in upper capitals

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