How to create a function with complete date hour:min:sec in RTC to upload PLOTLY

Hello, firstly thanks for your help.

I`m using plotly with good results, but the downside is the wrong time (x axis). In the example sketch we have

graph.plot(millis(), t, tokens[0]);
graph.plot(millis(), h, tokens[1]);
graph.plot(millis(), t2, tokens[2]);
graph.plot(millis(), h2, tokens[3]);

And i have changed this to (now.hour() or (now.second(), but the problem is that with those functions, or it is looping itself because of the (now.second() every 60 secs, or its stopped for one hour....

Could anyone help me create a function like (now.time() or something like that in order to upload the right time to plotly x axis??

Thank you very much.

Just use epoch time, time_t type. Install the Time library.

Aarg, thanks for the quick response! I tried to include the Time.h, but had this error compiling:

WARNING: Spurious .github folder in 'DHT sensor library' library
In file included from /Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:10:0:
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:18:18: error: variable 'monthStr1' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr1 PROGMEM = "January";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:19:18: error: variable 'monthStr2' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr2 PROGMEM = "February";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:20:18: error: variable 'monthStr3' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr3 PROGMEM = "March";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:21:18: error: variable 'monthStr4' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr4 PROGMEM = "April";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:22:18: error: variable 'monthStr5' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr5 PROGMEM = "May";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:23:18: error: variable 'monthStr6' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr6 PROGMEM = "June";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:24:18: error: variable 'monthStr7' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr7 PROGMEM = "July";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:25:18: error: variable 'monthStr8' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr8 PROGMEM = "August";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:26:18: error: variable 'monthStr9' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr9 PROGMEM = "September";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:27:19: error: variable 'monthStr10' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr10 PROGMEM = "October";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:28:19: error: variable 'monthStr11' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr11 PROGMEM = "November";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:29:19: error: variable 'monthStr12' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthStr12 PROGMEM = "December";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:31:22: error: variable 'monthNames_P' must be const in order to be put into read-only section by means of 'attribute((progmem))'
PGM_P monthNames_P PROGMEM =

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:37:26: error: variable 'monthShortNames_P' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char monthShortNames_P PROGMEM = "ErrJanFebMarAprMayJunJulAugSepOctNovDec";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:39:16: error: variable 'dayStr0' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayStr0 PROGMEM = "Err";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:40:16: error: variable 'dayStr1' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayStr1 PROGMEM = "Sunday";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:41:16: error: variable 'dayStr2' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayStr2 PROGMEM = "Monday";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:42:16: error: variable 'dayStr3' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayStr3 PROGMEM = "Tuesday";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:43:16: error: variable 'dayStr4' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayStr4 PROGMEM = "Wednesday";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:44:16: error: variable 'dayStr5' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayStr5 PROGMEM = "Thursday";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:45:16: error: variable 'dayStr6' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayStr6 PROGMEM = "Friday";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:46:16: error: variable 'dayStr7' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayStr7 PROGMEM = "Saturday";

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:48:20: error: variable 'dayNames_P' must be const in order to be put into read-only section by means of 'attribute((progmem))'
PGM_P dayNames_P PROGMEM = { dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7};

^
/Users/erics/Documents/Arduino/libraries/Time/DateStrings.cpp:49:24: error: variable 'dayShortNames_P' must be const in order to be put into read-only section by means of 'attribute((progmem))'
char dayShortNames_P PROGMEM = "ErrSunMonTueWedThrFriSat";

^
Multiple libraries were found for "DHT.h"
Used: /Users/erics/Documents/Arduino/libraries/DHT_sensor_library
Not used: /Users/erics/Documents/Arduino/libraries/DHT-sensor-library-master
exit status 1
Error compiling.

Could you please help me where I should Const these entries?? Below is my code:

Ok got pass it by installing the latest library. Now i compiled successfully using the Time.h, but i dont know how to use the Epoch function…

#include <Time.h>
#include <TimeLib.h>



#include <SPI.h>
#include <Ethernet.h>
#include <Ethernet.h>
#include "plotly_streaming_ethernet.h"
#include "DHT.h"
#include "RTClib.h"

time_t epoch = 0;

// Sign up to plotly here: https://plot.ly
// View your API key and streamtokens here: https://plot.ly/settings
#define nTraces 4
// View your tokens here: https://plot.ly/settings
// Supply as many tokens as data traces
// e.g. if you want to ploty A0 and A1 vs time, supply two tokens
char *tokens[nTraces] = {"v6phsraq7t", "khjswh0ww2", "18lhednkcu", "xmts2n3t8i"};
// arguments: username, api key, streaming token, filename
plotly graph("rootss", "16xc952unk", tokens, "filename", nTraces);
char layout[]="{}";


// DHT Sensor Setup
#define DHTPIN 2 // We have connected the DHT to Digital Pin 2
#define DHT2PIN 3 // We have connected the DHT to Digital Pin 2
#define DHTTYPE DHT22 // This is the type of DHT Sensor (Change it to DHT11 if you're using that model)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT object
DHT dht2(DHT2PIN, DHTTYPE); // Initialize DHT object
RTC_DS1307 RTC;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = { 192, 168, 1, 40 }; // google will tell you: "public ip address" 


void startEthernet(){
    Serial.println("... Initializing ethernet");
    if(Ethernet.begin(mac) == 0){
        Serial.println("... Failed to configure Ethernet using DHCP");
        // no point in carrying on, so do nothing forevermore:
        // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, my_ip);
        
    }

    
    Serial.println("... Done initializing ethernet");
    delay(1000);
   
}

  
void setup() {
   graph.convertTimestamp = true;
  graph.fileopt = "extend";
  graph.maxpoints = 10000 ;
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   RTC.begin();
if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
 //   RTC.adjust(DateTime(__DATE__, __TIME__));

 
 char *timezone = "America/Sao_Paulo";
 
  }
  
  

// get time to display "sensor up time"
  uint16_t time = millis();
 // tft.fillScreen(ST7735_BLACK); //black screen
  time = millis() - time;

Serial.println(time, DEC);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  startEthernet();

  bool success;
  success = graph.init();
  if(!success){while(true){}}
  graph.openStream();
  dht.begin();
  dht2.begin();
  
}

float h, t, t2, h2;

void loop() {

//clock start
  DateTime now = RTC.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();


 //hour conversion to 12 hour start
   int x;
   int timeresult;
   x = (now.hour());
   if (x > 12) {
   timeresult = x - 12;
   }
   else 
   {
    timeresult = x;
   }
   Serial.println(timeresult);
  
      graph.fileopt = "extend";
      h = dht.readHumidity();
      h2 = dht2.readHumidity();
      t = dht.readTemperature();
      t2 = dht2.readTemperature();
      graph.plot(millis(), t, tokens[0]);
      graph.plot(millis(), h, tokens[1]);
      graph.plot(millis(), t2, tokens[2]);
      graph.plot(millis(), h2, tokens[3]);
      
}

You didn’t use code tags to post your code. It’s important that you use them - if you don’t, the forum software sometimes interprets sequences of characters in the code as directives to format text in some way. It won’t display those sequences, and it will unexpectedly reformat the rest of the text. When that happens, and someone tries to copy your code and paste it into the IDE, it often throws an error, and readers will complain that the code fails to compile. Using code tags also makes it easier to read, and can be copied with a single mouse click.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, open it in a text editor, then copy it to the IDE. It’s much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

What is TimeLib.h? Was that in the original sketch?

You are already reading an epoch time here:

  DateTime now = RTC.now();

If you try printing "now" you will see that it is an integer that increments once per second. Its value is the epoch, or Unix time.

Im sorry for the noobie code paste, im going to do as you said...

That Timelib, just goes together when i select the Time.h library....

What do you mean, "goes together"? I don't follow. What do you mean "select" the Time library?

Also, please answer the second question I asked you in reply #5.

Ok, what I mean by “select” is when I go Sketch/Include Library/Time on the IDE, it automatically adds

#include <Time.h>
#include <TimeLib.h>

In the sketch…

About post #5, Ok, but if I try to run the sketch with “graph.plot(now(), t, tokens[0]);”

I get this error:

no match for call to ‘(DateTime) ()’

This has become a little crazy. I think you already had the DateTime library in your sketch. The DateTime library is just another version of the Time library. So you should not have both.

It’s best if you only use one, so can you please go back to your original sketch, without the Time library? Try doing the same change with that one. Also please post the original sketch so we can get back on track. Sorry for the mixup.

rootss:
About post #5, Ok, but if I try to run the sketch with "graph.plot(now(), t, tokens[0]);"

I get this error:

no match for call to '(DateTime) ()'

It should be, "graph.plot(now, t, tokens[0]);"

aarg:
This has become a little crazy. I think you already had the DateTime library in your sketch. The DateTime library is just another version of the Time library. So you should not have both.

It’s best if you only use one, so can you please go back to your original sketch, without the Time library? Try doing the same change with that one. Also please post the original sketch so we can get back on track. Sorry for the mixup.

Sure! No prob…

Ok, this is the original plotly sketch…

#include <SPI.h>
#include <Ethernet.h>
#include "plotly_streaming_ethernet.h"
#include "DHT.h"

// Sign up to plotly here: https://plot.ly
// View your API key and streamtokens here: https://plot.ly/settings
#define nTraces 2
// View your tokens here: https://plot.ly/settings
// Supply as many tokens as data traces
// e.g. if you want to ploty A0 and A1 vs time, supply two tokens
char *tokens[nTraces] = {"25tm9197rz", "unbi52ww8a"};
// arguments: username, api key, streaming token, filename
plotly graph("workshop", "v6w5xlbx9j", tokens, "filename", nTraces);

// DHT Sensor Setup
#define DHTPIN 2 // We have connected the DHT to Digital Pin 2
#define DHTTYPE DHT22 // This is the type of DHT Sensor (Change it to DHT11 if you're using that model)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT object

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = { 199, 168, 222, 18 }; // google will tell you: "public ip address"

void startEthernet(){
    Serial.println("... Initializing ethernet");
    if(Ethernet.begin(mac) == 0){
        Serial.println("... Failed to configure Ethernet using DHCP");
        // no point in carrying on, so do nothing forevermore:
        // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, my_ip);
    }
    Serial.println("... Done initializing ethernet");
    delay(1000);
}


void setup() {
  graph.maxpoints = 100;
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  startEthernet();

  bool success;
  success = graph.init();
  if(!success){while(true){}}
  graph.openStream();
  dht.begin();
}

float h, t;

void loop() {
      h = dht.readHumidity();
      t = dht.readTemperature();
      graph.plot(millis(), t, tokens[0]);
      graph.plot(millis(), h, tokens[1]);
}

After that, have added another 2 streams, and have copied and pasted some lines from a sketch that had the RTClib…

This is my current working sketch,

#include <SPI.h>
#include <Ethernet.h>
#include "plotly_streaming_ethernet.h"
#include "DHT.h"
#include "RTClib.h"

// Sign up to plotly here: https://plot.ly
// View your API key and streamtokens here: https://plot.ly/settings
#define nTraces 4
// View your tokens here: https://plot.ly/settings
// Supply as many tokens as data traces
// e.g. if you want to ploty A0 and A1 vs time, supply two tokens
char *tokens[nTraces] = {"xxxxx", "xxxxx", "xxxxx", "xxxxx"};
// arguments: username, api key, streaming token, filename
plotly graph("xxxxx", "xxxxx", tokens, "filename", nTraces);
char layout[]="{}";


// DHT Sensor Setup
#define DHTPIN 2 // We have connected the DHT to Digital Pin 2
#define DHT2PIN 3 // We have connected the DHT to Digital Pin 2
#define DHTTYPE DHT22 // This is the type of DHT Sensor (Change it to DHT11 if you're using that model)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT object
DHT dht2(DHT2PIN, DHTTYPE); // Initialize DHT object
RTC_DS1307 RTC;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = { 192, 168, 1, 40 }; // google will tell you: "public ip address" 


void startEthernet(){
    Serial.println("... Initializing ethernet");
    if(Ethernet.begin(mac) == 0){
        Serial.println("... Failed to configure Ethernet using DHCP");
        // no point in carrying on, so do nothing forevermore:
        // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, my_ip);
        
    }

    
    Serial.println("... Done initializing ethernet");
    delay(1000);
   
}

  
void setup() {
   graph.convertTimestamp = true;
  graph.fileopt = "extend";
  graph.maxpoints = 10000 ;
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   RTC.begin();
if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
 //   RTC.adjust(DateTime(__DATE__, __TIME__));

 
 char *timezone = "America/Sao_Paulo";
 
  }
  
  

// get time to display "sensor up time"
  uint16_t time = millis();
 // tft.fillScreen(ST7735_BLACK); //black screen
  time = millis() - time;

Serial.println(time, DEC);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  startEthernet();

  bool success;
  success = graph.init();
  if(!success){while(true){}}
  graph.openStream();
  dht.begin();
  dht2.begin();
  
}

float h, t, t2, h2;

void loop() {

//clock start
  DateTime now = RTC.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();


 //hour conversion to 12 hour start
   int x;
   int timeresult;
   x = (now.hour());
   if (x > 12) {
   timeresult = x - 12;
   }
   else 
   {
    timeresult = x;
   }
   Serial.println(timeresult);
  
      graph.fileopt = "extend";
      h = dht.readHumidity();
      h2 = dht2.readHumidity();
      t = dht.readTemperature();
      t2 = dht2.readTemperature();
      graph.plot(millis(), t, tokens[0]);
      graph.plot(millis(), h, tokens[1]);
      graph.plot(millis(), t2, tokens[2]);
      graph.plot(millis(), h2, tokens[3]);
      
}

But I cant upload the complete hour to plotly… when I replaced the

graph.plot(millis(), t, tokens[0]);

for

graph.plot(now.second(), t, tokens[0]);

I got a 60 sec loop in plotly. So if I had something like a graph.plot(now.time(), t, tokens[0]); or something I think it would be it…

Again thanks for all the help…

For heaven's sake! You completely ignored what I said in reply #10!

You obtained the epoch time here:

//clock start
  DateTime now = RTC.now();

All you need to do is use it!

aarg:
For heaven’s sake! You completely ignored what I said in reply #10!

You obtained the epoch time here:

//clock start

DateTime now = RTC.now();




All you need to do is use it!

I was answering the one before first…

When I try with the

graph.plot(now, t, tokens[0]);
graph.plot(now, h, tokens[0]);
graph.plot(now, t2, tokens[0]);
graph.plot(now, h2, tokens[0]);

I get:

no matching function for call to ‘plotly::plot(DateTime&, float&, char*&)’

graph.plot( (unsigned long)now, t, tokens[0]);

Got an error:

invalid cast from type 'DateTime' to type 'long unsigned int'

//clock start
  unsigned long now = RTC.now();
graph.plot( now, t, tokens[0]);

I don't have your RTC library. Where did you get it? What type does RTC.now() return?

I got it here: GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC library

You mean when I put it like this?

graph.plot(RTC.now(), t, tokens[0]);

It gives:

no matching function for call to 'plotly::plot(DateTime, float&, char*&)'

Why would I mean something other than what I said? Read reply #16.

aarg:

//clock start

unsigned long now = RTC.now();
graph.plot( now, t, tokens[0]);




I don't have your RTC library. Where did you get it? What type does RTC.now() return?

Im sorry, I dont understand what it means… Where should I put this RTC.now()? Should I create a new sketch?