// DEVICE CONFIGURATION
// Libraries definition
#include <Wire.h> // Library for I2C communications protocol
#include <RTClib.h> // Library for DS1307 RTC management
#include <eepromi2c.h> // Library for 24LC256 EEPROM management
#include <OneWire.h> // Library for 1-Wire communications protocol
#include <DallasTemperature.h> // Library for DS18B20 temperature sensor
#include <SD.h> // Library for SD card management
#include <Sleep_n0m1.h> // Library for sleep and power save
// Analogue pins definition
const int Battery = 0; // Battery voltage - Analog pin 0
const int SolarCell = 1; // Solar cell voltage - Analog pin 1
const int ThermalFlux1 = 3; // Geothermal Flux device - Analog pin 2
const int ThermalFlux2 = 2; // Geothermal Flux sensor - Analog pin 3
// Digital pins definition
//const int TestButton = 2; // PermArduino working test button - Digital pin 2
const int TestLed = 9; // PermArduino working led - Digital pin 9
const int chipSelect = 10; // SD Card configuration - Digital pin 10
const int Power = 8; // Activation pin to power sensors - Digital pin 8
const int AirTemp = 4; // Air temperature sensor - Digital pin 4
const int SnowTemp = 5; // Snow temperature sensors chain - Digital pin 5
const int SurfaceTemp = 6; // Surface temperature sensor - Digital pin 6
const int GroundTemp = 7; // Ground temperature sensors chain- Digital pin 7
// Variables definition
boolean abortSleep; // Cancel sleep cycle
unsigned int n = 0; // Measurements counter
volatile byte e = 0; // Error type
byte sn[4]; // Sensors in each bus
unsigned int batt; // Store battery voltage
unsigned int solar; // Store solar cell voltage
float temp; // Store temperature data
int flux1; // Store geothermal flux (device) data
float flux2; // Store geothermal flux (sensor) data
// Constants definition
const float FluxRes = 41.8; // Resolution of Geothermal Flux 2 sensor (W·cm^-2/mV)
// EEPROM data configuration
struct config {
String header;
} config;
// Log files
File datafile; // File to store the adquired data
char filename1[] = "Data.csv"; // Name of the file to store the data
// Libraries configuration
RTC_DS1307 RTC; // Configure library to read the Real Time Clock
Sleep sleep; // Configure the library to sleep the Arduino board
unsigned long sleepTime; // Define the sleep time (in ms)
OneWire oneWire1(AirTemp); // Configure library to read air temperature sensors
DallasTemperature sensors1(&oneWire1);
OneWire oneWire2(SnowTemp); // Configure library to read snow temperature sensors
DallasTemperature sensors2(&oneWire2);
OneWire oneWire3(SurfaceTemp); // Configure library to read surface temperature sensors
DallasTemperature sensors3(&oneWire3);
OneWire oneWire4(GroundTemp); // Configure library to read ground temperature sensors
DallasTemperature sensors4(&oneWire4);
const byte sensorsnumber[4] = {1,14,1,10}; // Number of sensors in each bus
const byte Precision = 12; // DS18B20 precision configuration to 12 bits (0.0625ºC)
// Sensors identification
uint8_t airsensors[1][8] = { // Air temperature sensors
{0x28,0x96,0x38,0x20,0x05,0x00,0x00,0xE2} // Sensor at 160 cm heigth
};
uint8_t snowsensors[14][8] = { // Snow temperature sensors
{0x28,0x72,0x87,0x05,0x05,0x00,0x00,0xC6}, // Sensor at 2.5 cm height
{0x28,0x72,0x8E,0x04,0x05,0x00,0x00,0xBA}, // Sensor at 5 cm height
{0x28,0x22,0xF8,0x05,0x05,0x00,0x00,0xEA}, // Sensor at 10 cm height
{0x28,0xB4,0xAE,0x04,0x05,0x00,0x00,0x6F}, // Sensor at 20 cm height
{0x28,0xCB,0x84,0x04,0x05,0x00,0x00,0x96}, // Sensor at 30 cm height
{0x28,0x79,0x10,0x06,0x05,0x00,0x00,0x50}, // Sensor at 40 cm height
{0x28,0x52,0x30,0x05,0x05,0x00,0x00,0x6E}, // Sensor at 50 cm height
{0x28,0xF8,0x77,0x05,0x05,0x00,0x00,0x45}, // Sensor at 60 cm height
{0x28,0x60,0x41,0x06,0x05,0x00,0x00,0xC5}, // Sensor at 70 cm height
{0x28,0xD2,0x8F,0x04,0x05,0x00,0x00,0x2B}, // Sensor at 80 cm height
{0x28,0x04,0xA4,0x05,0x05,0x00,0x00,0x5A}, // Sensor at 100 cm height
{0x28,0x51,0xF7,0x05,0x05,0x00,0x00,0x44}, // Sensor at 120 cm height
{0x28,0x7E,0x2E,0x05,0x05,0x00,0x00,0x7B}, // Sensor at 140 cm height
{0x28,0x42,0xEF,0x04,0x05,0x00,0x00,0x8B} // Sensor at 160 cm height
};
uint8_t surfacesensors[1][8] = { // Surface temperatue sensors
{0x28,0x4A,0x86,0x20,0x05,0x00,0x00,0xB6} // Sensor at 1 cm depth
};
uint8_t groundsensors[10][8]={ // Ground temperature sensors
{0x28,0x1C,0xC4,0x04,0x05,0x00,0x00,0x3E}, // Sensor at 2.5 cm depth
{0x28,0xD4,0x2B,0x05,0x05,0x00,0x00,0x3A}, // Sensor at 5 cm depth
{0x28,0x7E,0xFD,0x05,0x05,0x00,0x00,0x6B}, // Sensor at 10 cm depth
{0x28,0xA5,0x57,0x04,0x05,0x00,0x00,0x56}, // Sensor at 20 cm depth
{0x28,0xAF,0x1C,0x06,0x05,0x00,0x00,0x07}, // Sensor at 30 cm depth
{0x28,0x7D,0x1E,0x05,0x05,0x00,0x00,0xA6}, // Sensor at 40 cm depth
{0x28,0x6D,0xA6,0x04,0x05,0x00,0x00,0x03}, // Sensor at 50 cm depth
{0x28,0x1E,0x86,0x05,0x05,0x00,0x00,0xB5}, // Sensor at 60 cm depth
{0x28,0x3F,0x36,0x05,0x05,0x00,0x00,0x7B}, // Sensor at 70 cm depth
{0x28,0xB4,0xFB,0x05,0x05,0x00,0x00,0xA7} // Sensor at 80 cm depth
};