Skip to content

Commit

Permalink
Added begin. Can't figure out why needed.
Browse files Browse the repository at this point in the history
Signed-off-by: Sara Damiano <[email protected]>
  • Loading branch information
SRGDamia1 committed Sep 15, 2023
1 parent 6a30577 commit fde0f83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions examples/DRWI_Mayfly1_WiFi/DRWI_Mayfly1_WiFi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ void setup() {

// Begin the logger
dataLogger.begin();
EnviroDIYPOST.begin(dataLogger, &modem.gsmClient, registrationToken,
samplingFeature);

// Note: Please change these battery voltages to match your battery
// Set up the sensors, except at lowest battery level
Expand Down
8 changes: 8 additions & 0 deletions src/LoggerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,14 @@ void Logger::begin() {
PRINTOUT(F("Sampling feature UUID is:"), _samplingFeatureUUID);
}


for (uint8_t i = 0; i < MAX_NUMBER_SENDERS; i++) {
if (dataPublishers[i] != nullptr) {
PRINTOUT(F("Data will be published to ["), i, F("]"),
dataPublishers[i]->getEndpoint());
}
}

PRINTOUT(F("Logger portion of setup finished.\n"));
}

Expand Down
19 changes: 14 additions & 5 deletions src/publishers/EnviroDIYPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LogBuffer EnviroDIYPublisher::_logBuffer;
// I want to refer to these more than once while ensuring there is only one copy
// in memory
const char* EnviroDIYPublisher::postEndpoint = "/api/data-stream/";
const char* EnviroDIYPublisher::enviroDIYHost = "<private>";
const char* EnviroDIYPublisher::enviroDIYHost = "data.envirodiy.org";
const int EnviroDIYPublisher::enviroDIYPort = 80;
const char* EnviroDIYPublisher::tokenHeader = "\r\nTOKEN: ";
const char* EnviroDIYPublisher::contentLengthHeader = "\r\nContent-Length: ";
Expand All @@ -35,10 +35,14 @@ const char* EnviroDIYPublisher::timestampTag = "\",\"timestamp\":[";
// Constructors
EnviroDIYPublisher::EnviroDIYPublisher() : dataPublisher() {}
EnviroDIYPublisher::EnviroDIYPublisher(Logger& baseLogger, int sendEveryX)
: dataPublisher(baseLogger, sendEveryX) {}
: dataPublisher(baseLogger, sendEveryX) {
_logBuffer.setNumVariables(_baseLogger->getArrayVarCount());
}
EnviroDIYPublisher::EnviroDIYPublisher(Logger& baseLogger, Client* inClient,
int sendEveryX)
: dataPublisher(baseLogger, inClient, sendEveryX) {}
: dataPublisher(baseLogger, inClient, sendEveryX) {
_logBuffer.setNumVariables(_baseLogger->getArrayVarCount());
}
EnviroDIYPublisher::EnviroDIYPublisher(Logger& baseLogger,
const char* registrationToken,
const char* samplingFeatureUUID,
Expand Down Expand Up @@ -70,6 +74,10 @@ void EnviroDIYPublisher::setToken(const char* registrationToken) {
uint16_t EnviroDIYPublisher::calculateJsonSize() {
uint8_t variables = _logBuffer.getNumVariables();
int records = _logBuffer.getNumRecords();
MS_DBG(F("Number of records in log buffer:"), records);
MS_DBG(F("Number of variables in log buffer:"), variables);
MS_DBG(F("Number of variables in base logger:"),
_baseLogger->getArrayVarCount());

uint16_t jsonLength = strlen(samplingFeatureTag);
jsonLength += 36; // sampling feature UUID
Expand All @@ -95,6 +103,7 @@ uint16_t EnviroDIYPublisher::calculateJsonSize() {
}
}
jsonLength += 1; // }
MS_DBG(F("Outgoing JSON size:"), jsonLength);

return jsonLength;
}
Expand Down Expand Up @@ -123,6 +132,7 @@ bool EnviroDIYPublisher::connectionNeeded(void) {
// have less of a chance of losing data
int interval = _sendEveryX;
uint8_t percent = _logBuffer.getPercentFull();
MS_DBG(F("Buffer is"), percent, F("percent full"));
if (percent >= 50) {
interval /= 2;
} else if (percent >= 75) {
Expand Down Expand Up @@ -171,6 +181,7 @@ int16_t EnviroDIYPublisher::publishData(Client* outClient, bool forceFlush) {
// that function said so we know to do it after we record this data point.
// we also flush if requested (in which case the internet is connected too)
bool willFlush = connectionNeeded() || forceFlush;
MS_DBG(F("Publishing record to buffer. Will flush:"), willFlush);

// create record to hold timestamp and variable values in the log buffer
int record = _logBuffer.addRecord(Logger::markedLocalEpochTime);
Expand Down Expand Up @@ -199,8 +210,6 @@ int16_t EnviroDIYPublisher::flushDataBuffer(Client* outClient) {
char tempBuffer[37] = "";
uint16_t did_respond = 0;

MS_DBG(F("Outgoing JSON size:"), calculateJsonSize());

// Open a TCP/IP connection to the Enviro DIY Data Portal (WebSDL)
MS_DBG(F("Connecting client"));
MS_START_DEBUG_TIMER;
Expand Down

4 comments on commit fde0f83

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All sensor and variable subclasses must be included in the Menu a la Carte example
missing_menu_docs

@SRGDamia1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tpwrules I can't figure out why this isn't working for me. I'm running the DRWI_Mayfly1_WiFi example. My only guess is that it's some "static initialization order fiasco" related to the initialization of the static log buffer. The call to the _logBuffer.setNumVariables(_baseLogger->getArrayVarCount()); in the EnviroDIYPublisher isn't setting the correct number. I've combed through the constructor, member initialization, and script construction orders for the array, logger, and publisher, and they all look fine. The call to the logger and then to the array should return the right number; it's just not being set correctly in the log buffer.

With my debugging, I'm seeing this:

Number of records in log buffer: 5 <--EnviroDIYPublisher
Number of variables in log buffer: 0 <--EnviroDIYPublisher
Number of variables in base logger: 7 <--EnviroDIYPublisher

If I add the begin statement for the publisher to the example, which forces _logBuffer.setNumVariables(_baseLogger->getArrayVarCount()); to be called at runtime, it works perfectly.

@SRGDamia1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tpwrules If I make the log-buffer non-static, I don't have any problems with the initialization.

@tpwrules
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should probably make it non static. I will do that in my branch.

Please sign in to comment.