Skip to content

Cleanup ota example #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2025

Conversation

soylentOrange
Copy link
Contributor

This is just a quality of service update. No functional changes to the library.

  • Stripped factory.py from the OTA environment to speed up processing before upload
  • Added build time and date to the example website to see the effect of a newly build OTA upload
  • minor changes to readme.md (corrected a typo, renamed section heading, added some more hints on usage)
  • Forgot to remove variants folder for the waveshare_esp32_s3_tiny (which I used just for testing). Now it's deleted...

- Stripped factory.py from the OTA environment to speed up processing before upload
- Added build time and date to the example website to see the effect of a newly build OTA upload
- minor changes to readme.md (corrected a typo, renamed section heading, added some more hints on usage)
Forgot to remove variants folder for the waveshare_esp32_s3_tiny (which I used just for testing)
@soylentOrange
Copy link
Contributor Author

@mathieucarbou Sorry, I cannot keep up with your lightspeed pace. As soon as I try out something, you have already implemented it yourself (better than I could), tested, documented and merged everything into the library already...

Just a note on "Platformio experience", I tried to re-route the monitor port in the OTA environments to telnet (the pySerial base for platformio's monitoring would support a socket connection). Actually, it would require adding additional dependencies like karol-brejna-i/RemoteDebug, or something similar. All of the libraries I looked at would require some major rework to be compatible with ESPConnect.
In the end, I think it's not worth the effort, webserial would give you the same output (though not in platformio), but neither webserial nor a telnet based solution would give any output before the network connection is established. I'm afraid that the most important parts are missing from the logging then...

mathieucarbou
mathieucarbou previously approved these changes Mar 28, 2025
@mathieucarbou
Copy link
Owner

Thanks for the PR :-) I will merge!

I'm afraid that the most important parts are missing from the logging then...

I agree, and also adding some features now would maybe require to disable mDNS or something else. I think the way it is right now is nice.

About logging the startup, I had this use case in YaSolR (https://github.com/mathieucarbou/YaSolR) to help the support.

So I am using a combination of ets_install_putc1() and MycilaLogger to save the startup logs (at most the first 32k) on flash. This slows down startup, but only when debug mode is activated in the app. And I provide a way to download the logs.

Here is my startup sequence:

Mycila::Logger logger;

// redirect logs to file system
class LogStream : public Print {
  public:
    LogStream(const char* path, size_t limit) : _path(path), _limit(limit) {
      File file = LittleFS.open(_path, "r");
      _logSize = file ? file.size() : 0;
      file.close();
    }
    size_t write(const uint8_t* buffer, size_t size) override {
      if (_logSize + size > _limit)
        return 0;
      File file = LittleFS.open(_path, "a");
      if (!file)
        return 0;
      size_t written = file.write(buffer, size);
      file.close();
      _logSize += written;
      return written;
    }
    size_t write(uint8_t c) override {
      assert(false);
      return 0;
    }

  private:
    const char* _path;
    const size_t _limit;
    size_t _logSize = 0;
};

// Arduino setup()
void setup() {
  // HardwareSerial
  Serial.begin(YASOLR_SERIAL_BAUDRATE);
#if ARDUINO_USB_CDC_ON_BOOT
  Serial.setTxTimeoutMs(0);
  delay(100);
#else
  while (!Serial)
    yield();
#endif

  // MycilaLogger - uses ets_install_putc1 hook
  logger.redirectArduinoLogs();
  logger.forwardTo(&Serial);
  
  // TODO: not shown here: 
  // - initialize LittleFS - LittleFS.begin()
  // - initialize NVS 
  // - load app config from NVS
  
  logger.info(TAG, "Initialize logging");

  // remove previous log file
  if (LittleFS.remove(YASOLR_LOG_FILE))
    logger.info(TAG, "Previous log file removed");

  // only in debug mode, redirect logs to WebSerial and flash
  if (config.getBool(KEY_ENABLE_DEBUG)) {
    logger.setLevel(ARDUHAL_LOG_LEVEL_DEBUG);
    esp_log_level_set("*", static_cast<esp_log_level_t>(ARDUHAL_LOG_LEVEL_DEBUG));

    logger.info(TAG, "Redirecting logs to WebSerial");
    webSerial = new WebSerial();
    webSerial->begin(&webServer, "/console");
    logger.forwardTo(webSerial);

    logger.info(TAG, "Redirecting logs to " YASOLR_LOG_FILE);
    webServer.on("/api" YASOLR_LOG_FILE, HTTP_GET, [](AsyncWebServerRequest* request) {
      LittleFS.open(YASOLR_LOG_FILE, "r");
      request->send(LittleFS, YASOLR_LOG_FILE, "text/plain");
    });
    logger.forwardTo(new LogStream(YASOLR_LOG_FILE, 32 * 1024));

  } else {
    logger.setLevel(ARDUHAL_LOG_LEVEL_INFO);
    esp_log_level_set("*", static_cast<esp_log_level_t>(ARDUHAL_LOG_LEVEL_INFO));
  }
}

Currently, Arduino Core has a bug (IMO) that prevents log redirect on some boards.
I have opened a PR to fix that here: espressif/arduino-esp32#11159
But they need to check all the boards to verify...
So for now, only logs from MycilaLogger can be redirected, but soon, even arduino logs in INFO, WARN or ERROR.

@mathieucarbou mathieucarbou merged commit 6e2493e into mathieucarbou:main Mar 28, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants