Hola gente;
Acabo de terminar de disenar my projecto (estacion meteorologica) con un pluviometro y tres sensores de temperatura adaptados a una adafruit datalogging con SD card y RTC.
Todo va bien, excepto que no consigo imprimir los valores de temperatura en la sd card.
Este es el codigo completo
/*-----( Declare Libraries )-----*/
#include <SPI.h>
#include <SD.h> //Ensure that you downlaod these libraries and store them in your Arduino libraries folder
#include <Wire.h> //Remember no semi-colon after these commands
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h> //Get DallasTemperature Library here: http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
/*-----( Declare Constants and Pin Numbers )-----*/
const int REED = 9; //The reed switch outputs to digital pin 9
int val = 0; //Current value of reed switch
int old_val = 0; //Old value of reed switch
int REEDCOUNT = 0; //The intial count is zero
const int chipSelect = 10;
File logfile;
#define SYNC_INTERVAL 60000
#define ONE_WIRE_BUS_PIN 2
uint32_t syncTime = 0; // time of last sync()
/*-----( Declare objects )-----*/
RTC_Millis rtc; /////RTC_DS1307 RTC; // define the Real Time Clock object
OneWire oneWire(ONE_WIRE_BUS_PIN);// Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
/*-----( Declare Variables )-----*/
DeviceAddress Probe01 = {
0x28, 0x5A, 0xB7, 0x00, 0x00, 0x00, 0x80, 0x6D };// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress Probe02 = {
0x28, 0x5C, 0xB7, 0x00, 0x00, 0x00, 0x80, 0xDF };// See the tutorial on how to obtain these addresses:
DeviceAddress Probe03 = {
0x28, 0x12, 0x6D, 0x73, 0x06, 0x00, 0x00, 0x27 };// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
/////////////Program Set-up/////////////////////////////////////////////////////////////////////
void setup(void){ /****** SETUP: RUNS ONCE ******/
Serial.begin(9600);// start serial port to show results
pinMode (REED, INPUT_PULLUP); //Activate the internal pull-up resistor
/////////////Setting up the file on the SD Card///////////////////////////////
Serial.println("Initializing SD card...");
pinMode(10, OUTPUT); // make sure that the default chip select pin is set to output
if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
Serial.println("Card failed, or not present");
}
Serial.println("card initialized."); // create a new file
char filename[] = "LOGGER00.CSV"; //The file names must be in the 8.3 format
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) {
Serial.println("could not create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
/////rtc.adjust(DateTime(2015, 7, 7, 12, 40, 0));
Serial.print("Initializing temp. sensors.");
Serial.println(" Reading 3 probes:");
logfile.print("Temperatures and ");
sensors.begin();// Initialize the Temperature measurement library
sensors.setResolution(Probe01, 10);// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Probe02, 10);
sensors.setResolution(Probe03, 10);
Serial.println("Rainfall, Reading/count, Time, Date:");
logfile.println("Rainfall, Reading/count Time Date");
}//--(end setup )---
///////////////////The Program Loop//////////////////////////////////////////////////////////////////////
void loop(){ //****** LOOP: RUNS CONSTANTLY ******/
DateTime now = rtc.now(); /////DateTime now; // and record date/time of change
val = digitalRead(REED);
if ((val == LOW) && (old_val == HIGH))//Low means that the Reed switch is open (which happens when the magnet passess).
{
delay(10); // Delay put in to deal with any "bouncing" in the switch.
char buf1[50];
Serial.print("Precipitation");
Serial.print(" = ");
REEDCOUNT = REEDCOUNT + 10;
old_val = val;
Serial.print(REEDCOUNT);
Serial.print(" ml of rain ");
sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
Serial.println(buf1);
logfile.print("Precipitation");
logfile.print(" = ");
logfile.print("10");
logfile.print("ml at ");
logfile.println(buf1);
}
else {
old_val = val; //If the status hasn't changed then do nothing
}
if ((millis() - syncTime) < SYNC_INTERVAL) return;
logfile.flush();
syncTime = millis();
Serial.println();
Serial.print("Number of Devices found on bus = ");
Serial.println(sensors.getDeviceCount());
Serial.print("Getting temperatures... ");
Serial.println();
//char buf1[50];
sensors.requestTemperatures(); // Command all devices on bus to read temperature
Serial.print("Probe 01 temperature is: ");
printTemperature(Probe01);
Serial.println();
Serial.print("Probe 02 temperature is: ");
printTemperature(Probe02);
Serial.println();
Serial.print("Probe 03 temperature is: ");
printTemperature(Probe03);
Serial.println();
// Serial.println(sensors.getDeviceCount());
//logfile.println("Temperature Probe 01 = ");
//logfile.println("Temperature Probe 02 = ");
//logfile.println("Temperature Probe 03 = ");
//logfile.println(buf1);
//sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
//Serial.println(buf1);
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
Serial.print("Error getting temperature ");
}
else
{
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
End printTemperature
}
He intentado cambiar los codigos, las localizaciones pero cuando aplico esta parte del codigo simplemente la sd card no me crea ningun file
// Serial.println(sensors.getDeviceCount());
//logfile.println("Temperature Probe 01 = ");
//logfile.println("Temperature Probe 02 = ");
//logfile.println("Temperature Probe 03 = ");
//logfile.println(buf1);
//sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
//Serial.println(buf1);
Sabe alguien como puedo obtener las lecturas de precipitacion y temperaturas en la sd card? muchas gracias