esp32cam..problem with server.uri() function ...code attached

iPankaj
Posts: 1
Joined: Fri Jul 05, 2019 7:19 am

esp32cam..problem with server.uri() function ...code attached

Postby iPankaj » Fri Jul 05, 2019 7:26 am

  1. [//*
  2.   SDWebServer - Example WebServer with SD Card backend for esp8266
  3.  
  4.   Have a FAT Formatted SD Card connected to the SPI port of the ESP8266
  5.   The web root is the SD Card root folder
  6.   File extensions with more than 3 charecters are not supported by the SD Library
  7.   File Names longer than 8 charecters will be truncated by the SD library, so keep filenames shorter
  8.   index.htm is the default index (works on subfolders as well)
  9.  
  10.   upload the contents of SdRoot to the root of the SDcard and access the editor by going to http://esp8266sd.local/edit
  11.  
  12. */
  13.  
  14. /* include(s) and define (S) to store in SD card */
  15. #include "esp_camera.h"
  16. #include "esp_timer.h"
  17. #include "img_converters.h"
  18. #include "Arduino.h"
  19. #include "fb_gfx.h"
  20. #include "fd_forward.h"
  21. #include "fr_forward.h"
  22. #include "FS.h"                /* SD Card ESP32 */
  23. #include "SD_MMC.h"            /* SD Card ESP32 */
  24. #include "soc/soc.h"           /* Disable brownour problems */
  25. #include "soc/rtc_cntl_reg.h"  /* Disable brownour problems */
  26. #include "dl_lib.h"
  27. #include "driver/rtc_io.h"
  28. #include <EEPROM.h>            /* read and write from flash memory */
  29. #define EEPROM_SIZE 1    /* define the number of bytes you want to access */
  30.  
  31. /* Pin definition for CAMERA_MODEL_AI_THINKER */
  32. #define PWDN_GPIO_NUM     32
  33. #define RESET_GPIO_NUM    -1
  34. #define XCLK_GPIO_NUM      0
  35. #define SIOD_GPIO_NUM     26
  36. #define SIOC_GPIO_NUM     27
  37.  
  38. #define Y9_GPIO_NUM       35
  39. #define Y8_GPIO_NUM       34
  40. #define Y7_GPIO_NUM       39
  41. #define Y6_GPIO_NUM       36
  42. #define Y5_GPIO_NUM       21
  43. #define Y4_GPIO_NUM       19
  44. #define Y3_GPIO_NUM       18
  45. #define Y2_GPIO_NUM        5
  46. #define VSYNC_GPIO_NUM    25
  47. #define HREF_GPIO_NUM     23
  48. #define PCLK_GPIO_NUM     22
  49.  
  50. #include <WiFi.h>
  51. #include <WiFiClient.h>
  52. #include <WebServer.h>
  53. #include <ESPmDNS.h>
  54. #include <SPI.h>
  55. #include <SD.h>
  56.  
  57. #define DBG_OUTPUT_PORT Serial
  58.  
  59. const char* ssid = "sj tech";
  60. const char* password = "26356813";
  61. const char* host = "https://colonymaintenance.com/77/IOT/Camera/api.php?action=image&device_id=";               //https://colonymaintenance.com/77/IOT/Camera/api.php?action=image
  62.  
  63. WebServer server(80);
  64.  
  65. static bool hasSD = false;
  66. File uploadFile;
  67.  
  68. void CamInToSD()
  69. {
  70.   int pictureNumber = 0;
  71.   {
  72.     WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); /* disable brownout detector */
  73.     //Serial.setDebugOutput(true);
  74.     //Serial.println();
  75.     camera_config_t config;
  76.     config.ledc_channel = LEDC_CHANNEL_0;
  77.     config.ledc_timer = LEDC_TIMER_0;
  78.     config.pin_d0 = Y2_GPIO_NUM;
  79.     config.pin_d1 = Y3_GPIO_NUM;
  80.     config.pin_d2 = Y4_GPIO_NUM;
  81.     config.pin_d3 = Y5_GPIO_NUM;
  82.     config.pin_d4 = Y6_GPIO_NUM;
  83.     config.pin_d5 = Y7_GPIO_NUM;
  84.     config.pin_d6 = Y8_GPIO_NUM;
  85.     config.pin_d7 = Y9_GPIO_NUM;
  86.     config.pin_xclk = XCLK_GPIO_NUM;
  87.     config.pin_pclk = PCLK_GPIO_NUM;
  88.     config.pin_vsync = VSYNC_GPIO_NUM;
  89.     config.pin_href = HREF_GPIO_NUM;
  90.     config.pin_sscb_sda = SIOD_GPIO_NUM;
  91.     config.pin_sscb_scl = SIOC_GPIO_NUM;
  92.     config.pin_pwdn = PWDN_GPIO_NUM;
  93.     config.pin_reset = RESET_GPIO_NUM;
  94.     config.xclk_freq_hz = 20000000;
  95.     config.pixel_format = PIXFORMAT_JPEG;
  96.  
  97.     if (psramFound())
  98.     {
  99.       config.frame_size = FRAMESIZE_UXGA;  /* FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA */
  100.       config.jpeg_quality = 10;
  101.       config.fb_count = 2;
  102.     }
  103.     else
  104.     {
  105.       config.frame_size = FRAMESIZE_SVGA;
  106.       config.jpeg_quality = 12;
  107.       config.fb_count = 1;    
  108.     }
  109.    
  110.     esp_err_t err = esp_camera_init(&config);  /* Init Camera */
  111.     if (err != ESP_OK)
  112.     {
  113.       Serial.printf("Camera init failed with error 0x%x", err);
  114.       return;
  115.     }
  116.  
  117.     Serial.println("Starting SD Card");
  118.     if (!SD_MMC.begin()) {
  119.       Serial.println("SD Card Mount Failed");
  120.       return;
  121.     }
  122.  
  123.     uint8_t cardType = SD_MMC.cardType();
  124.     if (cardType == CARD_NONE) {
  125.       Serial.println("No SD Card attached");
  126.       return;
  127.     }
  128.  
  129.     camera_fb_t * fb = NULL;
  130.     fb = esp_camera_fb_get(); /*  Take Picture with Camera */
  131.     if (!fb) {
  132.       Serial.println("Camera capture failed");
  133.       return;
  134.     }
  135.     EEPROM.begin(EEPROM_SIZE);   /* initialize EEPROM with predefined size */
  136.     pictureNumber = EEPROM.read(0) + 1;
  137.    
  138.     String path = "/picture" + String(pictureNumber) + ".jpg"; /* Path where new picture will be saved in SD Card */
  139.     fs::FS &fs = SD_MMC;
  140.     Serial.printf("Picture file name: %s\n", path.c_str());
  141.     File file = fs.open(path.c_str(), FILE_WRITE);
  142.     if (!file)
  143.     {
  144.       Serial.println("Failed to open file in writing mode");
  145.     }
  146.     else
  147.     {
  148.       file.write(fb->buf, fb->len); // payload (image), payload length
  149.       Serial.printf("Saved file to path: %s\n", path.c_str());
  150.       EEPROM.write(0, pictureNumber);
  151.       EEPROM.commit();
  152.     }
  153.     file.close();
  154.  
  155.  
  156.     // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  157. //    pinMode(4, OUTPUT);
  158. //    digitalWrite(4, LOW);
  159. //    rtc_gpio_hold_en(GPIO_NUM_4);
  160. //    
  161. //    delay(2000);
  162. //    Serial.println("Going to sleep now");
  163. //    delay(2000);
  164. //    esp_deep_sleep_start();
  165. //    Serial.println("This will never be printed");
  166.   }
  167. }
  168.  
  169. void returnOK()
  170. {
  171.   server.send(200, "text/plain", "");       /* http status code 200 returns OK */
  172. }
  173.  
  174. void returnFail(String msg)
  175. {
  176.   server.send(500, "text/plain", msg + "\r\n");  /* http status code 500 for some unexpected error in server */
  177. }
  178.  
  179. bool loadFromSdCard(String path)
  180. {
  181.    CamInToSD();
  182.   String dataType = "text/plain";
  183.   if (path.endsWith("/")) {
  184.     path += "index.htm";
  185.   }
  186.   if (path.endsWith(".src")) {
  187.     path = path.substring(0, path.lastIndexOf("."));
  188.   } else if (path.endsWith(".htm")) {
  189.     dataType = "text/html";
  190.   } else if (path.endsWith(".css")) {
  191.     dataType = "text/css";
  192.   } else if (path.endsWith(".js")) {
  193.     dataType = "application/javascript";
  194.   } else if (path.endsWith(".png")) {
  195.     dataType = "image/png";
  196.   } else if (path.endsWith(".gif")) {
  197.     dataType = "image/gif";
  198.   } else if (path.endsWith(".jpg")) {       /* for image data type */
  199.     dataType = "image/jpeg";
  200.   } else if (path.endsWith(".ico")) {
  201.     dataType = "image/x-icon";
  202.   } else if (path.endsWith(".xml")) {
  203.     dataType = "text/xml";
  204.   } else if (path.endsWith(".pdf")) {
  205.     dataType = "application/pdf";
  206.   } else if (path.endsWith(".zip")) {
  207.     dataType = "application/zip";
  208.   }
  209.  
  210.   File dataFile = SD.open(path.c_str());
  211.   if (dataFile.isDirectory()) {
  212.     path += "/index.htm";
  213.     dataType = "text/html";
  214.     dataFile = SD.open(path.c_str());
  215.   }
  216.  
  217.   if (!dataFile) {
  218.     return false;
  219.   }
  220.  
  221.   if (server.hasArg("download")) {
  222.     dataType = "application/octet-stream";           /* for any type of data type */
  223.   }
  224.  
  225.   if (server.streamFile(dataFile, dataType) != dataFile.size()) {
  226.     DBG_OUTPUT_PORT.println("Sent less data than expected!");
  227.   }
  228.  
  229.   dataFile.close();
  230.   return true;         /* closing, reading file from SD card */
  231. }
  232.  
  233. void handleFileUpload() {
  234.   if (server.uri() != host) {
  235.     return;
  236.   }
  237.   HTTPUpload& upload = server.upload();
  238.   if (upload.status == UPLOAD_FILE_START) {
  239.     if (SD.exists((char *)upload.filename.c_str())) {
  240.       SD.remove((char *)upload.filename.c_str());
  241.     }
  242.     uploadFile = SD.open(upload.filename.c_str(), FILE_WRITE);
  243.     DBG_OUTPUT_PORT.print("Upload: START, filename: "); DBG_OUTPUT_PORT.println(upload.filename);
  244.   } else if (upload.status == UPLOAD_FILE_WRITE) {
  245.     if (uploadFile) {
  246.       uploadFile.write(upload.buf, upload.currentSize);
  247.     }
  248.     DBG_OUTPUT_PORT.print("Upload: WRITE, Bytes: "); DBG_OUTPUT_PORT.println(upload.currentSize);
  249.   } else if (upload.status == UPLOAD_FILE_END) {
  250.     if (uploadFile) {
  251.       uploadFile.close();
  252.     }
  253.     DBG_OUTPUT_PORT.print("Upload: END, Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
  254.   }
  255. }
  256.  
  257. void deleteRecursive(String path) {
  258.   File file = SD.open((char *)path.c_str());
  259.   if (!file.isDirectory()) {
  260.     file.close();
  261.     SD.remove((char *)path.c_str());
  262.     return;
  263.   }
  264.  
  265.   file.rewindDirectory();
  266.   while (true) {
  267.     File entry = file.openNextFile();
  268.     if (!entry) {
  269.       break;
  270.     }
  271.     String entryPath = path + "/" + entry.name();
  272.     if (entry.isDirectory()) {
  273.       entry.close();
  274.       deleteRecursive(entryPath);
  275.     } else {
  276.       entry.close();
  277.       SD.remove((char *)entryPath.c_str());
  278.     }
  279.     yield();
  280.   }
  281.  
  282.   SD.rmdir((char *)path.c_str());
  283.   file.close();
  284. }
  285.  
  286. void handleDelete() {
  287.   if (server.args() == 0) {
  288.     return returnFail("BAD ARGS");
  289.   }
  290.   String path = server.arg(0);
  291.   if (path == "/" || !SD.exists((char *)path.c_str())) {
  292.     returnFail("BAD PATH");
  293.     return;
  294.   }
  295.   deleteRecursive(path);
  296.   returnOK();
  297. }
  298.  
  299. void handleCreate() {
  300.   if (server.args() == 0) {
  301.     return returnFail("BAD ARGS");
  302.   }
  303.   String path = server.arg(0);
  304.   if (path == "/" || SD.exists((char *)path.c_str())) {
  305.     returnFail("BAD PATH");
  306.     return;
  307.   }
  308.  
  309.   if (path.indexOf('.') > 0) {
  310.     File file = SD.open((char *)path.c_str(), FILE_WRITE);
  311.     if (file) {
  312.       file.write(0);
  313.       file.close();
  314.     }
  315.   } else {
  316.     SD.mkdir((char *)path.c_str());
  317.   }
  318.   returnOK();
  319. }
  320.  
  321. void printDirectory() {
  322.   if (!server.hasArg("dir"))
  323.   {
  324.     return returnFail("BAD ARGS");
  325.   }
  326.   String path = server.arg("dir");
  327.   if (path != "/" && !SD.exists((char *)path.c_str()))
  328.   {
  329.     return returnFail("BAD PATH");
  330.   }
  331.   File dir = SD.open((char *)path.c_str());
  332.   path = String();
  333.   if (!dir.isDirectory())
  334.   {
  335.     dir.close();
  336.     return returnFail("NOT DIR");
  337.   }
  338.   dir.rewindDirectory();
  339.   server.setContentLength(CONTENT_LENGTH_UNKNOWN);
  340.   server.send(200, "text/json", host);   //"" changed to host
  341.   WiFiClient client = server.client();
  342.  
  343.   server.sendContent("[");
  344.   for (int cnt = 0; true; ++cnt)
  345.   {
  346.     File entry = dir.openNextFile();
  347.     if (!entry)
  348.     {
  349.       break;
  350.     }
  351.  
  352.     String output;
  353.     if (cnt > 0)
  354.     {
  355.       output = ',';
  356.     }
  357.  
  358.     output += "{\"type\":\"";
  359.     output += (entry.isDirectory()) ? "dir" : "file";
  360.     output += "\",\"name\":\"";
  361.     output += entry.name();
  362.     output += "\"";
  363.     output += "}";
  364.     server.sendContent(output);
  365.     entry.close();
  366.   }
  367.   server.sendContent("]");
  368.   dir.close();
  369. }
  370.  
  371. void handleNotFound() {
  372.   if (hasSD && loadFromSdCard(server.uri())) {
  373.     return;
  374.   }
  375.   String message = "SDCARD Not Detected\n\n";
  376.   message += "URI: ";
  377.   message += server.uri();
  378.   message += "\nMethod: ";
  379.   message += (server.method() == HTTP_GET) ? "GET" : "POST";
  380.   message += "\nArguments: ";
  381.   message += server.args();
  382.   message += "\n";
  383.   for (uint8_t i = 0; i < server.args(); i++) {
  384.     message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
  385.   }
  386.   server.send(404, "text/plain", message);
  387.   DBG_OUTPUT_PORT.print(message);
  388. }
  389.  
  390. ////////////////////////////////////////////// setup code //////////////////////////////////////////////////
  391.  
  392. void setup(void) {
  393.   DBG_OUTPUT_PORT.begin(115200);
  394.   DBG_OUTPUT_PORT.setDebugOutput(true);
  395.   DBG_OUTPUT_PORT.print("\n");
  396.   WiFi.mode(WIFI_STA);
  397.   WiFi.begin(ssid, password);
  398.   DBG_OUTPUT_PORT.print("Connecting to ");
  399.   DBG_OUTPUT_PORT.println(ssid);
  400.  
  401.   // Wait for connection
  402.   uint8_t i = 0;
  403.   while (WiFi.status() != WL_CONNECTED && i++ < 20) {//wait 10 seconds
  404.     delay(500);
  405.   }
  406.   if (i == 21) {
  407.     DBG_OUTPUT_PORT.print("Could not connect to");
  408.     DBG_OUTPUT_PORT.println(ssid);
  409.     while (1) {
  410.       delay(500);
  411.     }
  412.   }
  413.   DBG_OUTPUT_PORT.print("Connected! IP address: ");
  414.   DBG_OUTPUT_PORT.println(WiFi.localIP());
  415.  
  416.   if (MDNS.begin(host)) {
  417.     MDNS.addService("https", "tcp", 80);
  418.     DBG_OUTPUT_PORT.println("MDNS responder started");
  419.     DBG_OUTPUT_PORT.print("You can now connect to https://");
  420.     DBG_OUTPUT_PORT.println(host);
  421.     //DBG_OUTPUT_PORT.println(".local");
  422.   }
  423.  
  424.  
  425.   server.on("/list", HTTP_GET, printDirectory);
  426.   server.on("/edit", HTTP_DELETE, handleDelete);
  427.   server.on("/edit", HTTP_PUT, handleCreate);
  428.   server.on("/edit", HTTP_POST, []() {
  429.     returnOK();
  430.   }, handleFileUpload
  431.   );
  432.   server.onNotFound(handleNotFound);
  433.  
  434.   server.begin();
  435.   DBG_OUTPUT_PORT.println("HTTP server started");
  436.  
  437.   if (SD_MMC.begin()) {
  438.     DBG_OUTPUT_PORT.println("SD Card initialized.");
  439.     hasSD = true;
  440.   }
  441. }
  442.  
  443. ////////////////////////////////////////// loop code ////////////////////////////////////////////////
  444. void loop(void)
  445. {
  446.   server.handleClient();
  447. }]
  448.  
  449. Hi,
  450. My project is about interfacing the "esp32cam" module with camera and storing the photo clicked in sd card , and then retreiving back the photo from sd card and sending that photo to an API Server (which is basically a php Server).
  451. i am not sure what i am doing wrong with my code, I guess something is wrong with the server.uri() function because in my code
  452. loadFromSdCard(server.uri())
  453. function is returning FALSE value ...
  454.  
  455. code is attached..
  456. please reply as soon as possible..

Who is online

Users browsing this forum: Bing [Bot] and 117 guests