diff --git a/.github/label-commenter-config.yml b/.github/label-commenter-config.yml new file mode 100644 index 00000000..c28cefdf --- /dev/null +++ b/.github/label-commenter-config.yml @@ -0,0 +1,36 @@ +# Reply Bot Configuration +# See https://github.com/peaceiris/actions-label-commenter + + labels: + ####################################################################### + # Bot Response: Web Console + ####################################################################### + - name: bot-reply Web Console + labeled: + issue: + body: | + You can use the [Web Console](https://jomjol.github.io/AI-on-the-edge-device/index.html) to get USB log from the device. + The USB log contains more information about the startup and operation of the device than the normal Web UI log + discussion: + body: | + You can use the [Web Console](https://jomjol.github.io/AI-on-the-edge-device/index.html) to get USB log from the device. + The USB log contains more information about the startup and operation of the device than the normal Web UI log + + + ####################################################################### + # Bot Response: Rolling Build + ####################################################################### + - name: bot-reply Rolling Build + labeled: + issue: + body: | + You can use the latest [Automatic Build](https://github.com/jomjol/AI-on-the-edge-device/actions/workflows/build.yaml?query=branch%3Arolling) of the the `rolling` branch. It might already contain a fix for your issue. + Pick the most top passing entry (it has a green circle with a tick in it), then scoll down to the **Artifacts** and download the file named `update_*`. + discussion: + body: | + You can use the latest [Automatic Build](https://github.com/jomjol/AI-on-the-edge-device/actions/workflows/build.yaml?query=branch%3Arolling) of the the `rolling` branch. It might already contain a fix for your issue. + Pick the most top passing entry (it has a green circle with a tick in it), then scoll down to the **Artifacts** and download the file named `update_*`. + + + + diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 30586889..a410eacc 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -54,60 +54,10 @@ jobs: key: ${{ github.run_number }} - ######################################################################################### -## Pack for old OTA (v1) +## Pack for new OTA ######################################################################################### - pack-for-OTA-v1: - # Old OTA concept - # firmware__*.zip needs to be unpacked before attaching to the release! - # The bin filename can contain versioning. - runs-on: ubuntu-latest - needs: build - - steps: - - uses: actions/checkout@v3 - - - name: Get generated files from cache - uses: actions/cache@v3 - with: - path: | - ./code/.pio/build/esp32cam/firmware.bin - ./code/.pio/build/esp32cam/partitions.bin - ./code/.pio/build/esp32cam/bootloader.bin - ./sd-card/html/version.txt - key: ${{ github.run_number }} - - - name: Set Variables - id: vars - run: | - echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT - echo "branch=$(echo ${{ github.ref_name }} | tr / __)" >> $GITHUB_OUTPUT - - - name: Rename firmware file to contain versioning (old ota) - run: | - mkdir -p ./dist_old_ota - cp "./code/.pio/build/esp32cam/firmware.bin" "./dist_old_ota/firmware__${{ steps.vars.outputs.branch }}_(${{ steps.vars.outputs.sha_short }}).bin" - - - name: Upload Firmware artifact (old OTA concept) - uses: actions/upload-artifact@v3 - with: -# name: "firmware__${{ github.ref_name }}__(${{ steps.vars.outputs.sha_short }})__(extract_before_upload__only_needed_for_migration_from_11.3.1)" - name: "firmware__(extract_before_upload)__${{ steps.vars.outputs.branch }}__(${{ steps.vars.outputs.sha_short }})" - path: ./dist_old_ota/* - - - name: Upload Web interface artifact (old OTA concept) - uses: actions/upload-artifact@v3 - with: - name: "html__${{ steps.vars.outputs.branch }}__(${{ steps.vars.outputs.sha_short }})" - path: ./sd-card/html/* - - - -######################################################################################### -## Pack for new OTA (v2) -######################################################################################### - pack-for-OTA-v2: + pack-for-OTA: # New OTA concept # update__version.zip file with following content: # - /firmware.bin @@ -140,21 +90,9 @@ jobs: mkdir -p ./dist cp "./code/.pio/build/esp32cam/firmware.bin" "dist/firmware.bin" -# - name: Upload update.zip Artifact (Firmware only) -# uses: actions/upload-artifact@v3 -# with: -# name: "update_firmware_only__${{ github.ref_name }}_(${{ steps.vars.outputs.sha_short }})" -# path: ./dist/* - - name: Add Web UI to dist run: cp -r ./sd-card/html ./dist/ -# - name: Upload update.zip artifact (Firmware + Web UI) -# uses: actions/upload-artifact@v3 -# with: -# name: "update_firmware+webinterface__${{ github.ref_name }}_(${{ steps.vars.outputs.sha_short }})" -# path: ./dist/* - - name: Add CNN to dist run: | mkdir ./dist/config/ @@ -171,7 +109,7 @@ jobs: uses: actions/cache@v3 with: path: dist - key: ${{ github.run_number }}-pack-for-OTA-v2 + key: ${{ github.run_number }}-pack-for-OTA @@ -235,7 +173,7 @@ jobs: ######################################################################################### release: runs-on: ubuntu-latest - needs: [pack-for-OTA-v2, pack-for-fresh-install] + needs: [pack-for-OTA, pack-for-fresh-install] if: startsWith(github.ref, 'refs/tags/') steps: @@ -245,22 +183,12 @@ jobs: run: | echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT - - name: Get generated files from cache - uses: actions/cache@v3 - with: - path: | - ./code/.pio/build/esp32cam/firmware.bin - ./code/.pio/build/esp32cam/partitions.bin - ./code/.pio/build/esp32cam/bootloader.bin - ./sd-card/html/version.txt - key: ${{ github.run_number }} - # import the changes from - name: Get generated files from cache uses: actions/cache@v3 with: path: dist - key: ${{ github.run_number }}-pack-for-OTA-v2 + key: ${{ github.run_number }}-pack-for-OTA # import cached artifacts from pack-for-fresh-install - name: Get generated files from cache @@ -269,23 +197,16 @@ jobs: path: firmware key: ${{ github.run_number }}-pack-for-fresh-install - - - name: Prepare artifacts for release run: | mkdir -p release mkdir -p dist # create a update.zip like "update__rolling" pwd - ls ./dist cd ./dist zip -r ../release/update.zip . cd ../firmware zip -r ../release/initial_esp32_setup.zip . - cd ../sd-card/html - zip -r ../../firmware/html-from-11.3.1.zip . - - # extract the version used in next step - id: get_version if: startsWith(github.ref, 'refs/tags/') @@ -318,8 +239,6 @@ jobs: body: ${{ steps.extract-release-notes.outputs.release_notes }} files: | release/* - firmware/firmware.bin - firmware/html-from-11.3.1.zip # Commit&Push Changelog to master branch. Must be manually merged back to rolling diff --git a/.github/workflows/reply-bot.yml b/.github/workflows/reply-bot.yml new file mode 100644 index 00000000..deea0b0b --- /dev/null +++ b/.github/workflows/reply-bot.yml @@ -0,0 +1,42 @@ +# Reply Bot +# It uses the configuration in .github/label-commenter-config.yml +# See https://github.com/peaceiris/actions-label-commenter + +name: Reply-Bot + +on: + issues: + types: [labeled] + discussion: + types: [labeled] + +permissions: + contents: read + issues: write + pull-requests: write + discussions: write + +jobs: + comment: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + + - name: Remove 'Web Console' label again (issues only) + if: github.event.label.name == 'bot-reply Web Console' + uses: actions-cool/issues-helper@v2 + with: + actions: 'remove-labels' + labels: 'bot-reply Web Console' + + - name: Remove 'Rolling Build' label again (issues only) + if: github.event.label.name == 'bot-reply Rolling Build' + uses: actions-cool/issues-helper@v2 + with: + actions: 'remove-labels' + labels: 'bot-reply Rolling Build' + + - name: Write Response + uses: peaceiris/actions-label-commenter@c2d00660c86f2b9ed0fb35b372c451558eba85b3 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/Changelog.md b/Changelog.md index 68f37932..9cab2406 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,13 +2,13 @@ ## [Unreleased] -## [13.0.7] - 2022-12-11 +## [13.0.7] 2022-12-11 **Home Assistant MQTT Discovery Support** ### Update Procedure -:bangbang: **Make sure to read the instructions below carfully!**. +:bangbang: **Make sure to read the instructions below carfully!** 1. Backup your configuration (use the `System -> Backup/Restore` page)! @@ -19,13 +19,15 @@ 4. Let it restart and check on the `System -> Info` page that the Firmware as well as the Web UI got updated. If only one got updated, redo the update. If it fails several times, you also can update the Firmware and the Web UI separately. 5. Safe way: - 1. Update first the `firmware.bin` (extract from zip file) and do the Reboot + 1. Update first the `firmware.bin` (extract it from one of the provided zip files) and do the Reboot 2. Update with the full zip file (`update-*.zip`, ignore the version warning after the reboot) 6. Please go to `Settings -> Configuration` and address the changed parameters: - DataLogging (storing the values for data graph) - Debug (extended by different debug reporting levels) +7. Make sure it starts to do the digitalization (check the Error field on the overview page). If it does not start a round within a minute, restart the device. + If anything breaks you can try to enforce manual update as following: **OTA:** @@ -65,10 +67,17 @@ If anything breaks you can try to enforce manual update as following: - NEW v13.0.2: Update Tool "Logfile downloader and combiner" to handle the new csv file format. - NEW v13.0.2: MQTT: Added MQTT topic `status` (Digitalization Status), Timezone to MQTT topic `timestamp`.# - NEW v13.0.2: Logging: Disable heap logs by default, cleanup -- **NEW v13.0.5**: - - log NTP server name - - Improved log messages - - Various preparations for next release +- NEW v13.0.7: + - log NTP server name + - Improved log messages + - Various preparations for next release +- **NEW v13.0.8**: + - Continue booting on PSRAM issues, Web UI will show an error + - Updated models + - Various UI enhancements + - Various internal improvements + - Show uptime in log + - Show uptime and round on overview page ### Fixed @@ -84,13 +93,19 @@ If anything breaks you can try to enforce manual update as following: - NEW v13.0.3: Re-updated build environment to v5.2.0 (from accidental downgrad to v4.4.0) - NEW v13.0.4: Fix for reboot in case of MQTT not used - NEW v13.0.5: No reboot in case of missing NTP-connection -- **NEW v13.0.5**: - - Prevent autoreboot on cam framebuffer init error - - Properly protect `wlan.ini` against deletion - - Fixed various MQTT topic content issues - - Fix Digit detected as 10 () - - Fix frozen time in datafile on error - - Various minor fixes +- NEW v13.0.7: + - Prevent autoreboot on cam framebuffer init error + - Properly protect `wlan.ini` against deletion + - Fixed various MQTT topic content issues + - Fix Digit detected as 10 (https://github.com/jomjol/AI-on-the-edge-device/pull/1525) + - Fix frozen time in datafile on error + - Various minor fixes +- **NEW v13.0.8**: + - Fix Rate Problem ([#1578](https://github.com/jomjol/AI-on-the-edge-device/issues/1578), [#1572](https://github.com/jomjol/AI-on-the-edge-device/issues/1572)) + - Stabilized MQTT + - Fixed redundant calls in OTA + - Block REST API calls till resource is ready + - Fixed number renaming ([#1635](https://github.com/jomjol/AI-on-the-edge-device/issues/1635)) ### Removed diff --git a/FeatureRequest.md b/FeatureRequest.md index c2d77d5a..56677ba5 100644 --- a/FeatureRequest.md +++ b/FeatureRequest.md @@ -10,6 +10,14 @@ ____ +#### #34 implement state and Roi for water leak detection +for example see Roi on the next picture.. +![grafik](https://user-images.githubusercontent.com/38385805/207858812-2a6ba41d-1a8c-4fa1-9b6a-53cdd113c106.png) +in case of position change between the measurments set this state to true, if there is no change set it back to false. +In a defined time window this movement can lead into an alarm state / water leak.. +haveing this state in the mqtt broker can trigger functions like closing the ater pipe walve and so on... + + #### #33 Implement MATTER protocoll diff --git a/README.md b/README.md index cd655d5d..663a8179 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,9 @@ There are different ways to flash your ESP32: See the [Wiki](https://github.com/jomjol/AI-on-the-edge-device/wiki/Installation) for more information. +### Flashing the SD-Card +The SD-Card must be flashed separately, see the [Wiki](https://github.com/jomjol/AI-on-the-edge-device/wiki/Installation) for details. + ## Casing A 3d-printable housing can be found here: diff --git a/code/components/jomjol_controlcamera/ClassControllCamera.cpp b/code/components/jomjol_controlcamera/ClassControllCamera.cpp index 3b047c2d..666993e6 100644 --- a/code/components/jomjol_controlcamera/ClassControllCamera.cpp +++ b/code/components/jomjol_controlcamera/ClassControllCamera.cpp @@ -164,6 +164,7 @@ static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size return len; } + bool CCamera::SetBrightnessContrastSaturation(int _brightness, int _contrast, int _saturation) { bool result = false; @@ -281,7 +282,6 @@ void CCamera::EnableAutoExposure(int flashdauer) } - esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay) { string ftype; @@ -543,6 +543,7 @@ esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay) return res; } + void CCamera::LightOnOff(bool status) { GpioHandler* gpioHandler = gpio_handler_get(); @@ -578,6 +579,7 @@ void CCamera::LightOnOff(bool status) } } + void CCamera::LEDOnOff(bool status) { // Init the GPIO @@ -638,6 +640,7 @@ void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol }; } + framesize_t CCamera::TextToFramesize(const char * _size) { if (strcmp(_size, "QVGA") == 0) @@ -669,6 +672,7 @@ CCamera::CCamera() ledc_init(); } + esp_err_t CCamera::InitCam() { ESP_LOGD(TAG, "Init Camera"); @@ -682,9 +686,11 @@ esp_err_t CCamera::InitCam() return err; } + CameraInitSuccessful = true; return ESP_OK; } + void CCamera::SetLEDIntensity(float _intrel) { _intrel = min(_intrel, (float) 100); @@ -694,3 +700,9 @@ void CCamera::SetLEDIntensity(float _intrel) ESP_LOGD(TAG, "Set led_intensity to %d of 8191", led_intensity); } + + +bool CCamera::getCameraInitSuccessful() +{ + return CameraInitSuccessful; +} diff --git a/code/components/jomjol_controlcamera/ClassControllCamera.h b/code/components/jomjol_controlcamera/ClassControllCamera.h index 86065006..3bac8f0e 100644 --- a/code/components/jomjol_controlcamera/ClassControllCamera.h +++ b/code/components/jomjol_controlcamera/ClassControllCamera.h @@ -26,6 +26,7 @@ class CCamera { int led_intensity = 4095; void ledc_init(void); + bool CameraInitSuccessful = false; public: int image_height, image_width; @@ -42,6 +43,7 @@ class CCamera { void SetLEDIntensity(float _intrel); void EnableAutoExposure(int flashdauer); + bool getCameraInitSuccessful(); framesize_t TextToFramesize(const char * text); diff --git a/code/components/jomjol_controlcamera/server_camera.cpp b/code/components/jomjol_controlcamera/server_camera.cpp index a782079d..b5955451 100644 --- a/code/components/jomjol_controlcamera/server_camera.cpp +++ b/code/components/jomjol_controlcamera/server_camera.cpp @@ -17,7 +17,8 @@ char scratch2[SCRATCH_BUFSIZE2]; //#define DEBUG_DETAIL_ON -void PowerResetCamera(){ +void PowerResetCamera() +{ ESP_LOGD(TAG, "Resetting camera by power down line"); gpio_config_t conf; conf.intr_type = GPIO_INTR_DISABLE; @@ -37,182 +38,223 @@ void PowerResetCamera(){ esp_err_t handler_lightOn(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_lightOn - Start"); - ESP_LOGD(TAG, "handler_lightOn uri: %s", req->uri); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_lightOn - Start"); + ESP_LOGD(TAG, "handler_lightOn uri: %s", req->uri); + #endif - Camera.LightOnOff(true); - const char* resp_str = (const char*) req->user_ctx; - httpd_resp_send(req, resp_str, strlen(resp_str)); + if (Camera.getCameraInitSuccessful()) + { + Camera.LightOnOff(true); + const char* resp_str = (const char*) req->user_ctx; + httpd_resp_send(req, resp_str, strlen(resp_str)); + } + else + { + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Light On API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; + } -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_lightOn - Done"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_lightOn - Done"); + #endif return ESP_OK; -}; +} + esp_err_t handler_lightOff(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_lightOff - Start"); - ESP_LOGD(TAG, "handler_lightOff uri: %s", req->uri); -#endif - Camera.LightOnOff(false); - const char* resp_str = (const char*) req->user_ctx; - httpd_resp_send(req, resp_str, strlen(resp_str)); + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_lightOff - Start"); + ESP_LOGD(TAG, "handler_lightOff uri: %s", req->uri); + #endif -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_lightOff - Done"); -#endif + if (Camera.getCameraInitSuccessful()) + { + Camera.LightOnOff(false); + const char* resp_str = (const char*) req->user_ctx; + httpd_resp_send(req, resp_str, strlen(resp_str)); + } + else + { + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Light Off API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; + } + + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_lightOff - Done"); + #endif return ESP_OK; -}; +} + esp_err_t handler_capture(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_capture - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_capture - Start"); + #endif - int quality; - framesize_t res; + if (Camera.getCameraInitSuccessful()) + { + int quality; + framesize_t res; - Camera.GetCameraParameter(req, quality, res); + Camera.GetCameraParameter(req, quality, res); -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Size: %d, Quality: %d", res, quality); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Size: %d, Quality: %d", res, quality); + #endif - Camera.SetQualitySize(quality, res); + Camera.SetQualitySize(quality, res); - esp_err_t ressult; - ressult = Camera.CaptureToHTTP(req); + esp_err_t result; + result = Camera.CaptureToHTTP(req); -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_capture - Done"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_capture - Done"); + #endif - return ressult; -}; + return result; + } + else + { + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; + } +} esp_err_t handler_capture_with_ligth(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_capture_with_ligth - Start"); -#endif - char _query[100]; - char _delay[10]; - - int quality; - framesize_t res; - int delay = 2500; - - if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK) + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_capture_with_ligth - Start"); + #endif + + if (Camera.getCameraInitSuccessful()) { - ESP_LOGD(TAG, "Query: %s", _query); - if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK) + char _query[100]; + char _delay[10]; + + int quality; + framesize_t res; + int delay = 2500; + + if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK) { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Delay: %s", _delay); -#endif - delay = atoi(_delay); + ESP_LOGD(TAG, "Query: %s", _query); + if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK) + { + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Delay: %s", _delay); + #endif + delay = atoi(_delay); - if (delay < 0) - delay = 0; + if (delay < 0) + delay = 0; + } } - }; - Camera.GetCameraParameter(req, quality, res); + Camera.GetCameraParameter(req, quality, res); -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Size: %d, Quality: %d", res, quality); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Size: %d, Quality: %d", res, quality); + #endif - Camera.SetQualitySize(quality, res); - Camera.LightOnOff(true); - const TickType_t xDelay = delay / portTICK_PERIOD_MS; - vTaskDelay( xDelay ); + Camera.SetQualitySize(quality, res); + Camera.LightOnOff(true); + const TickType_t xDelay = delay / portTICK_PERIOD_MS; + vTaskDelay( xDelay ); - esp_err_t ressult; - ressult = Camera.CaptureToHTTP(req); + esp_err_t result; + result = Camera.CaptureToHTTP(req); - Camera.LightOnOff(false); + Camera.LightOnOff(false); -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_capture_with_ligth - Done"); -#endif - - return ressult; -}; + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_capture_with_ligth - Done"); + #endif + return result; + } + else + { + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture + flashlight API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; + } +} esp_err_t handler_capture_save_to_file(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_capture_save_to_file - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_capture_save_to_file - Start"); + #endif - char _query[100]; - char _delay[10]; - int delay = 0; - char filename[100]; - std::string fn = "/sdcard/"; - - - int quality; - framesize_t res; - - if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK) + if (Camera.getCameraInitSuccessful()) { - ESP_LOGD(TAG, "Query: %s", _query); - if (httpd_query_key_value(_query, "filename", filename, 100) == ESP_OK) + char _query[100]; + char _delay[10]; + int delay = 0; + char filename[100]; + std::string fn = "/sdcard/"; + + + int quality; + framesize_t res; + + if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK) { - fn.append(filename); -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Filename: %s", fn.c_str()); -#endif + ESP_LOGD(TAG, "Query: %s", _query); + if (httpd_query_key_value(_query, "filename", filename, 100) == ESP_OK) + { + fn.append(filename); + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Filename: %s", fn.c_str()); + #endif + } + else + fn.append("noname.jpg"); + + if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK) + { + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Delay: %s", _delay); + #endif + delay = atoi(_delay); + + if (delay < 0) + delay = 0; + } } else fn.append("noname.jpg"); - if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK) - { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Delay: %s", _delay); -#endif - delay = atoi(_delay); + Camera.GetCameraParameter(req, quality, res); + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Size: %d, Quality: %d", res, quality); + #endif + Camera.SetQualitySize(quality, res); - if (delay < 0) - delay = 0; - } + esp_err_t result; + result = Camera.CaptureToFile(fn, delay); + const char* resp_str = (const char*) fn.c_str(); + httpd_resp_send(req, resp_str, strlen(resp_str)); + + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_capture_save_to_file - Done"); + #endif + + return result; } - else - fn.append("noname.jpg"); - - Camera.GetCameraParameter(req, quality, res); -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Size: %d, Quality: %d", res, quality); -#endif - Camera.SetQualitySize(quality, res); - - esp_err_t ressult; - ressult = Camera.CaptureToFile(fn, delay); - - const char* resp_str = (const char*) fn.c_str(); - httpd_resp_send(req, resp_str, strlen(resp_str)); - -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_capture_save_to_file - Done"); -#endif - - return ressult; -}; - + else + { + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture + save API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; + } +} void register_server_camera_uri(httpd_handle_t server) diff --git a/code/components/jomjol_fileserver_ota/server_file.cpp b/code/components/jomjol_fileserver_ota/server_file.cpp index ac2c5d72..00efaeeb 100644 --- a/code/components/jomjol_fileserver_ota/server_file.cpp +++ b/code/components/jomjol_fileserver_ota/server_file.cpp @@ -935,7 +935,11 @@ std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::st // Get and print information about each file in the archive. int numberoffiles = (int)mz_zip_reader_get_num_files(&zip_archive); - for (sort_iter = 0; sort_iter < 2; sort_iter++) + ESP_LOGI(TAG, "Numbers of files to be extrated: %d", numberoffiles); + + + sort_iter = 0; +// for (sort_iter = 0; sort_iter < 2; sort_iter++) { memset(&zip_archive, 0, sizeof(zip_archive)); status = mz_zip_reader_init_file(&zip_archive, _in_zip_file.c_str(), sort_iter ? MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY : 0); diff --git a/code/components/jomjol_fileserver_ota/server_ota.cpp b/code/components/jomjol_fileserver_ota/server_ota.cpp index 8d3d3652..41d5e128 100644 --- a/code/components/jomjol_fileserver_ota/server_ota.cpp +++ b/code/components/jomjol_fileserver_ota/server_ota.cpp @@ -29,6 +29,9 @@ #include "server_tflite.h" #include "server_file.h" #include "server_GPIO.h" +#ifdef ENABLE_MQTT + #include "interface_mqtt.h" +#endif //ENABLE_MQTT #include "ClassLogFile.h" @@ -55,6 +58,18 @@ esp_err_t handler_reboot(httpd_req_t *req); std::string _file_name_update; + +static void infinite_loop(void) +{ + int i = 0; + ESP_LOGI(TAG, "When a new firmware is available on the server, press the reset button to download it"); + while(1) { + ESP_LOGI(TAG, "Waiting for a new firmware... %d", ++i); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} + + void task_do_Update_ZIP(void *pvParameter) { std::string filetype = toUpper(getFileType(_file_name_update)); @@ -107,26 +122,14 @@ void CheckUpdate() BaseType_t xReturned; int _i = configMINIMAL_STACK_SIZE; xReturned = xTaskCreate(&task_do_Update_ZIP, "task_do_Update_ZIP", configMINIMAL_STACK_SIZE * 35, NULL, tskIDLE_PRIORITY+1, NULL); - TickType_t xDelay; - xDelay = 2000000 / portTICK_PERIOD_MS; - ESP_LOGD(TAG, "Wait for Update to be finished: sleep for: %ldms", (long) xDelay); - vTaskDelay( xDelay ); -} - - - -static void infinite_loop(void) -{ - int i = 0; - ESP_LOGI(TAG, "When a new firmware is available on the server, press the reset button to download it"); - while(1) { - ESP_LOGI(TAG, "Waiting for a new firmware... %d", ++i); - vTaskDelay(2000 / portTICK_PERIOD_MS); + while(1) { // wait until reboot within task_do_Update_ZIP + vTaskDelay(1000 / portTICK_PERIOD_MS); } } + static bool ota_update_task(std::string fn) { esp_err_t err; @@ -601,6 +604,9 @@ void doReboot(){ xTaskCreate(&task_reboot, "reboot", configMINIMAL_STACK_SIZE * 64, NULL, 10, NULL); // KillTFliteTasks(); // kills itself gpio_handler_destroy(); + #ifdef ENABLE_MQTT + MQTTdestroy_client(); + #endif //ENABLE_MQTT vTaskDelay(5000 / portTICK_PERIOD_MS); esp_restart(); hard_restart(); diff --git a/code/components/jomjol_flowcontroll/ClassFlowImage.cpp b/code/components/jomjol_flowcontroll/ClassFlowImage.cpp index 0816a42d..89732104 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowImage.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowImage.cpp @@ -93,7 +93,7 @@ void ClassFlowImage::RemoveOldLogs() if (!isLogImage) return; - ESP_LOGI(TAG, "remove old images"); + ESP_LOGD(TAG, "remove old images"); if (logfileRetentionInDays == 0) { return; } @@ -132,7 +132,7 @@ void ClassFlowImage::RemoveOldLogs() } } } - ESP_LOGI(TAG, "Image folder deleted: %d | Image folder not deleted: %d", deleted, notDeleted); + ESP_LOGD(TAG, "Image folder deleted: %d | Image folder not deleted: %d", deleted, notDeleted); closedir(dir); } diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index c7078831..3ad5f8de 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -189,36 +189,26 @@ string ClassFlowMQTT::GetMQTTMainTopic() } -bool ClassFlowMQTT::Start(float AutoIntervall) { - -// printf("URI: %s, MAINTOPIC: %s", uri.c_str(), maintopic.c_str()); - - if ((uri.length() == 0) || (maintopic.length() == 0)) - { - LogFile.WriteToFile(ESP_LOG_INFO, TAG, "MQTT not started because URI or Maintopic is not set. MQTT will be disabled."); - MQTTdisable(); - return false; - } - +bool ClassFlowMQTT::Start(float AutoIntervall) +{ roundInterval = AutoIntervall; // Minutes keepAlive = roundInterval * 60 * 2.5; // Seconds, make sure it is greater thatn 2 rounds! std::stringstream stream; stream << std::fixed << std::setprecision(1) << "Digitizer interval is " << roundInterval << " minutes => setting MQTT LWT timeout to " << ((float)keepAlive/60) << " minutes."; - LogFile.WriteToFile(ESP_LOG_INFO, TAG, stream.str()); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, stream.str()); mqttServer_setParameter(flowpostprocessing->GetNumbers(), keepAlive, roundInterval); - MQTT_Configure(uri, clientname, user, password, maintopic, LWT_TOPIC, LWT_CONNECTED, LWT_DISCONNECTED, - keepAlive, SetRetainFlag, (void *)&GotConnected); + bool MQTTConfigCheck = MQTT_Configure(uri, clientname, user, password, maintopic, LWT_TOPIC, LWT_CONNECTED, + LWT_DISCONNECTED, keepAlive, SetRetainFlag, (void *)&GotConnected); - if (!MQTT_Init()) { - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init at startup failed! Retry with next publish call"); + if (!MQTTConfigCheck) { return false; } - return true; + return (MQTT_Init() == 1); } @@ -237,7 +227,7 @@ bool ClassFlowMQTT::doFlow(string zwtime) publishSystemData(); - if (flowpostprocessing) + if (flowpostprocessing && getMQTTisConnected()) { std::vector* NUMBERS = flowpostprocessing->GetNumbers(); diff --git a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp index 21a3738e..57b3e590 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp @@ -707,6 +707,8 @@ bool ClassFlowPostProcessing::doFlow(string zwtime) NUMBERS[j]->ReturnValue = ""; NUMBERS[j]->ErrorMessageText = ""; NUMBERS[j]->Value = -1; + /* TODO to be discussed, see https://github.com/jomjol/AI-on-the-edge-device/issues/1617 */ +// NUMBERS[j]->lastvalue = imagetime; // must only be set in case of good value !!! --> move to the end NUMBERS[j]->lastvalue = imagetime; UpdateNachkommaDecimalShift(); @@ -764,6 +766,9 @@ bool ClassFlowPostProcessing::doFlow(string zwtime) { string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText; LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw); + /* TODO to be discussed, see https://github.com/jomjol/AI-on-the-edge-device/issues/1617 */ + NUMBERS[j]->lastvalue = imagetime; + WriteDataLog(j); continue; // es gibt keinen Zahl, da noch ein N vorhanden ist. } @@ -848,7 +853,9 @@ bool ClassFlowPostProcessing::doFlow(string zwtime) if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue)) { - NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Rate too high - Read: " + RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma) + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma); + WriteDataLog(j); + + NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Rate too high - Read: " + RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma) + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " - Rate: " + RundeOutput(_ratedifference, NUMBERS[j]->Nachkomma); NUMBERS[j]->Value = NUMBERS[j]->PreValue; NUMBERS[j]->ReturnValue = ""; NUMBERS[j]->ReturnRateValue = ""; @@ -865,6 +872,8 @@ bool ClassFlowPostProcessing::doFlow(string zwtime) NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma); NUMBERS[j]->PreValue = NUMBERS[j]->Value; NUMBERS[j]->PreValueOkay = true; + NUMBERS[j]->lastvalue = imagetime; + NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma); diff --git a/code/components/jomjol_helper/Helper.cpp b/code/components/jomjol_helper/Helper.cpp index f5af0125..1e7e28a1 100644 --- a/code/components/jomjol_helper/Helper.cpp +++ b/code/components/jomjol_helper/Helper.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -35,6 +36,8 @@ static const char* TAG = "HELPER"; using namespace std; +unsigned int systemStatus = 0; + sdmmc_cid_t SDCardCid; sdmmc_csd_t SDCardCsd; @@ -770,6 +773,38 @@ string getMac(void) { return macFormated; } + +void setSystemStatusFlag(SystemStatusFlag_t flag) { + systemStatus = systemStatus | flag; // set bit + + char buf[20]; + snprintf(buf, sizeof(buf), "0x%08X", getSystemStatus()); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "New System Status: " + std::string(buf)); +} + +void clearSystemStatusFlag(SystemStatusFlag_t flag) { + systemStatus = systemStatus | ~flag; // clear bit + + char buf[20]; + snprintf(buf, sizeof(buf), "0x%08X", getSystemStatus()); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "New System Status: " + std::string(buf)); +} + +int getSystemStatus(void) { + return systemStatus; +} + +bool isSetSystemStatusFlag(SystemStatusFlag_t flag) { + //ESP_LOGE(TAG, "Flag (0x%08X) is set (0x%08X): %d", flag, systemStatus , ((systemStatus & flag) == flag)); + + if ((systemStatus & flag) == flag) { + return true; + } + else { + return false; + } +} + string getResetReason(void) { std::string reasonText; @@ -792,6 +827,30 @@ string getResetReason(void) { return reasonText; } +/** + * Returns the current uptime formated ad xxf xxh xxm [xxs] + */ +std::string getFormatedUptime(bool compact) { + char buf[20]; + #pragma GCC diagnostic ignored "-Wformat-truncation" + + int uptime = (uint32_t)(esp_timer_get_time()/1000/1000); // in seconds + + int days = int(floor(uptime / (3600*24))); + int hours = int(floor((uptime - days * 3600*24) / (3600))); + int minutes = int(floor((uptime - days * 3600*24 - hours * 3600) / (60))); + int seconds = uptime - days * 3600*24 - hours * 3600 - minutes * 60; + + if (compact) { + snprintf(buf, sizeof(buf), "%dd%02dh%02dm%02ds", days, hours, minutes, seconds); + } + else { + snprintf(buf, sizeof(buf), "%3dd %02dh %02dm %02ds", days, hours, minutes, seconds); + } + + return std::string(buf); +} + const char* get404(void) { return "
\n\n\n\n"
diff --git a/code/components/jomjol_helper/Helper.h b/code/components/jomjol_helper/Helper.h
index fd8ddf3d..63e324fa 100644
--- a/code/components/jomjol_helper/Helper.h
+++ b/code/components/jomjol_helper/Helper.h
@@ -63,6 +63,28 @@ string getSDCardCapacity();
 string getSDCardSectorSize();
 
 string getMac(void);
