- [//*
- SDWebServer - Example WebServer with SD Card backend for esp8266
- Have a FAT Formatted SD Card connected to the SPI port of the ESP8266
- The web root is the SD Card root folder
- File extensions with more than 3 charecters are not supported by the SD Library
- File Names longer than 8 charecters will be truncated by the SD library, so keep filenames shorter
- index.htm is the default index (works on subfolders as well)
- upload the contents of SdRoot to the root of the SDcard and access the editor by going to http://esp8266sd.local/edit
- */
- /* include(s) and define (S) to store in SD card */
- #include "esp_camera.h"
- #include "esp_timer.h"
- #include "img_converters.h"
- #include "Arduino.h"
- #include "fb_gfx.h"
- #include "fd_forward.h"
- #include "fr_forward.h"
- #include "FS.h" /* SD Card ESP32 */
- #include "SD_MMC.h" /* SD Card ESP32 */
- #include "soc/soc.h" /* Disable brownour problems */
- #include "soc/rtc_cntl_reg.h" /* Disable brownour problems */
- #include "dl_lib.h"
- #include "driver/rtc_io.h"
- #include <EEPROM.h> /* read and write from flash memory */
- #define EEPROM_SIZE 1 /* define the number of bytes you want to access */
- /* Pin definition for 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
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <WebServer.h>
- #include <ESPmDNS.h>
- #include <SPI.h>
- #include <SD.h>
- #define DBG_OUTPUT_PORT Serial
- const char* ssid = "sj tech";
- const char* password = "26356813";
- 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
- WebServer server(80);
- static bool hasSD = false;
- File uploadFile;
- void CamInToSD()
- {
- int pictureNumber = 0;
- {
- WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); /* disable brownout detector */
- //Serial.setDebugOutput(true);
- //Serial.println();
- 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;
- if (psramFound())
- {
- config.frame_size = FRAMESIZE_UXGA; /* FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA */
- config.jpeg_quality = 10;
- config.fb_count = 2;
- }
- else
- {
- config.frame_size = FRAMESIZE_SVGA;
- config.jpeg_quality = 12;
- config.fb_count = 1;
- }
- esp_err_t err = esp_camera_init(&config); /* Init Camera */
- if (err != ESP_OK)
- {
- Serial.printf("Camera init failed with error 0x%x", err);
- return;
- }
- Serial.println("Starting SD Card");
- if (!SD_MMC.begin()) {
- Serial.println("SD Card Mount Failed");
- return;
- }
- uint8_t cardType = SD_MMC.cardType();
- if (cardType == CARD_NONE) {
- Serial.println("No SD Card attached");
- return;
- }
- camera_fb_t * fb = NULL;
- fb = esp_camera_fb_get(); /* Take Picture with Camera */
- if (!fb) {
- Serial.println("Camera capture failed");
- return;
- }
- EEPROM.begin(EEPROM_SIZE); /* initialize EEPROM with predefined size */
- pictureNumber = EEPROM.read(0) + 1;
- String path = "/picture" + String(pictureNumber) + ".jpg"; /* Path where new picture will be saved in SD Card */
- 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();
- // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
- // pinMode(4, OUTPUT);
- // digitalWrite(4, LOW);
- // rtc_gpio_hold_en(GPIO_NUM_4);
- //
- // delay(2000);
- // Serial.println("Going to sleep now");
- // delay(2000);
- // esp_deep_sleep_start();
- // Serial.println("This will never be printed");
- }
- }
- void returnOK()
- {
- server.send(200, "text/plain", ""); /* http status code 200 returns OK */
- }
- void returnFail(String msg)
- {
- server.send(500, "text/plain", msg + "\r\n"); /* http status code 500 for some unexpected error in server */
- }
- bool loadFromSdCard(String path)
- {
- CamInToSD();
- String dataType = "text/plain";
- if (path.endsWith("/")) {
- path += "index.htm";
- }
- if (path.endsWith(".src")) {
- path = path.substring(0, path.lastIndexOf("."));
- } else if (path.endsWith(".htm")) {
- dataType = "text/html";
- } else if (path.endsWith(".css")) {
- dataType = "text/css";
- } else if (path.endsWith(".js")) {
- dataType = "application/javascript";
- } else if (path.endsWith(".png")) {
- dataType = "image/png";
- } else if (path.endsWith(".gif")) {
- dataType = "image/gif";
- } else if (path.endsWith(".jpg")) { /* for image data type */
- dataType = "image/jpeg";
- } else if (path.endsWith(".ico")) {
- dataType = "image/x-icon";
- } else if (path.endsWith(".xml")) {
- dataType = "text/xml";
- } else if (path.endsWith(".pdf")) {
- dataType = "application/pdf";
- } else if (path.endsWith(".zip")) {
- dataType = "application/zip";
- }
- File dataFile = SD.open(path.c_str());
- if (dataFile.isDirectory()) {
- path += "/index.htm";
- dataType = "text/html";
- dataFile = SD.open(path.c_str());
- }
- if (!dataFile) {
- return false;
- }
- if (server.hasArg("download")) {
- dataType = "application/octet-stream"; /* for any type of data type */
- }
- if (server.streamFile(dataFile, dataType) != dataFile.size()) {
- DBG_OUTPUT_PORT.println("Sent less data than expected!");
- }
- dataFile.close();
- return true; /* closing, reading file from SD card */
- }
- void handleFileUpload() {
- if (server.uri() != host) {
- return;
- }
- HTTPUpload& upload = server.upload();
- if (upload.status == UPLOAD_FILE_START) {
- if (SD.exists((char *)upload.filename.c_str())) {
- SD.remove((char *)upload.filename.c_str());
- }
- uploadFile = SD.open(upload.filename.c_str(), FILE_WRITE);
- DBG_OUTPUT_PORT.print("Upload: START, filename: "); DBG_OUTPUT_PORT.println(upload.filename);
- } else if (upload.status == UPLOAD_FILE_WRITE) {
- if (uploadFile) {
- uploadFile.write(upload.buf, upload.currentSize);
- }
- DBG_OUTPUT_PORT.print("Upload: WRITE, Bytes: "); DBG_OUTPUT_PORT.println(upload.currentSize);
- } else if (upload.status == UPLOAD_FILE_END) {
- if (uploadFile) {
- uploadFile.close();
- }
- DBG_OUTPUT_PORT.print("Upload: END, Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
- }
- }
- void deleteRecursive(String path) {
- File file = SD.open((char *)path.c_str());
- if (!file.isDirectory()) {
- file.close();
- SD.remove((char *)path.c_str());
- return;
- }
- file.rewindDirectory();
- while (true) {
- File entry = file.openNextFile();
- if (!entry) {
- break;
- }
- String entryPath = path + "/" + entry.name();
- if (entry.isDirectory()) {
- entry.close();
- deleteRecursive(entryPath);
- } else {
- entry.close();
- SD.remove((char *)entryPath.c_str());
- }
- yield();
- }
- SD.rmdir((char *)path.c_str());
- file.close();
- }
- void handleDelete() {
- if (server.args() == 0) {
- return returnFail("BAD ARGS");
- }
- String path = server.arg(0);
- if (path == "/" || !SD.exists((char *)path.c_str())) {
- returnFail("BAD PATH");
- return;
- }
- deleteRecursive(path);
- returnOK();
- }
- void handleCreate() {
- if (server.args() == 0) {
- return returnFail("BAD ARGS");
- }
- String path = server.arg(0);
- if (path == "/" || SD.exists((char *)path.c_str())) {
- returnFail("BAD PATH");
- return;
- }
- if (path.indexOf('.') > 0) {
- File file = SD.open((char *)path.c_str(), FILE_WRITE);
- if (file) {
- file.write(0);
- file.close();
- }
- } else {
- SD.mkdir((char *)path.c_str());
- }
- returnOK();
- }
- void printDirectory() {
- if (!server.hasArg("dir"))
- {
- return returnFail("BAD ARGS");
- }
- String path = server.arg("dir");
- if (path != "/" && !SD.exists((char *)path.c_str()))
- {
- return returnFail("BAD PATH");
- }
- File dir = SD.open((char *)path.c_str());
- path = String();
- if (!dir.isDirectory())
- {
- dir.close();
- return returnFail("NOT DIR");
- }
- dir.rewindDirectory();
- server.setContentLength(CONTENT_LENGTH_UNKNOWN);
- server.send(200, "text/json", host); //"" changed to host
- WiFiClient client = server.client();
- server.sendContent("[");
- for (int cnt = 0; true; ++cnt)
- {
- File entry = dir.openNextFile();
- if (!entry)
- {
- break;
- }
- String output;
- if (cnt > 0)
- {
- output = ',';
- }
- output += "{\"type\":\"";
- output += (entry.isDirectory()) ? "dir" : "file";
- output += "\",\"name\":\"";
- output += entry.name();
- output += "\"";
- output += "}";
- server.sendContent(output);
- entry.close();
- }
- server.sendContent("]");
- dir.close();
- }
- void handleNotFound() {
- if (hasSD && loadFromSdCard(server.uri())) {
- return;
- }
- String message = "SDCARD Not Detected\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET) ? "GET" : "POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i = 0; i < server.args(); i++) {
- message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- DBG_OUTPUT_PORT.print(message);
- }
- ////////////////////////////////////////////// setup code //////////////////////////////////////////////////
- void setup(void) {
- DBG_OUTPUT_PORT.begin(115200);
- DBG_OUTPUT_PORT.setDebugOutput(true);
- DBG_OUTPUT_PORT.print("\n");
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- DBG_OUTPUT_PORT.print("Connecting to ");
- DBG_OUTPUT_PORT.println(ssid);
- // Wait for connection
- uint8_t i = 0;
- while (WiFi.status() != WL_CONNECTED && i++ < 20) {//wait 10 seconds
- delay(500);
- }
- if (i == 21) {
- DBG_OUTPUT_PORT.print("Could not connect to");
- DBG_OUTPUT_PORT.println(ssid);
- while (1) {
- delay(500);
- }
- }
- DBG_OUTPUT_PORT.print("Connected! IP address: ");
- DBG_OUTPUT_PORT.println(WiFi.localIP());
- if (MDNS.begin(host)) {
- MDNS.addService("https", "tcp", 80);
- DBG_OUTPUT_PORT.println("MDNS responder started");
- DBG_OUTPUT_PORT.print("You can now connect to https://");
- DBG_OUTPUT_PORT.println(host);
- //DBG_OUTPUT_PORT.println(".local");
- }
- server.on("/list", HTTP_GET, printDirectory);
- server.on("/edit", HTTP_DELETE, handleDelete);
- server.on("/edit", HTTP_PUT, handleCreate);
- server.on("/edit", HTTP_POST, []() {
- returnOK();
- }, handleFileUpload
- );
- server.onNotFound(handleNotFound);
- server.begin();
- DBG_OUTPUT_PORT.println("HTTP server started");
- if (SD_MMC.begin()) {
- DBG_OUTPUT_PORT.println("SD Card initialized.");
- hasSD = true;
- }
- }
- ////////////////////////////////////////// loop code ////////////////////////////////////////////////
- void loop(void)
- {
- server.handleClient();
- }]
- Hi,
- 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).
- 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
- loadFromSdCard(server.uri())
- function is returning FALSE value ...
- code is attached..
- please reply as soon as possible..
esp32cam..problem with server.uri() function ...code attached
esp32cam..problem with server.uri() function ...code attached
Who is online
Users browsing this forum: Bing [Bot] and 108 guests