RE: ESP32-CAM startStream
Hi amithlon,
ich habe deine "Mini-Cam-FTP-Timer-Web.ino" mal so weit vergewaltigt, das der Upload funktioniert und "etwas" tut
Code:
#include <WiFi.h>
#include <WiFiClient.h>
#include "esp_camera.h"
#include "mywifi.h"
#define DEBUG
#define DEBUG_FTP false // true
#define DEBUG_CAM false
void startCameraServer();
// PIR-Cam ist das Modul mit PIR und OLED, M5Cam ist die kleine M5Stack-Cam, diese mit ESP32 Devkit ohne PSRAM compilieren.
//#define PIR_CAM // für Modul mit PIR
//#define M5_CAM // für M5Stack-Cam
//#define LED_CAM // für AI-Thinker Modul mit LEDC_CHANNEL_0
#define CAMERA_MODEL_AI_THINKER
#include "camera_pins.h"
IPAddress local_IP(192, 168, 101, 72);
IPAddress gateway(192, 168, 101, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(192, 168, 101, 1); //optional
#define BUTTON 34 // L aktiv
#define PIR_OUT 33 // H aktiv
//const char* ssid = in mywifi.h
//const char* password = in mywifi.h
// DeepSleep Timer
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 30 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
void print_wakeup_reason(void);
// FTP
#define FTP_USER micha
#define FTP_PASS amiga
IPAddress ftpserver( 192, 168, 0, 6 );
uint16_t ftpport = 21;
String ftp_pfad = "ftp/ESP32";
#define WAKE_TIMER 1
#define WAKE_PIR 2
uint16_t wake_reason = 0; // Grund für das Aufwecken
#define FTP_ANZAHL 4
String ftp_comm[] = {"USER micha","PASS amiga","SYST","Type I"};
String ftp_ret[] = {"331","230","215","200"};
WiFiClient ftpclient;
WiFiClient dclient;
String uploadFTP(String filename);
String ftpReceive(void);
bool ftpCommand(uint8_t index);
esp_err_t camera_init(void);
esp_err_t camera_capture(void);
void setSensordaten(void);
void grabPicture(String filename);
//#########################################################
void setup(void)
{
Serial.begin(115200);
Serial.setDebugOutput(true);
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;
}
// 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;
}
setSensordaten();
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_special_effect(s, 2); // 0 = No Effect; 2 = Grayscale
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS))
{
Serial.println("STA Failed to configure");
}
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Wifi connect");
// 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());
}
//----------------------------------------------------------
void loop(void)
{
String filename = "Bild.jpg";
String dateiname = " ";
Serial.println(filename);
uploadFTP(filename);
Serial.println("Going to sleep now");
delay(100);
Serial.flush();
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
btStop();
#if defined (PIR_CAM)
esp_sleep_enable_ext0_wakeup((gpio_num_t )BUTTON, LOW);
esp_sleep_enable_ext0_wakeup((gpio_num_t )PIR_OUT, HIGH);
pinMode(PWDN_GPIO_NUM,PULLUP); // Kamera aus
digitalWrite(PWDN_GPIO_NUM, HIGH);
#endif
Serial.println("ESP going deep sleep");
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
//#########################################################
//-------------------- Sleep Tools ------------------------
//#########################################################
void print_wakeup_reason()
{
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0:
Serial.println("Wakeup caused by external signal using RTC_IO");
wake_reason = WAKE_PIR;
break;
case ESP_SLEEP_WAKEUP_EXT1:
Serial.println("Wakeup caused by external signal using RTC_CNTL");
break;
case ESP_SLEEP_WAKEUP_TIMER:
Serial.println("Wakeup caused by timer");
wake_reason = WAKE_TIMER;
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
Serial.println("Wakeup caused by touchpad");
break;
case ESP_SLEEP_WAKEUP_ULP:
Serial.println("Wakeup caused by ULP program");
break;
default:
Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason);
break;
}
}
//#########################################################
//--------------------- CAM Tools -------------------------
//#########################################################
void setSensordaten()
{
size_t res = 0;
sensor_t * s = esp_camera_sensor_get();
// res = s->set_framesize(s, (framesize_t)val); // Framesize FRAMESIZE_(QQVGA, HQVGA, QVGQ, CIF, VGA, SVGA, XGA, SXGA, UXGA)
res = s->set_quality(s, 10); // JPEG Quality (10...63) 10
res = s->set_contrast(s, 0); // Kontrast (-2...+2) 0
res = s->set_brightness(s, 0); // Helligkeit (-2...+2) 0
res = s->set_saturation(s, 0); // Farbsättigung (-2...+2) 0
res = s->set_gainceiling(s, (gainceiling_t) GAINCEILING_2X); // Verstärkung GAINCEILING_(2x, 4x, 8x, 16x, 32x, 64x, 128x) 2x
res = s->set_colorbar(s, 0); // Farbbalken (off/on) off
res = s->set_whitebal(s, 1); // Weißbalance (off/on) on
res = s->set_gain_ctrl(s, 1); // AGC (off/on) on
res = s->set_exposure_ctrl(s, 1); // AEC (off/on) on
// res = s->set_hmirror(s, val); // H-Mirror (off/on)
// res = s->set_vflip(s, val); // V-Flip (off/on)
// res = s->set_awb_gain(s, val); // Verstärkung AWB
// res = s->set_agc_gain(s, val); // Verstärkung AGC
// res = s->set_aec_value(s, val); // Wert AEC (0...1200)
// res = s->set_aec2(s, val); // Wert AEC2
res = s->set_dcw(s, 1); // Downsize (off/on) on
res = s->set_bpc(s, 0); // (off/on) off
res = s->set_wpc(s, 1); // (off/on) on
res = s->set_raw_gma(s, 1); // RAW-Gamma (off/on) on
res = s->set_lenc(s, 1); // Linsenkorr. (off/on) on
// res = s->set_special_effect(s, val); // Special Effekt
// res = s->set_wb_mode(s, val); // WB Mode (Auto/Sunny/..)
res = s->set_ae_level(s, 0); // AE Level (-2...+2) 0
}
//#########################################################
//--------------------- FTP Tools -------------------------
//#########################################################
String uploadFTP(String filename)
{
for (uint8_t i = 0; i < 3; i++)
{
camera_fb_t * fb = esp_camera_fb_get();
esp_camera_fb_return(fb);
delay(10);
}
camera_fb_t * fb = esp_camera_fb_get();
String returnText = "";
String meldung = "";
bool flag_ftp_ok = true;
String bildnummer = "00000" + String(bootCount, DEC);
bildnummer = bildnummer.substring(bildnummer.length() - 5);
//String dateiname = String(wake_reason, DEC) + "_" + BILDNAME + bildnummer + ".jpg";
String dateiname = String(wake_reason, DEC) + "_" + "1_" + bildnummer + ".jpg";
if (ftpclient.connect(ftpserver, ftpport)) // 21 = FTP server
{
Serial.println(F("Command connected"));
}
else
{
Serial.println(F("Command connection failed"));
return "FTP Error";
}
returnText = ftpReceive();
meldung = returnText.substring(0,3); // erste 3 Zeichen
if (DEBUG_FTP) Serial.println("Return: " + meldung);
if (meldung == "220")
{
for (uint8_t i = 0; i < FTP_ANZAHL; i++)
{
if (!ftpCommand(i))
{
flag_ftp_ok = false;
break;
}
}
if (flag_ftp_ok) // alles klar, PASV senden
{
uint8_t werte[6];
uint8_t pos = 0;
uint8_t start = 0;
uint8_t komma = 0;
uint16_t dport = 0;
if (DEBUG_FTP) Serial.println("PASV");
ftpclient.println("PASV");
String returnText = ftpReceive();
String meldung = returnText.substring(0,3); // erste 3 Zeichen
if (DEBUG_FTP) Serial.println("Return: " + meldung);
if (meldung == "227")
{
start = returnText.indexOf("(");
for (uint8_t i = 0; i < 5; i++)
{
komma = returnText.indexOf(",", start + 1);
werte[pos] = returnText.substring(start + 1, komma).toInt();
if (DEBUG_FTP) Serial.println(werte[pos], DEC);
pos++;
start = komma;
}
werte[pos] = returnText.substring(start + 1).toInt();
if (DEBUG_FTP) Serial.println(werte[pos], DEC);
dport = (werte[4] << 8) | werte[5];
if (DEBUG_FTP) Serial.println("Datenport: " + String(dport, DEC));
if (dclient.connect(ftpserver, dport))
{
Serial.println(F("Data connected"));
}
else
{
Serial.println(F("Data connection failed"));
ftpclient.stop();
return "Error";
}
if (DEBUG_FTP) Serial.println("CWD " + ftp_pfad);
ftpclient.println("CWD " + ftp_pfad);
String returnText = ftpReceive();
String meldung = returnText.substring(0,3); // erste 3 Zeichen
if (DEBUG_FTP) Serial.println("Return: " + meldung);
if (meldung == "250")
{
Serial.println(dateiname);
if (DEBUG_FTP) Serial.println("STOR " + dateiname);
ftpclient.println("STOR " + dateiname);
String returnText = ftpReceive();
String meldung = returnText.substring(0,3); // erste 3 Zeichen
if (DEBUG_FTP) Serial.println("Return: " + meldung);
if (meldung == "150")
{
Serial.print(F("Writing..."));
dclient.write((const uint8_t *)fb->buf, fb->len);
Serial.println(F("OK"));
}
}
dclient.stop();
Serial.println(F("Data disconnected"));
}
}
}
ftpclient.println(F("QUIT"));
ftpclient.stop();
Serial.println(F("Command disconnected"));
esp_camera_fb_return(fb);
return "OK";
}
//----------------------------------------------------------
String ftpReceive()
{ // gibt nur die letzte Zeile zurück
char thisByte;
uint8_t count = 0;
String outText[20];
String retText = "";
while (!ftpclient.available()) delay(1); // auf Daten warten
while (ftpclient.available())
{
thisByte = ftpclient.read();
#ifdef DEBUG_FTP
if (DEBUG_FTP) Serial.write(thisByte);
#endif
if (thisByte == 13) // return
{
count++; // nächste Zeile
}
else if (thisByte != 10) // newline
{
outText[count] += thisByte;
}
}
for (uint8_t i = 20; i > 0; i--)
{
if (outText[i - 1] > "")
{
retText = outText[i - 1];
break;
}
}
return retText;
}
//----------------------------------------------------------
bool ftpCommand(uint8_t index)
{
if (DEBUG_FTP) Serial.println(ftp_comm[index]);
ftpclient.println(ftp_comm[index]);
String returnText = ftpReceive();
String meldung = returnText.substring(0,3); // erste 3 Zeichen
if (DEBUG_FTP) Serial.println("Return: " + meldung);
if (meldung == ftp_ret[index])
{
return true;
}
else
{
return false;
}
}
SerialMonitor
Code:
19:24:00.341 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
19:24:00.341 -> configsip: 0, SPIWP:0xee
19:24:00.341 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
19:24:00.341 -> mode:DIO, clock div:1
19:24:00.341 -> load:0x3fff0018,len:4
19:24:00.341 -> load:0x3fff001c,len:1216
19:24:00.341 -> ho 0 tail 12 room 4
19:24:00.341 -> load:0x40078000,len:9720
19:24:00.374 -> ho 0 tail 12 room 4
19:24:00.374 -> load:0x40080400,len:6352
19:24:00.374 -> entry 0x400806b8
19:24:02.763 -> Boot number: 1
19:24:02.763 -> Wakeup was not caused by deep sleep: 0
19:24:02.929 -> Setup ESP32 to sleep for every 30 Seconds
19:24:02.929 -> Wifi connect.
19:24:03.427 -> Connected to FB1
19:24:03.427 -> IP address: 192.168.101.72
19:24:03.427 -> Bild.jpg
19:24:21.876 -> Command connection failed
19:24:21.876 -> Going to sleep now
19:24:21.976 -> ESP going deep sleep
19:24:51.867 -> ets Jun 8 2016 00:22:57
19:24:51.867 ->
19:24:51.867 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
19:24:51.867 -> configsip: 0, SPIWP:0xee
19:24:51.867 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
19:24:51.867 -> mode:DIO, clock div:1
19:24:51.867 -> load:0x3fff0018,len:4
19:24:51.867 -> load:0x3fff001c,len:1216
19:24:51.867 -> ho 0 tail 12 room 4
19:24:51.867 -> load:0x40078000,len:9720
19:24:51.867 -> ho 0 tail 12 room 4
19:24:51.867 -> load:0x40080400,len:6352
19:24:51.900 -> entry 0x400806b8
19:24:54.289 -> Boot number: 2
19:24:54.289 -> Wakeup caused by timer
19:24:54.322 -> Setup ESP32 to sleep for every 30 Seconds
19:24:54.322 -> Wifi connect.
19:24:54.820 -> Connected to FB1
19:24:54.820 -> IP address: 192.168.101.72
19:24:54.853 -> Bild.jpg
Ich muss dann noch den FTP anpassen ...
Zumindest der deep sleep funktioniert schon mal.
Da kümmer ich mich morgen drum.
DANKE für das "Hirn-Futter"
ESP32 Anfänger mit Migrations-Hintergrund (komme von RasPi & Python) 
Gruß aus Franken
Dietmar
|