Q) image sending bia bluetooth

Grandfather
Posts: 1
Joined: Mon Jul 26, 2021 10:23 am

Q) image sending bia bluetooth

Postby Grandfather » Mon Jul 26, 2021 10:49 am

hello,

[Bluetooth + microSD = Sending Image - Using Arduino / Storage - Arduino Forum](https://forum.arduino.cc/t/bluetooth-mi ... mage/81195)

im having similar problem above, but im using esp32 board
i sent the image as jpeg using bluetooth Serial function
`SerialBT.write` and `SerialBT.flush`
to smartphone app
and also i saved the same image to the SD card
(using same image buffer)

But the most of the image was gone.. in both method

for example, this image is sent(or saved)
1111111111
1111111111
1111111111
1111111111

but i recieved(or what was saved on sd card is..) like this..
1111111111
1100000000
0000000000
0000000000

half or 3/4 of the image data is going somewhere..

is this software problem? or hardware problem?
i think it caused by 4MB flash (image buffer size was almost 2.5MB when i tested it)

Code: Select all

#include "esp_camera.h"
#define CAMERA_MODEL_AI_THINKER
#include "camera_pins.h"
#include "SD_MMC.h"
#include "Arduino.h"
#include "BluetoothSerial.h"
//#include <EEPROM.h>
//#include "Base64.h"


BluetoothSerial SerialBT;

//RTC_DATA_ATTR static int cbCount = 0;

void initSD(){
  Serial.println("Starting SD Card");  
  if(!SD_MMC.begin()){
    Serial.println("SD Card Mount Failed");
    ESP.restart();
  }else{
    Serial.println("SD Card Mount Succes");
  }
 
  uint8_t cardType = SD_MMC.cardType();
  if(cardType == CARD_NONE){
    Serial.println("No SD Card attached");
    return;
  }
}

void initCamera(){
  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 = 2;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 2;
    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);
    ESP.restart();
  }

}


void initBT(){
  if(!SerialBT.begin("ESP32CAM-CLASSIC-BT")){
    Serial.println("An error occurred initializing Bluetooth");
    ESP.restart();
  }else{
    Serial.println("Bluetooth initialized");
  }
  //Bluetooth를 통한 Serial Port Profile을 시작 -> bluetooth 통신이 이루어질 때마다 실행되는 함수
  SerialBT.register_callback(btCallback);
  
  Serial.println("The device started, now you can pair it with bluetooth");
}





void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
  if(event == ESP_SPP_SRV_OPEN_EVT){  // BT Server가 처음 시작되면 발생하는 Event
    Serial.println("Client Connected!");
    
  }else if(event == ESP_SPP_DATA_IND_EVT){  // BT SPP로 data를 수신하면 발생하는 Event
    // len : Length of data   handle : Information of which is(are) connected
    Serial.printf("ESP_SPP_DATA_IND_EVT len=%d, handle=%d\n\n", param->data_ind.len, param->data_ind.handle);

    // 수신한 Data는 String 형식이므로 이를 int로 바꾸어 주는 작업
    String stringRead = String(*param->data_ind.data);
    int paramInt = stringRead.toInt() - 48;
    Serial.printf("paramInt: %d\n", paramInt); 
    setCameraParam(paramInt);
  }
}



void setCameraParam(int paramInt){
  sensor_t *s = esp_camera_sensor_get();
  switch(paramInt){
    case 5:{
      s->set_framesize(s, FRAMESIZE_UXGA);
      
      pinMode(12,INPUT);
      for(int i=0;i<10;i++){
        if(digitalRead(12)==HIGH){
          capture();
          delay(500);
        }
      }
      
      break;
    }
    case 4:
      s->set_framesize(s, FRAMESIZE_UXGA);
    break;

    case 3:
      s->set_framesize(s, FRAMESIZE_SXGA);
    break;

    case 2:
      s->set_framesize(s, FRAMESIZE_XGA);
    break;

    case 1:
      s->set_framesize(s, FRAMESIZE_SVGA);
    break;

    case 0:
    default:
      s->set_framesize(s, FRAMESIZE_VGA);
    break;
  }

  capture();
}

void capture(){
  camera_fb_t *fb = NULL;
  esp_err_t res = ESP_OK;
  fb = esp_camera_fb_get();
  if(!fb){
    esp_camera_fb_return(fb);
    return;
  }

  if(fb->format != PIXFORMAT_JPEG){
    return;
  }

 
  writeSerialBT(fb);
  saveSD(fb);
  esp_camera_fb_return(fb);
}

void writeSerialBT(camera_fb_t *fb){
  SerialBT.write(fb->buf, fb->len);
  SerialBT.flush();
}

void saveSD(camera_fb_t *fb){
    fs::FS &fs = SD_MMC;
    
    String path = "/picture" + ((String) cbCount++) +".jpg";
    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");
      Serial.println("Exiting now"); 
      ESP.restart();   
    }
    else {
      file.write(fb->buf, fb->len); 
      Serial.printf("Saved file to path in SD card : %s\n", path.c_str());
    }
    file.close();
}







void setup() {
  Serial.begin(115200);
  initCamera();
  initSD();
  initBT();
}

void loop() { }

Who is online

Users browsing this forum: No registered users and 49 guests