Hi everyone:
Since a couple of months ago I have been working in a code for a scientific data logger that can be used to aquire environmental data from the field during months without turning off. The variables I wish to aquire (and the sensors used to achieve this task) are:
- Date (DS3231)
- Time (DS3231)
- Ambient temperatura (AM2305, enclosed)
- Relative humidity (AM2305, enclosed)
- Soil temperatura (AM2305, enclosed)
- Soil humidity (AM2305, enclosed)
- LUX index (TSL2561)
The main controller I am using is an ATmega328P, which comes included in the Seeeduino stalker board V2.3 from SEEEDSTUIDO (specs here: Seeeduino Stalker v2.3 | Seeed Studio Wiki). I am using this board not only because it is arduino compatible, but also because:
- It comes with RTC (DS3231) included and socket for 3.3 CR32032 battery
- It ALWAYS uses 3.3V, so it is easy to connect 3.3V sensor directly without resistors or level converters
- It comes with solar cell based LiPo charger and solar cell
- It comes with JST socket for 3.7V lipo battery
- It comes with XBEE socket (power can be turned on/off via digital pin 5)
- It comes with mSD socket directly wired to ISP lines (power can be turned on/off via digital pin 4)
By itself, this board is supposed to offer a stand alone platform to log data for long periods in practically any environment (if you use it with its plastic waterproof enclosure), so the only thing I did was to add a TSL2561 sensor to it via GROVE (Stalker comes with 2.0mm grove connector for I2C peripherals) and a couple of enclosed AM2305 sensors directly soldered to board's pins (D7 and D8, also available via 2.0mm grove connector directly from the board).
I have been working hard in the coding part and, till moment I have achieved some good results. Nevertheless, a couple of weeks ago I decided to ask for profesional help with an external consultant and he came up with this code:
#include <avr/sleep.h>
#include <avr/power.h>
#include<SPI.h>
#include <Wire.h>
#include <SD.h>
#include <DS3231.h>
DS3231 RTC; //Create RTC object for DS3231 RTC come Temp Sensor
#include "RTClib.h"
RTC_DS3231 rtc;
#include <dht.h>
dht DHT44b;
dht DHT44a;
#define DHT44b_PIN 8
#define DHT44a_PIN 7
#include <Digital_Light_TSL2561.h>
//static uint8_t prevSecond=0;
static DateTimeDS interruptTime;
//const int chipSelect = 10;
char minute_m = 0;
char minute_l = 0;
void setup()
{
/*Initialize INT0 pin for accepting interrupts */
PORTD |= 0x04;
DDRD &=~ 0x04;
pinMode(4,INPUT);//extern power
// RTC.begin();
Serial.begin(9600);
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(DATE), F(TIME)));
TSL2561.init();
pinMode(5, OUTPUT);
digitalWrite(5,HIGH);
pinMode(10, OUTPUT);
SD.begin(10);
delay(2000);
Serial.println("PLEASE ENTER SAMPLING TIME IN MINUTE :)");
Serial.println(" ");
delay(3000);
while( !(Serial.available() > 0));
minute_m = Serial.read();
Serial.print(minute_m);
while( !(Serial.available() > 0));
minute_l = Serial.read();
Serial.print(minute_l);
// minute_m = 0x30;
// minute_l = 0x37;
minute_m -= 0x30;
minute_l -= 0x30;
minute_m = (minute_m*10) + minute_l;
attachInterrupt(0, INT0_ISR, LOW); //Only LOW level interrupt can wake up from PWR_DOWN
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
//Enable Interrupt
//RTC.enableInterrupts(EveryMinute); //interrupt at EverySecond, EveryMinute, EveryHour
// or this
DateTimeDS start = RTC.now();
interruptTime = DateTimeDS(start.get() + 5); //Add 5 mins in seconds to start time
delay(3000);
digitalWrite(5,LOW);
}
int counter = 0;
void loop()
{
DateTime now = rtc.now();
delay(100);
DHT44b.read44(DHT44b_PIN);
delay(100);
DHT44a.read44(DHT44a_PIN);
delay (100);
Serial.print("Sample: ");
Serial.println(counter);
Serial.print("Date: ");
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.println(now.year(), DEC);
Serial.print("Hour: ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
Serial.print("LUX index: ");
Serial.println(TSL2561.readVisibleLux());
Serial.print("Ambient temperature: ");
Serial.print(DHT44b.temperature, 1);
Serial.println(" *C ");
Serial.print("Relative humidity: ");
Serial.print(DHT44b.humidity, 1);
Serial.println(" %");
Serial.print("Soil temperature: ");
Serial.print(DHT44a.temperature, 1);
Serial.println(" *C ");
Serial.print("Soil humidity: ");
Serial.print(DHT44a.humidity, 1);
Serial.println(" %");
Serial.println(" ");
delay (100);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{
dataFile.print(" Sammple: ");
dataFile.print(counter);
dataFile.print(",");
dataFile.print(" Date: ");
dataFile.print(now.day(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.year(), DEC);
dataFile.print(",");
dataFile.print(" Hour: ");
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print(",");
dataFile.print(" LUX index: ");
dataFile.print(TSL2561.readVisibleLux());
dataFile.print(",");
dataFile.print(" Ambient temperature: ");
dataFile.print(DHT44b.temperature, 1);
dataFile.print(" *C ");
dataFile.print(",");
dataFile.print(" Relative humidity: ");
dataFile.print(DHT44b.humidity, 1);
dataFile.print(" %");
dataFile.print(",");
dataFile.print(" Soil temperature: ");
dataFile.print(DHT44a.temperature, 1);
dataFile.print(" *C ");
dataFile.print(",");
dataFile.print(" Soil humidity: ");
dataFile.print(DHT44a.humidity, 1);
dataFile.print(" %");
dataFile.println("");
dataFile.close();
}
else
{
Serial.println("");
Serial.println("UNABLE TO LOG DATA");
Serial.println("");
}
counter++;
/////////////////////////////////////////////////////////////////////////////////////////////////////////SLEEP MODE START
/* DateTimeDS nowDS = RTC.now(); //get the current date-time
if((now.second()) != prevSecond )
{
//print only when there is a change
Serial.print(nowDS.year(), DEC);
Serial.print('/');
Serial.print(nowDS.month(), DEC);
Serial.print('/');
Serial.print(nowDS.date(), DEC);
Serial.print(' ');
Serial.print(nowDS.hour(), DEC);
Serial.print(':');
Serial.print(nowDS.minute(), DEC);
Serial.print(':');
Serial.print(nowDS.second(), DEC);
Serial.print(" ");
Serial.println();
}
prevSecond = nowDS.second();*/
RTC.clearINTStatus(); //This function call is a must to bring /INT pin HIGH after an interrupt.
RTC.enableInterrupts(interruptTime.hour(),interruptTime.minute(),interruptTime.second()); // set the interrupt at (h,m,s)
attachInterrupt(0, INT0_ISR, LOW); //Enable INT0 interrupt (as ISR disables interrupt). This strategy is required to handle LEVEL triggered interrupt
delay(1000);
cli();
sleep_enable(); // Set sleep enable bit
sleep_bod_disable(); // Disable brown out detection during sleep. Saves more power
sei();
power_all_disable(); //This shuts down ADC, TWI, SPI, Timers and USART
sleep_cpu(); // Sleep the CPU as per the mode set earlier(power down)
sleep_disable(); // Wakes up sleep and clears enable bit. Before this ISR would have executed
power_all_enable(); //This shuts enables ADC, TWI, SPI, Timers and USART
/////////////////////////////////////////////////////////////////////////////////////////////////////////SLEEP MODE END
delay (3000);
}
void INT0_ISR()
{
detachInterrupt(0);
unsigned char temp_delay = 10;
interruptTime = DateTimeDS(interruptTime.get() + 60); //decide the time for next interrupt, configure next interrupt
}
The problema is that, even when he has no problems running the firmware in the exactly same components, I just cannot compile it. The coding erros seem to be very simple, but I just cannot fix them. Any ideas? I attach the arduino libraries that are needed to run the sketch.
Thank you in advance,
J
LIBRERIAS ARDUINO.zip (111 KB)