+
+
+/* Error bit fields
+   One bit per error
+   Make sure it matches https://github.com/jomjol/AI-on-the-edge-device/wiki/Error-Codes */
+enum SystemStatusFlag_t {          // One bit per error
+    // First Byte
+    SYSTEM_STATUS_PSRAM_BAD         = 1 << 0, //  1, Critical Error
+    SYSTEM_STATUS_HEAP_TOO_SMALL    = 1 << 1, //  2, Critical Error
+    SYSTEM_STATUS_CAM_BAD           = 1 << 2, //  4, Critical Error
+
+    // Second Byte
+    SYSTEM_STATUS_CAM_FB_BAD        = 1 << (0+8), //  8, Flow still might work
+    SYSTEM_STATUS_NTP_BAD           = 1 << (1+8), //  9, Flow will work but time will be wrong
+};
+
+void setSystemStatusFlag(SystemStatusFlag_t flag);
+void clearSystemStatusFlag(SystemStatusFlag_t flag);
+int getSystemStatus(void);
+bool isSetSystemStatusFlag(SystemStatusFlag_t flag);
+
 string getResetReason(void);
+std::string getFormatedUptime(bool compact);
 
 const char* get404(void);
diff --git a/code/components/jomjol_logfile/ClassLogFile.cpp b/code/components/jomjol_logfile/ClassLogFile.cpp
index e4b6b53f..99b5fc3b 100644
--- a/code/components/jomjol_logfile/ClassLogFile.cpp
+++ b/code/components/jomjol_logfile/ClassLogFile.cpp
@@ -115,7 +115,7 @@ void ClassLogFile::WriteToDedicatedFile(std::string _fn, esp_log_level_t level,
 {
     FILE* pFile;
     std::string zwtime;
-    std::string logline = "";
+    std::string ntpTime = "";
 
     if (level > loglevel) {// Only write to file if loglevel is below threshold
         return;
@@ -138,7 +138,7 @@ void ClassLogFile::WriteToDedicatedFile(std::string _fn, esp_log_level_t level,
             strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
 
             zwtime = std::string(buffer);
-            logline = zwtime;
+            ntpTime = zwtime;
         }
 
         std::string loglevelString; 
@@ -163,11 +163,11 @@ void ClassLogFile::WriteToDedicatedFile(std::string _fn, esp_log_level_t level,
                 loglevelString = "NONE";
                 break;
         }
-        
-        char uptime[20];
-        snprintf(uptime, sizeof(uptime), "%8d", (uint32_t)(esp_timer_get_time()/1000/1000)); // in seconds
-        logline = "[" + std::string(uptime) + "] "  + logline + "\t<" + loglevelString + ">\t" + message + "\n";
-        fputs(logline.c_str(), pFile);
+
+        std::string formatedUptime = getFormatedUptime(true);
+
+        ntpTime = "[" + formatedUptime + "] "  + ntpTime + "\t<" + loglevelString + ">\t" + message + "\n";
+        fputs(ntpTime.c_str(), pFile);
         fclose(pFile);    
     } else {
         ESP_LOGE(TAG, "Can't open log file %s", _fn.c_str());
@@ -298,7 +298,7 @@ void ClassLogFile::RemoveOldLogFile()
         return;
     }
 
-    ESP_LOGI(TAG, "Remove old log files");
+    ESP_LOGD(TAG, "Remove old log files");
 
     time_t rawtime;
     struct tm* timeinfo;
@@ -350,7 +350,7 @@ void ClassLogFile::RemoveOldDataLog()
         return;
     }
 
-    ESP_LOGI(TAG, "Remove old data files");
+    ESP_LOGD(TAG, "Remove old data files");
 
     time_t rawtime;
     struct tm* timeinfo;
diff --git a/code/components/jomjol_mqtt/CMakeLists.txt b/code/components/jomjol_mqtt/CMakeLists.txt
index be8b3ae6..6e59dc68 100644
--- a/code/components/jomjol_mqtt/CMakeLists.txt
+++ b/code/components/jomjol_mqtt/CMakeLists.txt
@@ -2,4 +2,4 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.*)
 
 idf_component_register(SRCS ${app_sources}
                     INCLUDE_DIRS "."
-                    REQUIRES tflite-lib mqtt  jomjol_tfliteclass jomjol_helper jomjol_mqtt jomjol_wlan)
+                    REQUIRES tflite-lib mqtt jomjol_tfliteclass jomjol_helper jomjol_mqtt jomjol_wlan)
diff --git a/code/components/jomjol_mqtt/interface_mqtt.cpp b/code/components/jomjol_mqtt/interface_mqtt.cpp
index 4d9c80fd..ec6848e9 100644
--- a/code/components/jomjol_mqtt/interface_mqtt.cpp
+++ b/code/components/jomjol_mqtt/interface_mqtt.cpp
@@ -1,8 +1,8 @@
 #ifdef ENABLE_MQTT
 #include "interface_mqtt.h"
 
