Go Down

Topic: Estacion meteorologica  (Read 2578 times) previous topic - next topic

Acachinero

Mar 04, 2016, 11:51 am Last Edit: Mar 04, 2016, 01:32 pm by Acachinero
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
Code: [Select]
/*-----( 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
Code: [Select]
// 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

surbyte

#1
Mar 04, 2016, 01:45 pm Last Edit: Mar 04, 2016, 01:59 pm by surbyte
Edito mi respuesta porque leí mal tu consulta
esto que reporta
Code: [Select]

  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");
  }


Crea el archivo?
En esta línea falta algo

Code: [Select]
logfile = SD.open(filename, FILE_WRITE);
debería decir
Code: [Select]
File logfile = SD.open(filename, FILE_WRITE);

Acachinero

Todo eso lo tengo mas que mirado y probado. Todas esas librerias funcionan cuando las aplico a mi sd card.
Mi problema viene cuando intento combinar las lecturas de precipitacion y temperaturas. El serial monitor me muestra lo que quiero obtener pero solo me graba en la sd card los valores de precipitacion hasta que empieza a marcar los valores de temperatura (en el serial monitor).
He intentado miles de combinaciones, la gente del foro en ingles me han intentado pero no damos con la modificacion que debo hacer al codigo.
El problema esta en esta parte
Code: [Select]
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");
  }

tambien he intentado este, pero sigue sin crearme el fichero en la sd card cuando aplico ambos logfile para precipitacion y temperaturas
Code: [Select]
char filename[] = "LOGGER00.CSV";      //The file names must be in the 8.3 format
  int i= 0;
  while(SD.exists(filename)) { // find a file name you haven't used before
    i++;
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
  }    // create the file as it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);

  if (! logfile) {
    Serial.println("could not create file because there is 100 files already");
  }

Gracias

surbyte

#3
Mar 04, 2016, 02:00 pm Last Edit: Mar 04, 2016, 02:01 pm by surbyte
Revisa que edite 2 veces.
Esta mal inicializado el objeto logfile

Acachinero

Como hago para que edite 2 veces?

surbyte

#5
Mar 04, 2016, 02:11 pm Last Edit: Mar 04, 2016, 02:13 pm by surbyte
Yo edité dos veces. Lee mi respuesta anterior.
La repito porque no me entiendes

En esta línea falta algo

Code: [Select]
logfile = SD.open(filename, FILE_WRITE);


debería decir
Code: [Select]
File logfile = SD.open(filename, FILE_WRITE);



Acachinero

Acabo de hacerlo y el serial monitor sigue marcando los valores pero dice Could not create a file (no se pudo crear fichero)
Code: [Select]
#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
      File 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);
    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);

  }
  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();   

     
    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();

   
}//--(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
}


surbyte

Has leido esto?
Quote
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(10, OUTPUT);

Acachinero

Si, lo tuve en cuenta.
La activacion o desactivacion de esta parte es lo que hace a la tarjeta grabar solo los valores de precipitacion (cuando los logfile de temperatura estan desactivados) o no grabar ninguno (cuando los activo) y viceversa.
Code: [Select]
//Serial.println(sensors.getDeviceCount());
    //logfile.println("Temperature Probe 01 = ");
    //logfile.println("Temperature Probe 02 = ");
    //logfile.println("Temperature Probe 03 = ");
    //logfile.println(buf1);

surbyte

No me refiero a usar ChipSelect 4 o 10. 
No dices que SD estas usando?

Acachinero


surbyte

usa los tags de imagenes por favor. Yo ya no descargo mas fotos adjuntadas

Acachinero

la targeta es una SDHC Card 8GB SanDisk

surbyte

#13
Mar 04, 2016, 07:08 pm Last Edit: Mar 04, 2016, 07:10 pm by surbyte
No me refería a la tarjeta sino a que modulo lector lo conectas. SI es un modulo SD CARD o es el que viene con la ethernet. Pero no me expliqué bien.

Las fotos como enlaces y códigos deben insertarse usando tags, algo que esta bien explicado en las normas del foro.
EN el caso de las fotos tienes que usar algun repositorio de imagenes como www.tinypic.com

Acachinero

Lo comente al principio, es una adafruit datalogging con real time clock https://www.adafruit.com/products/1141

Go Up