System Uptime: millis to DD:HH:MM code optimization question (esp8266)

Hello fellows from the Cayenne Shire!

I’m actually displaying my System Uptime using millis.
(It shows a value of DAYS:HOURS:MINUTES since the esp8266 NodeMCU’s boot time). I am aware the millis() function rolls over in about 47 days, and that it’s not an atomic clock, but it’s more than enough to debug my projects without using external hardware…

This is my dashboard…
image

QUESTION:
I am making use (abuse maybe :grin:) of the “modulo” function. It sends each value to a different Cayenne Channel. This is the code snippet…

image

I’m pretty shure there IS a better way of doing this :rofl:
(BTW, it actually works just fine and it’s really stable!)

Any ideas/suggestions for optimizing the code?
Thanks for your time guys…

Cheers!
Marc

2 Likes

Hello,
Before some time I also need the time from my ESP. I was using this community to read more about and found this thread:
http://www.esp8266.com/viewtopic.php?f=19&t=8916

Maybe it will be useful for you ?

Hello,
this is what i use : GitHub - SensorsIot/SNTPtime: Library for ESP8266 to get time from NTP servers using SNTP protocol
Very simple : Get internet time first , and after its the internal clock of ESP saying time.
You must ynchronize frequently your ESP : all 1hour for example with the NTPch.setSNTPtime() code.

NTPch : “ch” is for switzerland, for me , in France is NTPfr look on google to find your corresponding data.

2 Likes

Hello,
Looking for this kind of solutions I search this community and stumbled upon this conversion

@spacefolder thanks for your example I implemented it straight away after overtyping your solution (not always says a picture more than a thousand words) believe me communities (read me) are lazy and likes to cut and paste. BTW There is a dirty solution / workaround to display HH:MM in the Cayenne environment. The result will look like this:

@samarchri thanks for your input this was very useful. But how do you extract the Unixtime? I realise I should search the community with that search :grinning:.

2 Likes

time
it’s 15h 30min 19sec : the last data receive by Cayenne Dashboard

it’s perhaps usefull : i made this code to Tx time all minutes :

//#define CAYENNE_DEBUG //decommenter pour afficher le DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "xxxx";
char wifiPassword[] = "yyyy";


// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "aaaa";
char password[] = "bbbb";
char clientID[] = "cccc";

 //pr2 : horloge
#include <SNTPtime.h>
SNTPtime NTPfr("pool.ntp.org");
strDateTime dateTime;
int stringTxTime;
float floatTxTime;
//===================================
//=              SETUP              =
//===================================
void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  //pr2 : mise a jour de l'heure
  while (!NTPfr.setSNTPtime()) {
    Serial.print(".");
  }// FIN de pendant que l'horloge se met a l'heure
  Serial.println("+++ Horloge a l'heure internet! +++\n");
} // FIN de setup
//===================================
//=              LOOP               =
//===================================
void loop() {
  Cayenne.loop();
  // pr2 : heure
  heure();    //  Aller a la fct Heure : affichage de l'heure
  Serial.print("Tx heure-> "); Serial.println(floatTxTime);
  Cayenne.virtualWrite(2, floatTxTime);

delay(30000); //30000ms= 1min

} //FIN de loop

//==================================================================
float heure() {// ***************** fonction heure ***********************
  //Debug.print("Demande heure: ");
  Serial.print("Mesure HEURE interne: ");
  // first parameter: Time zone in floating point (for India); second parameter: 1 for European summer time; 2 for US daylight saving time (not implemented yet)
  dateTime = NTPfr.getTime(1.0, 1); // get time from internal clock
  //NTPfr.printDateTime(dateTime);//affiche l'heure en brut

  byte actualHour = dateTime.hour;
  byte actualMinute = dateTime.minute;
  byte actualsecond = dateTime.second;
  int actualyear = dateTime.year;
  byte actualMonth = dateTime.month;
  byte actualday = dateTime.day;
  byte actualdayofWeek = dateTime.dayofWeek;
  String stringHeur, stringMin, stringSec;

  stringHeur = actualHour;
  if (actualHour < 10) {  //heures < 10 ?
    stringHeur = 0 + stringHeur;
  }
  stringMin = actualMinute;
  if (actualMinute < 10) {  //minutes < 10 ?
    stringMin = 0 + stringMin;
  }
  stringSec = actualsecond;
  if (actualsecond < 10) {  //secondes < 10 ?
    stringSec = 0 + stringSec;
  }

  Serial.println( String() + stringHeur + ":" + stringMin + ":" + stringSec );

  // transform string en Int pour TX a Cayenne
  stringHeur += stringMin + stringSec;  // concatene les 3 string
  floatTxTime = stringHeur.toFloat(); // convert string en float
  floatTxTime /= 100; // /100 pour mettre les secondes apres le virgule
  //Serial.println(floatTxTime);
  return floatTxTime;
}   //FIN DE heure()
1 Like

Hi @samarchri, this is helpful though I encountered issues while copying your sketch
The first was off cource my French language skills (seulement un pitit en ecole) and an error in the sketch caused by likely our different keyboard settings where " became “ ” and produced an error. Secondly missed an important line:
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
Bit confusing to delete this one.
I also did this while there was likely some maintenance and numorous errors batterred me including the most simple:
Error downloading http://arduino.esp8266.com/stable/package_esp8266com_index.json

Back to your sketch:
Your frequency to refresh this is 30 seconds so why display seconds?
Your “hour”-routine becomes rather lengthy, my version (skipping the seconds) would only take an 1-liner:
time_now_d = dateTime.hour * 1.0 + ( dateTime.minute / 100.0);
Your transform string part was good ecucation for me (hope to remember it) so thanks for your input.

1 Like

@wj4me sorry for errors (mising Cayenne.begin) i pick some piece of code without testing it entirely :blush:

this package got probleme last time, i tried this one https://github.com/esp8266/Arduino/releases/download/2.3.0/package_esp8266com_index.json and its ok

30s is an example. If i would refresh all 10s i’ll see new data received well because of display seconds.

In short we both have a time widget on our wish list.

3 Likes