-//#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
 #include "esp_log.h"
+#include "connect_wlan.h"
 #include "mqtt_client.h"
 #include "ClassLogFile.h"
 #include "server_tflite.h"
@@ -18,86 +18,93 @@ std::map>* subscribeFu
 
 
 int failedOnRound = -1;
-
-bool MQTT_Enabled = true;
  
-esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
+esp_mqtt_event_id_t esp_mqtt_ID = MQTT_EVENT_ANY;
 // ESP_EVENT_ANY_ID
 
+bool mqtt_enabled = false;
+bool mqtt_configOK = false;
 bool mqtt_initialized = false;
 bool mqtt_connected = false;
+
 esp_mqtt_client_handle_t client = NULL;
 std::string uri, client_id, lwt_topic, lwt_connected, lwt_disconnected, user, password, maintopic;
 int keepalive, SetRetainFlag;
 void (*callbackOnConnected)(std::string, int) = NULL;
 
 
-void MQTTdisable()
+bool MQTTPublish(std::string _key, std::string _content, int retained_flag) 
 {
-    MQTT_Enabled = false;
-}
-
-bool MQTTPublish(std::string _key, std::string _content, int retained_flag) {
-    int msg_id;
-    std::string zw;
-
-    if (failedOnRound == getCountFlowRounds()) { // we already failed in this round, do not retry until the next round
-        return true; // Fail quietly
+    if (!mqtt_enabled) {                            // MQTT sevice not started / configured (MQTT_Init not called before)      
+        return false;
     }
 
+    if (failedOnRound == getCountFlowRounds()) {    // we already failed in this round, do not retry until the next round
+        return true; // Fail quietly
+    }
 
     #ifdef DEBUG_DETAIL_ON  
         LogFile.WriteHeapInfo("MQTT Publish");
     #endif
 
-    if (!mqtt_initialized) {
-        if (!MQTT_Init()) {
-            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init failed, skipping all MQTT publishings in this round!");
-            failedOnRound = getCountFlowRounds();
-            return false;
+    MQTT_Init(); // Re-Init client if not initialized yet/anymore
+
+    if (mqtt_initialized && mqtt_connected) {
+        #ifdef DEBUG_DETAIL_ON 
+            long long int starttime = esp_timer_get_time();
+        #endif
+        int msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag);
+        #ifdef DEBUG_DETAIL_ON 
+            ESP_LOGD(TAG, "Publish msg_id %d in %lld ms", msg_id, (esp_timer_get_time() - starttime)/1000);
+        #endif
+        if (msg_id == -1) {
+            LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Failed to publish topic '" + _key + "', re-trying...");   
+            #ifdef DEBUG_DETAIL_ON 
+                starttime = esp_timer_get_time();
+            #endif
+            msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag);
+            #ifdef DEBUG_DETAIL_ON 
+                ESP_LOGD(TAG, "Publish msg_id %d in %lld ms", msg_id, (esp_timer_get_time() - starttime)/1000);
+            #endif
+            if (msg_id == -1) {
+                LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to publish topic '" + _key + "', skipping all MQTT publishings in this round!");
+                failedOnRound = getCountFlowRounds();
+                return false;
+            }
         }
-    }
 
-    msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag);
-    if (msg_id < 0) {
-        LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Failed to publish topic '" + _key + "', re-trying...");
-        esp_mqtt_client_reconnect(client);
-        
-        msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag);
-        if (msg_id < 0) {
-            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to publish topic '" + _key + "', skipping all MQTT publishings in this round!");
-            failedOnRound = getCountFlowRounds();
-            return false;
+        if (_content.length() > 80) { // Truncate message if too long
+            _content.resize(80);
+            _content.append("..");
         }
+
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Published topic: " + _key + ", content: " + _content + " (msg_id=" + std::to_string(msg_id) + ")");
+        return true;
     }
-
-    if (_content.length() > 80) { // Truncate message if too long
-        _content.resize(80);
-        _content.append("..");
+    else {
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publish skipped. Client not initalized or not connected. (topic: " + _key + ")");
+        return false;
     }
-
-    zw = "Published topic: " + _key + ", content: " + _content + " (msg_id=" + std::to_string(msg_id) + ")";
-    LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw);
-
-    return true;
 }
 
 
