RTC Project and Datalogging to an SD  

Page 2 / 2
  RSS

Pugwash
(@pugwash)
Prominent Member
Joined: 9 months ago
Posts: 880
2019-09-20 7:09 am  

@berner @robo-pi

Looks like I owe you guys an apology, it wasn't your code I was criticising, it was Bills.

So I guess I have now committed heresy, by posting my version of how this should be done.

😎 

SteveC - Topple Rudd Poltman


ReplyQuote
Robo Pi
(@robo-pi)
Noble Member
Joined: 9 months ago
Posts: 1462
2019-09-20 8:05 am  
Posted by: @pugwash

Looks like I owe you guys an apology, it wasn't your code I was criticising, it was Bills.

So I guess I have now committed heresy, by posting my version of how this should be done.

DroneBot Heresy

DroneBot Workshop Robotics Engineer
James


ReplyQuote
Spyder
(@spyder)
Honorable Member
Joined: 9 months ago
Posts: 627
2019-09-20 4:56 pm  
Posted by: @pugwash

Fahrenheit is only kept to humor {American spelling) Americans! Other than that it is of no use at all and has to be calculated.

One day, we might even get them to stop measuring wavelength in feet and inches!

There was a Thursday, quite some number of decades ago, when all of America was supposed to be switching over to the metric system

But, Americans, as they are wont to be, were quite adverse to the change in their daily lifestyle, clinging as they do to their guns and their Bibles, and their foot long sandwiches

So, when this fateful Thursday in November rolled around when they were supposed to give up their standard measuring sticks, and footwear whose size is actually based on the size of a barleycorn rather than... feet, they promptly, and as a whole, completely ignored the entire absurd idea, and instead opted to give up smoking for a day


ReplyQuote
Berner
(@berner)
Eminent Member
Joined: 6 months ago
Posts: 32
2019-09-22 2:35 pm  
Posted by: @pugwash

@berner

Not sure if this is the place to ask this one, but does anyone know if the AM2320 is capable of outputting the temp in F? In my other test sketches I figured how to do it with a formula in the code, but just thought I would ask. Again..... thanks.

I find it hard to believe you asked this question. ICs that output Fahrenheit are about as rare as "rocking horse manure". Fahrenheit is only kept to humor {American spelling) Americans! Other than that it is of no use at all and has to be calculated.

One day, we might even get them to stop measuring wavelength in feet and inches! 🤣 🤣 🤣 🤣 🤣 

Edit: I am not entirely correct, Fahrenheit can be used for calculating BTUs, but I prefer joules!

I think the MCP9808  has functionality for rocking horse manure, so I thought I would ask.  I do agree with your commentary though, here in the states we were taught early on to learn metric because it was coming! Only it never did, what happened with that? Oh wait, I know, its a system that makes sense and we cant have that now can we! Didn't we even lose a Mars rover because someone missed a metric conversion? I guess I wont ask for the code to convert ounces to stones. 😎 

In all seriousness though, the reason I asked about looking for Fahrenheit was because I am assisting with my sons 7th grade STEM class and thought making a small sensor project would be fun. Since everyone relates to F, I thought I would want to display results in that way. 

I also wanted to thank you for the code assist, it helped me a lot. I am still working through some things on it as I try to understand it. 👍 

 

 


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 9 months ago
Posts: 880
2019-09-22 2:43 pm  

@berner

Sorry but there is nothing on this datasheet that mentions Fahrenheit!

 

Didn't we even lose a Mars rover because someone missed a metric conversion?

Yes, it was an ESA/NASA joint venture, the descent was calculated in feet per second but the landing actually happened in metres per second! SPLATT!

This post was modified 5 months ago by Pugwash

SteveC - Topple Rudd Poltman


ReplyQuote
Berner
(@berner)
Eminent Member
Joined: 6 months ago
Posts: 32
2019-09-22 3:02 pm  

@pugwash

MCP9808: Then I think the library I used had it built in function to do that... I just know I didn't have to do a conversion. 


ReplyQuote
Berner
(@berner)
Eminent Member
Joined: 6 months ago
Posts: 32
2019-09-23 5:30 pm  

@pugwash

I worked on implementing the code you provided here for my project, and it works great. I am now working on a few edits to tweak it for my project and hoping you can offer some more guidance. The original DBWS code writes to the serial monitor every second, but only takes a temp reading every 10 seconds. In my data logging project, I only want to capture and write data every 30 seconds. I changed the code as follows:

 

// Read Temp Humid sensor every 30 seconds    
if (ticks ==30){
   // Read Humidity
    h = am2320.readHumidity();
    // Read temperature as Celsius
    t = am2320.readTemperature();
    printCurrentTime(h,t);
    ticks = 0;

  }
}

