public
Last active

Arduino - Ethernet Shield Connect the Ethernet shield to the network, and Internet, with a fixed IP address Get the current date & time from an NTP server and apply time zone adjustment Create some files on the SD card with default and fixed datetime stamps Create a file on the SD card, write some lines to it, set the datetime stamps on the file with the now() values Read the file size and the free space on the SD card

  • Download Gist
EthernetShieldSDFileTimes.ino
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/*
* Ethernet Shield *
Fixed IP Address
Get Time from NTP with TZ adjustment
SD Card create CSV file
Set file date & time
Read remaining free space on SD card
Ethernet functions
Based on Ethernet shield code
created 18 Dec 2009
modified 9 Apr 2012
by David A. Mellis
SD functions
Based on Example Code
from Timstamp sketch in SdFat library
Current Code
Created by Derek Erb 13/01/2013
Last Modified : 13/01/13
by Derek Erb
Requirement : Arduino w/ Ethernet Shield and a FAT formatted SD card installed
*/
 
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h>
#include <SdFat.h>
 
// Version number
const float fVerNum = 0.1;
 
// Local port to listen for UDP packets
unsigned int uiLocalPort = 8888;
 
// Time Server fixed IP address (nist1-ny.ustiming.org)
IPAddress ipTimeServer(64, 90, 182, 55);
 
// NTP time stamp from first 48 bytes of the message
const int iNTP_PACKET_SIZE = 48;
 
// Buffer to hold incoming / outgoing packets
byte byPacketBuffer[ iNTP_PACKET_SIZE];
 
// Time Zone (Difference from GMT in seconds)
const long lTZ = 3600;
 
// UPD instance
EthernetUDP Udp;
 
// Arduino Ethernet Shield = pin 4
const int iSDPin = 4;
 
// Global card and file variables
SdFat sd;
SdFile file;
char strFname[] = "WRITETST.CSV"; // File name
 
int i = 1; // Global counter
 
// Global time variable
time_t t;
 
////////////////////////////////////////////////////////
//
// sendNTPpacket
//
// Send an NTP request to the NTP time server at the IP address
//
//
unsigned long sendNTPpacket(IPAddress& address) {
 
// set all bytes in the buffer to 0
memset(byPacketBuffer, 0, iNTP_PACKET_SIZE);
 
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
byPacketBuffer[0] = 0b11100011; // LI, Version, Mode
byPacketBuffer[1] = 0; // Stratum, or type of clock
byPacketBuffer[2] = 6; // Polling Interval
byPacketBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
byPacketBuffer[12] = 49;
byPacketBuffer[13] = 0x4E;
byPacketBuffer[14] = 49;
byPacketBuffer[15] = 52;
 
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(byPacketBuffer, iNTP_PACKET_SIZE);
Udp.endPacket();
}
 
////////////////////////////////////////////////////////
//
// PrintIP
//
// Serial Print IP address
//
void PrintIP(byte byArr[]) {
Serial.print(byArr[0]);
Serial.print(F("."));
Serial.print(byArr[1]);
Serial.print(F("."));
Serial.print(byArr[2]);
Serial.print(F("."));
Serial.print(byArr[3]);
}
 
////////////////////////////////////////////////////////
//
// PrintTime
//
// Serial Print time (HH:MM:SS - Day, DD/MM/YYYY
//
void PrintTime(time_t t) {
char* strDays[] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
lzPrint(hour(t));
Serial.print(F(":"));
lzPrint(minute(t));
Serial.print(F(":"));
lzPrint(second(t));
Serial.print(F(" - "));
Serial.print(strDays[weekday(t)]);
Serial.print(F(", "));
lzPrint(day(t));
Serial.print(F("/"));
lzPrint(month(t));
Serial.print(F("/"));
lzPrint(year(t));
Serial.println();
}
 
////////////////////////////////////////////////////////
//
// lzPrint
//
// Serial Print iVal with a leading zero
//
 
void lzPrint(int iVal) {
if (iVal < 10)
Serial.print('0');
Serial.print(iVal);
}
 
