Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/Devices/PushingBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Devices/Serial_TC.h"
#include "Devices/TempProbe_TC.h"
#include "TC_util.h"
#include "TankControllerLib.h"

// class variables
PushingBox* PushingBox::_instance = nullptr;
Expand Down Expand Up @@ -61,25 +62,34 @@ void PushingBox::sendData() {
serial("Set Tank ID in order to send data to PushingBox");
return;
}
char format[] =
"GET /pushingbox?devid=%s&tankid=%i&tempData=%.2f&pHdata=%.3f HTTP/1.1\r\n"
"Host: api.pushingbox.com\r\n"
"Connection: close\r\n"
"\r\n";
char buffer[200];
// look up tankid, temperature, ph
float temperature = TempProbe_TC::instance()->getRunningAverage();
float pH = PHProbe::instance()->getPh();
snprintf(buffer, sizeof(buffer), format, DevID, tankID, temperature, pH);
size_t i = 0;
for (; i < sizeof(buffer); ++i) {
if (buffer[i] == '\r') {
buffer[i] = '\0';
break;
if (TankControllerLib::instance()->isInCalibration()) {
char format[] =
"GET /pushingbox?devid=%s&tankid=%i&tempData=C&pHdata=C HTTP/1.1\r\n"
"Host: api.pushingbox.com\r\n"
"Connection: close\r\n"
"\r\n";
snprintf(buffer, sizeof(buffer), format, DevID, tankID);
} else {
char format[] =
"GET /pushingbox?devid=%s&tankid=%i&tempData=%.2f&pHdata=%.3f HTTP/1.1\r\n"
"Host: api.pushingbox.com\r\n"
"Connection: close\r\n"
"\r\n";
// look up tankid, temperature, ph
float temperature = TempProbe_TC::instance()->getRunningAverage();
float pH = PHProbe::instance()->getPh();
snprintf(buffer, sizeof(buffer), format, DevID, tankID, temperature, pH);
size_t i = 0;
for (; i < sizeof(buffer); ++i) {
if (buffer[i] == '\r') {
buffer[i] = '\0';
break;
}
}
serial(buffer);
buffer[i] = '\r';
}
serial(buffer);
buffer[i] = '\r';
serial("attempting to connect to PushingBox...");
if (client.connected() || client.connect(server, 80)) {
serial("connected");
Expand Down
42 changes: 25 additions & 17 deletions src/TankControllerLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,34 @@ const char *TankControllerLib::version() {
*/
void TankControllerLib::writeDataToSD() {
static uint32_t nextWriteTime = 0;
static const char header[] = "time,tankid,temp,temp setpoint,pH,pH setpoint,onTime,Kp,Ki,Kd";
static const char format[] =
"%02i/%02i/%4i %02i:%02i:%02i, %3i, %4.2f, %4.2f, %5.3f, %5.3f, %4i, %8.1f, %8.1f, %8.1f";
uint32_t msNow = millis();
COUT("nextWriteTime: " << nextWriteTime << "; now = " << msNow);
if (nextWriteTime <= msNow) {
char buffer[128];
DateTime_TC dtNow = DateTime_TC::now();
PID_TC *pPID = PID_TC::instance();
uint16_t tankId = EEPROM_TC::instance()->getTankID();
snprintf(buffer, sizeof(buffer), format, (uint16_t)dtNow.month(), (uint16_t)dtNow.day(), (uint16_t)dtNow.year(),
(uint16_t)dtNow.hour(), (uint16_t)dtNow.minute(), (uint16_t)dtNow.second(), (uint16_t)tankId,
(float)TempProbe_TC::instance()->getRunningAverage(),
(float)TemperatureControl::instance()->getTargetTemperature(), (float)PHProbe::instance()->getPh(),
(float)PHControl::instance()->getTargetPh(), (uint16_t)(millis() / 1000), (float)pPID->getKp(),
(float)pPID->getKi(), (float)pPID->getKd());
SD_TC::instance()->appendData(header, buffer);
nextWriteTime = msNow / 1000 * 1000 + 1000; // round up to next second
COUT(buffer);
if (nextWriteTime > msNow) {
return;
}
char currentTemp[10];
char currentPh[10];
if (isInCalibration()) {
snprintf(currentTemp, sizeof(currentTemp), "C");
snprintf(currentPh, sizeof(currentPh), "C");
} else {
snprintf(currentTemp, sizeof(currentTemp), "%4.2f", (float)TempProbe_TC::instance()->getRunningAverage());
snprintf(currentPh, sizeof(currentPh), "%5.3f", (float)PHProbe::instance()->getPh());
}
static const char header[] = "time,tankid,temp,temp setpoint,pH,pH setpoint,onTime,Kp,Ki,Kd";
static const char format[] = "%02i/%02i/%4i %02i:%02i:%02i, %3i, %s, %4.2f, %s, %5.3f, %4i, %8.1f, %8.1f, %8.1f";
char buffer[128];
DateTime_TC dtNow = DateTime_TC::now();
PID_TC *pPID = PID_TC::instance();
uint16_t tankId = EEPROM_TC::instance()->getTankID();
snprintf(buffer, sizeof(buffer), format, (uint16_t)dtNow.month(), (uint16_t)dtNow.day(), (uint16_t)dtNow.year(),
(uint16_t)dtNow.hour(), (uint16_t)dtNow.minute(), (uint16_t)dtNow.second(), (uint16_t)tankId, currentTemp,
(float)TemperatureControl::instance()->getTargetTemperature(), currentPh,
(float)PHControl::instance()->getTargetPh(), (uint16_t)(millis() / 1000), (float)pPID->getKp(),
(float)pPID->getKi(), (float)pPID->getKd());
SD_TC::instance()->appendData(header, buffer);
nextWriteTime = msNow / 1000 * 1000 + 1000; // round up to next second
COUT(buffer);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/UIState/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ MainMenu::MainMenu(TankControllerLib *tc) : UIState(tc) {
void MainMenu::handleKey(char key) {
switch (key) {
case 'A': // Set pH set_point
this->setNextState((UIState *)new SetPHSetPoint(tc));
this->setNextState(static_cast<UIState *>(new SetPHSetPoint(tc)));
break;
case 'B': // Set Temperature set_point
this->setNextState((UIState *)new SetTempSetPoint(tc));
this->setNextState(static_cast<UIState *>(new SetTempSetPoint(tc)));
break;
case 'D': // Reset
level1 = 0;
Expand Down
File renamed without changes.
29 changes: 28 additions & 1 deletion test/PushingBoxTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Devices/TempProbe_TC.h"
#include "Devices/TemperatureControl.h"
#include "TankControllerLib.h"
#include "UIState/PHCalibrationMid.h"

unittest_setup() {
TankControllerLib::instance("PushingBoxIdentifier");
Expand Down Expand Up @@ -56,7 +57,6 @@ unittest(SendData) {
// set pH
state->serialPort[1].dataIn = "7.125\r"; // the queue of data waiting to be read
pTC->serialEvent1(); // fake interrupt

EthernetClient::startMockServer(pPushingBox->getServer(), 80);
assertEqual(0, pPushingBox->getClient()->writeBuffer().size());
const uint8_t response[] = "[PushingBox response]\r\n";
Expand Down Expand Up @@ -89,4 +89,31 @@ unittest(SendData) {
EthernetClient::stopMockServer(pPushingBox->getServer(), 80);
}

unittest(inCalibration) {
PushingBox *pPushingBox = PushingBox::instance();
pPushingBox->getClient()->stop(); // clears the writeBuffer and readBuffer
TankControllerLib *pTC = TankControllerLib::instance();
TempProbe_TC *tempProbe = TempProbe_TC::instance();

// set tank id
EEPROM_TC::instance()->setTankID(99);
PHCalibrationMid *test = new PHCalibrationMid(pTC);
pTC->setNextState(test, true);
assertTrue(pTC->isInCalibration());
EthernetClient::startMockServer(pPushingBox->getServer(), 80);
assertEqual(0, pPushingBox->getClient()->writeBuffer().size());
delay(60 * 20 * 1000); // wait for 20 minutes to ensure we send again
pTC->loop();
deque<uint8_t> buffer = pPushingBox->getClient()->writeBuffer();
String bufferResult;
for (int i = 0; i < buffer.size(); i++) {
bufferResult += buffer[i];
}
char expected1[] =
"GET /pushingbox?devid=PushingBoxIdentifier&tankid=99&tempData=C&pHdata=C HTTP/1.1\r\n"
"Host: api.pushingbox.com\r\n"
"Connection: close\r\n"
"\r\n";
assertEqual(expected1, bufferResult.c_str());
}
unittest_main()
53 changes: 53 additions & 0 deletions test/SDTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "DateTime_TC.h"
#include "SD_TC.h"
#include "TC_util.h"
#include "TankControllerLib.h"
#include "UIState/PHCalibrationMid.h"

unittest_setup() {
SD.removeAll();
Expand All @@ -20,6 +22,57 @@ unittest(singleton) {
assertEqual(thing1, thing2);
}

unittest(tankControllerLoop) {
char data[250];
TankControllerLib* tc = TankControllerLib::instance();
DateTime_TC d1(2021, 4, 15);
d1.setAsCurrent();
assertFalse(SD.exists("20210415.csv"));
tc->loop();
delay(1000);
tc->loop();
assertTrue(SD.exists("20210415.csv"));
File file = SD.open("20210415.csv");
assertTrue(file.size() < sizeof(data));
if (file.size() < sizeof(data)) {
file.read(data, file.size());
data[file.size()] = '\0';
assertEqual(
"time,tankid,temp,temp setpoint,pH,pH setpoint,onTime,Kp,Ki,Kd\n"
"04/15/2021 00:00:00, 0, -242.02, 20.00, 0.000, 8.100, 1, 100000.0, 0.0, 0.0\n"
"04/15/2021 00:00:01, 0, -242.02, 20.00, 0.000, 8.100, 2, 100000.0, 0.0, 0.0\n",
data);
}
file.close();
}

Comment thread
jgfoster marked this conversation as resolved.
unittest(loopInCalibration) {
TankControllerLib* pTC = TankControllerLib::instance();
PHCalibrationMid* test = new PHCalibrationMid(pTC);
pTC->setNextState(test, true);
assertTrue(pTC->isInCalibration());
char data[250];
TankControllerLib* tc = TankControllerLib::instance();
DateTime_TC d1(2021, 4, 15);
d1.setAsCurrent();
assertFalse(SD.exists("20210415.csv"));
tc->loop();
delay(1000);
tc->loop();
assertTrue(SD.exists("20210415.csv"));
File file = SD.open("20210415.csv");
assertTrue(file.size() < sizeof(data));
if (file.size() < sizeof(data)) {
file.read(data, file.size());
data[file.size()] = '\0';
assertEqual(
"time,tankid,temp,temp setpoint,pH,pH setpoint,onTime,Kp,Ki,Kd\n"
"04/15/2021 00:00:01, 0, C, 20.00, C, 8.100, 3, 100000.0, 0.0, 0.0\n",
data);
}
file.close();
}

unittest(appendData) {
char data[80];
DateTime_TC d1(2021, 4, 15), d2(2021, 4, 16);
Expand Down