Skip to content

Commit

Permalink
- Firmware: refactored clock handling into a polymorphic hyerarchy fo…
Browse files Browse the repository at this point in the history
…r hardware RTC and software timekeeping

- Firmware: this fixes the RTC adjust time bug
  • Loading branch information
jguillaumes committed Sep 25, 2018
1 parent cbeda00 commit 048a205
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 40 deletions.
65 changes: 26 additions & 39 deletions Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#define DEBUG 1

#include "RTClib.h"
#include "Time.h"
#include "MeteoClockRTC.h"
#include "MeteoClockSoft.h"
#include "ReadTemperature.h"
#include "ReadHumidity.h"
#include "ReadBarometer.h"
Expand All @@ -16,9 +16,11 @@ const int pollDelay=5000;
const int pollDelay=15000;
#endif

#define FWVERSION "02.00.01"
#define FWVERSION "02.01.00"

MeteoClockRTC rtcClock;
MeteoClockSoft softClock;

RTC_DS3231 rtc;
HC05Module bt(2);

bool firstConnection = true;
Expand All @@ -28,14 +30,21 @@ enum deviceEnum { DE_CLOCK=0, DE_THERMOMETER=1, DE_HIGROMETER=2,
char devList[] = " ";

void checkClock() {
rtc.begin();
if (rtc.now().year() > 2100) {
bool clockNow = rtcPresent;
if (!rtcClock.checkClock()) {
rtcPresent = false;
devList[DE_CLOCK] = '-';
} else {
rtcPresent = true;
devList[DE_CLOCK] = 'C';
}
if (clockNow != rtcPresent) {
if (rtcPresent) {
Serial.println("HARDW: Clock detected");
} else {
Serial.println("HARDW: Clock lost!! - Using software timekeeping");
}
}
}

void setup() {
Expand Down Expand Up @@ -80,12 +89,9 @@ void setup() {
devList[DE_LIGHT] = 'L';

checkClock();
if (!rtcPresent) {
Serial.println("ERROR: RTC not present");
} else {
DateTime ara = rtc.now();
setTime(ara.hour(), ara.minute(), ara.second(),
ara.day(), ara.month(), ara.year());
if (rtcPresent) {
String t = rtcClock.getClock();
softClock.setClock(t);
}

sprintf(msg, "INFO : F:%s", FWVERSION);
Expand All @@ -97,24 +103,12 @@ void processCommand(String cmd) {

String theCmd = cmd.substring(0,5);
if (theCmd.equals("TIME ")) {
String t = cmd.substring(5,17);
checkClock();
if (rtcPresent) {
DateTime dt(atoi(cmd.substring(5,9).c_str()),
atoi(cmd.substring(9,11).c_str()),
atoi(cmd.substring(11,13).c_str()),
atoi(cmd.substring(13,15).c_str()),
atoi(cmd.substring(15,17).c_str()),
atoi(cmd.substring(17,19).c_str()));
rtc.adjust(dt);
} else {
setTime(atoi(cmd.substring(13,15).c_str()),
atoi(cmd.substring(15,17).c_str()),
atoi(cmd.substring(17,19).c_str()),
atoi(cmd.substring(11,13).c_str()),
atoi(cmd.substring(9,11).c_str()),
atoi(cmd.substring(5,9).c_str()));
checkClock();
if (rtcPresent) Serial.println("HARDW: Clock detected");
rtcClock.setClock(t);
}
softClock.setClock(t);
cmdOk = true;
} else if (theCmd.equals("BYE ")) {
Serial.println("OK-BYE");
Expand Down Expand Up @@ -144,29 +138,22 @@ void processCommand(String cmd) {
// BYE

void loop() {
DateTime now;
char timbuf[17];
String theTime;
char line[80];
float temp, press, humdt, light;

// If we have an RTC, check it's still working. If not, switch to MPU soft clock
if (rtcPresent) {
checkClock();
if (rtcPresent) {
now = rtc.now();
sprintf(timbuf, "%04d%02d%02d%02d%02d%02d", now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
} else {
Serial.println("HARDW: Clock lost!! - Using software timer");
theTime = rtcClock.getClock();
}
}

// If we've got no RTC, use software clock and check if it has came back
if (!rtcPresent) {
sprintf(timbuf, "%04d%02d%02d%02d%02d%02d", year(), month(), day(),
hour(), minute(), second());
theTime = softClock.getClock();
checkClock();
if (rtcPresent) Serial.println("HARDW: Clock detected");
}
char stemp[10], shumt[10], spres[10], slght[10];

Expand Down Expand Up @@ -220,7 +207,7 @@ void loop() {
dtostrf(humdt, 5, 2, shumt);
dtostrf(light, 6, 2, slght);

sprintf(line, "DATA :C%s:F%s:T%s:H%s:P%s:L%s:D%s", timbuf, FWVERSION, stemp,
sprintf(line, "DATA :C%s:F%s:T%s:H%s:P%s:L%s:D%s", theTime.c_str(), FWVERSION, stemp,
shumt, spres, slght, devList);

if (bt.checkConnection()) {
Expand Down
29 changes: 29 additions & 0 deletions MeteoClock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* MeteoClock.h
*
* Abstract class to represent clock devices.
* It defines the basic operations of clocks. There will be
* implementations based on RTC (DS3231) and software timing.
*
* Created on: Sep 25, 2018
* Author: jguillaumes
*
*/

#ifndef METEOCLOCK_H_
#define METEOCLOCK_H_

#include <Arduino.h>


class MeteoClock {
public:
virtual ~MeteoClock() {}
virtual void setClock(String &timestamp) = 0;
virtual void setClock(int year, int month, int day,
int hour, int minute, int second) = 0;

virtual String getClock() = 0;
};

#endif /* METEOCLOCK_H_ */
88 changes: 88 additions & 0 deletions MeteoClockRTC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* MeteoClockRTC.cpp
*
* Created on: Sep 25, 2018
* Author: jguillaumes
*/

#include <Arduino.h>
#include <RTCLib.h>

#include "MeteoClockRTC.h"


MeteoClockRTC::~MeteoClockRTC() {
RTC_DS3231 *rtc = (RTC_DS3231 *) _theRTC;
delete rtc;
_theRTC = NULL;
}

//+
// Constructor: creates a new RTC_DS3231 instance and stores a pointer
// to it as a private variable.
// The variable is stored as a void pointer so the header file does not
// have to #include the RTClib.h. If it did there would be name conflicts
// with the software clock
//-
MeteoClockRTC::MeteoClockRTC() {
RTC_DS3231 *rtc = new RTC_DS3231;
this->_theRTC = (void *) rtc;
}

//+
// Set (adjust) the clock from a YYYMMDDhhmmss string
// There is no formal checking done. If the string is not correct the
// results are undefined.
//-
void MeteoClockRTC::setClock(String &timestamp) {
uint16_t year = atoi(timestamp.substring(0,4).c_str());
uint8_t month = atoi(timestamp.substring(4,6).c_str());
uint8_t day = atoi(timestamp.substring(6,8).c_str());
uint8_t hour = atoi(timestamp.substring(8,10).c_str());
uint8_t minute = atoi(timestamp.substring(10,12).c_str());
uint8_t second = atoi(timestamp.substring(12,14).c_str());

this->setClock(year, month, day, hour, minute, second);
}

//+
// Set (adjust) the clock using separate parameters for each
// date and time component.
// There is no formal checking done. If the combo is not correct the
// results are undefined.
//-
void MeteoClockRTC::setClock(int year, int month, int day,
int hour, int minute, int second){
DateTime dt(year, month, day, hour, minute, second);
((RTC_DS3231 *) this->_theRTC)->adjust(dt);
}

//+
// Get the current date and time as a YYYYMMDDhhmmss string
//-
String MeteoClockRTC::getClock() {
DateTime now;
char timbuf[17];

now = ((RTC_DS3231 *) this->_theRTC)->now();
sprintf(timbuf, "%04d%02d%02d%02d%02d%02d", now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
return String(timbuf);
}

//+
// Verify if the clock is physically present on the 2WI bus
// responds with consistent results.
//-
bool MeteoClockRTC::checkClock() {
bool rtcPresent;
RTC_DS3231 *rtc = (RTC_DS3231 *) _theRTC;

rtc->begin();
if (rtc->now().year() > 2100) {
rtcPresent = false;
} else {
rtcPresent = true;
}
return rtcPresent;
}
34 changes: 34 additions & 0 deletions MeteoClockRTC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* MeteoClockRTC.h
*
* Clock handling based on an DS3231 RTC chip.
*
* Created on: Sep 25, 2018
* Author: jguillaumes
*/

#ifndef METEOCLOCKRTC_H_
#define METEOCLOCKRTC_H_

#include "Arduino.h"
#include "MeteoClock.h"

class MeteoClockRTC: public MeteoClock {
public:
virtual ~MeteoClockRTC();
MeteoClockRTC();

virtual void setClock(String &timestamp) ;
virtual void setClock(int year, int month, int day,
int hour, int minute, int second);
virtual String getClock();

bool checkClock();

private:
void *_theRTC; // Pointer to a RTC_DS3231 object, stored as void*
// to avoid having to include the RTClib header

};

#endif /* METEOCLOCKRTC_H_ */
64 changes: 64 additions & 0 deletions MeteoClockSoft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* MeteoClockSoft.cpp
*
* Created on: Sep 25, 2018
* Author: jguillaumes
*/

#include <Arduino.h>
#include <Time.h>

#include "MeteoClockSoft.h"

//+
// Empty constructor
//-
MeteoClockSoft::MeteoClockSoft() {
}

//+
// Empty destructor
//-
MeteoClockSoft::~MeteoClockSoft() {
}

//+
// Set (adjust) the time from a YYYMMDDhhmmss string
// There is no formal checking done. If the string is not correct the
// results are undefined.
//-
void MeteoClockSoft::setClock(String &timestamp) {
uint16_t year = atoi(timestamp.substring(0,4).c_str());
uint8_t month = atoi(timestamp.substring(4,6).c_str());
uint8_t day = atoi(timestamp.substring(6,8).c_str());
uint8_t hour = atoi(timestamp.substring(8,10).c_str());
uint8_t minute = atoi(timestamp.substring(10,12).c_str());
uint8_t second = atoi(timestamp.substring(12,14).c_str());

this->setClock(year, month, day, hour, minute, second);
}

//+
// Set (adjust) the time using separate parameters for each
// date and time component.
// There is no formal checking done. If the combo is not correct the
// results are undefined.
//-

void MeteoClockSoft::setClock(int year, int month, int day,
int hour, int minute, int second) {
setTime(hour, minute, second, day, month, year);
}

//+
// Get the current date and time as a YYYYMMDDhhmmss string
//-
String MeteoClockSoft::getClock() {
char timbuf[17];

sprintf(timbuf, "%04d%02d%02d%02d%02d%02d", year(), month(), day(),
hour(), minute(), second());


return String(timbuf);
}
27 changes: 27 additions & 0 deletions MeteoClockSoft.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* MeteoClockSoft.h
*
* Clock handling based on software timekeeping
*
* Created on: Sep 25, 2018
* Author: jguillaumes
*/

#ifndef METEOCLOCKSOFT_H_
#define METEOCLOCKSOFT_H_

#include "MeteoClock.h"

class MeteoClockSoft: public MeteoClock {
public:
MeteoClockSoft();
virtual ~MeteoClockSoft();

virtual void setClock(String &timestamp) ;
virtual void setClock(int year, int month, int day,
int hour, int minute, int second) ;
virtual String getClock();

};

#endif /* METEOCLOCKSOFT_H_ */
2 changes: 1 addition & 1 deletion ReadTemperature.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _READ_TEMPERATURE_H
#define _READ_TEMPERATURE_H

#define DEBUG 1
// #define DEBUG 1

#include <OneWire.h>

Expand Down

0 comments on commit 048a205

Please sign in to comment.