Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Commit

Permalink
Implementaion of ESP32-CAM
Browse files Browse the repository at this point in the history
  • Loading branch information
jomjol committed Sep 3, 2019
1 parent e319a90 commit 4a8e025
Show file tree
Hide file tree
Showing 31 changed files with 1,692 additions and 496 deletions.
80 changes: 80 additions & 0 deletions ESP32-CAM_Server-GitHub/ESP32-CAM_Server-GitHub.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <WiFi.h>
//#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>


#include "arduino.h"
#include "JomjolGitESP32CAM-Server-Class.h"

const char* ssid = "SSID";
const char* password = "Password";
const char* host = "Kamera";

#define LEDPin 4 // Pin für Steuerung LED-Leiste GPIO4
#define INT_LED 2 // Interne LED zum Blinken bei WiFi-Connect at pin GPIO16 (D0).

GitESP32CAMServerLibrary::ESP32CAMServerClass ESP32CAMServer(LEDPin, 10, 50, 16); // Pin, Anzahl LEDs, Brightness, CS

void setup() {
Serial.begin(115200);
Serial.println("gestartet - setup");
WifiReConnect();
OTA_setup();


ESP32CAMServer.setup();
}


void loop() {
ESP32CAMServer.loop();
OTA_loop();
}


void WifiReConnect()
{
pinMode(INT_LED, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(INT_LED, HIGH); // turn the LED on.

if (WiFi.status() != WL_CONNECTED)
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

for (int i=0;i<3;i++){
digitalWrite(INT_LED, LOW); // turn the LED on.
delay(500); // wait
digitalWrite(INT_LED, HIGH); // turn the LED off.
delay(500); // wait
delay(0); //to prevent watchdog firing.
}
}
}


void OTA_setup()
{
ArduinoOTA.setHostname(host);
ArduinoOTA.setPassword(password);
ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); });
ArduinoOTA.begin();
}

void OTA_loop()
{
ArduinoOTA.handle();
}
222 changes: 222 additions & 0 deletions ESP32-CAM_Server-GitHub/JomjolGitESP32CamComm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
#include "JomjolGitESP32CAMComm.h"


// Constructor
GitESP32CAMCommLibrary::ESP32CAMCommClass::ESP32CAMCommClass(int _pin, int _num_led, int _brightness, int _CS, WebServer *_server)
{
// LEDBandComm = new LEDBandCommLibrary::LEDBandCommClass(_pin, _num_led, _brightness);
light = new Adafruit_NeoPixel(_num_led, _pin, NEO_GRBW + NEO_KHZ800);
brightness = _brightness;

server = _server;
Serial.println("ArduCAMCommClass - Setup Done");
}

size_t GitESP32CAMCommLibrary::ESP32CAMCommClass::jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
jpg_chunking_t *j = (jpg_chunking_t *)arg;
// WiFiClient client = server->client();
if(!index){
j->len = 0;
}
// if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
if(j->client.write((const char *)data, len) != ESP_OK){
return 0;
}
j->len += len;
return len;
}


void GitESP32CAMCommLibrary::ESP32CAMCommClass::camCaptureMITHeader() {
WiFiClient client = server->client();
if (!client.connected()) return;
String response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: image/jpeg\r\n";
// response += "Content-len: " + String(len) + "\r\n\r\n";
response += "Content-Disposition: inline; filename=capture.jpg\r\n\r\n";
server->sendContent(response);


////////////////////////////////////////////////////////////////////
camera_fb_t * fb = NULL;
esp_err_t res = ESP_OK;
int64_t fr_start = esp_timer_get_time();

fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
// httpd_resp_send_500(req);
// return ESP_FAIL;
return;
}

uint8_t * out_buf;
bool s;
bool detected = false;
int face_id = 0;
size_t fb_len = 0;

/*
if(fb->format == PIXFORMAT_JPEG){
fb_len = fb->len;
client.write((const char *)fb->buf, fb->len);
// res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
}
*/
if(fb->format == PIXFORMAT_JPEG){
fb_len = fb->len;
Serial.println("Pixformat = jpeg");
client.write((const char *)fb->buf, fb->len);
// res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
}
else {
Serial.println("Pixformat geht in else");
jpg_chunking_t jchunk = {client, 0};
// res = frame2jpg_cb(fb, 80, std::bind(&ESP32CAMCommClass::jpg_encode_stream, this), &jchunk)?ESP_OK:ESP_FAIL;
res = frame2jpg_cb(fb, 80, this->jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
// httpd_resp_send_chunk(req, NULL, 0);
fb_len = jchunk.len;
}



esp_camera_fb_return(fb);
int64_t fr_end = esp_timer_get_time();
Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
////////////////////////////////////////////////////////////


}



