Hey,
I m trying to code an ESP32-CAM to send photos to a MyPhP data base.
I found an old code that did exactly that but it seems to not work because of the "ledc" migration
I don t understand how to "rewrite" those lines to make it work again
Thanks
/*
ESP32-CAM Save a captured photo(Base64) to MySQL.
Author : ChungYi Fu (Kaohsiung, Taiwan) 2019-9-28 19:00
https://www.facebook.com/francefu
Library
https://github.com/ChuckBell/MySQL_Connector_Arduino
*/
const char* ssid = "my-wifi";
const char* password = "my-pw";
IPAddress server_addr(my-ip-with",");
int port = 3306;
char dbUser[] = "root";
char dbPassword[] = "";
String databaseName = "botanicam_db";
String tableName = "images";
String columnName = "data"; //Data Type: TEXT, MEDIUMTEXT...
#include <WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
WiFiClient client;
MySQL_Connection conn((Client *)&client);
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "Base64.h"
#include "esp_camera.h"
// WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
// or another board which has PSRAM enabled
//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
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println("");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
long int StartTime=millis();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if ((StartTime+10000) < millis()) break;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.print("Camera Ready! Use 'http://");
Serial.print(WiFi.localIP());
Serial.println("' to connect");
Serial.println("");
}
else {
Serial.println("Connection failed");
delay(1000);
ESP.restart();
return;
}
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
//init with high specs to pre-allocate larger buffers
if(psramFound()){
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10; //0-63 lower number means higher quality
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12; //0-63 lower number means higher quality
config.fb_count = 1;
}
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
delay(1000);
ESP.restart();
}
//drop down frame size for higher initial frame rate
sensor_t * s = esp_camera_sensor_get();
s->set_framesize(s, FRAMESIZE_VGA); // VGA|CIF|QVGA|HQVGA|QQVGA
SaveCapaturedPhoto2mySQL();
}
void loop() {
delay(10000);
}
void SaveCapaturedPhoto2mySQL() {
Serial.println("Connect to mySQL");
if (conn.connect(server_addr, port, dbUser, dbPassword)) {
Serial.println("Connection successful");
camera_fb_t * fb = NULL;
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
delay(1000);
ESP.restart();
return;
}
char *input = (char *)fb->buf;
char output[base64_enc_len(3)];
String imageFile = "data:image/jpeg;base64,";
for (int i=0;i<fb->len;i++) {
base64_encode(output, (input++), 3);
if (i%3==0) imageFile += urlencode(String(output));
}
esp_camera_fb_return(fb);
Serial.println("Image size = " + String(imageFile.length()));
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
String INSERT_SQL = "INSERT INTO "+databaseName+"."+tableName+" ("+columnName+") VALUES ('"+imageFile+"')";
cur_mem->execute(INSERT_SQL.c_str()); // How to modify the code to upload a big image? (UXGA|SXGA|XGA|SVG)
delete cur_mem;
Serial.println("Data stored!");
Serial.println("");
}
else {
ledcAttach(4, 3, 5000, 8);
ledcWrite(3,10);
delay(200);
ledcWrite(3,0);
delay(200);
ledcDetach(3);
Serial.println("Connected to " + String(server_addr) + " failed.");
}
client.stop();
}
//https://www.arduino.cc/reference/en/libraries/urlencode/
String urlencode(String str) {
const char *msg = str.c_str();
const char *hex = "0123456789ABCDEF";
String encodedMsg = "";
while (*msg != '\0') {
if (('a' <= *msg && *msg <= 'z') || ('A' <= *msg && *msg <= 'Z') || ('0' <= *msg && *msg <= '9') || *msg == '-' || *msg == '_' || *msg == '.' || *msg == '~') {
encodedMsg += *msg;
} else {
encodedMsg += '%';
encodedMsg += hex[(unsigned char)*msg >> 4];
encodedMsg += hex[*msg & 0xf];
}
msg++;
}
return encodedMsg;
}
ERROR MESSAGES :
C:\Users\grego\Documents\Arduino\esp32cam\ESP32-CAM_MySQL\ESP32-CAM_MySQL.ino: In function 'void SaveCapaturedPhoto2mySQL()':
C:\Users\grego\Documents\Arduino\esp32cam\ESP32-CAM_MySQL\ESP32-CAM_MySQL.ino:174:15: error: too many arguments to function 'bool ledcAttach(uint8_t, uint32_t, uint8_t)'
174 | ledcAttach(4, 3, 5000, 8);
| ~~~~^
In file included from C:\Users\grego\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.1.1\cores\esp32/esp32-hal.h:95,
from C:\Users\grego\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.1.1\cores\esp32/Arduino.h:36,
from C:\Users\grego\AppData\Local\Temp\arduino\sketches\D65BA56BA023BD046D0810624901CBAA\sketch\ESP32-CAM_MySQL.ino.cpp:1:
C:\Users\grego\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.1.1\cores\esp32/esp32-hal-ledc.h:87:6: note: declared here
87 | bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution);
| ^~~~~~~~~~
exit status 1
Compilation error: too many arguments to function 'bool ledcAttach(uint8_t, uint32_t, uint8_t)'
MIGRATION INFO :
https://docs.espressif.com/projects/arduino-esp32/en/latest/migration_guides/2.x_to_3.0.html#ledc