NTP-based WIFI time for v1 clocks

More
1 month 2 days ago #10635 by Gq
Wow, great! Thanks for this.

Also, I want to mention that at this time, there is some incompatibility with the existing code in the repo and JSON v6 specifically affecting getConfigfromSpiffs and saveConfigToSpiffs. The changes regarding JSON buffers are explained by Benoit Blanchon in this YouTube video: How to deserialize a JSON document with ArduinoJson 6

Please Log in or Create an account to join the conversation.

More
1 month 2 days ago #10636 by Ian
Any chance of making a pull request for that? It seems a little silly for me to type it all in again and re-do the work that has already been done? I can give it a test and then we can have a new release... :-)

Please Log in or Create an account to join the conversation.

More
1 month 2 days ago #10643 by Froula
I’ll open a pull request for the timezone changes I made. The fixes have been running well for the past month or so in my home brew v1 clock.

Don

Please Log in or Create an account to join the conversation.

More
1 month 2 days ago #10653 by Ian
I appreciate it, Don, thanks!

Please Log in or Create an account to join the conversation.

More
1 month 1 day ago #10659 by Gq

Any chance of making a pull request for that?


I would if I knew how to use Git/Github. Rookie here:silly: But here is the part of the code that I changed:
// ----------------------------------------------------------------------------------------------------
// ------------------------------------------- JSON functions -----------------------------------------
// ----------------------------------------------------------------------------------------------------

/* Arduino IDE v1.8.10 ArduinoJson v6 == DynamicJsonBuffer -> DynamicJsonDocument
   changes tested and compiled on Wemos D1 Mini R2*/

bool getConfigfromSpiffs() {
  bool loaded = false;
  if (SPIFFS.begin()) { 
    debugMsg("mounted file system for read");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      debugMsg("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        debugMsg("opened config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        
        /* BEGIN CHANGES */
        // DynamicJsonBuffer jsonBuffer;
        // JsonObject& json = jsonBuffer.parseObject(buf.get());
        // json.printTo(Serial);
        DynamicJsonDocument jsonDoc(2048); // <-- fixed space MUST be allocated - IS 2048 ENOUGH?
        auto error = deserializeJson(jsonDoc, buf.get()); // <-- new error handling
        serializeJson(jsonDoc, Serial); // <-- new call to serialize
        debugMsg("\n");
        
        if (!error) {
          //if (json.success()) {
          debugMsg("parsed json");

          timeServerURL = jsonDoc["time_zone"].as<String>();
          debugMsg("Loaded time server URL: " + timeServerURL);

          ntpServer = jsonDoc["ntp_server"].as<String>();
          debugMsg("Loaded NTP server: " + ntpServer);

          ntpTZ = jsonDoc["ntp_tz"].as<String>();
          debugMsg("Loaded NTP timezone: " + ntpTZ);
          myTz.setPosix(ntpTZ);

          ntpInterval = jsonDoc["ntp_interval"].as<int>();
          debugMsg("Loaded NTP interval: " + String(ntpInterval));

          loaded = true;
        } else {
          debugMsg("failed to load json config");
        }
        debugMsg("Closing config file");
        configFile.close();
      }
    }
  } else {
    debugMsg("failed to mount FS");
  }

  SPIFFS.end();
  return loaded;
}

void saveConfigToSpiffs() { // SAME CHANGES AS ABOVE FOR DynamicJsonDocument
  if (SPIFFS.begin()) {
    debugMsg("mounted file system for write");
    debugMsg("saving config");

    DynamicJsonDocument jsonDoc(2048); // Again, here with 2kb allocation
    //JsonObject& json = jsonDoc.createObject();
    jsonDoc["time_zone"]    = timeServerURL;
    jsonDoc["ntp_server"]   = ntpServer;
    jsonDoc["ntp_tz"]       = ntpTZ;
    jsonDoc["ntp_interval"] = ntpInterval;

    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      debugMsg("failed to open config file for writing");
      configFile.close();
      return;
    }

    serializeJson(jsonDoc, Serial);
    debugMsg("\n");
  
    serializeJson(jsonDoc, configFile);
    configFile.close();
    debugMsg("Saved config");
    //end save
  } else {
    debugMsg("failed to mount FS");
  }
  SPIFFS.end();
}

Please Log in or Create an account to join the conversation.

More
1 month 1 day ago #10660 by Gq
I forgot to point out that I've included Froula's line:

myTz.setPosix(ntpTZ);

in the code segment above.

Please Log in or Create an account to join the conversation.

Moderators: AccutronTy_EeberfestIan
Time to create page: 0.217 seconds

Search

Tube Suppliers

Go to top
JSN Boot template designed by JoomlaShine.com