Ideas For Converting a value/int into point of time in future

Hello All!

First off, I just wanted to say I've recently been messing around with ESP8266 modules (like the Wemos D1 Mini), and am currently using David Payne's Printer Monitor code - GitHub - Qrome/printer-monitor: OctoPrint 3D Printer Monitor using Wemos D1 Mini ESP8266 to monitor 3D prints... it's been awesome, and I've donated to him for the work!

There is a change I would like to make though (and I am not by any means a coding expert, or even n00b level)... and I was hoping it would be simple enough so make the change myself, and hopefully provide the code and it's contributors' names back to David for implementation as an option in his project.

I would like to change the 'Time Remaining', which is a value pulled from OctoPrint via Rest API/JSON in seconds, and then formatted into hours/minutes/seconds. Instead, I'm hoping to have it take that value, and provide an output of a future 12-hr formatted 'Time of Completion' of the print.

For example: If I have a value of 300 seconds being reported by OctoPrint, and it is currently 12:00 PM, then it should report '12:05 PM'.

Any thoughts or suggestions would be greatly appreciated!! Thank you all!

You must have tried something?

Do you have the IDE downloaded and installed and have you tried any of the sample programs?

Paul

Get the current time. Convert it from hours, minutes, and second into just seconds. Add the duration in seconds. Convert the resulting seconds back into hours, minutes, and seconds.

int h, m, s;
unsigned long timeInSeconds = h;
timeInSeconds *= 60;
timeInSeconds += m;
timeInSeconds *= 60;
timeInSeconds += s;

unsigned long completionTime =  timeInSeconds + secondsUntilCompletion.

int completionTimeSeconds = completionTime% 60;
completionTime /= 60;
int completionTimeMinutes = completionTime % 60;
int completionTimeHours = completionTime / 60;

Thank you johnwasser! (was typing all of this prior to your response)

I will work on that this evening!!

In response to Paul:

Yes, I've been digging through David's code on this... here's the Rest API Call (print progress from JSON)from Octoprint:

"progress": {
"completion": 13.619305423730854,
"filepos": 339357,
"printTime": 973,
"printTimeLeft": 3773,
"printTimeLeftOrigin": "estimate"
},

Here is the code that pulls that data into a variable:

printerData.progressPrintTimeLeft = (const char*)root["progress"]["printTimeLeft"];

From there, and this is where I feel like I'm losing out due to inexperience here, it appears that the value above is being separated into their own int values for output to screen:

display->drawString(64 + x, 0 + y, "Time Remaining");
  //display->setTextAlignment(TEXT_ALIGN_LEFT);
  display->setFont(ArialMT_Plain_24);
  int val = printerClient.getProgressPrintTimeLeft().toInt();
  int hours = numberOfHours(val);
  int minutes = numberOfMinutes(val);
  int seconds = numberOfSeconds(val);

There is also TimeClient.cpp/h files, with the purpose of establishing time from an NTP source of course, that I can use as way to manipulate 'origin' data to add the 'seconds' reported by Octoprint too.

Hey Guys,

I have added another frame of data, which is successfully being displayed on the OLED screen with the JSON data as a string:

String OctoTime = printerClient.getProgressPrintTimeLeft();

Afterwards, I am lost. I am working with a TimeClient.cpp that is pulling data from www.google.com for 'NTP' time:

/**The MIT License (MIT)

Copyright (c) 2015 by Daniel Eichhorn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/*
Modified by David Payne for use in the Scrolling Marquee
*/

#include "TimeClient.h"

TimeClient::TimeClient(float utcOffset) {
  myUtcOffset = utcOffset;
}

