ESP32 CAM QR Code Scanner
ESP32 CAM QR Code Scanner
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>> 01 ESP32 Cam QR Code Scanner
/*
* Reference :
* - ESP32-CAM QR Code Reader (off-line) : https://www.youtube.com/watch?
v=ULZL37YqJc8
* - ESP32QRCodeReader_Page :
https://github.com/fustyles/Arduino/tree/master/ESP32-CAM_QRCode_Recognition/
ESP32QRCodeReader_Page
*
* The source of the "quirc" library I shared on this project:
https://github.com/fustyles/Arduino/tree/master/ESP32-CAM_QRCode_Recognition/
ESP32QRCodeReader_Page
*/
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 19
#define Y4_GPIO_NUM 18
#define Y3_GPIO_NUM 5
#define Y2_GPIO_NUM 4
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#elif defined(CAMERA_MODEL_ESP_EYE)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 4
#define SIOD_GPIO_NUM 18
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 36
#define Y8_GPIO_NUM 37
#define Y7_GPIO_NUM 38
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 35
#define Y4_GPIO_NUM 14
#define Y3_GPIO_NUM 13
#define Y2_GPIO_NUM 34
#define VSYNC_GPIO_NUM 5
#define HREF_GPIO_NUM 27
#define PCLK_GPIO_NUM 25
#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
#define XCLK_GPIO_NUM 27
#define SIOD_GPIO_NUM 25
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 19
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 18
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 5
#define Y4_GPIO_NUM 34
#define Y3_GPIO_NUM 35
#define Y2_GPIO_NUM 32
#define VSYNC_GPIO_NUM 22
#define HREF_GPIO_NUM 26
#define PCLK_GPIO_NUM 21
#elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
#define XCLK_GPIO_NUM 27
#define SIOD_GPIO_NUM 25
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 19
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 18
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 5
#define Y4_GPIO_NUM 34
#define Y3_GPIO_NUM 35
#define Y2_GPIO_NUM 17
#define VSYNC_GPIO_NUM 22
#define HREF_GPIO_NUM 26
#define PCLK_GPIO_NUM 21
#elif defined(CAMERA_MODEL_AI_THINKER)
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#else
#error "Camera model not selected"
#endif
/* ======================================== */
/* ________________________________________________________________________________
VOID SETTUP() */
void setup() {
// put your setup code here, to run once:
#if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
#endif
sensor_t * s = esp_camera_sensor_get();
s->set_framesize(s, FRAMESIZE_QVGA);
/* ________________________________________________________________________________
The function to be executed by "QRCodeReader_Task" */
// This function is to instruct the camera to take or capture a QR Code image, then
it is processed and translated into text.
void QRCodeReader( void * pvParameters ){
/* ---------------------------------------- */
Serial.println("QRCodeReader is ready.");
Serial.print("QRCodeReader running on core ");
Serial.println(xPortGetCoreID());
Serial.println();
/* ---------------------------------------- */
fb = esp_camera_fb_get();
if (!fb)
{
Serial.println("Camera capture failed");
continue;
}
if (err){
Serial.println("Decoding FAILED");
QRCodeResult = "Decoding FAILED";
} else {
Serial.printf("Decoding successful:\n");
dumpData(&data);
}
Serial.println();
}
esp_camera_fb_return(fb);
fb = NULL;
image = NULL;
quirc_destroy(q);
}
/* ---------------------------------------- */
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
Function to display the results of reading the QR Code on the serial monitor. */
void dumpData(const struct quirc_data *data)
{
Serial.printf("Version: %d\n", data->version);
Serial.printf("ECC level: %c\n", "MLHQ"[data->ecc_level]);
Serial.printf("Mask: %d\n", data->mask);
Serial.printf("Length: %d\n", data->payload_len);
Serial.printf("Payload: %s\n", data->payload);
pinMode(LED_Blue, OUTPUT);
pinMode(LED_Green, OUTPUT);
###################################################################################
##############
###################################################################################
##############
###################################################################################
##############
###################################################################################
##############
###################################################################################
##############
###################################################################################
##############
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>> 02 ESP32 Cam QR Code Scanner Stream Web Server SM (Station Mode)
/*
* Reference :
* - ESP32-CAM QR Code Reader (off-line) : https://www.youtube.com/watch?
v=ULZL37YqJc8
* - ESP32QRCodeReader_Page :
https://github.com/fustyles/Arduino/tree/master/ESP32-CAM_QRCode_Recognition/
ESP32QRCodeReader_Page
* - Copying strings to the clipboard using pure Javascript :
https://techoverflow.net/2018/03/30/copying-strings-to-the-clipboard-using-pure-
javascript/
* - Multitasking on ESP32 with Arduino and FreeRTOS :
https://savjee.be/2020/01/multitasking-esp32-arduino-freertos/
* - How TO - Range Sliders :
https://www.w3schools.com/howto/howto_js_rangeslider.asp
*
* The source of the "quirc" library I shared on this project:
https://github.com/fustyles/Arduino/tree/master/ESP32-CAM_QRCode_Recognition/
ESP32QRCodeReader_Page
*/
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
////////////////
// In this project, I recommend using a sufficient power supply for the ESP32 Cam.
Because in this project in addition to the camera, the LEDs on the ESP32 Cam board
are also lit, //
// added to light the external LEDs, namely the green and blue LEDs. You can find
more detailed information about the power consumption of the ESP32 Cam on the
internet. //
// I say this because initially there were many errors in this project, such as the
LED on the ESP32 Cam not working properly, the ESP32 Cam restarting continuously,
//
// the camera could not display on web pages and so on. After I double-checked, it
turned out that it was due to a lack of power supply.
//
// So in this project I used a 5V 2A adapter power supply. Using that adapter, the
project went well.
//
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
////////////////
// I realized that the code in this project could have been simplified or improved.
But because of my limited knowledge of arduino code, so I can't do it yet.
//
// I'm also still learning.
//
// So if you can simplify or improve the Arduino code of this project, please let
me know, so I can try it and share it.
//
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
////////////////
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 19
#define Y4_GPIO_NUM 18
#define Y3_GPIO_NUM 5
#define Y2_GPIO_NUM 4
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#elif defined(CAMERA_MODEL_ESP_EYE)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 4
#define SIOD_GPIO_NUM 18
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 36
#define Y8_GPIO_NUM 37
#define Y7_GPIO_NUM 38
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 35
#define Y4_GPIO_NUM 14
#define Y3_GPIO_NUM 13
#define Y2_GPIO_NUM 34
#define VSYNC_GPIO_NUM 5
#define HREF_GPIO_NUM 27
#define PCLK_GPIO_NUM 25
#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
#define XCLK_GPIO_NUM 27
#define SIOD_GPIO_NUM 25
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 19
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 18
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 5
#define Y4_GPIO_NUM 34
#define Y3_GPIO_NUM 35
#define Y2_GPIO_NUM 32
#define VSYNC_GPIO_NUM 22
#define HREF_GPIO_NUM 26
#define PCLK_GPIO_NUM 21
#elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
#define XCLK_GPIO_NUM 27
#define SIOD_GPIO_NUM 25
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 19
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 18
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 5
#define Y4_GPIO_NUM 34
#define Y3_GPIO_NUM 35
#define Y2_GPIO_NUM 17
#define VSYNC_GPIO_NUM 22
#define HREF_GPIO_NUM 26
#define PCLK_GPIO_NUM 21
#elif defined(CAMERA_MODEL_AI_THINKER)
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#else
#error "Camera model not selected"
#endif
/* ======================================== */
// LEDs GPIO
#define LED_OnBoard 4
#define LED_Green 12
#define LED_Blue 13
/* ======================================== */
bool ws_run = false;
int wsLive_val = 0;
int last_wsLive_val;
byte get_wsLive_interval = 0;
bool get_wsLive_val = true;
/* ======================================== */
/* ======================================== */
#define PART_BOUNDARY "123456789000000000000987654321"
static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary="
PART_BOUNDARY;
static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\
r\n\r\n";
/* ======================================== */
<head>
<style>
body { font-family: Arial; text-align: center; margin:0px auto; padding-top:
10px;}
/* ----------------------------------- Slider */
.slidecontainer {
width: 100%;
}
.slider {
-webkit-appearance: none;
width: 50%;
height: 10px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: #04AA6D;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 20px;
height: 20px;
border-radius: 50%;
background: #04AA6D;
cursor: pointer;
}
/* ----------------------------------- */
</style>
</head>
<body>
<br><br>
<div class="slidecontainer">
<span style="font-size:15;">LED Flash </span>
<input type="range" min="0" max="20" value="0" class="slider" id="mySlider">
</div>
<br>
<br>
<script>
/* ----------------------------------- Calls the video stream link and
displays it */
window.onload = document.getElementById("vdstream").src =
window.location.href.slice(0, -1) + ":81/stream";
/* ----------------------------------- */
</body>
</html>
)rawliteral";
/* ======================================== */
/* ________________________________________________________________________________
Index handler function to be called during GET or uri request */
static esp_err_t index_handler(httpd_req_t *req){
httpd_resp_set_type(req, "text/html");
return httpd_resp_send(req, (const char *)INDEX_HTML, strlen(INDEX_HTML));
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
stream handler function to be called during GET or uri request. */
static esp_err_t stream_handler(httpd_req_t *req){
ws_run = true;
vTaskDelete(QRCodeReader_Task);
Serial.print("stream_handler running on core ");
Serial.println(xPortGetCoreID());
camera_fb_t * fb = NULL;
esp_err_t res = ESP_OK;
size_t _jpg_buf_len = 0;
uint8_t * _jpg_buf = NULL;
char * part_buf[64];
if (err){
QRCodeResult = "Decoding FAILED";
Serial.println(QRCodeResult);
} else {
Serial.printf("Decoding successful:\n");
dumpData(&data);
}
Serial.println();
}
image = NULL;
quirc_destroy(q);
wsLive_val++;
if (wsLive_val > 999) wsLive_val = 0;
}
/* ---------------------------------------- */
return res;
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
cmd handler function to be called during GET or uri request. */
static esp_err_t cmd_handler(httpd_req_t *req){
char* buf;
size_t buf_len;
char variable[32] = {0,};
buf_len = httpd_req_get_url_query_len(req) + 1;
if (buf_len > 1) {
buf = (char*)malloc(buf_len);
if(!buf){
httpd_resp_send_500(req);
return ESP_FAIL;
}
if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
if (httpd_query_key_value(buf, "go", variable, sizeof(variable)) == ESP_OK) {
} else {
free(buf);
httpd_resp_send_404(req);
return ESP_FAIL;
}
} else {
free(buf);
httpd_resp_send_404(req);
return ESP_FAIL;
}
free(buf);
} else {
httpd_resp_send_404(req);
return ESP_FAIL;
}
int res = 0;
if(res){
return httpd_resp_send_500(req);
}
/* ________________________________________________________________________________
qrcoderslt handler function to be called during GET or uri request. */
static esp_err_t qrcoderslt_handler(httpd_req_t *req){
if (QRCodeResult != "Decoding FAILED") QRCodeResultSend = QRCodeResult;
httpd_resp_send(req, QRCodeResultSend.c_str(), HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
Subroutine for starting the web server / startCameraServer. */
void startCameraWebServer(){
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.server_port = 80;
httpd_uri_t index_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = index_handler,
.user_ctx = NULL
};
httpd_uri_t cmd_uri = {
.uri = "/action",
.method = HTTP_GET,
.handler = cmd_handler,
.user_ctx = NULL
};
httpd_uri_t qrcoderslt_uri = {
.uri = "/getqrcodeval",
.method = HTTP_GET,
.handler = qrcoderslt_handler,
.user_ctx = NULL
};
httpd_uri_t stream_uri = {
.uri = "/stream",
.method = HTTP_GET,
.handler = stream_handler,
.user_ctx = NULL
};
config.server_port += 1;
config.ctrl_port += 1;
if (httpd_start(&stream_httpd, &config) == ESP_OK) {
httpd_register_uri_handler(stream_httpd, &stream_uri);
}
Serial.println();
Serial.println("Camera Server started successfully");
Serial.print("Camera Stream Ready! Go to: http://");
Serial.println(WiFi.localIP());
Serial.println();
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
VOID SETUP() */
void setup() {
// put your setup code here, to run once:
pinMode(LED_OnBoard, OUTPUT);
pinMode(LED_Green, OUTPUT);
pinMode(LED_Blue, OUTPUT);
#if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
#endif
sensor_t * s = esp_camera_sensor_get();
s->set_framesize(s, FRAMESIZE_QVGA);
void loop() {
// put your main code here, to run repeatedly:
if (ws_run == true) {
if (get_wsLive_val == true) {
last_wsLive_val = wsLive_val;
get_wsLive_val = false;
}
get_wsLive_interval++;
if (get_wsLive_interval > 2) {
get_wsLive_interval = 0;
get_wsLive_val = true;
if (wsLive_val == last_wsLive_val) {
ws_run = false;
last_wsLive_val = 0;
createTaskQRCodeReader();
}
}
}
}
/* ---------------------------------------- */
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
create "QRCodeReader_Task" using the xTaskCreatePinnedToCore() function */
void createTaskQRCodeReader() {
xTaskCreatePinnedToCore(
QRCodeReader, /* Task function. */
"QRCodeReader_Task", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&QRCodeReader_Task, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
QRCodeReader() */
void QRCodeReader( void * pvParameters ){
Serial.print("QRCodeReader running on core ");
Serial.println(xPortGetCoreID());
while(!ws_run){
camera_fb_t * fb = NULL;
q = quirc_new();
if (q == NULL){
Serial.print("can't create quirc object\r\n");
continue;
}
fb = esp_camera_fb_get();
if (!fb)
{
Serial.println("Camera capture failed (QRCodeReader())");
continue;
}
if (err){
QRCodeResult = "Decoding FAILED";
Serial.println(QRCodeResult);
} else {
Serial.printf("Decoding successful:\n");
dumpData(&data);
}
Serial.println();
}
esp_camera_fb_return(fb);
fb = NULL;
image = NULL;
quirc_destroy(q);
}
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
Function to display the results of reading the QR Code on the serial monitor. */
void dumpData(const struct quirc_data *data)
{
Serial.printf("-Version: %d\n", data->version);
Serial.printf("-ECC level: %c\n", "MLHQ"[data->ecc_level]);
Serial.printf("-Mask: %d\n", data->mask);
Serial.printf("-Length: %d\n", data->payload_len);
Serial.printf("-Payload: %s\n", data->payload);
/* ________________________________________________________________________________
Subroutine to execute commands based on the results of reading the QR Code. */
void cmd_execution() {
if (QRCodeResult == "LED BLUE ON") digitalWrite(LED_Blue, HIGH);
if (QRCodeResult == "LED BLUE OFF") digitalWrite(LED_Blue, LOW);
if (QRCodeResult == "LED GREEN ON") digitalWrite(LED_Green, HIGH);
if (QRCodeResult == "LED GREEN OFF") digitalWrite(LED_Green, LOW);
}
/* ________________________________________________________________________________
*/
/* ________________________________________________________________________________
String function to process the data command */
// I got this from : https://www.electroniclinic.com/reyax-lora-based-multiple-
sensors-monitoring-using-arduino/
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
###################################################################################
##############
###################################################################################
##############
###################################################################################
##############
###################################################################################
##############
###################################################################################
##############
###################################################################################
##############
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>> 03 ESP32 Cam QR Code Scanner Stream Web Server AM (Access Point Mode)
/* Additional code for Access Point Mode. */
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
/* ======================================== */
Serial.println(local_ip);