Hi all, I hope this is a very stupid question, so the answer could be easy.
I'm using the code below with an Arduino UNO rev.3 and an SD card connected as follow.
Connection:
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS - depends on your SD card shield or module.
Code:
#include <SPI.h>
#include "RF24.h"
#include "DHT.h"
#include <SD.h>
#include <Wire.h>
//#include "SSD1306.h"
//#include "OLEDDisplayUi.h"
#include <TimeLib.h>
#include <DS1307RTC.h>
// DEBUG
#define DEBUG // Enable debug
#include "DebugUtils.h"
// SD
// .csv: add comma (",") to separate different data (each one in a column), printLN to new row
const int chipSelect = 4;
String current_year = "";
String current_month = "";
char current_year_char[5] = {};
int contNode[6] = {4,4,4,4,4,4}; // Write on SD every 60 minute (every 6 "cont")
float temp, hum;
// Data struct that contains Temp, Hum and the number of the node
struct data {
float Temp;
float Hum;
int node = 4; // Not stored in the addresses variable
} TandH;
unsigned long previousMillis = 0; // will store last time DHT was read
const long interval = 600000; // read after 10 minute
unsigned long currentMillis; // Store current intervall
void setup() {
Serial.begin(9600);
while (!Serial); // wait until Arduino Serial Monitor opens
// Start SD library
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
SERIAL_PRINTLN("WARNING: card initialization failed, or not present");
}
else SERIAL_PRINTLN("card initialized.");
// Start TIME library
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
SERIAL_PRINTLN("Unable to sync with the RTC");
else
SERIAL_PRINTLN("RTC has set the system time");
// Update current year and mounth variable
// WARNING: the variables have String type, but the function return int variable
current_year = String(year());
current_year.toCharArray(current_year_char, 5); //Trasforming the year variable in char
SD.mkdir(current_year_char); // Create a new folder with the name of the year
SERIAL_PRINT("New folder created with the name: ");
SERIAL_PRINTLN(current_year_char);
//SERIAL_PRINTLN("The current year is: " + current_year);
//current_month = monthStr(month()); // WARNING: not sure if it is needed
SERIAL_PRINTLN("Boot OK");
SERIAL_PRINTLN("NODE: cameraPC");
}
void loop() {
readDHT(0);
delay(1000);
}
// Write the data on the SD card every hour
void write(float temp, float hum, int node) {
contNode[node]++; // Increment the conunt
// check if is passed an hour
if (contNode[node] == 5) {
contNode[node] = 0; // Reset the variable
// Check if we need a new folder
SERIAL_PRINTLN("Checking if the current year is changed...");
//SERIAL_PRINT("Current year: ");
//SERIAL_PRINTLN(current_year);
if (String(year()) != current_year) {
//SERIAL_PRINTLN("Current year is changed, updating the variable...");
current_year = String(year()); // Update the variable
//String path = "/" + current_year; // WARNING: not sure what this line do...
current_year.toCharArray(current_year_char, 5); //Trasforming the year variable in char
SD.mkdir(current_year_char); // Create a new folder with the name of the year
SERIAL_PRINT("New folder created with the name: ");
SERIAL_PRINTLN(current_year_char);
}
// Assembling the file name using char
char fileName[50];
strcpy(&fileName[0], "/" ); //fileName[0] = "/";
strcpy(&fileName[1], current_year_char );
strcpy(&fileName[5], "/");
strcpy(&fileName[6], monthStr(month()) );
strcat(&fileName[6], ".csv");
SERIAL_PRINT("Filename: ");
SERIAL_PRINTLN(fileName);
File dataFile = SD.open(fileName, FILE_WRITE); // Open the file
// Check if the file opened properly
if (dataFile) {
SERIAL_PRINT("Opening the file in path: ");
SERIAL_PRINTLN(fileName);
SERIAL_PRINTLN("The file opened correctly!");
// Make the line with all the data
dataFile.print( String(day()) );
dataFile.print(",");
dataFile.print( String(hour()) );
dataFile.print(":");
dataFile.print( String(minute()) );
dataFile.print(":");
dataFile.print( String(second()) );
dataFile.print(",");
dataFile.print( String(temp) );
dataFile.print(",");
dataFile.print( String(hum) );
dataFile.print(",");
dataFile.print( String(node) );
dataFile.println();
SERIAL_PRINT("Data wrote on SD");
dataFile.close();
}
// if the file isn't open:
else {
SERIAL_PRINTLN("WARNING: error opening the file!");
}
}
}
void readDHT(int address){
// Read sensor value
int error = 0;
do {
//delay(2000);
temp = random(0,100);
hum = random(0,100);
error++;
} while (temp == 0 || hum == 0 && error <= 15);
if(error >= 15){
SERIAL_PRINTLN("Error while reading, NOT READ");
}
// Not send the value, this is the master
// Writing it on SD, with the code of the reading module
// cameraPC is not present in the address, the next number however is 4
write(temp, hum, 4);
SERIAL_PRINT("Data read = ");
SERIAL_PRINT(temp);
SERIAL_PRINT(" ");
SERIAL_PRINTLN(hum);
}
After assembling the file name correctly (as I can see from the console output), the console says that the Arduino was not able to open the fileName
path.
Also with an arbitrary fileName = "/test/test.cvs"
it does not open.
The connections are fine because the SD writing example in the library works well.
Any suggestion?
Thanks!