Created
April 1, 2020 04:25
-
-
Save nazt/60e2cb5d00285be404d5cc8d9fe47197 to your computer and use it in GitHub Desktop.
sdcard-ds3201.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mySD.h> | |
#include <Wire.h> // must be included here so that Arduino library object file references work | |
#include <RtcDS3231.h> | |
RtcDS3231<TwoWire> Rtc(Wire); | |
#define countof(a) (sizeof(a) / sizeof(a[0])) | |
#define LED_BUILTIN 22 //Blink pin | |
File sdcard; | |
char datestring[20]; | |
char filenamestring[20]; | |
char timestring[20]; | |
float temperatureC; | |
bool rtcOk = false; | |
static const uint8_t MA_SDA = 21; | |
static const uint8_t MA_SCL = 23; | |
void RtcSetup () | |
{ | |
Serial.print("compiled: "); | |
Serial.print(__DATE__); | |
Serial.println(__TIME__); | |
// Wire | |
Rtc.Begin(MA_SDA, MA_SCL); | |
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); | |
Serial.println(); | |
if (!Rtc.IsDateTimeValid()) | |
{ | |
if (Rtc.LastError() != 0) | |
{ | |
// we have a communications error | |
// see https://www.arduino.cc/en/Reference/WireEndTransmission for | |
// what the number means | |
Serial.print("RTC communications error = "); | |
Serial.println(Rtc.LastError()); | |
} | |
else | |
{ | |
// Common Causes: | |
// 1) first time you ran and the device wasn't running yet | |
// 2) the battery on the device is low or even missing | |
Serial.println("RTC lost confidence in the DateTime!"); | |
Rtc.SetDateTime(compiled); | |
} | |
} | |
if (!Rtc.GetIsRunning()) | |
{ | |
Serial.println("RTC was not actively running, starting now"); | |
Rtc.SetIsRunning(true); | |
} | |
RtcDateTime now = Rtc.GetDateTime(); | |
if (now < compiled) | |
{ | |
Serial.println("RTC is older than compile time! (Updating DateTime)"); | |
Rtc.SetDateTime(compiled); | |
} | |
// never assume the Rtc was last configured by you, so | |
// just clear them to your needed state | |
Rtc.Enable32kHzPin(false); | |
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); | |
} | |
void sensorTask(void *parameter) | |
{ | |
int delay; | |
vTaskDelay(10 * 1000 / portTICK_PERIOD_MS); | |
while (1) | |
{ | |
sdcard = SD.open(filenamestring, FILE_WRITE); | |
digitalWrite(LED_BUILTIN, HIGH); | |
if (sdcard && rtcOk) { | |
sdcard.print(datestring); | |
sdcard.print(","); | |
sdcard.print(timestring); | |
sdcard.print(","); | |
sdcard.print(temperatureC); | |
sdcard.println(); | |
sdcard.flush(); | |
sdcard.close(); | |
Serial.println("Write done..."); | |
delay = 10; | |
} else { | |
delay = 1; | |
Serial.println("Write failed."); | |
} | |
vTaskDelay(20 / portTICK_PERIOD_MS); | |
digitalWrite(LED_BUILTIN, LOW); | |
vTaskDelay(delay * 1000 / portTICK_PERIOD_MS); | |
} | |
vTaskDelete( NULL ); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
pinMode(LED_BUILTIN, OUTPUT); | |
Serial.print("Initializing SD card..."); | |
RtcSetup(); | |
/* initialize SD library with SPI pins */ | |
if (!SD.begin(13, 15, 2, 14)) { //T1:13,15,2,14 T2: 23,5,19,18 M5:4,23,19,18 uint8_t csPin, int8_t mosi, int8_t miso, int8_t sck | |
Serial.println("initialization failed!"); | |
return; | |
} | |
Serial.println("initialization done."); | |
xTaskCreate(sensorTask, "sensorTask", 1024, NULL, 1, NULL); | |
} | |
void loop() | |
{ | |
if (Rtc.IsDateTimeValid()) { | |
RtcDateTime dt = Rtc.GetDateTime(); | |
RtcTemperature temp = Rtc.GetTemperature(); | |
temperatureC = temp.AsFloatDegC(); | |
snprintf_P(filenamestring, | |
countof(filenamestring), | |
PSTR("%02u%02u%02u.csv"), | |
dt.Year(), | |
dt.Month(), | |
dt.Day()); | |
snprintf_P(datestring, | |
countof(datestring), | |
PSTR("%02u/%02u/%04u"), | |
dt.Month(), | |
dt.Day(), | |
dt.Year()); | |
snprintf_P(timestring, | |
countof(timestring), | |
PSTR("%02u:%02u:%02u"), | |
dt.Hour(), | |
dt.Minute(), | |
dt.Second() ); | |
Serial.print(datestring); | |
Serial.print(" "); | |
Serial.println(timestring); | |
rtcOk = true; | |
} | |
else | |
{ | |
rtcOk = true; | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment