Skip to content

Commit

Permalink
Use SDK NTP and better RTC lib
Browse files Browse the repository at this point in the history
Signed-off-by: Erwan Queffélec <[email protected]>
  • Loading branch information
3rwww1 committed Jun 20, 2018
1 parent 46eefdf commit bfb21e8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 389 deletions.
59 changes: 30 additions & 29 deletions src/core/CoolBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,36 @@ void CoolBoard::begin() {
pinMode(BOOTSTRAP_PIN, INPUT);
digitalWrite(ENABLE_I2C_PIN, HIGH);
delay(100);

DEBUG_LOG("Start RTC configuration...");
this->coolTime.config();
this->coolTime.offGrid();
delay(100);

this->coolBoardSensors.config();
this->coolBoardSensors.begin();
delay(100);

this->coolBoardActuator.config();
this->coolBoardActuator.begin();
delay(100);
this->printConf();
this->coolBoardLed.printConf();
this->coolBoardSensors.printConf();
this->coolBoardActuator.printConf();
this->coolTime.printConf();

if (this->jetpackActive) {
this->jetPack.config();
this->jetPack.begin();
this->jetPack.printConf();
delay(100);
}

if (this->ireneActive) {
this->irene3000.config();
this->irene3000.begin();
this->irene3000.calibrate(this->coolBoardLed);
this->irene3000.printConf();
delay(100);
}

if (this->externalSensorsActive) {
this->externalSensors->config();
this->externalSensors->begin();
delay(100);
}
this->coolTime.begin();
delay(100);
SPIFFS.end();
}
Expand Down Expand Up @@ -166,6 +156,7 @@ int CoolBoard::connect() {
}
delay(100);
if (this->coolWifi->state() == WL_CONNECTED) {
this->coolTime.begin();
delay(100);
this->coolBoardLed.blink(GREEN, 5);
if (this->mqttConnect() != 0) {
Expand Down Expand Up @@ -198,16 +189,15 @@ void CoolBoard::sendSavedMessages() {

void CoolBoard::handleActuators(JsonObject &reported) {
if (this->manual == 0) {
Date date = this->coolTime.rtc.getDate();

INFO_LOG("Actuators configuration: automatic");
tmElements_t tm;
tm = this->coolTime.getTimeDate();
if (this->jetpackActive) {
DEBUG_LOG("Collecting Jetpack actuators data...");
this->jetPack.doAction(reported, int(tm.Hour), int(tm.Minute));
DEBUG_LOG("Updating and recording Jetpack state...");
this->jetPack.doAction(reported, date.getHour(), date.getMinutes());
}
DEBUG_LOG("Collecting onboard actuator data...");
if (this->coolBoardActuator.doAction(reported, int(tm.Hour),
int(tm.Minute))) {
DEBUG_LOG("Updating and recording onboard actuator state...");
if (this->coolBoardActuator.doAction(reported, date.getHour(), date.getMinutes())) {
this->coolBoardActuator.write(1);
reported["ActB"] = 1;
} else {
Expand Down Expand Up @@ -375,7 +365,7 @@ void CoolBoard::readSensors(JsonObject &reported) {
}

void CoolBoard::readBoardData(JsonObject &reported) {
reported["timestamp"] = this->coolTime.getESDate();
reported["timestamp"] = this->coolTime.getIso8601DateTime();
reported["mac"] = this->mqttId;
reported["firmwareVersion"] = COOL_FW_VERSION;
if (this->isConnected()) {
Expand Down Expand Up @@ -450,10 +440,15 @@ void CoolBoard::startAP() {
}

void CoolBoard::mqttProblem() {
this->coolBoardLed.blink(RED, 0.2);
delay(200);
this->coolBoardLed.blink(RED, 0.2);
delay(200);
for (int i = 0; i < 10; i++) {
this->coolBoardLed.blink(ORANGE, 0.2);
}
}

void CoolBoard::rtcProblem() {
for (int i = 0; i < 10; i++) {
this->coolBoardLed.blink(RED, 0.2);
}
}

void CoolBoard::spiffsProblem() {
Expand Down Expand Up @@ -524,16 +519,22 @@ int CoolBoard::mqttConnect() {
}

void CoolBoard::mqttLog(String data) {
DEBUG_VAR("Message to publish:", data);
DEBUG_VAR("Message size:", data.length());
bool messageSent = false;

DEBUG_VAR("Message to log:", data);
DEBUG_VAR("Message size:", data.length());
if (this->isConnected()) {
messageSent = this->mqttPublish(data);
}
if (!this->isConnected() || !messageSent) {
ERROR_LOG("MQTT publish failed, data saved on SPIFFS");
CoolFileSystem::saveLogToFile(data.c_str());
this->mqttProblem();
if (!messageSent) {
if (this->coolTime.rtc.hasStopped()) {
ERROR_LOG("RTC has stopped and network failed, log discarded");
this->rtcProblem();
} else {
CoolFileSystem::saveLogToFile(data.c_str());
WARN_LOG("Log not sent, saved on SPIFFS");
this->mqttProblem();
}
} else {
INFO_LOG("MQTT publish successful");
this->messageSent();
Expand Down Expand Up @@ -638,7 +639,7 @@ void CoolBoard::tryFirmwareUpdate() {

void CoolBoard::updateFirmware(String firmwareVersion, String firmwareUrl,
String firmwareUrlFingerprint) {
this->coolBoardLed.write(BROWN);
this->coolBoardLed.write(ORANGE);
delete this->coolPubSubClient;
delete this->externalSensors;
delete this->wifiClientSecure;
Expand Down
1 change: 1 addition & 0 deletions src/core/CoolBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class CoolBoard {
void sendConfig(const char *moduleName, const char *filePath);
void readPublicIP(JsonObject &reported);
void startAP();
void rtcProblem();
void mqttProblem();
void spiffsProblem();
void messageSent();
Expand Down
2 changes: 1 addition & 1 deletion src/core/CoolBoardLed.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#define YELLOW 30, 30, 0
#define BLUE 0, 0, 50
#define FUCHSIA 30, 0, 30
#define BROWN 50, 25, 0
#define ORANGE 50, 25, 0

class CoolBoardLed {

Expand Down
Loading

0 comments on commit bfb21e8

Please sign in to comment.