pentiumcadiz
Published © GPL3+

Pool Logger

Keeping an eye on a pool.

IntermediateShowcase (no instructions)3,421
Pool Logger

Things used in this project

Story

Read more

Schematics

pool_logger schema

Code

Pool Log to microSD

C/C++
/*
  Realiza la lectura de varios sensores (temperatura del agua, ruido,...) y graba 
  información en una microSD (externa) cada n segundos.
  Developed by Pentiumcadiz - sept 2017
  
  Based on "SD card read/write", created by David A. Mellis and modified by Tom Igoe

*/

// Tarjeta microSD   --------------------------------
#include <SD.h>

#define chipSelect 10    // Funciona Ok con tarjeta micro SD Catalex dic2014

File logfile;  // Fichero de log de las temperaturas

/*
microSD card attached to SPI bus as follows:
  ** MOSI - pin 11
  ** MISO - pin 12
  ** CLK - pin 13
  ** CS - pin 10
*/


// Sensor de temperatura  --------------------------------

#include <OneWire.h> // librerías para sensor de temp
#include <DallasTemperature.h>
#define Pin 7 //pin donde se conectará el sensor de temp

OneWire ourWire(Pin); //pin asignado al bus para la comunicación OneWire
DallasTemperature sensors(&ourWire); //Se instancia la librería DallasTemperature

// Sensor de ruido  --------------------------------

# define pin_ruido 6      // pin de entrada del sensor de ruido (Digital)

boolean unavez = true;
boolean encendido = false;

String motorstatus = "apagado  "; 

// RTC (reloj)   --------------------------------

 #include <Wire.h>
 #include <Time.h>
 #include <DS1307RTC.h>

 String cadena;

//  -------  Fin de declaración de variables -----------

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  sensors.begin(); //Se inician los sensores de Onewire

  Serial.print("Initializing SD card...");
  // 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);

 // See if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Tarjeta microSD - Fallo al iniciar!");
    return;
  }
  Serial.println("Tarjeta microSD - Iniciada con éxito !");


  // Asignación de pines digitales a Vcc y Gnd

  pinMode(pin_ruido, INPUT);  // Entrada digital del sensor de sonido
}

void loop(void) 
{
  
  // ¿Suena el motor?
  encendido = digitalRead(pin_ruido);
  if (encendido)
  { motorstatus= "Encendido" ;  }
  else
  { motorstatus = "   OFF   " ; }
  
  // Qué temperatura tiene el agua?
  sensors.requestTemperatures(); //Prepara el sensor para la lectura

  if (unavez)
  {
    Serial.print("Medidas actuales\n");
  }

// Fecha y hora -----------------------------------
  tmElements_t tm;

  if (RTC.read(tm)) 
  {
  
  cadena = cadena + tm.Day + "/" + tm.Month;
  cadena = cadena + "\t" + tm.Hour + ":" + tm.Minute;

  Serial.println(cadena);

  } 
  else 
  {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  
// Fin(Fecha y hora) ----------------------------------- 

 //Mostramos mensaje con las temperaturas
  Serial.print(cadena);
  Serial.print("Motor: ");
  Serial.print(motorstatus);
  Serial.print("\t");
  Serial.print(encendido);
//  Serial.print("\t Temperatura: \t");
//  Serial.print(sensors.getTempCByIndex(0)); //Se lee e imprime la temperatura en grados Celsius
  Serial.println(" grados Centigrados");



  // Grabar los datos leidos en la tarjeta

// Open the file.
  File logFile = SD.open("temp.log", FILE_WRITE);
  if (logFile)
  {
   if (unavez)
     {
       logFile.println(" Fecha \tHora \tTemp \t   Motor  ");
       logFile.println(" ===== \t===== \t===== \t==========");
      }
   logFile.print(cadena);
   logFile.print("\t");
   logFile.print(sensors.getTempCByIndex(0));
   logFile.print("\t");
   logFile.println(motorstatus);
   // close the file:
   logFile.close();
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("error opening temp.log");
  }
  delay(20000);  //Usamos un retardo de n segundos entre lectura y lectura
  unavez = false;
  cadena = " ";

}

Credits

pentiumcadiz
2 projects • 7 followers
Software professional. Hardware enthusiast.
Contact

Comments

Please log in or sign up to comment.