////////////////////////////////////////////////////////
//
// PrintTimestamps
//
// Serial print all of file's datetime stamps
//
void PrintTimestamps(SdFile& f) {
dir_t d;
if (!f.dirEntry(&d))
Serial.println(F("ERROR : f.dirEntry failed"));
Serial.print(F("Created :\t"));
f.printFatDate(d.creationDate);
Serial.print(F(" "));
f.printFatTime(d.creationTime);
Serial.println(F("\t(yyyy-mm-dd hh:mm:ss)"));
 
Serial.print(F("Modified :\t"));
f.printFatDate(d.lastWriteDate);
Serial.print(F(" "));
f.printFatTime(d.lastWriteTime);
Serial.println(F("\t(yyyy-mm-dd hh:mm:ss)"));
 
Serial.print(F("Accessed :\t"));
f.printFatDate(d.lastAccessDate);
Serial.println(F("\t\t(yyyy-mm-dd)"));
}
 
////////////////////////////////////////////////////////
//
// ShowFreeSpace
//
// Serial print space available on SD card
//
void ShowFreeSpace() {
// Calculate free space (volume free clusters * blocks per clusters / 2)
long lFreeKB = sd.vol()->freeClusterCount();
lFreeKB *= sd.vol()->blocksPerCluster()/2;
 
// Display free space
Serial.print(F("Free space: "));
Serial.print(lFreeKB);
Serial.println(F(" KB"));
}
 
////////////////////////////////////////////////////////
//
// GetSetTime
//
// Get the time via NTP, adjust for Time Zone and set time variable
//
void GetSetTime() {
// Set an NTP packet to the NTP time server
Serial.println(F("\nSending NTP Packet..."));
sendNTPpacket(ipTimeServer);
// wait to see if a reply is available
Serial.println(F("\nWaiting for reply..."));
delay(1000);
 
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
Serial.println(F("Reading received packed..."));
Udp.read(byPacketBuffer, iNTP_PACKET_SIZE);
 
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(byPacketBuffer[40], byPacketBuffer[41]);
unsigned long lowWord = word(byPacketBuffer[42], byPacketBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print(F("Seconds since Jan 1, 1900 = "));
Serial.println(secsSince1900);
 
// now convert NTP time into everyday time:
Serial.print(F("Unix time = "));
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);
 
Serial.println(F("Setting time var to UNIX time..."));
t = epoch;
Serial.print(F("The current UTC time is "));
PrintTime(t);
Serial.println(F("Adjusting for local time zone..."));
t = epoch + lTZ;
Serial.print(F("The current local time is "));
PrintTime(t);
}
}
////////////////////////////////////////////////////////
//
// SETUP
//
void setup(void) {
// Ethernet Shield MAC
byte byMAC[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xA3, 0x39 };
// Fixed IP address config
byte byDNS[] = { 8, 8, 8, 8 };
byte byGateway[] = { 192, 168, 0, 1 };
byte bySubnet[] = { 255, 255, 255, 0 };
// Ethernet Shield Fixed IP address
IPAddress ipA(192,168,0,51);
// Setup serial monitor
Serial.begin(9600);
// Wait 3 seconds
delay(3000);
// Some declaring text
Serial.println(F("\nArduino Ethernet Shield"));
Serial.println(F("Fixed IP & NTP & Time Zone &"));
Serial.println(F("SD Card File & date-time stamp &"));
Serial.println(F("File Size & Free Space"));
Serial.print(F("Version : "));
Serial.println(fVerNum);
Serial.println(F("Arduino - Derek Erb\n"));
// Initialise Ethernet network connection
Ethernet.begin(byMAC, ipA, byDNS, byGateway, bySubnet);
// give the Ethernet shield a second to initialise :
delay(1000);
Serial.println(F("Connecting to Ethernet network and Internet...\n"));
// Local IP address
Serial.println(F("Local IP info : "));
Serial.print(F("IP address : "));
Serial.print(Ethernet.localIP());
Serial.print(F("\nSubnet : "));
PrintIP(bySubnet);
Serial.print(F("\nGateway : "));
PrintIP(byGateway);
Serial.print(F("\nDNS : "));
PrintIP(byDNS);
Serial.print(F("\n"));
// Initialise UDP connection
Serial.println(F("\nInitialising UDP connection..."));
Udp.begin(uiLocalPort);
 
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!sd.begin(iSDPin, SPI_HALF_SPEED))
sd.initErrorHalt();
 
