Salve a tutti,
Il mio progetto è creare un lettore per controllare il consumo di energia nella mia casa tramite il contattore elettronico Enel (mi sono inspirato a questo sito Enerduino: Enerduino (Italiano) ma il mio hardware è diverso) leggendo con una fotoresistenza il led luminoso che è sull'apposito contatore.
Sono riuscito a fare quasi tutto ma non riesco a far memorizzare al mio logshield la data per poi usarla in modalità embedded (cioè senza computer), cioè fino a quando uso il mio logshield+ArduinoUNo connesso al mio mac la data è quella giusta, quando lo disconnetto per farlo funzionare senza il computer (ovviamente con un alimentatore) mi da sempre la data 2000/0/0.
Ho già ricontrollato tutto il circuito con il tester, sembra che l'elettronica funzioni bene. La batteria da 3 Volt è carica ed il suo alloggio è perfettamente saldato sulla sua posizione.
Hardware:
Arduino Uno
Logshield (Data-Logger Shield for Arduino)
Sensore (Photo cell (CdS photoresistor) : ID 161 : $0.95 : Adafruit Industries, Unique & fun DIY electronics and kits)
Grazie mille,
Piero
Inserisco anche il mio codice qui in appendice
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
RTC_DS1307 RTC; // define the Real Time Clock object
// the digital pins that connect to the LEDs
#define redLEDpin 3
#define greenLEDpin 2
unsigned long flash=1;
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(redLEDpin, HIGH);
while(1);
}
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
//DateTime now = RTC.now();
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
logfile.println("Millis, Datetime,Logical Value,Count Flash,Flash/NoFalsh, Light value");
#if ECHO_TO_SERIAL
Serial.println("Millis, Datetime,Logical Value,Count Flash,Flash/NoFalsh, Light value");
#endif //ECHO_TO_SERIAL
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
}
void loop(void) {
//DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
digitalWrite(greenLEDpin, HIGH);
// log milliseconds since starting
uint32_t m = millis();
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
//RTC.adjust(DateTime(__DATE__, __TIME__));
DateTime now = RTC.now();
// log time
//logfile.print(now.unixtime()); // seconds since 1/1/1970
//logfile.print(", ");
logfile.print('"');
logfile.print(now.year(),DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(' ');
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
#if ECHO_TO_SERIAL
//Serial.print(now.unixtime()); // seconds since 1/1/1970
//Serial.print(", ");
Serial.print('"');
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.print('"');
#endif //ECHO_TO_SERIAL
analogRead(photocellPin);
delay(10);
int photocellReading = analogRead(photocellPin);
//Serial.print("Light reading = ");
//Serial.print(photocellReading); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (photocellReading > 450)
{
logfile.print(", ");
logfile.print(flash);
logfile.print(", ");
logfile.print("1");
logfile.print(", ");
logfile.print(" , Flash");
logfile.print(", ");
logfile.println(photocellReading);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(flash);
Serial.print(", ");
Serial.print("1");
Serial.print(", ");
Serial.print(" , Flash");
Serial.print(", ");
Serial.println(photocellReading);
#endif
flash++;
digitalWrite(greenLEDpin, LOW);
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
digitalWrite(redLEDpin, HIGH);
logfile.flush();
digitalWrite(redLEDpin, LOW);
}
else
{
flash=1;
logfile.print(", ");
logfile.print("0");
logfile.print(", ");
logfile.print(" , No Flash");
logfile.print(", ");
logfile.println(photocellReading);
//digitalWrite(redLEDpin, HIGH);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print("0");
Serial.print(", ");
Serial.print(" , No Flash");
Serial.print(", ");
Serial.println(photocellReading);
#endif
}
//logfile.println(flash);
//Serial.println(flash);
delay(100);
}