0

Been struggling with something for a few days.

Loading data from a website via JSON and it includes an updated date time in milliseconds. This is an EPOCH date and I can get the right value using some of the online convertor sites, epochconvertor.com

That said when I try and parse it in my sketch it’s not retuning the correct date.

JSON “currentupdatedms”:1588312648603

Convertor Shows as: 1 May 2020 05:57:28.603

My sketch is showing: 1 May 2020 02:33:00

That’s in the JSON and I can’t use a Long as it’s 13 digits. Tried Long Long and it accepts the value but when I later divide by 1000 to get seconds the number changes completely when I try printing it to serial or the OLED.

Plan is to use the Timeh library to display it as a human date time.

Any thoughts be appreciated.

Here is my code and I have tried various methods:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <TimeLib.h>
#include <time.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define ARDUINOJSON_JSON_LONG_LONG 1 //Not needed???
#define   ONE_HOUR   3600
const size_t capacity = JSON_OBJECT_SIZE(8) + 140;
DynamicJsonDocument doc(capacity);

/*Notes
   Need to look at conversion of Update and Changed times as they are not showing correctly
   Move the conversion of time datetime in ms to a seperate function
   Move the get request and JSON section to seperate function
*/

const char* ssid = "SSID";
const char* password = "PWD";

void setup () {

  Serial.begin(4800);

  Wire.begin(2, 0);           // set I2C pins (SDA = GPIO2, SCL = GPIO0), default clock is 100kHz

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);

  WiFi.begin(ssid, password);

  display.setCursor(0, 0);
  display.println(F("Connecting to Wifi..."));
  display.display();

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to Wifi...");
  }

  display.setCursor(0, 10);
  display.println(F("Wifi Connected..."));
  display.display();
}

void loop() {

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    display.setCursor(0, 20);
    display.println(F("Requesting Data..."));
    display.display();
    delay(2000); // Pause for 2 seconds

    HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("http://corona.tuply.co.za/DataHandler.ashx?w=s&uq=8f1fp8p5jdxi84481");  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request

    if (httpCode > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload

      deserializeJson(doc, payload);

      int TotalCases = doc["TotalCases"];
      int TotalCasesToday = doc["TotalCasesToday"];
      int TotalDeaths = doc["TotalDeaths"];
      int TotalDeathsToday = doc["TotalDeathsToday"];
      int TotalRecovered = doc["TotalRecovered"];
      int TotalTerritories = doc["TotalTerritories"];
      long long CurrentUpdatedMS = doc["CurrentUpdatedMS"];
      long long CurrentChangedMS = doc["CurrentChangedMS"];

      //delay(10000);

      time_t CurrentUpdateMS_t = (CurrentUpdatedMS/1000) + ONE_HOUR; //ONE_HOUR added for BST

      char buffUpdateMS[32];
      sprintf(buffUpdateMS, "%02d/%02d/%02d %02d:%02d:%02d", day(CurrentUpdateMS_t), month(CurrentUpdateMS_t), year(CurrentUpdateMS_t), hour(CurrentUpdateMS_t), minute(CurrentUpdateMS_t), second(CurrentUpdateMS_t));
      Serial.print("U:");
      Serial.println(buffUpdateMS);
      Serial.print("ctime: ");
      Serial.println(ctime(&CurrentUpdateMS_t));

      time_t CurrentChangedMS_t = CurrentChangedMS/1000 + ONE_HOUR; //ONE_HOUR added for BST

      char buffChangedMS[32];
      sprintf(buffChangedMS, "%02d/%02d/%02d %02d:%02d:%02d", day(CurrentChangedMS_t), month(CurrentChangedMS_t), year(CurrentChangedMS_t), hour(CurrentChangedMS_t), minute(CurrentChangedMS_t), second(CurrentChangedMS_t));
      Serial.print("C:");
      Serial.println(buffChangedMS);
      Serial.print("ctime: ");
      Serial.println(ctime(&CurrentChangedMS_t));

      display.clearDisplay();

      display.setCursor(0, 0);
      display.print(F("T Cases: "));
      display.println(TotalCases);

      Serial.print("Total Cases: ");
      Serial.println(TotalCases);

      Serial.print("Total Cases Today: ");
      Serial.println(TotalCasesToday);

      display.setCursor(0, 10);
      display.print(F("T Deaths: "));
      display.println(TotalDeaths);

      Serial.print("Total Deaths: ");
      Serial.println(TotalDeaths);

      Serial.print("Total Deaths Today: ");
      Serial.println(TotalDeathsToday);

      display.setCursor(0, 20);
      display.print(F("T Recov: "));
      display.println(TotalRecovered);

      Serial.print("Total Recovered: ");
      Serial.println(TotalRecovered);

      Serial.print("Total Territories: ");
      Serial.println(TotalTerritories);

      display.setCursor(0, 30);
      display.print(F("U T: "));
      display.println(buffUpdateMS);

      display.setCursor(0, 40);
      display.print(F("C T: "));
      display.println(buffChangedMS);

      display.display();

    }

    http.end();   //Close connection

  }

  delay(60000);    //Send a request every 60 seconds

}

If I run that now I get this JSON string:

{"TotalCases":4036128,"TotalCasesToday":26837,"TotalDeaths":276825,"TotalDeathsToday":849,"TotalRecovered":1400460,"TotalTerritories":214,"CurrentUpdatedMS":1589027095790,"CurrentChangedMS":1589026765665}

CurrentUpdateMS = 1589027095790 which is Sat May 09 2020 13:24:55, granted there may be an hour difference with BST.

Instead I am getting:

Sat May 9 13:23:32 2020

Also appears to stay the same after refreshes which I have set to 60 seconds.

I tried the Time.h library with same results.

Thankful for any input or suggestions.

  • 1. Do not mix time.h with TimeLib.h: they both define their own version of time_t. 2. This program is too long for a question here. You should break down your problem into small pieces and tackle them one at a time. First, Serial.println(CurrentUpdatedMS) in order to know whether the issue lies in the JSON deserialization or in the interpretation of the number as a time. Then, write a minimal, reproducible example addressing only that issue. See my answer as an example of what a “minimal, reproducible example” may look like. – Edgar Bonet May 9 at 13:08
1

It's hard to know what is wrong with your code... because you are not showing it in the question! Here is a basic test I tried on my Uno:

#include <time.h>

const uint64_t ms = 1588312648603;

void setup() {
    time_t t = ms/1000 - UNIX_OFFSET;
    Serial.begin(9600);
    Serial.println(ctime(&t));
}

void loop(){}

It's output is:

Fri May 01 05:57:28 2020

Note that this <time.h> comes from the avr-libc. It uses an epoch different from the Unix epoch, hence the UNIX_OFFSET correction.

Edit: I tried replacing avr-libc's <time.h> by Adafruit RTClib (declaring DateTime t(ms/1000);) and Paul Stoffregen’s Time library (with time_t t = ms/1000;). In all cases I get the same time displayed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.