// Update serial monitor display if a second has elapsed  
//if (ticks != old_tick_value) {
//        old_tick_value = ticks;
//        printCurrentTime(h,t);
       
//    }

I commented out the section that updates the serial monitor every second, and added printCurrentTime to the section above it that reads the sensor every 30 seconds.  This works great for what I see in the serial monitor, but on the SD card I still see data being logged every second. I know I am missing some simple detail/understanding, any thoughts?  Here is the code that I am working with:

String print2digits(int number){
  String tString = String(number);
    if (tString.length() < 2){
    tString = "0" + tString;
  }
  return tString;
}

// Print to the serial monitor
void printCurrentTime(float hum, float tem){
  tmElements_t tm;
  String dataString;
  if (RTC.read(tm)) {
    dataString = print2digits(tm.Hour) + ":" +
    print2digits(tm.Minute) + ":" +
    print2digits(tm.Second) + "-" +
    tmYearToCalendar(tm.Year) + "-" +
    print2digits(tm.Month) + "-" +
    print2digits(tm.Day) +
    " Hum: " + hum + "% - Temp: " + tem + " C" + "\n";

  Serial.println(dataString);
  }


// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
  File dataFile = SD.open("datalog1.txt", FILE_WRITE);

// if the file is available, write to it:
  if (dataFile) {
    dataFile.print(dataString);
    dataFile.close();
  }else {
    Serial.println("error opening datalog.txt");
  }
}

 


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 9 months ago
Posts: 880
2019-09-23 5:47 pm  

@berner

I can't see any logical reason why your SD card is being written to every second!

Can you attach the whole sketch to your next post, there may be a glitch elsewhere.

SteveC - Topple Rudd Poltman


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 9 months ago
Posts: 880
2019-09-23 6:00 pm  

@berner

This might help, it is at least worth a try!

String print2digits(int number){
  String tString = String(number);
    if (tString.length() < 2){
    tString = "0" + tString;
  }
  return tString;
}

// Print to the serial monitor

void printCurrentTime(float hum, float tem){
  tmElements_t tm;
  String dataString;
  if (RTC.read(tm)) {
    dataString = print2digits(tm.Hour) + ":" +
    print2digits(tm.Minute) + ":" +
    print2digits(tm.Second) + "-" +
    tmYearToCalendar(tm.Year) + "-" +
    print2digits(tm.Month) + "-" +
    print2digits(tm.Day) +
    " Hum: " + hum + "% - Temp: " + tem + " C" + "\n";

  Serial.println(dataString);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.

  File dataFile = SD.open("datalog1.txt", FILE_WRITE);

// if the file is available, write to it:

if (dataFile) {
    dataFile.print(dataString);
    dataFile.close();
  }else {
    Serial.println("error opening datalog.txt");
  }
}
}

Best of luck, let me know if it works or not!

This post was modified 5 months ago by Pugwash

SteveC - Topple Rudd Poltman


ReplyQuote
Berner
(@berner)
Eminent Member
Joined: 6 months ago
Posts: 32
2019-09-23 6:43 pm  

@pugwash

Maybe my brain is getting too mushy..... I worked on so many copies of my code over the weekend I very well may have crossed a few wires. I have my original sketches on my workbench setup, I will grab that when I get home and send a copy. 


ReplyQuote
Berner
(@berner)
Eminent Member
Joined: 6 months ago
Posts: 32
2019-09-23 11:47 pm  

Nevermind! I just came home and looked at my code, I missed an end bracket for the section that deals with updating the serial monitor every second. I think I am good to go (for the moment 🍻 ). You need a well organized thought process for coding and an eye for detail. 


ReplyQuote
dxj
 dxj
(@dxj)
Active Member
Joined: 4 weeks ago
Posts: 19
2020-02-14 4:23 am  

You can eliminate many lines of code and unneeded sub-routines by using the sprintf function. There are aged old arguments one way or the other ... readability vs. maintainability vs. efficiency etc. It's not without it's drawbacks, but thought I'd mention it for those interested.

/* Get date/time into a readable format */
char datastr[22];
sprintf(datastr, "%4d/%02d/%02d - %02d:%02d:%02d",
    year(), month(), day(), hour(), minute(), second());
Serial.println(datastr) // example: 2020/02/13 - 20:59:44

p.s. - Make sure your char buffer is large enough or you can use snprintf instead. Also Arduino doesn't support floats in sprintf, so you can use the dtostrf function for floats.

https://arduinobasics.blogspot.com/2019/05/sprintf-function.html

This post was modified 2 weeks ago 2 times by dxj

ReplyQuote
Page 2 / 2

Please Login or Register