-static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
-{
+static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
     int msg_id;
     std::string topic = "";
     switch (event->event_id) {
         case MQTT_EVENT_BEFORE_CONNECT:
             ESP_LOGD(TAG, "MQTT_EVENT_BEFORE_CONNECT");
+            mqtt_initialized = true;
             break;
         case MQTT_EVENT_CONNECTED:
             ESP_LOGD(TAG, "MQTT_EVENT_CONNECTED");
+            mqtt_initialized = true;
             mqtt_connected = true;
             MQTTconnected();
             break;
         case MQTT_EVENT_DISCONNECTED:
             ESP_LOGD(TAG, "MQTT_EVENT_DISCONNECTED");
+            LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Disconnected from broker");
             mqtt_connected = false;
             break;
         case MQTT_EVENT_SUBSCRIBED:
@@ -126,8 +133,15 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
             }
             break;
         case MQTT_EVENT_ERROR:
-            ESP_LOGD(TAG, "MQTT_EVENT_ERROR");
-            mqtt_initialized = false; // Force re-init on next publish call
+            #ifdef DEBUG_DETAIL_ON 
+                ESP_LOGD(TAG, "MQTT_EVENT_ERROR - esp_mqtt_error_codes:");
+                ESP_LOGD(TAG, "error_type:%d", event->error_handle->error_type);
+                ESP_LOGD(TAG, "connect_return_code:%d", event->error_handle->connect_return_code);
+                ESP_LOGD(TAG, "esp_transport_sock_errno:%d", event->error_handle->esp_transport_sock_errno);
+                ESP_LOGD(TAG, "esp_tls_last_esp_err:%d", event->error_handle->esp_tls_last_esp_err);
+                ESP_LOGD(TAG, "esp_tls_stack_err:%d", event->error_handle->esp_tls_stack_err);
+                ESP_LOGD(TAG, "esp_tls_cert_verify_flags:%d", event->error_handle->esp_tls_cert_verify_flags);
+            #endif
             mqtt_connected = false;
             break;
         default:
@@ -143,16 +157,14 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
 }
 
 
-void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
+bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
         std::string _maintopic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
-        int _keepalive, int _SetRetainFlag, void *_callbackOnConnected){
-#ifdef __HIDE_PASSWORD
-    LogFile.WriteToFile(ESP_LOG_INFO, TAG, "URI: " + _mqttURI + ", clientname: " + _clientid + 
-            ", user: " + _user + ", password: XXXXXXXX, maintopic: " + _maintopic + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive)); 
-#else
-    LogFile.WriteToFile(ESP_LOG_INFO, TAG, "URI: " + _mqttURI + ", clientname: " + _clientid + 
-            ", user: " + _user + ", password: " + _password + ", maintopic: " + _maintopic + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive)); 
-#endif
+                    int _keepalive, int _SetRetainFlag, void *_callbackOnConnected) {
+    if ((_mqttURI.length() == 0) || (_maintopic.length() == 0) || (_clientid.length() == 0)) 
+    {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init aborted! Config error (URI, MainTopic or ClientID missing)");
+        return false;
+    }
 
     uri = _mqttURI;
     client_id = _clientid;
@@ -168,42 +180,60 @@ void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
         user = _user;
         password = _password;
     }
+
+    #ifdef __HIDE_PASSWORD
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "URI: " + uri + ", clientname: " + client_id + ", user: " + user + ", password: XXXXXXXX, maintopic: "
+                            + maintopic + ", last-will-topic: " + lwt_topic + ", keepAlive: " + std::to_string(keepalive) + ", RetainFlag: " + std::to_string(SetRetainFlag)); 
+    #else
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "URI: " + uri + ", clientname: " + client_id + ", user: " + user + ", password: " + password  + ", maintopic: "
+                            + maintopic + ", last-will-topic: " + lwt_topic + ", keepAlive: " + std::to_string(keepalive)  + ", RetainFlag: " + std::to_string(SetRetainFlag)); 
+     #endif
+
+    mqtt_configOK = true;
+    return true;
 }
 
