Hello everybody,
for an educational project in a geography class we want to create a couple of data loggers.
They should record time, air temperature, rel. air humidity and atmospheric pressure on a SD card (to make it easier for the students to read out the data). No WiFi will be available at the locations.
To be able to compare the data between the single loggers, the data should be logged at every exact minute - though every 5, 10 or 15 minutes would also be sufficient.
As micro controller the ESP8266 NodeMCU 12E (Arduino Nano or so could be an alternative?) was chosen, as sensor the BME280, as RTC the DS3231 and a standard SD card adapter.
The energy is currently supplied with 4x AAA batteries via micro USB.
Below, I have posted code which works fine so far... but also with modem sleep after abt. 25 hours the batteries need to be replaced / recharged. :-\
Maybe, somebody can help to make it last longer? ... 3-4 days recording time would already be enough.
Thank you very much in advance!! ![]()
/*
Wiring:BME280, DS3231 <> ESP8266 NodeMCU 12E
SDA <> D3
SCL <> D4
VIN,VCC <> 3V3
GND <> GND
SQW (just DS3231) <> D1SD Card Module <> ESP8266 NodeMCU 12E
CS <> D8
SCK <> D5
MOSI <> D7
MISO <> D6
VCC <> VIN
GND <> GNDPower Supply via microUSB
*/#include <Wire.h>
#include <RtcDS3231.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <ESP8266WiFi.h>
#include <SD.h>#define CS_PIN D8
Adafruit_BME280 bme;
const byte interruptPin = D1;
volatile bool alarm = 0;RtcDS3231 rtcObject(Wire);
void setup() {
Serial.begin(115200);
Wire.begin(D3,D4);
WiFi.forceSleepBegin();
bme.begin();
SD.begin(CS_PIN);pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);rtcObject.Begin();
RtcDateTime timestamp = RtcDateTime(DATE, TIME);
rtcObject.SetDateTime(timestamp);rtcObject.Enable32kHzPin(false);
rtcObject.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne);DS3231AlarmOne alarm1(
0,
0,
0,
0,
DS3231AlarmOneControl_SecondsMatch);rtcObject.SetAlarmOne(alarm1);
rtcObject.LatchAlarmsTriggeredFlags();
}void loop() {
if (alarm == true) {
handleAlarm();
}
}void handleAlarm() {
alarm = false;RtcDateTime timestamp = rtcObject.GetDateTime();
float t = bme.readTemperature();
float h = bme.readHumidity();
float p = bme.readPressure();char time[20];
sprintf(time, "%d/%d/%d %d:%d:%d",
timestamp.Day(),
timestamp.Month(),
timestamp.Year(),
timestamp.Hour(),
timestamp.Minute(),
timestamp.Second()
);File dataFile = SD.open("tthp.CSV", FILE_WRITE);
if (dataFile) {
Serial.println("Data logged.");
dataFile.print(time);
dataFile.print(",");
dataFile.print(t);
dataFile.print(",");
dataFile.print(h);
dataFile.print(",");
dataFile.println(p);dataFile.close();
}rtcObject.LatchAlarmsTriggeredFlags();
}void handleInterrupt() {
alarm = true;
}
