esp with camera for printer monitoring - generic firmware

This commit is contained in:
Martin Bauer 2023-01-03 20:37:39 +01:00
parent eb75a98258
commit d467a4d7ab
8 changed files with 1265 additions and 0 deletions

5
esp32_cam/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

7
esp32_cam/.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}

11
esp32_cam/platformio.ini Normal file
View File

@ -0,0 +1,11 @@
[env:ttgo-camera]
platform = espressif32
board = ttgo-t1
upload_protocol = esptool
upload_port = /dev/ttyUSB0
monitor_speed = 115200
framework = arduino
lib_deps =
https://github.com/mathertel/OneButton
https://github.com/ThingPulse/esp8266-oled-ssd1306

393
esp32_cam/src/app_httpd.cpp Normal file
View File

@ -0,0 +1,393 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp_http_server.h"
#include "esp_timer.h"
#include "esp_camera.h"
#include "img_converters.h"
#include "camera_index.h"
#include "Arduino.h"
typedef struct {
httpd_req_t *req;
size_t len;
} jpg_chunking_t;
#define PART_BOUNDARY "123456789000000000000987654321"
static const char *_STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
static const char *_STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
static const char *_STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
httpd_handle_t stream_httpd = NULL;
httpd_handle_t camera_httpd = NULL;
static size_t jpg_encode_stream(void *arg, size_t index, const void *data, size_t len)
{
jpg_chunking_t *j = (jpg_chunking_t *)arg;
if (!index) {
j->len = 0;
}
if (httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK) {
return 0;
}
j->len += len;
return len;
}
static esp_err_t capture_handler(httpd_req_t *req)
{
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.printf("Camera capture failed");
httpd_resp_send_500(req);
return ESP_FAIL;
}
httpd_resp_set_type(req, "image/jpeg");
httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
size_t fb_len = 0;
if (fb->format == PIXFORMAT_JPEG) {
fb_len = fb->len;
res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
} else {
jpg_chunking_t jchunk = {req, 0};
res = frame2jpg_cb(fb, 80, 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", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start) / 1000));
return res;
}
static esp_err_t stream_handler(httpd_req_t *req)
{
camera_fb_t *fb = NULL;
esp_err_t res = ESP_OK;
size_t _jpg_buf_len = 0;
uint8_t *_jpg_buf = NULL;
char *part_buf[64];
static int64_t last_frame = 0;
if (!last_frame) {
last_frame = esp_timer_get_time();
}
res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
if (res != ESP_OK) {
return res;
}
while (true) {
fb = esp_camera_fb_get();
if (!fb) {
Serial.printf("Camera capture failed");
res = ESP_FAIL;
} else {
if (fb->format != PIXFORMAT_JPEG) {
bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
esp_camera_fb_return(fb);
fb = NULL;
if (!jpeg_converted) {
Serial.printf("JPEG compression failed");
res = ESP_FAIL;
}
} else {
_jpg_buf_len = fb->len;
_jpg_buf = fb->buf;
}
}
if (res == ESP_OK) {
size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
}
if (res == ESP_OK) {
res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
}
if (res == ESP_OK) {
res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
}
if (fb) {
esp_camera_fb_return(fb);
fb = NULL;
_jpg_buf = NULL;
} else if (_jpg_buf) {
free(_jpg_buf);
_jpg_buf = NULL;
}
if (res != ESP_OK) {
break;
}
int64_t fr_end = esp_timer_get_time();
int64_t frame_time = fr_end - last_frame;
last_frame = fr_end;
frame_time /= 1000;
Serial.printf("MJPG: %uB %ums (%.1ffps)\n"
, (uint32_t)(_jpg_buf_len),
(uint32_t)frame_time, 1000.0 / (uint32_t)frame_time
);
}
last_frame = 0;
return res;
}
static esp_err_t stream_hmi_handler(httpd_req_t *req)
{
Serial.println("stream_hmi_handler");
camera_fb_t *fb = NULL;
esp_err_t res = ESP_OK;
size_t _jpg_buf_len = 0;
uint8_t *_jpg_buf = NULL;
static int64_t last_frame = 0;
if (!last_frame) {
last_frame = esp_timer_get_time();
}
const char *request = "HTTP/1.1 200 OK\r\nContent-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n";
res = httpd_resp_send(req, request, strlen(request));
if (res != ESP_OK) {
return res;
}
while (true) {
fb = esp_camera_fb_get();
if (!fb) {
Serial.printf("Camera capture failed");
res = ESP_FAIL;
} else {
if (fb->format != PIXFORMAT_JPEG) {
bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
esp_camera_fb_return(fb);
fb = NULL;
if (!jpeg_converted) {
Serial.printf("JPEG compression failed");
res = ESP_FAIL;
}
} else {
_jpg_buf_len = fb->len;
_jpg_buf = fb->buf;
}
}
if (res == ESP_OK) {
uint8_t buff[4];
memcpy(buff, &_jpg_buf_len, sizeof(_jpg_buf_len));
res = httpd_send(req, (const char *)buff, sizeof(buff));
}
if (fb) {
esp_camera_fb_return(fb);
fb = NULL;
_jpg_buf = NULL;
} else if (_jpg_buf) {
free(_jpg_buf);
_jpg_buf = NULL;
}
if (res != ESP_OK) {
break;
}
}
last_frame = 0;
return res;
}
static esp_err_t cmd_handler(httpd_req_t *req)
{
char *buf;
size_t buf_len;
char variable[32] = {0,};
char value[32] = {0,};
buf_len = httpd_req_get_url_query_len(req) + 1;
if (buf_len > 1) {
buf = (char *)malloc(buf_len);
if (!buf) {
httpd_resp_send_500(req);
return ESP_FAIL;
}
if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
if (httpd_query_key_value(buf, "var", variable, sizeof(variable)) == ESP_OK &&
httpd_query_key_value(buf, "val", value, sizeof(value)) == ESP_OK) {
} else {
free(buf);
httpd_resp_send_404(req);
return ESP_FAIL;
}
} else {
free(buf);
httpd_resp_send_404(req);
return ESP_FAIL;
}
free(buf);
} else {
httpd_resp_send_404(req);
return ESP_FAIL;
}
int val = atoi(value);
sensor_t *s = esp_camera_sensor_get();
int res = 0;
if (!strcmp(variable, "framesize")) {
if (s->pixformat == PIXFORMAT_JPEG) res = s->set_framesize(s, (framesize_t)val);
} else if (!strcmp(variable, "quality")) res = s->set_quality(s, val);
else if (!strcmp(variable, "contrast")) res = s->set_contrast(s, val);
else if (!strcmp(variable, "brightness")) res = s->set_brightness(s, val);
else if (!strcmp(variable, "saturation")) res = s->set_saturation(s, val);
else if (!strcmp(variable, "gainceiling")) res = s->set_gainceiling(s, (gainceiling_t)val);
else if (!strcmp(variable, "colorbar")) res = s->set_colorbar(s, val);
else if (!strcmp(variable, "awb")) res = s->set_whitebal(s, val);
else if (!strcmp(variable, "agc")) res = s->set_gain_ctrl(s, val);
else if (!strcmp(variable, "aec")) res = s->set_exposure_ctrl(s, val);
else if (!strcmp(variable, "hmirror")) res = s->set_hmirror(s, val);
else if (!strcmp(variable, "vflip")) res = s->set_vflip(s, val);
else if (!strcmp(variable, "awb_gain")) res = s->set_awb_gain(s, val);
else if (!strcmp(variable, "agc_gain")) res = s->set_agc_gain(s, val);
else if (!strcmp(variable, "aec_value")) res = s->set_aec_value(s, val);
else if (!strcmp(variable, "aec2")) res = s->set_aec2(s, val);
else if (!strcmp(variable, "dcw")) res = s->set_dcw(s, val);
else if (!strcmp(variable, "bpc")) res = s->set_bpc(s, val);
else if (!strcmp(variable, "wpc")) res = s->set_wpc(s, val);
else if (!strcmp(variable, "raw_gma")) res = s->set_raw_gma(s, val);
else if (!strcmp(variable, "lenc")) res = s->set_lenc(s, val);
else if (!strcmp(variable, "special_effect")) res = s->set_special_effect(s, val);
else if (!strcmp(variable, "wb_mode")) res = s->set_wb_mode(s, val);
else if (!strcmp(variable, "ae_level")) res = s->set_ae_level(s, val);
else {
res = -1;
}
if (res) {
return httpd_resp_send_500(req);
}
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
return httpd_resp_send(req, NULL, 0);
}
static esp_err_t status_handler(httpd_req_t *req)
{
static char json_response[1024];
sensor_t *s = esp_camera_sensor_get();
char *p = json_response;
*p++ = '{';
p += sprintf(p, "\"framesize\":%u,", s->status.framesize);
p += sprintf(p, "\"quality\":%u,", s->status.quality);
p += sprintf(p, "\"brightness\":%d,", s->status.brightness);
p += sprintf(p, "\"contrast\":%d,", s->status.contrast);
p += sprintf(p, "\"saturation\":%d,", s->status.saturation);
p += sprintf(p, "\"special_effect\":%u,", s->status.special_effect);
p += sprintf(p, "\"wb_mode\":%u,", s->status.wb_mode);
p += sprintf(p, "\"awb\":%u,", s->status.awb);
p += sprintf(p, "\"awb_gain\":%u,", s->status.awb_gain);
p += sprintf(p, "\"aec\":%u,", s->status.aec);
p += sprintf(p, "\"aec2\":%u,", s->status.aec2);
p += sprintf(p, "\"ae_level\":%d,", s->status.ae_level);
p += sprintf(p, "\"aec_value\":%u,", s->status.aec_value);
p += sprintf(p, "\"agc\":%u,", s->status.agc);
p += sprintf(p, "\"agc_gain\":%u,", s->status.agc_gain);
p += sprintf(p, "\"gainceiling\":%u,", s->status.gainceiling);
p += sprintf(p, "\"bpc\":%u,", s->status.bpc);
p += sprintf(p, "\"wpc\":%u,", s->status.wpc);
p += sprintf(p, "\"raw_gma\":%u,", s->status.raw_gma);
p += sprintf(p, "\"lenc\":%u,", s->status.lenc);
p += sprintf(p, "\"hmirror\":%u,", s->status.hmirror);
p += sprintf(p, "\"dcw\":%u,", s->status.dcw);
p += sprintf(p, "\"colorbar\":%u", s->status.colorbar);
*p++ = '}';
*p++ = 0;
httpd_resp_set_type(req, "application/json");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
return httpd_resp_send(req, json_response, strlen(json_response));
}
static esp_err_t index_handler(httpd_req_t *req)
{
httpd_resp_set_type(req, "text/html");
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
return httpd_resp_send(req, (const char *)index_html_gz, index_html_gz_len);
}
void startCameraServer()
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_uri_t index_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = index_handler,
.user_ctx = NULL
};
httpd_uri_t status_uri = {
.uri = "/status",
.method = HTTP_GET,
.handler = status_handler,
.user_ctx = NULL
};
httpd_uri_t cmd_uri = {
.uri = "/control",
.method = HTTP_GET,
.handler = cmd_handler,
.user_ctx = NULL
};
httpd_uri_t capture_uri = {
.uri = "/capture",
.method = HTTP_GET,
.handler = capture_handler,
.user_ctx = NULL
};
httpd_uri_t stream_uri = {
.uri = "/stream",
.method = HTTP_GET,
.handler = stream_handler,
.user_ctx = NULL
};
httpd_uri_t hmi_uri = {
.uri = "/hmi",
.method = HTTP_GET,
.handler = stream_hmi_handler,
.user_ctx = NULL
};
Serial.printf("Starting web server on port: '%d'\n", config.server_port);
if (httpd_start(&camera_httpd, &config) == ESP_OK) {
httpd_register_uri_handler(camera_httpd, &index_uri);
httpd_register_uri_handler(camera_httpd, &cmd_uri);
httpd_register_uri_handler(camera_httpd, &status_uri);
httpd_register_uri_handler(camera_httpd, &capture_uri);
}
config.server_port += 1;
config.ctrl_port += 1;
Serial.printf("Starting stream server on port: '%d'\n", config.server_port);
if (httpd_start(&stream_httpd, &config) == ESP_OK) {
httpd_register_uri_handler(stream_httpd, &stream_uri);
httpd_register_uri_handler(stream_httpd, &hmi_uri);
}
}