-bool MQTT_Init() {
 
-    if (MQTT_Enabled == false)
-        return false;
-
-    if ((client_id.length() == 0) || (lwt_topic.length() == 0))
-    {
-        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, std::string("Init with no Client_ID (" + client_id + ") or Last Will Topic (" + lwt_topic + "). Abort Init!"));
-        return false;
+int MQTT_Init() { 
+    if (mqtt_initialized) {
+        return 0;
     }
-    
-    esp_err_t ret;
-    LogFile.WriteToFile(ESP_LOG_INFO, TAG, std::string("Init"));
 
+    if (mqtt_configOK) {                           
+        mqtt_enabled = true;
+    } else {
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init called, but client is not yet configured.");
+        return 0;
+    }
+
+    if (!getWIFIisConnected()) {
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init called, but WIFI is not yet connected.");
+        return 0;
+    }
+
+    LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Init");
     MQTTdestroy_client();
 
-    std::string lw = lwt_disconnected;
-
     esp_mqtt_client_config_t mqtt_cfg = {
         .uri = uri.c_str(),
         .client_id = client_id.c_str(),
         .lwt_topic = lwt_topic.c_str(),
-        .lwt_msg = lw.c_str(),
+        .lwt_msg = lwt_disconnected.c_str(),
         .lwt_retain = 1,
-        .lwt_msg_len = (int)(lw.length()),
+        .lwt_msg_len = (int)(lwt_disconnected.length()),
         .keepalive = keepalive,
-        .disable_auto_reconnect = false,        // Reconnection routine active
-        .reconnect_timeout_ms = 10000           // Try to reconnect to broker every 10s
+        .disable_auto_reconnect = false,        // Reconnection routine active (Default: false)
+        .buffer_size = 1536,                    // size of MQTT send/receive buffer (Default: 1024)
+        .reconnect_timeout_ms = 15000,          // Try to reconnect to broker (Default: 10000ms)
+        .network_timeout_ms = 20000,            // Network Timeout (Default: 10000ms)
+        .message_retransmit_timeout = 3000      // Tiem after message resent when broker not acknowledged (QoS1, QoS2)
+
     };
 
     if (user.length() && password.length()){
         mqtt_cfg.username = user.c_str();
         mqtt_cfg.password = password.c_str();
-    };
+    }
 
     #ifdef DEBUG_DETAIL_ON  
         LogFile.WriteHeapInfo("MQTT Client Init");
@@ -212,12 +242,12 @@ bool MQTT_Init() {
     client = esp_mqtt_client_init(&mqtt_cfg);
     if (client)
     {
-        ret = esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client);
+        esp_err_t ret = esp_mqtt_client_register_event(client, esp_mqtt_ID, mqtt_event_handler, client);
         if (ret != ESP_OK)
         {
             LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Could not register event (ret=" + std::to_string(ret) + ")!");
             mqtt_initialized = false;
-            return false;
+            return -1;
         }
 
         #ifdef DEBUG_DETAIL_ON  
@@ -226,40 +256,44 @@ bool MQTT_Init() {
         ret = esp_mqtt_client_start(client);
         if (ret != ESP_OK)
         {
-            LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Could not start client (ret=" + std::to_string(ret) + "), retrying...");
-            ret = esp_mqtt_client_start(client);
-            if (ret != ESP_OK)
-            {
-                LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Could not start client (ret=" + std::to_string(ret) + ")!");
-                mqtt_initialized = false;
-                return false;
-            }
+            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Client start failed (retval=" + std::to_string(ret) + ")!");
+            mqtt_initialized = false;
+            return -1;
+        }
+        else {
+            LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Client started, waiting for established connection...");
+            mqtt_initialized = true;
+            return 1;
         }
     }
     else
     {
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init failed, no handle created!");
         mqtt_initialized = false;
-        return false;
+        return -1;
     }
 
-    LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Client started, waiting for established connection...");
-    mqtt_initialized = true;
-    return true;
 }
 
 
 void MQTTdestroy_client() {
     if (client) {
+        if (mqtt_connected) {
+            esp_mqtt_client_disconnect(client);
+            mqtt_connected = false;
+        }        
         esp_mqtt_client_stop(client);
         esp_mqtt_client_destroy(client);
         client = NULL;
         mqtt_initialized = false;
-        mqtt_connected = false;
     }
 }
 
-bool MQTTisConnected() {
+bool getMQTTisEnabled() {
+    return mqtt_enabled;
+}
+
+bool getMQTTisConnected() {
     return mqtt_connected;
 }
 
@@ -347,4 +381,4 @@ void MQTTdestroySubscribeFunction(){
         subscribeFunktionMap = NULL;
     }
 }
-#endif //ENABLE_MQTT
\ No newline at end of file
+#endif //ENABLE_MQTT
diff --git a/code/components/jomjol_mqtt/interface_mqtt.h b/code/components/jomjol_mqtt/interface_mqtt.h
index e79fa8c7..23f67892 100644
--- a/code/components/jomjol_mqtt/interface_mqtt.h
+++ b/code/components/jomjol_mqtt/interface_mqtt.h
@@ -6,15 +6,16 @@
 #include 
 #include 
 
-void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
-        std::string _maintopic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
-        int _keepalive, int SetRetainFlag, void *callbackOnConnected);
-bool MQTT_Init();
+bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
+                    std::string _maintopic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
+                    int _keepalive, int SetRetainFlag, void *callbackOnConnected);
+int MQTT_Init();
 void MQTTdestroy_client();
 
 bool MQTTPublish(std::string _key, std::string _content, int retained_flag = 1);            // retained Flag as Standart
 
-bool MQTTisConnected();
+bool getMQTTisEnabled();
+bool getMQTTisConnected();
 
 void MQTTregisterConnectFunction(std::string name, std::function func);
 void MQTTunregisterConnectFunction(std::string name);
diff --git a/code/components/jomjol_mqtt/server_mqtt.cpp b/code/components/jomjol_mqtt/server_mqtt.cpp
index 2ad4003a..0b31ac0f 100644
--- a/code/components/jomjol_mqtt/server_mqtt.cpp
+++ b/code/components/jomjol_mqtt/server_mqtt.cpp
@@ -128,7 +128,10 @@ void sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
     MQTTPublish(topicFull, payload, true);
 }
 
