ESP-32 Cam AI Thinker and IR Sensor Program

kash66
Posts: 1
Joined: Fri Mar 11, 2022 9:03 pm

ESP-32 Cam AI Thinker and IR Sensor Program

Postby kash66 » Fri Mar 11, 2022 9:27 pm

Hey guys, I'm new to this forum but have taken Arduino up as a new hobby and am having some trouble with a project and was wondering if anyone could help me. I am programming my ESP 32 Cam through an Arduino and am currently trying to make a program which reads an IR sensor value and if movement is detected, it takes a pic and uploads it to firebase databases via WIFI. So far, I've got it to take a picture and upload the base 64 code to the database. But, am having trouble reading the analog value of the IR sensor. I am aware many of these GPIO pins are used for WIFI and the SD card. So, I am using pin 13 and have enabled the SD_MMC feature 1-bit mode. Although, when ever I try to read pin 13, it is always reading 4095. Also another important thing is I am using board manager software ESP 32 1.0.4 due to a compatibility issue with uploading to the firebase database. Although, when I do change the board manager software to ESP 32 1.0.6, the pin always reads 0 instead. Any help is appreciated, Thanks in advance. Note: In my code, I have a while loop to test the IR sensor readings, not apart of real code.
  1. const char* ssid = "WIFINAME";
  2. const char* password = "PASSWORD";
  3.  
  4. String FIREBASE_HOST = "www....";
  5. String FIREBASE_AUTH = "....";
  6.  
  7.  
  8. #include "FirebaseESP32.h"
  9. FirebaseData firebaseData;
  10. #include <WiFi.h>
  11. #include "soc/soc.h"
  12. #include "soc/rtc_cntl_reg.h"
  13. #include "Base64.h"
  14. #include "esp_camera.h"
  15. #include "ArduinoJson.h"
  16. #include "FS.h"
  17. #include "SD_MMC.h"
  18. #include "SPI.h"
  19. #include "driver/rtc_io.h"
  20.  
  21.  
  22.  
  23. // WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
  24. //            or another board which has PSRAM enabled
  25.  
  26.  
  27. //CAMERA_MODEL_AI_THINKER
  28.  
  29. #define PWDN_GPIO_NUM     32
  30. #define RESET_GPIO_NUM    -1
  31. #define XCLK_GPIO_NUM      0
  32. #define SIOD_GPIO_NUM     26
  33. #define SIOC_GPIO_NUM     27
  34. #define Y9_GPIO_NUM       35
  35. #define Y8_GPIO_NUM       34
  36. #define Y7_GPIO_NUM       39
  37. #define Y6_GPIO_NUM       36
  38. #define Y5_GPIO_NUM       21
  39. #define Y4_GPIO_NUM       19
  40. #define Y3_GPIO_NUM       18
  41. #define Y2_GPIO_NUM        5
  42. #define VSYNC_GPIO_NUM    25
  43. #define HREF_GPIO_NUM     23
  44. #define PCLK_GPIO_NUM     22
  45.  
  46.  
  47. void setup() {
  48.   Serial.begin(115200);
  49.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  50.   Serial.setDebugOutput(true);
  51.   Serial.println();
  52.   Serial.println("ssid: " + (String)ssid);
  53.   Serial.println("password: " + (String)password);
  54.   WiFi.begin(ssid, password);
  55.   long int StartTime=millis();
  56.  
  57.   while (WiFi.status() != WL_CONNECTED) {
  58.       delay(500);
  59.       if ((StartTime+10000) < millis()) break;
  60.   }
  61.  
  62.   if (WiFi.status() == WL_CONNECTED) {
  63.     char* apssid = "ESP32-CAM";
  64.     char* appassword = "12345678";         //AP password require at least 8 characters.
  65.     Serial.println("");
  66.     Serial.print("Camera Ready! Use 'http://");
  67.     Serial.print(WiFi.localIP());
  68.     Serial.println("' to connect");
  69.     WiFi.softAP((WiFi.localIP().toString()+"_"+(String)apssid).c_str(), appassword);            
  70.   }
  71.  
  72.   else {
  73.     Serial.println("Connection failed");
  74.     return;
  75.   }
  76.  
  77.   camera_config_t config;
  78.   config.ledc_channel = LEDC_CHANNEL_0;
  79.   config.ledc_timer = LEDC_TIMER_0;
  80.   config.pin_d0 = Y2_GPIO_NUM;
  81.   config.pin_d1 = Y3_GPIO_NUM;
  82.   config.pin_d2 = Y4_GPIO_NUM;
  83.   config.pin_d3 = Y5_GPIO_NUM;
  84.   config.pin_d4 = Y6_GPIO_NUM;
  85.   config.pin_d5 = Y7_GPIO_NUM;
  86.   config.pin_d6 = Y8_GPIO_NUM;
  87.   config.pin_d7 = Y9_GPIO_NUM;
  88.   config.pin_xclk = XCLK_GPIO_NUM;
  89.   config.pin_pclk = PCLK_GPIO_NUM;
  90.   config.pin_vsync = VSYNC_GPIO_NUM;
  91.   config.pin_href = HREF_GPIO_NUM;
  92.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  93.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  94.   config.pin_pwdn = PWDN_GPIO_NUM;
  95.   config.pin_reset = RESET_GPIO_NUM;
  96.   config.xclk_freq_hz = 20000000;
  97.   config.pixel_format = PIXFORMAT_JPEG;
  98.  
  99.   //init with high specs to pre-allocate larger buffers
  100.  
  101.   if(psramFound()){
  102.     config.frame_size = FRAMESIZE_UXGA;
  103.     config.jpeg_quality = 10;  //0-63 lower number means higher quality
  104.     config.fb_count = 2;
  105.   }
  106.   else {
  107.     config.frame_size = FRAMESIZE_SVGA;
  108.     config.jpeg_quality = 12;  //0-63 lower number means higher quality
  109.     config.fb_count = 1;
  110.   }
  111.  
  112.   // camera init
  113.   esp_err_t err = esp_camera_init(&config);
  114.   if (err != ESP_OK) {
  115.     Serial.printf("Camera init failed with error 0x%x", err);
  116.     delay(1000);
  117.     ESP.restart();
  118.   }
  119.  
  120.   //drop down frame size for higher initial frame rate
  121.   sensor_t * s = esp_camera_sensor_get();
  122.   s->set_framesize(s, FRAMESIZE_QQVGA);  // VGA|CIF|QVGA|HQVGA|QQVGA   ( UXGA? SXGA? XGA? SVGA? )
  123.   Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  124.   Firebase.reconnectWiFi(true);
  125.   Firebase.setMaxRetry(firebaseData, 3);
  126.   Firebase.setMaxErrorQueue(firebaseData, 30);
  127.   Firebase.enableClassicRequest(firebaseData, true);
  128.  
  129.   pinMode(13,INPUT);
  130.    
  131.   if (!SD_MMC.begin("/sdcard", true)){
  132.     Serial.println("Failed to mount SD card");
  133.     return;
  134.   }
  135.  
  136.   // Check for an SD card
  137.   uint8_t cardType = SD_MMC.cardType();
  138.   if (cardType == CARD_NONE){
  139.     Serial.println("No SD card attached");
  140.     return;
  141.   }
  142.  
  143.   pinMode(13,INPUT);
  144.  
  145.   while(1){
  146.     float IR = analogRead(13);
  147.     delay(1000);
  148.     Serial.println(IR);
  149.   }
  150.  
  151.   while(1){
  152.     //digitalWrite(4, HIGH);
  153.     //delay(1000);
  154.     //digitalWrite(4, LOW);
  155.     //float movement = analogRead(13);
  156.     //Serial.println(movement);
  157.    
  158.     FirebaseJson json1;
  159.     //json.setJsonData("{\"photo\":\"" + Photo2Base64() + "\"}");
  160.     String jsonData = "{\"photo\":\"" + Photo2Base64() + "\"}";
  161.   // json1.add("photo",5);
  162.  
  163.     //Serial.println(json["photo"]);
  164.     String photoPath = "/esp32-cam";
  165.     if (Firebase.pushJSON(firebaseData, photoPath,jsonData)) {
  166.       Serial.println(firebaseData.dataPath());
  167.       Serial.println(firebaseData.pushName());
  168.       Serial.println(firebaseData.dataPath() + "/"+ firebaseData.pushName());
  169.     }
  170.     else {
  171.       Serial.println(firebaseData.errorReason());
  172.     }
  173.     delay(10000);
  174.   }
  175. }
  176.  
  177. void loop() {
  178.   delay(10000);
  179. }
  180.  
  181. String Photo2Base64() {
  182.     camera_fb_t * fb = NULL;
  183.     fb = esp_camera_fb_get();  
  184.     if(!fb) {
  185.       Serial.println("Camera capture failed");
  186.       return "";
  187.     }
  188.    
  189.     String imageFile = "data:image/jpeg;base64,";
  190.     char *input = (char *)fb->buf;
  191.     char output[base64_enc_len(3)];
  192.     for (int i=0;i<fb->len;i++) {
  193.       base64_encode(output, (input++), 3);
  194.       if (i%3==0) imageFile += urlencode(String(output));
  195.     }
  196.     esp_camera_fb_return(fb);
  197.     return imageFile;
  198. }
  199.  
  200. String urlencode(String str){
  201.     String encodedString="";
  202.     char c;
  203.     char code0;
  204.     char code1;
  205.     char code2;
  206.  
  207.     for (int i =0; i < str.length(); i++){
  208.       c=str.charAt(i);
  209.       if (c == ' '){
  210.         encodedString+= '+';
  211.       } else if (isalnum(c)){
  212.         encodedString+=c;
  213.       } else{
  214.         code1=(c & 0xf)+'0';
  215.         if ((c & 0xf) >9){
  216.             code1=(c & 0xf) - 10 + 'A';
  217.         }
  218.         c=(c>>4)&0xf;
  219.         code0=c+'0';
  220.        
  221.         if (c > 9){
  222.             code0=c - 10 + 'A';
  223.         }
  224.  
  225.         code2='\0';
  226.         encodedString+='%';
  227.         encodedString+=code0;
  228.         encodedString+=code1;
  229.         //encodedString+=code2;
  230.       }
  231.       yield();
  232.     }
  233.     return encodedString;
  234. }
Last edited by kash66 on Wed Mar 16, 2022 12:03 am, edited 1 time in total.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP-32 Cam AI Thinker and IR Sensor Program

Postby lbernstone » Sat Mar 12, 2022 6:27 am

ADC2 cannot be used while WiFi is active. (https://docs.espressif.com/projects/esp ... imitations)
You can see which pins are available for ADC1 use on the pinmap. (https://raw.githubusercontent.com/espre ... layout.png)

alanesq
Posts: 86
Joined: Thu Dec 14, 2017 8:38 pm

Re: ESP-32 Cam AI Thinker and IR Sensor Program

Postby alanesq » Sat Mar 12, 2022 8:46 am

Hi,

The pins available for use on the esp32cam are:
13 free (used by sd card but free if using 1 bit mode)
12 free (must be low at boot, used by sd card but free if using 1 bit mode)
14 used by sd card (usable is SPI clock?)
2 used by sd card (usable as SPI MISO?)
15 used by sd card (usable as SPI CS?)
1 serial - output only?
3 serial - input only?
4 has the illumination/flash led on it - led could be removed and use as output?
33 onboard led - use as output?
more info here: https://github.com/raphaelbs/esp32-cam- ... n-notes.md

so it looks like adc is not available :-(

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 79 guests