#include <Wire.h>
#include "RTClib.h"
#include "mydata.h"
RTC_DS3231 _rtc;
// Control Variables
#define CLPERSEC 5
#define BASEYEAR 2022
#define TESTING 1
// Variables
MyData *_myData;
void setup ()
{
// Start the communications
Wire.begin();
Serial.begin(9600);
delay(3000); // wait for console opening
// Check the status of the RTC
if (! _rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (_rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Following line sets the RTC with an explicit date & time
// for example to set January 27 2017 at 12:56 you would call:
// rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
}
// Create the structure
_myData = new MyData();
// Set the initial data [TESTING ONLY]
if (TESTING) setInitialData();
// Print the structure
_myData->printData();
return;
}
void loop ()
{
// Refresh the current data
_myData->refreshData();
// Get the hours from base
DateTime now = _rtc.now();
uint16_t hrsFromBase = calcHoursSinceBase(now);
// Check to see if any action needed (presumably so)
int hrsLastDone = hrsFromBase - _myData->getLastTime();
if (hrsLastDone > 0) { // Now we need to process
_myData->processWater(hrsLastDone);
// Update the last time since we did this
writeLastTime(hrsFromBase);
}
// IF testing, we will forward the clock by a few minutes
if (TESTING) {
now = _rtc.now();
DateTime newTime (now+TimeSpan(0,0,60-now.minute()-1,0));
_rtc.adjust(newTime);
Serial.print("Adjusted Mins to "); Serial.println(newTime.minute());
}
// Calculate the amount of time we should be sleeping for
delay(calculateNextCheckTime());
}
void writeLastTime(uint16_t hrs) {
writeByte(RTCADDR, LASTTIME, (uint8_t)(hrs >> 8));
writeByte(RTCADDR, LASTTIME+1, (uint8_t)(hrs & 0xFF));
}
void setInitialData() {
// Set the values
_myData->setControl(CONTROLON);
_myData->setLastTime(6440);
_myData->setValveInfo(0, 5, 1, 5);
_myData->setValveInfo(1, 4, 2, 10);
_myData->setValveInfo(2, 3, 3, 15);
_myData->setValveInfo(3, 2, 4, 20);
_myData->setValveInfo(4, 0, 0, 0);
_myData->setValveInfo(5, 0, 0, 0);
_myData->setValveInfo(6, 1, 5, 25);
_myData->setValveInfo(7, 0, 0, 0);
// Now write the data to the eeprom
byte data[BUFSIZE];
Serial.println("Printing the set data");
_myData->printData();
_myData->getStream(data);
writePage(RTCADDR, STARTADDR, data, BUFSIZE);
delay(500);
}
void printDataStream(byte* data) {
for(int i = 0; i < BUFSIZE; i++) {
Serial.print(*(data+i));
Serial.print(" ");
}
Serial.println();
}
uint16_t calcHoursSinceBase(DateTime now) {
// Get the date time of the base
DateTime then(BASEYEAR, 01, 01, 00, 00, 00);
TimeSpan span = now-then;
//Serial.print(span.days()); Serial.print(" "); Serial.println(span.hours());
return (span.days() * 24) + span.hours();
}
int calculateNextCheckTime() {
DateTime now = _rtc.now();
int mins = 60 - now.minute();
Serial.print("Checking in minutes "); Serial.println(mins);
return ((mins*60) + 30) * 1000;
}
Loading
esp-01
esp-01