void GitESP32CAMCommLibrary::ESP32CAMCommClass::serverCaptureMITHeader() {

Serial.println(F("CAM Capturing"));
int total_time = 0;
total_time = millis();
total_time = millis() - total_time;
Serial.print(F("capture total_time used (in miliseconds):"));
Serial.println(total_time, DEC);
total_time = 0;
Serial.println(F("CAM Capture Done."));
total_time = millis();
camCaptureMITHeader();
total_time = millis() - total_time;
Serial.print(F("send total_time used (in miliseconds):"));
Serial.println(total_time, DEC);
Serial.println(F("CAM send Done."));
}

void GitESP32CAMCommLibrary::ESP32CAMCommClass::serverCaptureWithLigth()
{
LightOn();
Serial.println("Licht an");

Serial.println("Groesse auf 5 gesetzt ql=5");
delay(5000);
serverCaptureMITHeader();

LightOff();
Serial.println("Licht aus");
}

void GitESP32CAMCommLibrary::ESP32CAMCommClass::serverCapture()
{
serverCaptureMITHeader();
}


void GitESP32CAMCommLibrary::ESP32CAMCommClass::setup()
{
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 = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}

#if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
#endif

// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}

sensor_t * s = esp_camera_sensor_get();
//initial sensors are flipped vertically and colors are a bit saturated
if (s->id.PID == OV3660_PID) {
s->set_vflip(s, 1);//flip it back
s->set_brightness(s, 1);//up the blightness just a bit
s->set_saturation(s, -2);//lower the saturation
}
//drop down frame size for higher initial frame rate
// s->set_framesize(s, FRAMESIZE_QVGA);
s->set_framesize(s, FRAMESIZE_SVGA);

#if defined(CAMERA_MODEL_M5STACK_WIDE)
s->set_vflip(s, 1);
s->set_hmirror(s, 1);
#endif

// LEDBandComm->setup(); // funktioniert am Anfang nicht !!!!
light->setBrightness(brightness);
light->begin();
light->show(); // Initialize all pixels to 'off'
// colorWipe(strip.Color(0, 0, 0, 0), 0); // Aus

}


void GitESP32CAMCommLibrary::ESP32CAMCommClass::LightOn()
{
Serial.println("LEDBandCommClass - LightOn");
light->show(); // Initialize all pixels to 'off'
colorWipe(light->Color(255, 255, 255, 255), 0); // White
}

void GitESP32CAMCommLibrary::ESP32CAMCommClass::LightOff()
{
Serial.println("LEDBandCommClass - LightOff");
colorWipe(light->Color(0, 0, 0, 0), 0); // Aus
}


void GitESP32CAMCommLibrary::ESP32CAMCommClass::colorWipe(uint32_t c, uint8_t wait)
{
for(uint16_t i=0; i<light->numPixels(); i++) {
light->setPixelColor(i, c);
light->show();
delay(wait);
}
}
57 changes: 57 additions & 0 deletions ESP32-CAM_Server-GitHub/JomjolGitESP32CamComm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef _ESP32CAM_Comm_Library
#define _ESP32CAM_Comm_Library

#include <WebServer.h>
#include "esp_camera.h"

// Select camera model
//#define CAMERA_MODEL_WROVER_KIT
//#define CAMERA_MODEL_ESP_EYE
//#define CAMERA_MODEL_M5STACK_PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE
#define CAMERA_MODEL_AI_THINKER

#include "camera_pins.h"

#include <Adafruit_NeoPixel.h>

typedef struct {
WiFiClient client;
size_t len;
} jpg_chunking_t;


namespace GitESP32CAMCommLibrary
{
class ESP32CAMCommClass
{
private:


WebServer *server;
// ArduCAM *myCAM;

void serverCaptureMITHeader();
void camCaptureMITHeader();
static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len);


Adafruit_NeoPixel *light;
int brightness;
void colorWipe(uint32_t c, uint8_t wait);



public:
ESP32CAMCommClass(int _pin, int _num_led, int _brightness, int _CS, WebServer *_server);
virtual void setup();

void serverCaptureWithLigth();
void serverCapture();

void LightOn();
void LightOff();

};
}
#endif
Loading

0 comments on commit 4a8e025

Please sign in to comment.