From 2b1abc6f48f26e3d0d846d4703336ee50160dd9c Mon Sep 17 00:00:00 2001 From: hansju-11 <51207613+hansju-11@users.noreply.github.com> Date: Thu, 31 Mar 2022 15:54:36 +0200 Subject: [PATCH 1/5] Update esp32-cam-webserver.ino precondition install the LIB https://github.com/finitespace/BME280 Add #define HAS_BME280 to activate this function change in the code: - add declaration for BME280 - add setup in void setup() for BMS280 --- esp32-cam-webserver.ino | 90 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/esp32-cam-webserver.ino b/esp32-cam-webserver.ino index 38b22fa..ca7caa0 100644 --- a/esp32-cam-webserver.ino +++ b/esp32-cam-webserver.ino @@ -48,6 +48,23 @@ stationList[] = {{"ESP32-CAM-CONNECT","InsecurePassword", true}}; #endif + +/* + * use of BME280 Sensor on ESPCAM32, need https://github.com/finitespace/BME280 LIB to run, please install with Arduion LIB Manager + * + * Connection diagram, can be change in main source code + * ESP32CAM -- BME280 Sensor + * GPIO 14 -> SDA + * GPIO 15 -> SCL + * GND -> GND + * 5V -> VIN (3.3V was not working ??? + * + * #define HAS_BME280 here and in app_httpd.cpp to include the function +*/ + +// #define HAS_BME280 + + // Upstream version string #include "src/version.h" @@ -228,6 +245,30 @@ const int pwmMax = pow(2,pwmresolution)-1; // will be returned for all http requests String critERR = ""; + +#if defined(HAS_BME280) +// (set these in myconfig.h) + + #include + #include + + #define I2C_SDA 14 + #define I2C_SCL 15 + #define I2C_Freq 400000 + + BME280I2C::Settings settings( + BME280::OSR_X1, + BME280::OSR_X1, + BME280::OSR_X1, + BME280::Mode_Forced, + BME280::StandbyTime_1000ms, + BME280::Filter_Off, + BME280::SpiEnable_False, + BME280I2C::I2CAddr_0x76 // I2C address. I2C specific. + ); + BME280I2C bme(settings); +#endif + // Debug flag for stream and capture data bool debugData; @@ -241,6 +282,7 @@ void debugOff() { Serial.println("Camera debug data is disabled (send 'd' for status dump, or any other char to enable debug)"); } + // Serial input (debugging controls) void handleSerial() { if (Serial.available()) { @@ -335,7 +377,7 @@ void StartCamera() { config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = xclk * 1000000; config.pixel_format = PIXFORMAT_JPEG; - config.grab_mode = CAMERA_GRAB_LATEST; +// config.grab_mode = CAMERA_GRAB_LATEST; // not sure, I got an error, maby I have an old driver hansju // Pre-allocate large buffers if(psramFound()){ config.frame_size = FRAMESIZE_UXGA; @@ -635,12 +677,31 @@ void WifiSetup() { } } + +#if defined(HAS_BME280) + + float getBME280_hum() { + return bme.hum(); + } + + float getBME280_temp() { + BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); // you can change Unit to TempUnit_Fahrenheit + return bme.temp(tempUnit); + } + + float getBME280_pres(){ + BME280::PresUnit presUnit(BME280::PresUnit_hPa); // you can change Unit here https://github.com/finitespace/BME280#tempunit-enum + return bme.pres(presUnit); + } + +#endif + void setup() { Serial.begin(115200); Serial.setDebugOutput(true); Serial.println(); Serial.println("===="); - Serial.print("esp32-cam-webserver: "); + Serial.print("Nistkasten-ESP32Cam Server: "); Serial.println(myName); Serial.print("Code Built: "); Serial.println(myVer); @@ -648,6 +709,31 @@ void setup() { Serial.println(baseVersion); Serial.println(); + +#if defined(HAS_BME280) + Wire.begin(I2C_SDA , I2C_SCL); + while(!bme.begin()) + { + Serial.println("Could not find BME280I2C sensor!"); + delay(1000); + } + + switch(bme.chipModel()) + { + case BME280::ChipModel_BME280: + Serial.println("Found BME280 sensor! Success."); + break; + case BME280::ChipModel_BMP280: + Serial.println("Found BMP280 sensor! No Humidity available."); + break; + default: + Serial.println("Found UNKNOWN sensor! Error!"); + } + // Change some settings before using. + settings.tempOSR = BME280::OSR_X4; + bme.setSettings(settings); +#endif + // Warn if no PSRAM is detected (typically user error with board selection in the IDE) if(!psramFound()){ Serial.println("\r\nFatal Error; Halting"); From 27ce23fddefb54a86f9610ac1fabd6024319ebbb Mon Sep 17 00:00:00 2001 From: hansju-11 <51207613+hansju-11@users.noreply.github.com> Date: Thu, 31 Mar 2022 15:58:08 +0200 Subject: [PATCH 2/5] Update app_httpd.cpp add #define HAS_BME280 to activate the function change in code: add static esp_err_t readSensor_handler(httpd_req_t *req) to get the sensor data "inline" depending on HAS_BME280, load specific data from index_other.h --- app_httpd.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/app_httpd.cpp b/app_httpd.cpp index f2b11a9..e2b6fda 100644 --- a/app_httpd.cpp +++ b/app_httpd.cpp @@ -28,6 +28,10 @@ #include "src/logo.h" #include "storage.h" +// #define HAS_BME280 here and in esp32-cam-webserver.ino to include the function +// #define HAS_BME280 + + // Functions from the main .ino extern void flashLED(int flashtime); extern void setLamp(int newVal); @@ -91,6 +95,14 @@ uint8_t temprature_sens_read(); } #endif +#if defined (HAS_BME280) +// external function to get the values from sensor +extern float getBME280_hum(); +extern float getBME280_temp(); +extern float getBME280_pres(); + +#endif + void serialDump() { Serial.println(); // Module @@ -253,7 +265,7 @@ static esp_err_t stream_handler(httpd_req_t *req){ httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); if(res == ESP_OK){ - res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY)); + res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, HTTPD_RESP_USE_STRLEN); } while(true){ @@ -278,7 +290,7 @@ static esp_err_t stream_handler(httpd_req_t *req){ 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)); + res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, HTTPD_RESP_USE_STRLEN); } if(fb){ esp_camera_fb_return(fb); @@ -651,7 +663,7 @@ static esp_err_t streamviewer_handler(httpd_req_t *req){ Serial.println("Stream viewer requested"); httpd_resp_set_type(req, "text/html"); httpd_resp_set_hdr(req, "Content-Encoding", "identity"); - return httpd_resp_send(req, (const char *)streamviewer_html, streamviewer_html_len); + return httpd_resp_send(req, (const char *)streamviewer_html, HTTPD_RESP_USE_STRLEN); } static esp_err_t error_handler(httpd_req_t *req){ @@ -667,7 +679,7 @@ static esp_err_t error_handler(httpd_req_t *req){ s.replace(index, strlen(""), critERR.c_str()); httpd_resp_set_type(req, "text/html"); httpd_resp_set_hdr(req, "Content-Encoding", "identity"); - return httpd_resp_send(req, (const char *)s.c_str(), s.length()); + return httpd_resp_send(req, (const char *)s.c_str(), HTTPD_RESP_USE_STRLEN); } static esp_err_t index_handler(httpd_req_t *req){ @@ -711,16 +723,20 @@ static esp_err_t index_handler(httpd_req_t *req){ if (critERR.length() > 0) return error_handler(req); httpd_resp_set_type(req, "text/html"); httpd_resp_set_hdr(req, "Content-Encoding", "identity"); - return httpd_resp_send(req, (const char *)index_simple_html, index_simple_html_len); +#if defined(HAS_BME280) + return httpd_resp_send(req, (const char *)index_simple_sensor_html, HTTPD_RESP_USE_STRLEN); +#else + return httpd_resp_send(req, (const char *)index_simple_html, HTTPD_RESP_USE_STRLEN); +#endif } else if(strncmp(view,"full", sizeof(view)) == 0) { Serial.println("Full index page requested"); if (critERR.length() > 0) return error_handler(req); httpd_resp_set_type(req, "text/html"); httpd_resp_set_hdr(req, "Content-Encoding", "identity"); if (sensorPID == OV3660_PID) { - return httpd_resp_send(req, (const char *)index_ov3660_html, index_ov3660_html_len); + return httpd_resp_send(req, (const char *)index_ov3660_html, HTTPD_RESP_USE_STRLEN); } - return httpd_resp_send(req, (const char *)index_ov2640_html, index_ov2640_html_len); + return httpd_resp_send(req, (const char *)index_ov2640_html, HTTPD_RESP_USE_STRLEN); } else if(strncmp(view,"portal", sizeof(view)) == 0) { //Prototype captive portal landing page. Serial.println("Portal page requested"); @@ -734,7 +750,7 @@ static esp_err_t index_handler(httpd_req_t *req){ s.replace(index, strlen(""), myName); httpd_resp_set_type(req, "text/html"); httpd_resp_set_hdr(req, "Content-Encoding", "identity"); - return httpd_resp_send(req, (const char *)s.c_str(), s.length()); + return httpd_resp_send(req, (const char *)s.c_str(), HTTPD_RESP_USE_STRLEN); } else { Serial.print("Unknown page requested: "); Serial.println(view); @@ -743,6 +759,23 @@ static esp_err_t index_handler(httpd_req_t *req){ } } +#if defined(HAS_BME280) +static esp_err_t readSensor_handler(httpd_req_t *req){ + flashLED(75); + httpd_resp_set_type(req, "text/plane"); + float hum_result = getBME280_hum(); + float temp_result = getBME280_temp(); + float pres_result = getBME280_pres(); + + String valuesStrg = String(hum_result) + '#'+ String(temp_result) + '#' + String(pres_result) + '#'; + int strgLength = valuesStrg.length(); + char values_as_char[strgLength]; + valuesStrg.toCharArray(values_as_char, strgLength); + + return httpd_resp_send(req, (const char *)values_as_char, HTTPD_RESP_USE_STRLEN); +} +#endif + void startCameraServer(int hPort, int sPort){ httpd_config_t config = HTTPD_DEFAULT_CONFIG(); config.max_uri_handlers = 16; // we use more than the default 8 (on port 80) @@ -843,6 +876,14 @@ void startCameraServer(int hPort, int sPort){ .handler = error_handler, .user_ctx = NULL }; +#if defined(HAS_BME280) + httpd_uri_t readSensor_uri = { + .uri = "/readSensor", + .method = HTTP_GET, + .handler = readSensor_handler, + .user_ctx = NULL + }; +#endif // Request Handlers; config.max_uri_handlers (above) must be >= the number of handlers config.server_port = hPort; @@ -864,6 +905,9 @@ void startCameraServer(int hPort, int sPort){ httpd_register_uri_handler(camera_httpd, &logo_svg_uri); httpd_register_uri_handler(camera_httpd, &dump_uri); httpd_register_uri_handler(camera_httpd, &stop_uri); +#if defined(HAS_BME280) + httpd_register_uri_handler(camera_httpd, &readSensor_uri); +#endif } config.server_port = sPort; From a526ade8341100ef6a062de3553fba8542f53d3a Mon Sep 17 00:00:00 2001 From: hansju-11 <51207613+hansju-11@users.noreply.github.com> Date: Thu, 31 Mar 2022 15:59:04 +0200 Subject: [PATCH 3/5] Update index_other.h add section for sensor data in simple view --- index_other.h | 334 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 333 insertions(+), 1 deletion(-) diff --git a/index_other.h b/index_other.h index 1eda91e..9c46fa6 100644 --- a/index_other.h +++ b/index_other.h @@ -306,7 +306,339 @@ const uint8_t index_simple_html[] = R"=====( )====="; -size_t index_simple_html_len = sizeof(index_simple_html)-1; +/* with Sensor Data */ +const uint8_t index_simple_sensor_html[] = R"=====( + + + + + ESP32-CAM Simplified View + + + + + + + +
+ +
+ Sensor Hum: 0 %, Temp: 0 ℃, Pres: 0 hPa +
+
+ +
+ +
+
+
+ + + +)====="; + /* Stream Viewer */ From 649f4c87ba93afa3f061a0b7da375438538dc306 Mon Sep 17 00:00:00 2001 From: hansju-11 <51207613+hansju-11@users.noreply.github.com> Date: Thu, 31 Mar 2022 16:00:23 +0200 Subject: [PATCH 4/5] Update css.h add .card layout defition --- css.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/css.h b/css.h index 66a0deb..22ada64 100644 --- a/css.h +++ b/css.h @@ -362,6 +362,10 @@ select { @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } +} +.card { + width: 500px; + background: grey #424949; + box-sizing: border-box; })====="; -size_t style_css_len = sizeof(style_css)-1; From a37f2c0880b91c8ef45b12c5856393b3862ed38b Mon Sep 17 00:00:00 2001 From: hansju-11 <51207613+hansju-11@users.noreply.github.com> Date: Fri, 1 Apr 2022 13:43:19 +0200 Subject: [PATCH 5/5] Update app_httpd.cpp fix build failer line 658 replace with HTTPD_RESP_USE_STRLEN --- app_httpd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app_httpd.cpp b/app_httpd.cpp index e2b6fda..aed6f8b 100644 --- a/app_httpd.cpp +++ b/app_httpd.cpp @@ -655,7 +655,7 @@ static esp_err_t stop_handler(httpd_req_t *req){ static esp_err_t style_handler(httpd_req_t *req){ httpd_resp_set_type(req, "text/css"); httpd_resp_set_hdr(req, "Content-Encoding", "identity"); - return httpd_resp_send(req, (const char *)style_css, style_css_len); + return httpd_resp_send(req, (const char *)style_css, HTTPD_RESP_USE_STRLEN); } static esp_err_t streamviewer_handler(httpd_req_t *req){