-void MQTThomeassistantDiscovery() {    
+void MQTThomeassistantDiscovery() {  
+    if (!getMQTTisConnected()) 
+        return;
+
     LogFile.WriteToFile(ESP_LOG_INFO, TAG, "MQTT - Sending Homeassistant Discovery Topics (Meter Type: " + meterType + ", Value Unit: " + valueUnit + " , Rate Unit: " + rateUnit + ")...");
 
     //                              Group | Field            | User Friendly Name | Icon                      | Unit | Device Class     | State Class  | Entity Category
@@ -161,6 +164,9 @@ void MQTThomeassistantDiscovery() {
 }
 
 void publishSystemData() {
+    if (!getMQTTisConnected()) 
+        return;
+
     char tmp_char[50];
 
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publishing system MQTT topics...");
@@ -180,6 +186,9 @@ void publishSystemData() {
 
 
 void publishStaticData() {
+    if (!getMQTTisConnected()) 
+        return;
+
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publishing static MQTT topics...");
     MQTTPublish(maintopic + "/" + "MAC", getMac(), retainFlag);
     MQTTPublish(maintopic + "/" + "IP", *getIPAddress(), retainFlag);
@@ -204,6 +213,7 @@ esp_err_t sendDiscovery_and_static_Topics(httpd_req_t *req) {
 }
 
 void GotConnected(std::string maintopic, int retainFlag) {
+    vTaskDelay(10000 / portTICK_PERIOD_MS);     // Delay execution by 10s after connection got established   
     if (HomeassistantDiscovery) {
         MQTThomeassistantDiscovery();
     }
diff --git a/code/components/jomjol_tfliteclass/server_tflite.cpp b/code/components/jomjol_tfliteclass/server_tflite.cpp
index e46a0db9..6d4574fd 100644
--- a/code/components/jomjol_tfliteclass/server_tflite.cpp
+++ b/code/components/jomjol_tfliteclass/server_tflite.cpp
@@ -31,6 +31,7 @@ ClassFlowControll tfliteflow;
 TaskHandle_t xHandleblink_task_doFlow = NULL;
 TaskHandle_t xHandletask_autodoFlow = NULL;
 
+bool FlowInitDone = false;
 bool flowisrunning = false;
 
 long auto_intervall = 0;
@@ -47,17 +48,18 @@ int getCountFlowRounds() {
 }
 
 
-
 esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
 {
     return tfliteflow.GetJPGStream(_filename, req);
 }
 
+
 esp_err_t GetRawJPG(httpd_req_t *req)
 {
     return tfliteflow.SendRawJPG(req);
 }
 
+
 bool isSetupModusActive() {
     return tfliteflow.getStatusSetupModus();
     return false;
@@ -66,70 +68,72 @@ bool isSetupModusActive() {
 
 void KillTFliteTasks()
 {
-#ifdef DEBUG_DETAIL_ON          
-    ESP_LOGD(TAG, "Handle: xHandleblink_task_doFlow: %ld", (long) xHandleblink_task_doFlow);
-#endif  
+    #ifdef DEBUG_DETAIL_ON          
+        ESP_LOGD(TAG, "Handle: xHandleblink_task_doFlow: %ld", (long) xHandleblink_task_doFlow);
+    #endif  
     if (xHandleblink_task_doFlow != NULL)
     {
         TaskHandle_t xHandleblink_task_doFlowTmp = xHandleblink_task_doFlow;
         xHandleblink_task_doFlow = NULL;
         vTaskDelete(xHandleblink_task_doFlowTmp);
-#ifdef DEBUG_DETAIL_ON      
-        ESP_LOGD(TAG, "Killed: xHandleblink_task_doFlow");
-#endif
+        #ifdef DEBUG_DETAIL_ON      
+            ESP_LOGD(TAG, "Killed: xHandleblink_task_doFlow");
+        #endif
     }
 
-#ifdef DEBUG_DETAIL_ON      
-    ESP_LOGD(TAG, "Handle: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
-#endif
+    #ifdef DEBUG_DETAIL_ON      
+        ESP_LOGD(TAG, "Handle: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
+    #endif
     if (xHandletask_autodoFlow != NULL)
     {
         TaskHandle_t xHandletask_autodoFlowTmp = xHandletask_autodoFlow;
         xHandletask_autodoFlow = NULL;
         vTaskDelete(xHandletask_autodoFlowTmp);
-#ifdef DEBUG_DETAIL_ON      
-        ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
-#endif
+        #ifdef DEBUG_DETAIL_ON      
+            ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
+        #endif
     }
 
 }
 
+
 void doInit(void)
 {
-#ifdef DEBUG_DETAIL_ON             
-    ESP_LOGD(TAG, "Start tfliteflow.InitFlow(config);");
-#endif
+    #ifdef DEBUG_DETAIL_ON             
+        ESP_LOGD(TAG, "Start tfliteflow.InitFlow(config);");
+    #endif
     tfliteflow.InitFlow(CONFIG_FILE);
-#ifdef DEBUG_DETAIL_ON      
-    ESP_LOGD(TAG, "Finished tfliteflow.InitFlow(config);");
-#endif
+    FlowInitDone = true;
+    #ifdef DEBUG_DETAIL_ON      
+        ESP_LOGD(TAG, "Finished tfliteflow.InitFlow(config);");
+    #endif
     
-#ifdef ENABLE_MQTT
-    tfliteflow.StartMQTTService();
-#endif //ENABLE_MQTT
+    #ifdef ENABLE_MQTT
+        tfliteflow.StartMQTTService();
+    #endif //ENABLE_MQTT
 }
 
 
 bool doflow(void)
-{
-    
+{   
     std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
     ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
     flowisrunning = true;
     tfliteflow.doFlow(zw_time);
     flowisrunning = false;
 
-#ifdef DEBUG_DETAIL_ON      
-    ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
-#endif    
+    #ifdef DEBUG_DETAIL_ON      
+        ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
+    #endif    
     return true;
 }
 
+
 void blink_task_doFlow(void *pvParameter)
 {
-#ifdef DEBUG_DETAIL_ON          
-    ESP_LOGD(TAG, "blink_task_doFlow");
-#endif
+    #ifdef DEBUG_DETAIL_ON          
+        ESP_LOGD(TAG, "blink_task_doFlow");
+    #endif
     if (!flowisrunning)
     {
         flowisrunning = true;
@@ -143,10 +147,10 @@ void blink_task_doFlow(void *pvParameter)
 
 esp_err_t handler_init(httpd_req_t *req)
 {
-#ifdef DEBUG_DETAIL_ON      
-    LogFile.WriteHeapInfo("handler_init - Start");       
-    ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
-#endif
+    #ifdef DEBUG_DETAIL_ON      
+        LogFile.WriteHeapInfo("handler_init - Start");       
+        ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
+    #endif
 
     const char* resp_str = "Init started
"; httpd_resp_send(req, resp_str, strlen(resp_str)); @@ -158,18 +162,19 @@ esp_err_t handler_init(httpd_req_t *req) /* Respond with an empty chunk to signal HTTP response completion */ httpd_resp_send_chunk(req, NULL, 0); -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_init - Done"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_init - Done"); + #endif return ESP_OK; -}; +} + esp_err_t handler_doflow(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_doflow - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_doflow - Start"); + #endif ESP_LOGD(TAG, "handler_doFlow uri: %s", req->uri); @@ -188,199 +193,208 @@ esp_err_t handler_doflow(httpd_req_t *req) /* Respond with an empty chunk to signal HTTP response completion */ httpd_resp_send_chunk(req, NULL, 0); -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_doflow - Done"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_doflow - Done"); + #endif return ESP_OK; -}; +} esp_err_t handler_json(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_json - Start"); -#endif - + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_json - Start"); + #endif ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri); - - httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_set_type(req, "application/json"); - - std::string zw = tfliteflow.getJSON(); - if (zw.length() > 0) + + if (FlowInitDone) { - httpd_resp_send(req, zw.c_str(), zw.length()); + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + httpd_resp_set_type(req, "application/json"); + + std::string zw = tfliteflow.getJSON(); + if (zw.length() > 0) + { + httpd_resp_send(req, zw.c_str(), zw.length()); + } + else + { + httpd_resp_send(req, NULL, 0); + } } - else + else { - httpd_resp_send(req, NULL, 0); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "JSON API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; } -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_JSON - Done"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_JSON - Done"); + #endif return ESP_OK; -}; - +} esp_err_t handler_wasserzaehler(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_wasserzaehler - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_wasserzaehler - Start"); + #endif - bool _rawValue = false; - bool _noerror = false; - bool _all = false; - std::string _type = "value"; - string zw; - - ESP_LOGD(TAG, "handler_wasserzaehler uri: %s", req->uri); - - char _query[100]; - char _size[10]; - - if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK) + if (FlowInitDone) { -// ESP_LOGD(TAG, "Query: %s", _query); - if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK) + bool _rawValue = false; + bool _noerror = false; + bool _all = false; + std::string _type = "value"; + string zw; + + ESP_LOGD(TAG, "handler_wasserzaehler uri: %s", req->uri); + + char _query[100]; + char _size[10]; + + if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK) { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "all is found%s", _size); -#endif - _all = true; + // ESP_LOGD(TAG, "Query: %s", _query); + if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK) + { + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "all is found%s", _size); + #endif + _all = true; + } + + if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK) + { + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "all is found: %s", _size); + #endif + _type = std::string(_size); + } + + if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK) + { + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "rawvalue is found: %s", _size); + #endif + _rawValue = true; + } + if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK) + { + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "noerror is found: %s", _size); + #endif + _noerror = true; + } + } + + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + + if (_all) + { + httpd_resp_set_type(req, "text/plain"); + ESP_LOGD(TAG, "TYPE: %s", _type.c_str()); + int _intype = READOUT_TYPE_VALUE; + if (_type == "prevalue") + _intype = READOUT_TYPE_PREVALUE; + if (_type == "raw") + _intype = READOUT_TYPE_RAWVALUE; + if (_type == "error") + _intype = READOUT_TYPE_ERROR; + + + zw = tfliteflow.getReadoutAll(_intype); + ESP_LOGD(TAG, "ZW: %s", zw.c_str()); + if (zw.length() > 0) + httpd_resp_sendstr_chunk(req, zw.c_str()); + httpd_resp_sendstr_chunk(req, NULL); + return ESP_OK; } - if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK) - { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "all is found: %s", _size); -#endif - _type = std::string(_size); - } - - if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK) - { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "rawvalue is found: %s", _size); -#endif - _rawValue = true; - } - if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK) - { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "noerror is found: %s", _size); -#endif - _noerror = true; - } - } - - httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - - if (_all) - { - httpd_resp_set_type(req, "text/plain"); - ESP_LOGD(TAG, "TYPE: %s", _type.c_str()); - int _intype = READOUT_TYPE_VALUE; - if (_type == "prevalue") - _intype = READOUT_TYPE_PREVALUE; - if (_type == "raw") - _intype = READOUT_TYPE_RAWVALUE; - if (_type == "error") - _intype = READOUT_TYPE_ERROR; - - - zw = tfliteflow.getReadoutAll(_intype); - ESP_LOGD(TAG, "ZW: %s", zw.c_str()); + zw = tfliteflow.getReadout(_rawValue, _noerror); if (zw.length() > 0) httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); - return ESP_OK; - } - zw = tfliteflow.getReadout(_rawValue, _noerror); - if (zw.length() > 0) - httpd_resp_sendstr_chunk(req, zw.c_str()); - - string query = std::string(_query); -// ESP_LOGD(TAG, "Query: %s, query.c_str()); - if (query.find("full") != std::string::npos) - { - string txt, zw; - - txt = "

Aligned Image:

\n"; - txt = txt + "Digital Counter:

"; - httpd_resp_sendstr_chunk(req, txt.c_str()); - - std::vector htmlinfodig; - htmlinfodig = tfliteflow.GetAllDigital(); - - for (int i = 0; i < htmlinfodig.size(); ++i) + string query = std::string(_query); + // ESP_LOGD(TAG, "Query: %s, query.c_str()); + if (query.find("full") != std::string::npos) { - if (tfliteflow.GetTypeDigital() == Digital) - { - if (htmlinfodig[i]->val == 10) - zw = "NaN"; - else - zw = to_string((int) htmlinfodig[i]->val); + string txt, zw; + + txt = "

Aligned Image:

\n"; + txt = txt + "Digital Counter:

"; + httpd_resp_sendstr_chunk(req, txt.c_str()); + + std::vector htmlinfodig; + htmlinfodig = tfliteflow.GetAllDigital(); - txt = "filename + "\"> " + zw; + for (int i = 0; i < htmlinfodig.size(); ++i) + { + if (tfliteflow.GetTypeDigital() == Digital) + { + if (htmlinfodig[i]->val == 10) + zw = "NaN"; + else + zw = to_string((int) htmlinfodig[i]->val); + + txt = "filename + "\"> " + zw; + } + else + { + std::stringstream stream; + stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val; + zw = stream.str(); + + txt = "filename + "\"> " + zw; + } + httpd_resp_sendstr_chunk(req, txt.c_str()); + delete htmlinfodig[i]; } - else + htmlinfodig.clear(); + + txt = "

Analog Meter:

"; + httpd_resp_sendstr_chunk(req, txt.c_str()); + + std::vector htmlinfoana; + htmlinfoana = tfliteflow.GetAllAnalog(); + for (int i = 0; i < htmlinfoana.size(); ++i) { std::stringstream stream; - stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val; + stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val; zw = stream.str(); - txt = "filename + "\"> " + zw; + txt = "filename + "\"> " + zw; + httpd_resp_sendstr_chunk(req, txt.c_str()); + delete htmlinfoana[i]; } - httpd_resp_sendstr_chunk(req, txt.c_str()); - delete htmlinfodig[i]; - } - htmlinfodig.clear(); - - txt = "

Analog Meter:

"; - httpd_resp_sendstr_chunk(req, txt.c_str()); - - std::vector htmlinfoana; - htmlinfoana = tfliteflow.GetAllAnalog(); - for (int i = 0; i < htmlinfoana.size(); ++i) - { - std::stringstream stream; - stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val; - zw = stream.str(); + htmlinfoana.clear(); - txt = "filename + "\"> " + zw; - httpd_resp_sendstr_chunk(req, txt.c_str()); - delete htmlinfoana[i]; - } - htmlinfoana.clear(); + } - } + /* Respond with an empty chunk to signal HTTP response completion */ + httpd_resp_sendstr_chunk(req, NULL); + } + else + { + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Value API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; + } - - - - - - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_sendstr_chunk(req, NULL); - -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_wasserzaehler - Done"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_wasserzaehler - Done"); + #endif return ESP_OK; -}; +} esp_err_t handler_editflow(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_editflow - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_editflow - Start"); + #endif ESP_LOGD(TAG, "handler_editflow uri: %s", req->uri); @@ -392,9 +406,9 @@ esp_err_t handler_editflow(httpd_req_t *req) { if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK) { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "task is found: %s", _valuechar); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "task is found: %s", _valuechar); + #endif _task = string(_valuechar); } } @@ -427,10 +441,10 @@ esp_err_t handler_editflow(httpd_req_t *req) httpd_query_key_value(_query, "out", _valuechar, 30); out = string(_valuechar); -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "in: %s", in.c_str()); - ESP_LOGD(TAG, "out: %s", out.c_str()); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "in: %s", in.c_str()); + ESP_LOGD(TAG, "out: %s", out.c_str()); + #endif in = "/sdcard" + in; out = "/sdcard" + out; @@ -469,14 +483,14 @@ esp_err_t handler_editflow(httpd_req_t *req) zw = string(_valuechar); dy = stoi(zw); -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "in: %s", in.c_str()); - ESP_LOGD(TAG, "out: %s", out.c_str()); - ESP_LOGD(TAG, "x: %s", zw.c_str()); - ESP_LOGD(TAG, "y: %s", zw.c_str()); - ESP_LOGD(TAG, "dx: %s", zw.c_str()); - ESP_LOGD(TAG, "dy: %s", zw.c_str()); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "in: %s", in.c_str()); + ESP_LOGD(TAG, "out: %s", out.c_str()); + ESP_LOGD(TAG, "x: %s", zw.c_str()); + ESP_LOGD(TAG, "y: %s", zw.c_str()); + ESP_LOGD(TAG, "dx: %s", zw.c_str()); + ESP_LOGD(TAG, "dy: %s", zw.c_str()); + #endif if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK) { @@ -570,103 +584,144 @@ esp_err_t handler_editflow(httpd_req_t *req) /* Respond with an empty chunk to signal HTTP response completion */ httpd_resp_sendstr_chunk(req, NULL); -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_editflow - Done"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_editflow - Done"); + #endif return ESP_OK; -}; +} esp_err_t handler_statusflow(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_prevalue - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_prevalue - Start"); + #endif - const char* resp_str; + if (FlowInitDone) + { + const char* resp_str; -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "handler_prevalue: %s", req->uri); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "handler_prevalue: %s", req->uri); + #endif - string* zw = tfliteflow.getActStatus(); - resp_str = zw->c_str(); + string* zw = tfliteflow.getActStatus(); + resp_str = zw->c_str(); - httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_send(req, resp_str, strlen(resp_str)); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + httpd_resp_send(req, resp_str, strlen(resp_str)); + /* Respond with an empty chunk to signal HTTP response completion */ + httpd_resp_send_chunk(req, NULL, 0); + } + else + { + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flowstatus API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; + } -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_prevalue - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_prevalue - Done"); + #endif return ESP_OK; -}; +} + esp_err_t handler_cputemp(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_cputemp - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_cputemp - Start"); + #endif const char* resp_str; char cputemp[20]; - sprintf(cputemp, "CPU Temp: %4.1f°C", temperatureRead()); + sprintf(cputemp, "%4.1f°C", temperatureRead()); resp_str = cputemp; httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); httpd_resp_send(req, resp_str, strlen(resp_str)); /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_send_chunk(req, NULL, 0); -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_cputemp - End"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_cputemp - End"); + #endif return ESP_OK; -}; +} + esp_err_t handler_rssi(httpd_req_t *req) { + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_rssi - Start"); + #endif + + if (getWIFIisConnected()) + { + const char* resp_str; + char rssi[20]; + + sprintf(rssi, "%idBm", get_WIFI_RSSI()); + + resp_str = rssi; + + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + httpd_resp_send(req, resp_str, strlen(resp_str)); + /* Respond with an empty chunk to signal HTTP response completion */ + httpd_resp_send_chunk(req, NULL, 0); + } + else + { + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "RSSI API not yet initialized. Please retry later..."); + return ESP_ERR_NOT_FOUND; + } + + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_rssi - End"); + #endif + + return ESP_OK; +} + + +esp_err_t handler_uptime(httpd_req_t *req) +{ + #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_rssi - Start"); + LogFile.WriteHeapInfo("handler_uptime - Start"); #endif - - const char* resp_str; - char rssi[20]; - - sprintf(rssi, "RSSI: %idBm", get_WIFI_RSSI()); - - resp_str = rssi; + + std::string formatedUptime = getFormatedUptime(false); httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_send(req, resp_str, strlen(resp_str)); + httpd_resp_send(req, formatedUptime.c_str(), strlen(formatedUptime.c_str())); /* Respond with an empty chunk to signal HTTP response completion */ httpd_resp_send_chunk(req, NULL, 0); #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_rssi - End"); + LogFile.WriteHeapInfo("handler_uptime - End"); #endif return ESP_OK; -}; +} + esp_err_t handler_prevalue(httpd_req_t *req) { -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_prevalue - Start"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_prevalue - Start"); + #endif const char* resp_str; string zw; -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "handler_prevalue: %s", req->uri); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "handler_prevalue: %s", req->uri); + #endif char _query[100]; char _size[10] = ""; @@ -674,15 +729,15 @@ esp_err_t handler_prevalue(httpd_req_t *req) if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK) { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Query: %s", _query); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Query: %s", _query); + #endif if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK) { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Value: %s", _size); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Value: %s", _size); + #endif } httpd_query_key_value(_query, "numbers", _numbers, 50); @@ -706,12 +761,13 @@ esp_err_t handler_prevalue(httpd_req_t *req) /* Respond with an empty chunk to signal HTTP response completion */ httpd_resp_send_chunk(req, NULL, 0); -#ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("handler_prevalue - End"); -#endif + #ifdef DEBUG_DETAIL_ON + LogFile.WriteHeapInfo("handler_prevalue - End"); + #endif return ESP_OK; -}; +} + void task_autodoFlow(void *pvParameter) { @@ -721,6 +777,7 @@ void task_autodoFlow(void *pvParameter) LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Restarted due to an Exception/panic! Postponing first round start by 5 minutes to allow for an OTA or to fetch the log!"); LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Setting logfile level to DEBUG until the next reboot!"); LogFile.setLogLevel(ESP_LOG_DEBUG); + //MQTTPublish(GetMQTTMainTopic() + "/" + "status", "Postponing first round", false); vTaskDelay(60*5000 / portTICK_RATE_MS); // Wait 5 minutes to give time to do an OTA or fetch the log } @@ -744,20 +801,20 @@ void task_autodoFlow(void *pvParameter) if (flowisrunning) { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Autoflow: doFlow is already running!"); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Autoflow: doFlow is already running!"); + #endif } else { -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Autoflow: doFlow is started"); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Autoflow: doFlow is started"); + #endif flowisrunning = true; doflow(); -#ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Remove older log files"); -#endif + #ifdef DEBUG_DETAIL_ON + ESP_LOGD(TAG, "Remove older log files"); + #endif LogFile.RemoveOldLogFile(); LogFile.RemoveOldDataLog(); } @@ -784,6 +841,7 @@ void task_autodoFlow(void *pvParameter) ESP_LOGD(TAG, "task_autodoFlow: end"); } + void TFliteDoAutoStart() { BaseType_t xReturned; @@ -801,10 +859,9 @@ void TFliteDoAutoStart() ESP_LOGD(TAG, "ERROR task_autodoFlow konnte nicht erzeugt werden!"); } ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str()); - - } + #ifdef ENABLE_MQTT std::string GetMQTTMainTopic() { @@ -873,6 +930,11 @@ void register_server_tflite_uri(httpd_handle_t server) camuri.user_ctx = (void*) "Light Off"; httpd_register_uri_handler(server, &camuri); + camuri.uri = "/uptime"; + camuri.handler = handler_uptime; + camuri.user_ctx = (void*) "Light Off"; + httpd_register_uri_handler(server, &camuri); + camuri.uri = "/editflow"; camuri.handler = handler_editflow; camuri.user_ctx = (void*) "EditFlow"; diff --git a/code/components/jomjol_wlan/CMakeLists.txt b/code/components/jomjol_wlan/CMakeLists.txt index 2887f8ca..67878699 100644 --- a/code/components/jomjol_wlan/CMakeLists.txt +++ b/code/components/jomjol_wlan/CMakeLists.txt @@ -2,6 +2,6 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.*) idf_component_register(SRCS ${app_sources} INCLUDE_DIRS "." - REQUIRES nvs_flash jomjol_helper) + REQUIRES nvs_flash jomjol_helper jomjol_mqtt) diff --git a/code/components/jomjol_wlan/connect_wlan.cpp b/code/components/jomjol_wlan/connect_wlan.cpp index 0478892c..df7d5df9 100644 --- a/code/components/jomjol_wlan/connect_wlan.cpp +++ b/code/components/jomjol_wlan/connect_wlan.cpp @@ -13,6 +13,9 @@ #include "lwip/err.h" #include "lwip/sys.h" +#ifdef ENABLE_MQTT + #include "interface_mqtt.h" +#endif //ENABLE_MQTT #include #include @@ -38,6 +41,7 @@ static EventGroupHandle_t s_wifi_event_group; static const char *TAG = "WIFI"; static int s_retry_num = 0; +bool WIFIConnected = false; /////////////////////////////////////////////////////////// #define BLINK_GPIO GPIO_NUM_33 @@ -113,9 +117,11 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + WIFIConnected = false; LEDBlinkTask(200, 1, true); esp_wifi_connect(); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + WIFIConnected = false; // if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { esp_wifi_connect(); s_retry_num++; @@ -131,6 +137,14 @@ static void event_handler(void* arg, esp_event_base_t event_base, s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); LEDBlinkTask(1000, 5, true); + + WIFIConnected = true; + #ifdef ENABLE_MQTT + if (getMQTTisEnabled()) { + vTaskDelay(5000 / portTICK_PERIOD_MS); + MQTT_Init(); // Init when WIFI is getting connected + } + #endif //ENABLE_MQTT } } @@ -249,17 +263,17 @@ void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostna /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually * happened. */ if (bits & WIFI_CONNECTED_BIT) { -#ifdef __HIDE_PASSWORD - ESP_LOGI(TAG, "connected to ap SSID: %s, password: XXXXXXX", _ssid); -#else - ESP_LOGI(TAG, "connected to ap SSID: %s, password: %s", _ssid, _password); -#endif + #ifdef __HIDE_PASSWORD + ESP_LOGI(TAG, "connected to ap SSID: %s, password: XXXXXXX", _ssid); + #else + ESP_LOGI(TAG, "connected to ap SSID: %s, password: %s", _ssid, _password); + #endif } else if (bits & WIFI_FAIL_BIT) { -#ifdef __HIDE_PASSWORD - ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: XXXXXXXX", _ssid); -#else - ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: %s", _ssid, _password); -#endif + #ifdef __HIDE_PASSWORD + ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: XXXXXXXX", _ssid); + #else + ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: %s", _ssid, _password); + #endif } else { ESP_LOGE(TAG, "UNEXPECTED EVENT"); } @@ -289,3 +303,7 @@ void wifi_init_sta(const char *_ssid, const char *_password) wifi_init_sta(_ssid, _password, NULL, NULL, NULL, NULL, NULL); } +bool getWIFIisConnected() { + return WIFIConnected; +} + diff --git a/code/components/jomjol_wlan/connect_wlan.h b/code/components/jomjol_wlan/connect_wlan.h index ced17469..cb71333e 100644 --- a/code/components/jomjol_wlan/connect_wlan.h +++ b/code/components/jomjol_wlan/connect_wlan.h @@ -10,6 +10,7 @@ void wifi_init_sta(const char *_ssid, const char *_password); std::string* getIPAddress(); std::string* getSSID(); int get_WIFI_RSSI(); +bool getWIFIisConnected(); extern std::string hostname; extern std::string std_hostname; diff --git a/code/main/CMakeLists.txt b/code/main/CMakeLists.txt index 4776f371..e2572d5a 100644 --- a/code/main/CMakeLists.txt +++ b/code/main/CMakeLists.txt @@ -65,5 +65,6 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/main/*.*) # idf_component_register(SRCS ${app_sources}) idf_component_register(SRCS ${app_sources} - INCLUDE_DIRS ".") - + INCLUDE_DIRS "." + # REQUIRES esp_psram) # comming in IDF 5.0 + ) \ No newline at end of file diff --git a/code/main/main.cpp b/code/main/main.cpp index 614967e2..4fcebf52 100644 --- a/code/main/main.cpp +++ b/code/main/main.cpp @@ -5,6 +5,8 @@ #include "driver/gpio.h" #include "sdkconfig.h" +//#include "esp_psram.h" // Comming in IDF 5.0, see https://docs.espressif.com/projects/esp-idf/en/v5.0-beta1/esp32/migration-guides/release-5.x/system.html?highlight=esp_psram_get_size +#include "esp32/spiram.h" // SD-Card //////////////////// #include "nvs_flash.h" @@ -122,7 +124,7 @@ bool Init_NVS_SDCard() return true; } -void task_NoSDBlink(void *pvParameter) +void task_MainInitError_blink(void *pvParameter) { gpio_pad_select_gpio(BLINK_GPIO); gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT); @@ -147,7 +149,6 @@ void task_NoSDBlink(void *pvParameter) extern "C" void app_main(void) { TickType_t xDelay; - bool initSucessful = true; ESP_LOGI(TAG, "\n\n\n\n\n"); // Add mark on log to see when it restarted @@ -160,9 +161,9 @@ extern "C" void app_main(void) if (!Init_NVS_SDCard()) { - xTaskCreate(&task_NoSDBlink, "task_NoSDBlink", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, NULL); - return; - }; + xTaskCreate(&task_MainInitError_blink, "task_MainInitError_blink", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, NULL); + return; // No way to continue without SD-Card! + } string versionFormated = "Branch: '" + std::string(GIT_BRANCH) + \ "', Revision: " + std::string(GIT_REV) +", Date/Time: " + std::string(BUILD_TIME) + \ @@ -216,6 +217,7 @@ extern "C" void app_main(void) if (!setup_time()) { LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "NTP Initialization failed!"); + setSystemStatusFlag(SYSTEM_STATUS_NTP_BAD); } setBootTime(); @@ -231,13 +233,32 @@ extern "C" void app_main(void) std::string zw = gettimestring("%Y%m%d-%H%M%S"); ESP_LOGD(TAG, "time %s", zw.c_str()); + /* Check if PSRAM can be initalized */ + esp_err_t ret; + ret = esp_spiram_init(); + if (ret == ESP_FAIL) { // Failed to init PSRAM, most likely not available or broken + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to initialize PSRAM (" + std::to_string(ret) + ")!"); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Either your device misses the PSRAM chip or it is broken!"); + setSystemStatusFlag(SYSTEM_STATUS_PSRAM_BAD); + } + else { // PSRAM init ok + /* Check if PSRAM provides at least 4 MB */ + size_t psram_size = esp_spiram_get_size(); + // size_t psram_size = esp_psram_get_size(); // comming in IDF 5.0 + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "The device has " + std::to_string(psram_size/1024/1024) + " MBytes of PSRAM"); + if (psram_size < (4*1024*1024)) { // PSRAM is below 4 MBytes + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "At least 4 MBytes are required!"); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Does the device really have a 4 Mbytes PSRAM?"); + setSystemStatusFlag(SYSTEM_STATUS_PSRAM_BAD); + } + } + + /* Check available Heap memory */ size_t _hsize = getESPHeapSize(); - if (_hsize < 4000000) // Check for a bit less than 4 MB (but clearly over 2 MB) - { - std::string _zws = "Not enough PSRAM available. Expected around 4 MBytes - available: " + std::to_string((float)_hsize/1024/1024) + " MBytes!"; - _zws = _zws + "\nEither not initialized, too small (2 MByte only) or not present at all. Firmware cannot start!!"; - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zws); - } else { // Bad Camera Status, retry init + if (_hsize < 4000000) { // Check available Heap memory for a bit less than 4 MB (a test on a good device showed 4187558 bytes to be available) + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Not enough Heap memory available. Expected around 4 MBytes, but only " + std::to_string(_hsize) + " Bytes are available! That is not enough for this firmware!"); + setSystemStatusFlag(SYSTEM_STATUS_HEAP_TOO_SMALL); + } else { // Heap memory is ok if (camStatus != ESP_OK) { LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Failed to initialize camera module, retrying..."); @@ -251,7 +272,7 @@ extern "C" void app_main(void) if (camStatus != ESP_OK) { LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to initialize camera module!"); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Check that your camera module is working and connected properly!"); - initSucessful = false; + setSystemStatusFlag(SYSTEM_STATUS_CAM_BAD); } } else { // Test Camera camera_fb_t * fb = esp_camera_fb_get(); @@ -259,8 +280,8 @@ extern "C" void app_main(void) LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Camera Framebuffer cannot be initialized!"); /* Easiest would be to simply restart here and try again, how ever there seem to be systems where it fails at startup but still work corectly later. - Therefore we treat it still as successed! - //initSucessful = false; */ + Therefore we treat it still as successed! */ + setSystemStatusFlag(SYSTEM_STATUS_CAM_FB_BAD); } else { esp_camera_fb_return(fb); @@ -269,8 +290,6 @@ extern "C" void app_main(void) } } - - xDelay = 2000 / portTICK_PERIOD_MS; ESP_LOGD(TAG, "main: sleep for: %ldms", (long) xDelay*10); vTaskDelay( xDelay ); @@ -291,17 +310,24 @@ extern "C" void app_main(void) ESP_LOGD(TAG, "vor reg server main"); register_server_main_uri(server, "/sdcard"); - if (initSucessful) { + + /* Testing */ + //setSystemStatusFlag(SYSTEM_STATUS_CAM_FB_BAD); + //setSystemStatusFlag(SYSTEM_STATUS_PSRAM_BAD); + + /* Main Init has successed or only an error which allows to continue operation */ + if (getSystemStatus() == 0) { // No error flag is set LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Initialization completed successfully!"); ESP_LOGD(TAG, "vor do autostart"); TFliteDoAutoStart(); } - else { // Initialization failed - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Initialization failed. Will restart in 5 minutes!"); - vTaskDelay(60*4000 / portTICK_RATE_MS); // Wait 4 minutes to give time to do an OTA or fetch the log - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Initialization failed. Will restart in 1 minute!"); - vTaskDelay(60*1000 / portTICK_RATE_MS); // Wait 1 minute to give time to do an OTA or fetch the log - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Initialization failed. Will restart now!"); - doReboot(); + else if (isSetSystemStatusFlag(SYSTEM_STATUS_CAM_FB_BAD) || // Non critical errors occured, we try to continue... + isSetSystemStatusFlag(SYSTEM_STATUS_NTP_BAD)) { + LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Initialization completed with errors, but trying to continue..."); + ESP_LOGD(TAG, "vor do autostart"); + TFliteDoAutoStart(); + } + else { // Any other error is critical and makes running the flow impossible. + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Initialization failed. Not starting flows!"); } } diff --git a/code/main/server_main.cpp b/code/main/server_main.cpp index 3663cca3..39763e70 100644 --- a/code/main/server_main.cpp +++ b/code/main/server_main.cpp @@ -128,6 +128,14 @@ esp_err_t info_get_handler(httpd_req_t *req) httpd_resp_sendstr_chunk(req, NULL); return ESP_OK; } + else if (_task.compare("Round") == 0) + { + char formated[10] = ""; + snprintf(formated, sizeof(formated), "%d", getCountFlowRounds()); + httpd_resp_sendstr_chunk(req, formated); + httpd_resp_sendstr_chunk(req, NULL); + return ESP_OK; + } else if (_task.compare("SDCardPartitionSize") == 0) { std::string zw; @@ -230,9 +238,33 @@ esp_err_t hello_main_handler(httpd_req_t *req) } } - if (filetosend == "/sdcard/html/index.html" && isSetupModusActive()) { - ESP_LOGD(TAG, "System is in setup mode --> index.html --> setup.html"); - filetosend = "/sdcard/html/setup.html"; + if (filetosend == "/sdcard/html/index.html") { + if (isSetSystemStatusFlag(SYSTEM_STATUS_PSRAM_BAD) || // Initialization failed with crritical errors! + isSetSystemStatusFlag(SYSTEM_STATUS_CAM_BAD)) { + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "We have a critical error, not serving main page!"); + + char buf[20]; + std::string message = "

AI on the Edge Device

We have one or more critical errors:
"; + + for (int i = 0; i < 32; i++) { + if (isSetSystemStatusFlag((SystemStatusFlag_t)(1<"; + } + } + + message += "
Please check github.com/jomjol/AI-on-the-edge-device/wiki/Error-Codes for more information!"; + message += "

"; + message += " "; + message += " "; + message += " "; + httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, message.c_str()); + return ESP_FAIL; + } + else if (isSetupModusActive()) { + ESP_LOGD(TAG, "System is in setup mode --> index.html --> setup.html"); + filetosend = "/sdcard/html/setup.html"; + } } ESP_LOGD(TAG, "Filename: %s", filename); @@ -427,9 +459,9 @@ httpd_handle_t start_webserver(void) httpd_handle_t server = NULL; httpd_config_t config = { }; - config.task_priority = tskIDLE_PRIORITY+1; // 20210924 --> vorher +5 + config.task_priority = tskIDLE_PRIORITY+3; //20221211: before: tskIDLE_PRIORITY+1; // 20210924 --> vorher +5 config.stack_size = 32768; //20210921 --> vorher 32768 // bei 32k stürzt das Programm beim Bilderaufnehmen ab - config.core_id = tskNO_AFFINITY; + config.core_id = 0; //20221211 --> force all not flow related tasks to CPU0, before: tskNO_AFFINITY; config.server_port = 80; config.ctrl_port = 32768; config.max_open_sockets = 5; //20210921 --> vorher 7 @@ -437,8 +469,8 @@ httpd_handle_t start_webserver(void) config.max_resp_headers = 8; config.backlog_conn = 5; config.lru_purge_enable = true; // dadurch werden alte Verbindungen gekappt, falls neue benögt werden. - config.recv_wait_timeout = 5; // default: 5 20210924 --> vorher 30 - config.send_wait_timeout = 5; // default: 5 20210924 --> vorher 30 + config.recv_wait_timeout = 5; // default: 5 20210924 --> vorher 30 + config.send_wait_timeout = 5; // default: 5 20210924 --> vorher 30 config.global_user_ctx = NULL; config.global_user_ctx_free_fn = NULL; config.global_transport_ctx = NULL; diff --git a/code/sdkconfig.defaults b/code/sdkconfig.defaults index 3e102b4a..95caac5f 100644 --- a/code/sdkconfig.defaults +++ b/code/sdkconfig.defaults @@ -36,28 +36,31 @@ CONFIG_SPIRAM_MEMTEST=y CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=16384 CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=40960 CONFIG_SPIRAM_CACHE_WORKAROUND=y +CONFIG_SPIRAM_IGNORE_NOTFOUND=y CONFIG_ESP_INT_WDT_TIMEOUT_MS=300 CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024 - CONFIG_HTTPD_PURGE_BUF_LEN=16 CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=16 - CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM=16 CONFIG_FATFS_LFN_HEAP=y - CONFIG_FATFS_MAX_LFN=255 CONFIG_FATFS_API_ENCODING_ANSI_OEM=y CONFIG_FMB_TIMER_PORT_ENABLED=y +CONFIG_MQTT_MSG_ID_INCREMENTAL=y CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED=y +CONFIG_MQTT_TASK_CORE_SELECTION_ENABLED=y +CONFIG_MQTT_USE_CORE_0=y CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=n +CONFIG_CAMERA_CORE0=n +CONFIG_CAMERA_CORE1=y CONFIG_OV7670_SUPPORT=n CONFIG_OV7725_SUPPORT=n CONFIG_NT99141_SUPPORT=n diff --git a/docs/.gitignore b/docs/.gitignore deleted file mode 100644 index 089c9ea4..00000000 --- a/docs/.gitignore +++ /dev/null @@ -1,25 +0,0 @@ -# Remove if certain files are committed on purpose -.pio/ -.vscode/ -.code-workspace -/sd-card/htm./.vscode/ -/code/build -/code/.helper -/sd-card/html/debug/ -/firmware/ -version.txt -/dist/ -/dist_release/ -/dist_old_ota -CMakeLists.txt.user -CMakeCache.txt -CMakeFiles -CMakeScripts -Makefile -cmake_install.cmake -install_manifest.txt -compile_commands.json -CTestTestfile.cmake -_deps -code/edgeAI.code-workspace -.DS_Store diff --git a/docs/.gitmodules b/docs/.gitmodules deleted file mode 100644 index e2909c41..00000000 --- a/docs/.gitmodules +++ /dev/null @@ -1,9 +0,0 @@ -[submodule "code/components/esp32-camera"] - path = code/components/esp32-camera - url = https://github.com/espressif/esp32-camera.git -[submodule "code/components/esp-nn"] - path = code/components/esp-nn - url = https://github.com/espressif/esp-nn.git -[submodule "code/components/tflite-micro-esp-examples"] - path = code/components/tflite-micro-esp-examples - url = https://github.com/espressif/tflite-micro-esp-examples.git diff --git a/docs/index.html b/docs/index.html index ec21d894..4e24f5a8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -27,6 +27,7 @@

Notes:

  • For the installation, make sure to switch the ESP32 to Bootloader mode!
  • +
  • The SD-Card still must be flashed separately on a PC (See in the Wiki)!
  • After the installation, a manual reset might be required!
  • Please note that not all webbrowsers and operating systems support the needed access to USB!
  • Check the Wiki for additional information.
  • diff --git a/docs/manifest.json b/docs/manifest.json index cbd6e3c7..87fdac55 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -1,6 +1,6 @@ { "name": "AI-on-the-edge", - "version": "13.0.5", + "version": "13.0.7", "funding_url": "https://www.paypal.com/donate?hosted_button_id=8TRSVYNYKDSWL", "new_install_prompt_erase": false, "builds": [ @@ -16,10 +16,10 @@ "offset": 32768 }, { - "path": "releases/download/v13.0.5/firmware.bin", + "path": "releases/download/v13.0.7/firmware.bin", "offset": 65536 } ] } ] -} \ No newline at end of file +} diff --git a/docs/releases/download/v13.0.7/firmware.bin b/docs/releases/download/v13.0.7/firmware.bin new file mode 100644 index 00000000..de170721 Binary files /dev/null and b/docs/releases/download/v13.0.7/firmware.bin differ diff --git a/sd-card/config/ana-class100_0154_s1_q.tflite b/sd-card/config/ana-class100_0154_s1_q.tflite new file mode 100644 index 00000000..2d4945d9 Binary files /dev/null and b/sd-card/config/ana-class100_0154_s1_q.tflite differ diff --git a/sd-card/config/ana-cont_1104_s2_q.tflite b/sd-card/config/ana-cont_1104_s2_q.tflite new file mode 100644 index 00000000..c3891468 Binary files /dev/null and b/sd-card/config/ana-cont_1104_s2_q.tflite differ diff --git a/sd-card/config/dig-class100-0150_s2_q.tflite b/sd-card/config/dig-class100-0150_s2_q.tflite new file mode 100644 index 00000000..9bf8025c Binary files /dev/null and b/sd-card/config/dig-class100-0150_s2_q.tflite differ diff --git a/sd-card/html/data.html b/sd-card/html/data.html index c220ebda..a7ff1d33 100644 --- a/sd-card/html/data.html +++ b/sd-card/html/data.html @@ -25,7 +25,7 @@ flex: 0 1 auto; } - #log { + #data { font-family: 'Courier New', Courier, monospace; font-size: small; } @@ -37,54 +37,36 @@ + Show graph -



    Loading Logfile, please wait...
    +



    Loading Data file, please wait...