Here is a snippet from my project. It is used to change the .CSV filename of the on-board backup on the first trip through the loop after midnight.
/*
// Serial print commands are for PLX-DAQ
From cosm library example and lifts from a lot of others
particularly from Stanley in Kuala Lumpur.
Use your own DS18B20 addresses, keys etc.
*/
#include <DallasTemperature.h> // Dallas temp
#include <OneWire.h> // Dallas Temp
#include <Ethernet.h> // Ethernet
#include <SPI.h> // Ethernet
#include <HttpClient.h> // Cosm lib
#include <Cosm.h> // Cosm lib
#include <PCD8544.h> // Nokia 5110
#include <SD.h> // SD card, and is all it needs
#include <string.h> // from "Date As Filename"
#include "RTClib.h" // from "Date As Filename"
#include "Wire.h" // Original RTC lib for LCD, SD, serial
// This is also used by
#define DS1307_ADDRESS 0x68
RTC_DS1307 RTC;
static PCD8544 lcd;
File myFile;
char filename[] = "00000000.CSV";
// Custom symbols
static const byte DEGREES_CHAR = 1;
static const byte degrees_glyph[] = { 0x00, 0x07, 0x05, 0x07, 0x00 };
static const byte SLASH_CHAR = 2;
static const byte slash_glyph[] = {0x00,0x20,0x10,0x08};
byte InThermo[8] = {
0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0X9F};
byte OutThermo[8] = {
0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {
0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95};
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int second, minute, hour, weekDay, monthDay, month, year;
unsigned int frac;
// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";
char calcId1[] = "diff";
char calcId2[] = "flowRate";
char calcId3[] = "kW";
void setup() {
lcd.begin(84, 48);
// Register the custom symbols...
lcd.createChar(DEGREES_CHAR, degrees_glyph);
lcd.createChar(SLASH_CHAR, slash_glyph);
Wire.begin();
Serial.begin(9600);
Serial.print(" filename ");
delay(300);//Wait for newly restarted system to stabilize
lcd.setCursor (0,0);
lcd.print("Initializing");
delay(2000);
lcd.setCursor (0,1);
pinMode(10, OUTPUT);
if (!SD.begin(4))
{
lcd.print("failed!");
delay (2000);
return;
}
lcd.print("init. OK!");
delay(2000);
getFileName();
Serial.println(filename);
lcd.clear();
Serial.println("LABEL,Time,InTemp,OutTemp,diff,DrainTemp");
sensors.setResolution(InThermo, 12);
sensors.setResolution(OutThermo, 12);
sensors.setResolution(DrainThermo, 12);
Serial.println("Starting multiple datastream upload to Cosm...");
Serial.println();
while (Ethernet.begin(mac) != 1)
{
Serial.println("Error getting IP address via DHCP, trying again...");
delay(10000);
}
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowLitres = 0;
totalLitres = 0;
oldTime = 0;
// The Hall-effect sensor is connected to pin 17 (A3) which uses interrupt 0.
// Configured to trigger on a FALLING state change (transition from HIGH
// state to LOW state)
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
GetClock();
if (hour == 0 && minute == 0 && second <2)
{
getFileName();
Daily = kWh - Daily;
MaxRise = 0;
}
} // loop ends here
void getFileName(){
DateTime now = RTC.now();
filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
}