I'm trying to write soil moisture % to a micro Sd card with the date and time. I have run the following code for my RTC, which works. But when I load my program code, it is writing UTC time instead of EST.
Hardware used: Mega 2560, DS3231 AT24C32 IIC RTC, Micro SD Storage Board, 1.5inch RGB OLED display module, 128x128 pixels SSD1351, and Capacitive Soil Moisture Sensor
// Jack Christensen Aug 2012
#include <Wire.h>
#include <DS1307RTC.h> // https://github.com/PaulStoffregen/DS1307RTC
#include <Timezone.h> // https://github.com/JChristensen/Timezone
// US Eastern Time Zone (New York, Detroit)
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; //Standard time = UTC - 5 hours
Timezone myTZ(myDST, mySTD);
// If TimeChangeRules are already stored in EEPROM, comment out the three
// lines above and uncomment the line below.
//Timezone myTZ(100); //assumes rules stored at EEPROM address 100
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
void setup()
{
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
time_t utc = now();
time_t local = myTZ.toLocal(utc, &tcr);
Serial.println();
printDateTime(utc, "UTC");
printDateTime(local, tcr -> abbrev);
delay(10000);
}
// format and print a time_t value, with a time zone appended.
void printDateTime(time_t t, const char *tz)
{
char buf[32];
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
Serial.println(buf);
}
My program code:
#include <DS3231.h>
RTClib myRTC;
#include <Timezone.h>
// SD - Version: 1.2.4
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SPI.h>
#include <Wire.h>
// Screen dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 128 // Change this to 96 for 1.27" OLED.
// You can use any (4 or) 5 pins
#define SCLK_PIN 29
#define MOSI_PIN 31
#define DC_PIN 25
#define CS_PIN 27
#define RST_PIN 23
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Option 1: use any pins but a little slower
Adafruit_SSD1351 display = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, CS_PIN, DC_PIN, MOSI_PIN, SCLK_PIN, RST_PIN);
// Option 2: must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
//Adafruit_SSD1351 tft = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, CS_PIN, DC_PIN, RST_PIN);
float p = 3.1415926;
const int AirValue = 575; //you need to replace this value with Value_1
const int WaterValue = 290; //you need to replace this value with Value_2
int soilMoistureValue1 = 0;
int soilmoisturepercent1=0;
int soilMoistureValue2 = 0;
int soilmoisturepercent2=0;
File myFile;
void setup() {
Wire.begin();
Serial.begin(9600);
display.begin();
Serial.println("init");
uint16_t time = millis();
display.fillRect(0, 0, 128, 128, BLACK);
time = millis() - time;
Serial.println(time, DEC);
delay(500);
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int moist1;
int moist2;
Serial.print("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
void loop() {
soilMoistureValue1 = analogRead(A0); //put Sensor insert into soil
Serial.println(soilMoistureValue1);
soilmoisturepercent1 = map(soilMoistureValue1, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent1 >= 100)
{
Serial.println("100");
display.setCursor(10,0); //oled display
display.setTextSize(2);
display.setTextColor(RED);
display.println("1");
display.setCursor(2,18); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.println("100");
delay(1);
//display.fillScreen(BLACK);
}
else if(soilmoisturepercent1 <=0)
{
Serial.println("0");
display.setCursor(10,0); //oled display
display.setTextSize(2);
display.setTextColor(RED);
display.println("1");
display.setCursor(2,18); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.println("0");
delay(1);
//display.fillScreen(BLACK);
}
else if(soilmoisturepercent1 >0 && soilmoisturepercent1 < 100)
{
Serial.print(soilmoisturepercent1);
Serial.println("%");
display.setCursor(10,0); //oled display
display.setTextSize(2);
display.setTextColor(RED);
display.println("1");
display.setCursor(2,18); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.println(soilmoisturepercent1);
delay(1);
//display.fillScreen(BLACK);
}
soilMoistureValue2 = analogRead(A1); //put Sensor insert into soil
Serial.println(soilMoistureValue2);
soilmoisturepercent2 = map(soilMoistureValue2, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent2 >= 100)
{
Serial.println("100");
display.setCursor(35,0); //oled display
display.setTextSize(2);
display.setTextColor(RED);
display.println("2");
display.setCursor(30,18); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.println("100");
delay(1);
//display.fillScreen(BLACK);
}
else if(soilmoisturepercent2 <=0)
{
Serial.println("0");
display.setCursor(35,0); //oled display
display.setTextSize(2);
display.setTextColor(RED);
display.println("2");
display.setCursor(30,18); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.println("0");
delay(1);
//display.fillScreen(BLACK);
}
else if(soilmoisturepercent2 >0 && soilmoisturepercent2 < 100)
{
Serial.print(soilmoisturepercent2);
Serial.println("%");
display.setCursor(35,0); //oled display
display.setTextSize(2);
display.setTextColor(RED);
display.println("2");
display.setCursor(30,18); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.println(soilmoisturepercent2);
//delay(5000);
//display.fillScreen(BLACK);
}
//DateTime now;
myFile = SD.open("datalog.txt", FILE_WRITE);
DateTime now = myRTC.now();
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print('/');
myFile.print(now.year(), DEC);
myFile.print(' ');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(' ');
myFile.print(soilmoisturepercent1);
myFile.print(' ');
myFile.print(soilmoisturepercent2);
myFile.println();
myFile.close();
delay(5000);
display.fillScreen(BLACK);
}
I would also like it to write the time in 12 hour format, but not required and for the display to update every 5 minutes but write to the file every 30 minutes, again, not required