I have a problem, with the following components:
-ESP32cam ai-thinker (using camera and SD)
-CDM324 with amplifier (IF pin connected to GPIO_13)
The idea is that the CDM324 sensor sends a frequency to esp32, which can process, and according to the result, take the photo and store in the SD. And at the same time, be an FTPserver.
It would be two tasks, which cannot be done only in the loop ...
So for this, I have enabled in the "setup" instance, a task in core 0, so that it receives the frequency, processes it, takes a photo and saves it in the SD, while in the "loop", the FTPserver
The problem is that before any movement (frequency) that the sensor captures, the esp32 takes a photo, and returns any value (noise). I realize this because as soon as the amplifier LED turns on, take the picture.
The strange thing is that if I remove the ftpserver function, and the whole previous process I move it to the loop, it works, but using the deep sleep function and wake up (and I suppose it works because we tried it on the street).
Should I assign the pin as input? How do I name it? GPIO_NUM_13 or only 13? To capture the frequency, I use the pulsein, which, as I said, in Arduino and in ESP32 (task in setup or loop) works.
This is the code (which works) to read the frequency (thanks Markus Kantola) if i use only one core and won't use FTPserver:
Code: Select all
void setup() {
pinMode(13, INPUT); // or GPIO_NUM_13?
...
}
void loop() {
//read input 5 times with 10mS delay between reads
// in worst case it takes 1.1 sec ... usually less
a = pulseIn(13, HIGH, 50000); // or GPIO_NUM_13?
b = pulseIn(13, LOW, 50000);
cc = mm/(a+b);
Serial.println (String(cc));
delay (viive);
a = pulseIn(13, HIGH, 50000); // wait 50000uS for pulse (0,05 sec)
b = pulseIn(13, LOW, 50000); // to complete (over 10Hz needed)
dd = mm/(a+b); // as 0.05 LOW + 0.05 HIGH = 0.1 sec
Serial.println (String(dd));
delay (viive);
a = pulseIn(13, HIGH, 50000);
b = pulseIn(13, LOW, 50000);
ee = mm/(a+b);
Serial.println (String(ee));
delay (viive);
a = pulseIn(13, HIGH, 50000);
b = pulseIn(13, LOW, 50000);
ff = mm/(a+b);
Serial.println (String(ff));
delay (viive);
a = pulseIn(13, HIGH, 50000);
b = pulseIn(13, LOW, 50000);
gg = mm/(a+b);
Serial.println (String(gg));
}
Code: Select all
TaskHandle_t Task_1;
void Task( void * parameter )
{
for (;;){
//read input 5 times with 10mS delay between reads
// in worst case it takes 1.1 sec ... usually less
a = pulseIn(13, HIGH, 50000); // or GPIO_NUM_13?
b = pulseIn(13, LOW, 50000);
cc = mm/(a+b);
Serial.println (String(cc));
delay (viive);
a = pulseIn(13, HIGH, 50000); // wait 50000uS for pulse (0,05 sec)
b = pulseIn(13, LOW, 50000); // to complete (over 10Hz needed)
dd = mm/(a+b); // as 0.05 LOW + 0.05 HIGH = 0.1 sec
Serial.println (String(dd));
delay (viive);
a = pulseIn(13, HIGH, 50000);
b = pulseIn(13, LOW, 50000);
ee = mm/(a+b);
Serial.println (String(ee));
delay (viive);
a = pulseIn(13, HIGH, 50000);
b = pulseIn(13, LOW, 50000);
ff = mm/(a+b);
Serial.println (String(ff));
delay (viive);
a = pulseIn(13, HIGH, 50000);
b = pulseIn(13, LOW, 50000);
gg = mm/(a+b);
Serial.println (String(gg));
..... // frequency processing to convert to speed (ex)
ex = c+0.5; // ex is a value processed with frequencies
if (ex > 20) {
camera_fb_t * fb = NULL;
// Take Picture with Camera
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
}
// initialize EEPROM with predefined size
EEPROM.begin(EEPROM_SIZE);
pictureNumber = EEPROM.read(0) + 1;
// Path where new picture will be saved in SD Card
String path = "/picture" + String(pictureNumber) + "_" + String(ex) +"kmh.jpg";
fs::FS &fs = SD_MMC;
Serial.printf("Picture file name: %s\n", path.c_str());
File file = fs.open(path.c_str(), FILE_WRITE);
if(!file){
Serial.println("Failed to open file in writing mode");
}
else {
file.write(fb->buf, fb->len); // payload (image), payload length
Serial.printf("Saved file to path: %s\n", path.c_str());
EEPROM.write(0, pictureNumber);
EEPROM.commit();
}
file.close();
esp_camera_fb_return(fb);
Serial.println("\t\tEn nucleo ->" + String(xPortGetCoreID())); //to visualize which core is performing this task
delay(1000);
}
}
vTaskDelay(10);
}
void setup() {
//all serial and setting initializations
pinMode(13, INPUT); //or GPIO_NUM_13?
....
xTaskCreatePinnedToCore(
Task,
"Task",
10000,
NULL,
1,
&Task_1,
0);
delay(500);
//other initializations
....
}
void loop()
{
if (WiFi.status() != WL_CONNECTED) {
init_wifi();
Serial.println("***** WiFi reconnect *****");
}
wakeup = millis();
if (wakeup - last_wakeup > (10 * 60 * 1000) ) { // 10 minutes
last_wakeup = millis();
}
ftpSrv.handleFTP();
delay(10);
}
Another query, the function of deep sleep and awakening, only activates nucleus 1? or both? Can I define which core to wake up?
Please, for a year now I am with this project and I think I am in the last stage to finish it. I would greatly appreciate any help.