Time after time (no pun intended) I have trouble understanding the time related stuff with the arduino. I really tried hard and I spent (wasted) uncountable hours trying to get it.
Question:
[{"tm":1668701337,"net": 100014.077,"pwr": 515,"ts0":1668699585,"cs0": 172.986,"ps0": 0}]
Above Json output is from my Youless device which gets for example the power used in our house, power generated by our solar array and totals.
As you can see, also a TIME variable 'tm' is being sent and all I want to do is to get the HOUR/MIN/SEC value from the parsed tm value to reset a counter a midnight so I can display the solar array generated power for that day.
The only darn thing I need is the time extracted from the mysterious tm value.
THANK YOU for any help!
Not needed but here's a raw sketch:
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "Credentials.h"
// display library
#include <LedControl.h>
// time
#include <time.h>
// Youless readout refresh delay in SECONDS
#define REFRESH_DELAY 2
//----------------------------------------------------------------------------
// display stuff
/*
PIN connections for LedControl
Maxim 7221 DataIn > Wemos D1 pin D7 (MOSI)
Maxim 7221 CLK > Wemos D1 pin D5 (CLK)
Maxim 7221 CS/LOAD> Wemos D1 pin D0 (CS)
*/
// DataIn, CLK, LOAD, nr of displays
#define Maxim_DATA D7
#define Maxim_CLK D5
#define Maxim_CS D0
#define Maxim_NR_OF_DISPLAYS 1
LedControl lc = LedControl(Maxim_DATA, Maxim_CLK, Maxim_CS, Maxim_NR_OF_DISPLAYS);
/* we always wait a bit between updates of the display */
unsigned long delaytime = 250;
//----------------------------------------------------------------------------
// Your access point SSID
const char *ssid = WIFI_SSID;
// Your access point password
const char *password = WIFI_PASSWORD;
// Your Youless IP
String serverName = "http://192.168.178.109";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
unsigned long timerDelay = REFRESH_DELAY * 1000;
void setup()
{
Serial.begin(115200);
//---display stuff-----------------------------------------------------------
// display stuff
//
// The MAX72XX is in power-saving mode on startup, so do a wakeup call
lc.shutdown(0, false);
/* Set the brightness to a medium values */
lc.setIntensity(0, 2);
/* and clear the display */
lc.clearDisplay(0);
//---------------------------------------------------------------------------
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop()
{
// Send an HTTP POST request depending on timerDelay
if ((millis() - lastTime) > timerDelay)
{
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED)
{
WiFiClient client;
HTTPClient http;
String serverPath = serverName + "/e";
// Your Domain name with URL path or IP address with path
http.begin(client, serverPath.c_str());
// If you need Node-RED/server authentication, insert user and password below
// http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD");
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
//==============================================================================================================================
// Stream& input;
StaticJsonDocument<192> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonObject root_0 = doc[0];
long yl_tm = root_0["tm"]; // 1668621075
double yl_net = root_0["net"]; // 100002.933
int yl_pwr = root_0["pwr"]; // 388
long yl_ts0 = root_0["ts0"]; // 1668612963
float yl_cs0 = root_0["cs0"]; // 172.115
int yl_ps0 = root_0["ps0"]; // 0
// display PV
// convert the numbers into digits
int pvDigit3 = (yl_ps0 / 1000) % 10;
int pvDigit2 = (yl_ps0 / 100) % 10;
int pvDigit1 = (yl_ps0 / 10) % 10;
int pvDigit0 = (yl_ps0 % 10);
// remove leading zero's
lc.setChar(0, 7, (yl_ps0 < 1000 ? ' ' : pvDigit3), false);
lc.setChar(0, 6, (yl_ps0 < 100 ? ' ' : pvDigit2), false);
lc.setChar(0, 5, (yl_ps0 < 10 ? ' ' : pvDigit1), false);
lc.setChar(0, 4, pvDigit0, false);
// display power
// convert the numbers into digits
int pwrDigit3 = (yl_pwr / 1000) % 10;
int pwrDigit2 = (yl_pwr / 100) % 10;
int pwrDigit1 = (yl_pwr / 10) % 10;
int pwrDigit0 = (yl_pwr % 10);
// remove leading zero's
lc.setChar(0, 3, (yl_pwr < 1000 ? ' ' : pwrDigit3), false);
lc.setChar(0, 2, (yl_pwr < 100 ? ' ' : pwrDigit2), false);
lc.setChar(0, 1, (yl_pwr < 10 ? ' ' : pwrDigit1), false);
lc.setChar(0, 0, pwrDigit0, false);
//==============================================================================================================================
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else
{
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
/*
time zones:
https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
*/