void TimeClient::updateTime() {
  WiFiClient client;
  
  if (!client.connect(ntpServerName, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  // This will send the request to the server
  client.print(String("GET / HTTP/1.1\r\n") +
               String("Host: www.google.com\r\n") +
               String("Connection: close\r\n\r\n"));
  int repeatCounter = 0;
  while(!client.available() && repeatCounter < 10) {
    delay(1000);
    Serial.println(".");
    repeatCounter++;
  }

  String line;

  int size = 0;
  client.setNoDelay(false);
  while(client.connected()) {
    while((size = client.available()) > 0) {
      line = client.readStringUntil('\n');
      line.toUpperCase();
      // example:
      // date: Thu, 19 Nov 2015 20:25:40 GMT
      if (line.startsWith("DATE: ")) {
        Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" +line.substring(29, 31));
        int parsedHours = line.substring(23, 25).toInt();
        int parsedMinutes = line.substring(26, 28).toInt();
        int parsedSeconds = line.substring(29, 31).toInt();
        Serial.println(String(parsedHours) + ":" + String(parsedMinutes) + ":" + String(parsedSeconds));

        localEpoc = (parsedHours * 60 * 60 + parsedMinutes * 60 + parsedSeconds);
        Serial.println(localEpoc);
        localMillisAtUpdate = millis();
        client.stop();
      }
    }
  }

}

void TimeClient::setUtcOffset(float utcOffset) {
 myUtcOffset = utcOffset;
}

String TimeClient::getHours() {
    if (localEpoc == 0) {
      return "--";
    }
    int hours = ((getCurrentEpochWithUtcOffset()  % 86400L) / 3600) % 24;
    if (hours < 10) {
      return "0" + String(hours);
    }
    return String(hours); // print the hour (86400 equals secs per day)

}
String TimeClient::getMinutes() {
    if (localEpoc == 0) {
      return "--";
    }
    int minutes = ((getCurrentEpochWithUtcOffset() % 3600) / 60);
    if (minutes < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      return "0" + String(minutes);
    }
    return String(minutes);
}
String TimeClient::getSeconds() {
    if (localEpoc == 0) {
      return "--";
    }
    int seconds = getCurrentEpochWithUtcOffset() % 60;
    if ( seconds < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      return "0" + String(seconds);
    }
    return String(seconds);
}

String TimeClient::getAmPmHours() {
 int hours = getHours().toInt();
 if (hours >= 13) {
 hours = hours - 12;
 }
 if (hours == 0) {
 hours = 12;
 }
 return String(hours);
}

String TimeClient::getAmPm() {
 int hours = getHours().toInt();
 String ampmValue = "AM";
 if (hours >= 12) {
 ampmValue = "PM";
 }
 return ampmValue;
}

String TimeClient::getFormattedTime() {
  return getHours() + ":" + getMinutes() + ":" + getSeconds();
}

String TimeClient::getAmPmFormattedTime() {
 return getAmPmHours() + ":" + getMinutes() + " " + getAmPm();
}

long TimeClient::getCurrentEpoch() {
  return localEpoc + ((millis() - localMillisAtUpdate) / 1000);
}

long TimeClient::getCurrentEpochWithUtcOffset() {
  return (long)round(getCurrentEpoch() + 3600 * myUtcOffset + 86400L) % 86400L;
}

TimeClient.h

#pragma once

#include <ESP8266WiFi.h>

#define NTP_PACKET_SIZE 48

class TimeClient {

  private:
    float myUtcOffset = 0;
    long localEpoc = 0;
    long localMillisAtUpdate;
    const char* ntpServerName = "www.google.com";
    const int httpPort = 80;    
    byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

  public:
    TimeClient(float utcOffset);
    void updateTime();
    
    void setUtcOffset(float utcOffset);
    String getHours();
    String getAmPmHours();
    String getAmPm();
    String getMinutes();
    String getSeconds();
    String getFormattedTime();
    String getAmPmFormattedTime();
    long getCurrentEpoch();
    long getCurrentEpochWithUtcOffset();

};

I simply want to be able to 'get time' as suggested by John above, yet I do not feel like 'UNIX Epoch Time' is being pulled in the above sketch. Any help in getting the current time in Unix Epoch Time, so i can add the 'OctoTime' string too, would be greatly appreciated!