File diff suppressed because one or more lines are too long

560
esp32_cam/src/main.cpp Normal file
View File

@ -0,0 +1,560 @@
/*
_ _ _ _
| | (_) | | | |
| | ___ __ __ _ ___ | |_| | ___
| | / _ \\ \ /\ / /| |/ __|| _ | / _ \
| |____| __/ \ V V / | |\__ \| | | || __/
\_____/ \___| \_/\_/ |_||___/\_| |_/ \___|
Compatible with all TTGO camera products, written by LewisHe
03/28/2020
*/
#include <WiFi.h>
#include <Wire.h>
#include "esp_camera.h"
/***************************************
* Board select
**************************************/
/* Select your board here, see the board list in the README for details*/
// #define T_Camera_JORNAL_VERSION
// #define T_Camera_MINI_VERSION
// #define T_Camera_PLUS_VERSION
// #define T_Camera_V05_VERSION
// #define T_Camera_V16_VERSION
// #define T_Camera_V162_VERSION
#define T_Camera_V17_VERSION
// #define ESPRESSIF_ESP_EYE
/***************************************
* Function
**************************************/
// #define SOFTAP_MODE //The comment will be connected to the specified ssid
//When there is BME280, set the reading time here
#define DEFAULT_MEASUR_MILLIS 3000 /* Get sensor time by default (ms)*/
//When using timed sleep, set the sleep time here
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
/***************************************
* WiFi
**************************************/
#define WIFI_SSID "WLAN"
#define WIFI_PASSWD "Bau3rWLAN"
#include "select_pins.h"
#if defined(SOFTAP_MODE)
#endif
String macAddress = "";
String ipAddress = "";
extern void startCameraServer();
#if defined(BUTTON_1)
//Depend BME280 library ,See https://github.com/mathertel/OneButton
#include <OneButton.h>
OneButton button(BUTTON_1, true);
#endif
#if defined(ENABLE_BEM280)
// Depend BME280 library ,See https://github.com/sparkfun/SparkFun_BME280_Arduino_Library
#include "SparkFunBME280.h"
BME280 sensor;
String temp, pressure, altitude, humidity;
#endif
#if defined(SSD130_MODLE_TYPE)
// Depend OLED library ,See https://github.com/ThingPulse/esp8266-oled-ssd1306
#include "SSD1306.h"
#include "OLEDDisplayUi.h"
#define SSD1306_ADDRESS 0x3c
SSD1306 oled(SSD1306_ADDRESS, I2C_SDA, I2C_SCL, (OLEDDISPLAY_GEOMETRY)SSD130_MODLE_TYPE);
OLEDDisplayUi ui(&oled);
#endif
#if defined(ENABLE_TFT)
// Depend TFT_eSPI library ,See https://github.com/Bodmer/TFT_eSPI
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
#endif
bool setupSensor()
{
#if defined(ENABLE_BEM280)
bool status = sensor.beginI2C();
if (!status)return false;
sensor.setMode(MODE_SLEEP); //Sleep for now
sensor.setFilter(1); //0 to 4 is valid. Filter coefficient. See 3.4.4
sensor.setStandbyTime(0); //0 to 7 valid. Time between readings. See table 27.
sensor.setTempOverSample(1); //0 to 16 are valid. 0 disables temp sensing. See table 24.
sensor.setPressureOverSample(1); //0 to 16 are valid. 0 disables pressure sensing. See table 23.
sensor.setHumidityOverSample(1); //0 to 16 are valid. 0 disables humidity sensing. See table 19.
sensor.setMode(MODE_SLEEP); //MODE_SLEEP, MODE_FORCED, MODE_NORMAL is valid. See 3.3
#endif
#if defined(AS312_PIN)
pinMode(AS312_PIN, INPUT);
#endif
return true;
}
void readSensor()
{
#if defined(ENABLE_BEM280)
static long lastMillis;
if (millis() - lastMillis < DEFAULT_MEASUR_MILLIS)return;
sensor.setMode(MODE_FORCED); //Wake up sensor and take reading
long startTime = millis();
while (sensor.isMeasuring() == false) ; //Wait for sensor to start measurment
while (sensor.isMeasuring() == true) ; //Hang out while sensor completes the reading
long endTime = millis();
//Sensor is now back asleep but we get get the data
Serial.print(" Measure time(ms): ");
Serial.print(endTime - startTime);
Serial.print(" Humidity: ");
humidity = sensor.readFloatHumidity();
Serial.print(humidity);
Serial.print(" Pressure: ");
pressure = sensor.readFloatPressure();
Serial.print(pressure);
Serial.print(" Alt: ");
//Serial.print(sensor.readFloatAltitudeMeters(), 1);
altitude = String(sensor.readFloatAltitudeFeet());
Serial.print(altitude);
Serial.print(" Temp: ");
// Serial.print(sensor.readTempF(), 2);
temp = String(sensor.readTempC());
Serial.print(temp);
Serial.println();
#endif /*ENABLE_BEM280*/
}
bool deviceProbe(uint8_t addr)
{
Wire.beginTransmission(addr);
return Wire.endTransmission() == 0;
}
bool setupDisplay()
{
#if defined(ENABLE_TFT)
#if defined(T_Camera_PLUS_VERSION)
tft.init();
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tft.setTextDatum(MC_DATUM);
tft.drawString("TFT_eSPI", tft.width() / 2, tft.height() / 2);
tft.drawString("LilyGo Camera Plus", tft.width() / 2, tft.height() / 2 + 20);
pinMode(TFT_BL_PIN, OUTPUT);
digitalWrite(TFT_BL_PIN, HIGH);
#endif
#elif defined(SSD130_MODLE_TYPE)
static FrameCallback frames[] = {
[](OLEDDisplay * display, OLEDDisplayUiState * state, int16_t x, int16_t y)
{
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_10);
#if (SSD130_MODLE_TYPE)
display->drawString(64 + x, 0 + y, macAddress);
display->drawString(64 + x, 10 + y, ipAddress);
#else
display->drawString(64 + x, 9 + y, macAddress);
display->drawString(64 + x, 25 + y, ipAddress);
#endif
#if defined(AS312_PIN)
if (digitalRead(AS312_PIN)) {
display->drawString(64 + x, 40 + y, "AS312 Trigger");
}
#endif
},
[](OLEDDisplay * display, OLEDDisplayUiState * state, int16_t x, int16_t y)
{
#if defined(ENABLE_BME280)
display->setFont(ArialMT_Plain_16);
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->drawString(0 + x, 0 + y, temp);
display->drawString(0 + x, 16 + y, pressure);
display->drawString(0 + x, 32 + y, altitude);
display->drawString(0 + x, 48 + y, humidity);
#else
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_10);
#if (SSD130_MODLE_TYPE)
// if (oled.getHeight() == 32) {
display->drawString( 64 + x, 0 + y, "Camera Ready! Use");
display->drawString(64 + x, 10 + y, "http://" + ipAddress );
display->drawString(64 + x, 16 + y, "to connect");
// } else {
#else
display->drawString( 64 + x, 5 + y, "Camera Ready! Use");
display->drawString(64 + x, 25 + y, "http://" + ipAddress );
display->drawString(64 + x, 45 + y, "to connect");
// }
#endif /*SSD130_MODLE_TYPE*/
#endif /*ENABLE_BME280*/
}
};
if (!deviceProbe(SSD1306_ADDRESS))return false;
oled.init();
// Wire.setClock(100000); //! Reduce the speed and prevent the speed from being too high, causing the screen
oled.setFont(ArialMT_Plain_16);
oled.setTextAlignment(TEXT_ALIGN_CENTER);
// delay(50);
oled.drawString( oled.getWidth() / 2, oled.getHeight() / 2 - 10, "LilyGo CAM");
oled.display();
ui.setTargetFPS(30);
ui.setIndicatorPosition(BOTTOM);
ui.setIndicatorDirection(LEFT_RIGHT);
ui.setFrameAnimation(SLIDE_LEFT);
ui.setFrames(frames, sizeof(frames) / sizeof(frames[0]));
ui.setTimePerFrame(6000);
ui.disableIndicator();
#endif
return true;
}
void loopDisplay()
{
#if defined(SSD130_MODLE_TYPE)
if (ui.update()) {
#endif /*SSD130_MODLE_TYPE*/
#if defined(BUTTON_1)
button.tick();
#endif /*BUTTON_1*/
#if defined(SSD130_MODLE_TYPE)
}
#elif defined(ENABLE_TFT)
// ***
#endif /*SSD130_MODLE_TYPE & ENABLE_TFT*/
}
bool setupPower()
{
#if defined(ENABLE_IP5306)
#define IP5306_ADDR 0X75
#define IP5306_REG_SYS_CTL0 0x00
if (!deviceProbe(IP5306_ADDR))return false;
bool en = true;
Wire.beginTransmission(IP5306_ADDR);
Wire.write(IP5306_REG_SYS_CTL0);
if (en)
Wire.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
else
Wire.write(0x35); // 0x37 is default reg value
return Wire.endTransmission() == 0;
#elif defined(ENABLE_AXP192)
#define AXP192_ADDRESS 0x34
if (!deviceProbe(AXP192_ADDRESS))return false;
//Turn off no use power channel , Or you can do nothing
uint8_t val;
Wire.beginTransmission(AXP192_ADDRESS);
Wire.write(0x30);
Wire.endTransmission();
Wire.requestFrom(AXP192_ADDRESS, 1);
val = Wire.read();
Wire.beginTransmission(AXP192_ADDRESS);
Wire.write(0x30);
Wire.write( val & 0xFC);
Wire.endTransmission();
Wire.beginTransmission(AXP192_ADDRESS);
Wire.write(0x12);
Wire.endTransmission();
Wire.requestFrom(AXP192_ADDRESS, 1);
val = Wire.read();
Wire.beginTransmission(AXP192_ADDRESS);
Wire.write(0x12);
Wire.write(val & 0b10100010);
Wire.endTransmission();
#endif
#if defined(T_Camera_MINI_VERSION)
// There is a pin in the Mini to control the camera power
pinMode(POWER_CONTROL_PIN, OUTPUT);
digitalWrite(POWER_CONTROL_PIN, HIGH);
#endif
return true;
}
#if defined(SDCARD_CS_PIN)
#include <SD.h>
#endif
bool setupSDCard()
{
/*
T-CameraPlus Board, SD shares the bus with the LCD screen.
It does not need to be re-initialized after the screen is initialized.
If the screen is not initialized, the initialization SPI bus needs to be turned on.
*/
// SPI.begin(TFT_SCLK_PIN, TFT_MISO_PIN, TFT_MOSI_PIN);
#if defined(SDCARD_CS_PIN)
if (!SD.begin(SDCARD_CS_PIN)) {
tft.setTextColor(TFT_RED);
tft.drawString("SDCard begin failed", tft.width() / 2, tft.height() / 2 - 20);
tft.setTextColor(TFT_WHITE);
return false;
} else {
String cardInfo = String(((uint32_t)SD.cardSize() / 1024 / 1024));
tft.setTextColor(TFT_GREEN);
tft.drawString("SDcardSize=[" + cardInfo + "]MB", tft.width() / 2, tft.height() / 2 + 92);
tft.setTextColor(TFT_WHITE);
Serial.print("SDcardSize=[");
Serial.print(cardInfo);
Serial.println("]MB");
}
#endif
return true;
}
bool setupCamera()
{
camera_config_t config;
#if defined(Y2_GPIO_NUM)
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;
}
#endif
#if defined(ESPRESSIF_ESP_EYE) || defined(T_Camera_V162_VERSION) || defined(T_Camera_MINI_VERSION)
/* IO13, IO14 is designed for JTAG by default,
* to use it as generalized input,
* firstly declair it as pullup input */
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\n", err);
return false;
}
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);
#if defined(T_Camera_V162_VERSION)
s->set_vflip(s, 1);
s->set_hmirror(s, 1);
#endif
return true;
}
void setupNetwork()
{
macAddress = "LilyGo-CAM-";
#ifdef SOFTAP_MODE
WiFi.mode(WIFI_AP);
macAddress += WiFi.softAPmacAddress().substring(0, 5);
WiFi.softAP(macAddress.c_str());
ipAddress = WiFi.softAPIP().toString();
#else
WiFi.begin(WIFI_SSID, WIFI_PASSWD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
ipAddress = WiFi.localIP().toString();
macAddress += WiFi.macAddress().substring(0, 5);
#endif
#if defined(ENABLE_TFT)
#if defined(T_Camera_PLUS_VERSION)
tft.drawString("ipAddress:", tft.width() / 2, tft.height() / 2 + 50);
tft.drawString(ipAddress, tft.width() / 2, tft.height() / 2 + 72);
#endif
#endif
}
void setupButton()
{
#if defined(BUTTON_1)
button.attachClick([]() {
static bool en = false;
sensor_t *s = esp_camera_sensor_get();
en = en ? 0 : 1;
s->set_vflip(s, en);
#if defined(SSD130_MODLE_TYPE)
if (en) {
oled.resetOrientation();
} else {
oled.flipScreenVertically();
}
#endif
// delay(200);
});
button.attachDoubleClick([]() {
if (PWDN_GPIO_NUM > 0) {
pinMode(PWDN_GPIO_NUM, PULLUP);
digitalWrite(PWDN_GPIO_NUM, HIGH);
}
#if defined(SSD130_MODLE_TYPE)
ui.disableAutoTransition();
oled.setTextAlignment(TEXT_ALIGN_CENTER);
oled.setFont(ArialMT_Plain_10);
oled.clear();
#if defined(AS312_PIN) && defined(PIR_SUPPORT_WAKEUP)
oled.drawString(oled.getWidth() / 2, oled.getHeight() / 2, "Set to wake up from PIR");
#elif defined(BUTTON_1)
oled.drawString(oled.getWidth() / 2 - 5, oled.getHeight() / 2 - 20, "Deepsleep");
oled.drawString(oled.getWidth() / 2, oled.getHeight() / 2 - 10, "Set to be awakened by");
oled.drawString(oled.getWidth() / 2, oled.getHeight() / 2, "a key press");
#else
oled.drawString(oled.getWidth() / 2, oled.getHeight() / 2, "Set to wake up by timer");
#endif
oled.display();
delay(3000);
oled.clear();
oled.displayOff();
#else
delay(2000);
#endif /*SSD130_MODLE_TYPE*/
#if defined(AS312_PIN) && defined(PIR_SUPPORT_WAKEUP)
esp_sleep_enable_ext1_wakeup(((uint64_t)(((uint64_t)1) << AS312_PIN)), ESP_EXT1_WAKEUP_ANY_HIGH);
#elif defined(BUTTON_1)
// esp_sleep_enable_ext0_wakeup((gpio_num_t )BUTTON_1, LOW);
#if defined(T_Camera_MINI_VERSION)
esp_sleep_enable_ext1_wakeup(((uint64_t)(((uint64_t)1) << BUTTON_1)), ESP_EXT1_WAKEUP_ANY_HIGH);
#else
esp_sleep_enable_ext1_wakeup(((uint64_t)(((uint64_t)1) << BUTTON_1)), ESP_EXT1_WAKEUP_ALL_LOW);
#endif
#else
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
#endif
esp_deep_sleep_start();
});
#if defined(T_Camera_MINI_VERSION)
button.setClickTicks(200);
button.setDebounceTicks(0);
#endif
#endif /*BUTTON_1*/
}
void setup()
{
Serial.begin(115200);
#if defined(I2C_SDA) && defined(I2C_SCL)
Wire.begin(I2C_SDA, I2C_SCL);
#endif
bool status;
status = setupDisplay();
Serial.print("setupDisplay status "); Serial.println(status);
status = setupSDCard();
Serial.print("setupSDCard status "); Serial.println(status);
status = setupPower();
Serial.print("setupPower status "); Serial.println(status);
status = setupSensor();
Serial.print("setupSensor status "); Serial.println(status);
status = setupCamera();
Serial.print("setupCamera status "); Serial.println(status);
if (!status) {
delay(10000); esp_restart();
}
setupButton();
setupNetwork();
startCameraServer();
Serial.print("Camera Ready! Use 'http://");
Serial.print(ipAddress);
Serial.println("' to connect");
}
void loop()
{
loopDisplay();
}

View File

@ -0,0 +1,27 @@
;PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
; Serial Monitor options
monitor_speed = 115200
monitor_port = COM137
upload_port = COM137
lib_deps =
; SparkFun BME280@2.0.8
; U8g2@2.28.0
; Adafruit BME280 Library@2.0.1
; OneButton@c8651328e3
; ESP8266_SSD1306@4.1.0
; TFT_eSPI

260
esp32_cam/src/select_pins.h Normal file
View File

@ -0,0 +1,260 @@
/*
_ _ _ _
| | (_) | | | |
| | ___ __ __ _ ___ | |_| | ___
| | / _ \\ \ /\ / /| |/ __|| _ | / _ \
| |____| __/ \ V V / | |\__ \| | | || __/
\_____/ \___| \_/\_/ |_||___/\_| |_/ \___|
Compatible with all TTGO camera products, written by LewisHe
03/28/2020
*/
#if defined(T_Camera_V16_VERSION)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 4
#define SIOD_GPIO_NUM 18
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 36
#define Y8_GPIO_NUM 15
#define Y7_GPIO_NUM 12
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 35
#define Y4_GPIO_NUM 14
#define Y3_GPIO_NUM 13
#define Y2_GPIO_NUM 34
#define VSYNC_GPIO_NUM 5
#define HREF_GPIO_NUM 27
#define PCLK_GPIO_NUM 25
#define AS312_PIN 19
#define BUTTON_1 0
#define I2C_SDA 21
#define I2C_SCL 22
#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32
#define IIS_SCK 26
#define IIS_WS 32
#define IIS_DOUT 33
#elif defined(T_Camera_V05_VERSION)
#define PWDN_GPIO_NUM 26
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 32
#define SIOD_GPIO_NUM 13
#define SIOC_GPIO_NUM 12
#define Y9_GPIO_NUM 39
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 23
#define Y6_GPIO_NUM 18
#define Y5_GPIO_NUM 15
#define Y4_GPIO_NUM 4
#define Y3_GPIO_NUM 14
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 27
#define HREF_GPIO_NUM 25
#define PCLK_GPIO_NUM 19
#define AS312_PIN 33
#define BUTTON_1 34
#define I2C_SDA 21
#define I2C_SCL 22
#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32
#elif defined(T_Camera_JORNAL_VERSION)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
#define XCLK_GPIO_NUM 27
#define SIOD_GPIO_NUM 25
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 19
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 18
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 5
#define Y4_GPIO_NUM 34
#define Y3_GPIO_NUM 35
#define Y2_GPIO_NUM 17
#define VSYNC_GPIO_NUM 22
#define HREF_GPIO_NUM 26
#define PCLK_GPIO_NUM 21
#define I2C_SDA 14
#define I2C_SCL 13
#define BUTTON_1 32
#define SSD130_MODLE_TYPE 1 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32
#elif defined(T_Camera_PLUS_VERSION)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 4
#define SIOD_GPIO_NUM 18
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 36
#define Y8_GPIO_NUM 37
#define Y7_GPIO_NUM 38
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 35
#define Y4_GPIO_NUM 26
#define Y3_GPIO_NUM 13
#define Y2_GPIO_NUM 34
#define VSYNC_GPIO_NUM 5
#define HREF_GPIO_NUM 27
#define PCLK_GPIO_NUM 25
#define I2C_SDA 18
#define I2C_SCL 23
#define IIS_SCK 14
#define IIS_WS 32
#define IIS_DOUT 33
#define TFT_CS_PIN 12
#define TFT_DC_PIN 15
#define TFT_MOSI_PIN 19
#define TFT_MISO_PIN 22
#define TFT_SCLK_PIN 21
#define TFT_BL_PIN 2
#define SDCARD_CS_PIN 0
#define ENABLE_IP5306
#define ENABLE_TFT
#elif defined(T_Camera_V162_VERSION)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 4
#define SIOD_GPIO_NUM 18
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 36
#define Y8_GPIO_NUM 37
#define Y7_GPIO_NUM 38
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 35
#define Y4_GPIO_NUM 14
#define Y3_GPIO_NUM 13
#define Y2_GPIO_NUM 34
#define VSYNC_GPIO_NUM 5
#define HREF_GPIO_NUM 27
#define PCLK_GPIO_NUM 25
#define I2C_SDA 21
#define I2C_SCL 22
#define BUTTON_1 15
#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32
#define AS312_PIN 19 //not rtc io ,can't form deepsleep wakeup
#define IIS_SCK 26
#define IIS_WS 32
#define IIS_DOUT 33
#define ENABLE_IP5306
#elif defined(T_Camera_MINI_VERSION)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 32
#define SIOD_GPIO_NUM 13
#define SIOC_GPIO_NUM 12
#define Y9_GPIO_NUM 39
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 38
#define Y6_GPIO_NUM 37
#define Y5_GPIO_NUM 15
#define Y4_GPIO_NUM 4
#define Y3_GPIO_NUM 14
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 27
#define HREF_GPIO_NUM 25
#define PCLK_GPIO_NUM 19
#define CAMERA_PWR_PIN 26
#define I2C_SDA 21
#define I2C_SCL 22
#define BUTTON_1 33
#define POWER_CONTROL_PIN 26
#define ENABLE_AXP192
#elif defined(T_Camera_V17_VERSION)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 32
#define SIOD_GPIO_NUM 13
#define SIOC_GPIO_NUM 12
#define Y9_GPIO_NUM 39
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 23
#define Y6_GPIO_NUM 18
#define Y5_GPIO_NUM 15
#define Y4_GPIO_NUM 4
#define Y3_GPIO_NUM 14
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 27
#define HREF_GPIO_NUM 25
#define PCLK_GPIO_NUM 19
#define I2C_SDA 21
#define I2C_SCL 22
#define BUTTON_1 34
#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32
#define AS312_PIN 33
#define ENABLE_IP5306
#elif defined(ESPRESSIF_ESP_EYE)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 4
#define SIOD_GPIO_NUM 18
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 36
#define Y8_GPIO_NUM 37
#define Y7_GPIO_NUM 38
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 35
#define Y4_GPIO_NUM 14
#define Y3_GPIO_NUM 13
#define Y2_GPIO_NUM 34
#define VSYNC_GPIO_NUM 5
#define HREF_GPIO_NUM 27
#define PCLK_GPIO_NUM 25
#define BUTTON_1 0
#else
#error "Please select the model of the board you want to use in main.cpp"
#endif