// remove files if they exist
sd.remove("DEFAULT.TXT");
sd.remove("STAMP.TXT");
sd.remove(strFname);
 
// create a new file with default datetimestamps
if (!file.open("DEFAULT.TXT", O_CREAT | O_WRITE)) {
Serial.println(F("ERROR : open DEFAULT.TXT failed."));
}
Serial.println(F("DEFAULT.TXT created with default date/time stamps:"));
PrintTimestamps(file);
 
// close file
file.close();
Serial.print(F("\n"));
// create a new file with set datetimestamps
if (!file.open("STAMP.TXT", O_CREAT | O_WRITE)) {
Serial.println(F("ERROR : open STAMP.TXT failed"));
}
Serial.println(F("STAMP.TXT created with default date/time stamps:"));
PrintTimestamps(file);
// set creation date time 2012-01-01 22:27:00
if (!file.timestamp(T_CREATE, 2012, 1, 1, 22, 27, 00)) {
Serial.println(F("ERROR : set create time failed"));
}
// set write/modification date time 2012-12-31 04:42:24
if (!file.timestamp(T_WRITE, 2012, 12, 31, 4, 42, 24)) {
Serial.println(F("ERROR : set write time failed"));
}
// set access date 2013-01-08 16:49:49
if (!file.timestamp(T_ACCESS, 2013, 1, 8, 16, 49, 49)) {
Serial.println(F("ERRROR : set access time failed"));
}
Serial.println(F("STAMP.TXT modified with set date/time stamps:"));
PrintTimestamps(file);
 
file.close();
 
// Get and set time
GetSetTime();
// create a new file with current datetimestamps
if (!file.open(strFname, O_CREAT | O_WRITE)) {
Serial.print(F("ERROR : open "));
Serial.print(strFname);
Serial.print(F(" failed"));
}
Serial.print(F("\n"));
Serial.print(strFname);
Serial.println(F(" created with current date/time stamps:"));
PrintTimestamps(file);
// set creation date time to now
if (!file.timestamp(T_CREATE, year(t), month(t), day(t), hour(t), minute(t), second(t))) {
Serial.println(F("ERROR : set create time failed"));
}
// set write/modification date time to now
if (!file.timestamp(T_WRITE, year(t), month(t), day(t), hour(t), minute(t), second(t))) {
Serial.println(F("ERROR : set write time failed"));
}
// set access date to now
if (!file.timestamp(T_ACCESS, year(t), month(t), day(t), hour(t), minute(t), second(t))) {
Serial.println(F("ERRROR : set access time failed"));
}
Serial.print(strFname);
Serial.println(F(" created with current date/time stamps:"));
PrintTimestamps(file);
file.close();
 
Serial.println(F("\nSetup() Done."));
}
 
////////////////////////////////////////////////////////
//
// LOOP
//
void loop(void){
Serial.println(F("\nReading free space..."));
ShowFreeSpace();
 
// Open file for writing (appending)
if (!file.open(strFname, O_CREAT | O_WRITE | O_AT_END)) {
Serial.print(F("ERROR : open "));
Serial.print(strFname);
Serial.print(F(" failed"));
}
// Write to file
Serial.print(F("\nWriting to the file...\tIteration : "));
Serial.println(i);
for (int j = 0; j < 10; j++) {
file.print("\"FILE WRITE TEST\",1234");
file.print(j + ((i-1)*10));
file.println(",\"LINE WRITTEN\",\"ARDUINO FILE TIMESTAMP TESTING\"");
}
i++;
// set write/modification date time to now
if (!file.timestamp(T_WRITE, year(t), month(t), day(t), hour(t), minute(t), second(t))) {
Serial.println(F("ERROR : set write time failed"));
}
// set access date time to now
if (!file.timestamp(T_ACCESS, year(t), month(t), day(t), hour(t), minute(t), second(t))) {
Serial.println(F("ERRROR : set access time failed"));
}
Serial.print(strFname);
Serial.println(F(" modified with current date/time stamps:"));
PrintTimestamps(file);
file.close();
 
Serial.print(F("Current File Size :\t"));
Serial.print(file.fileSize());
Serial.println(F(" b"));
// Wait 30 seconds
Serial.println(F("Waiting 30 seconds..."));
delay(30000);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.