Update OneOpenAir.ino

This commit is contained in:
samuelbles07
2025-03-17 02:20:43 +07:00
parent 76b2b3f940
commit 299234ac40

View File

@ -263,7 +263,7 @@ void setup() {
mutexMeasurementCycleQueue = xSemaphoreCreateMutex(); mutexMeasurementCycleQueue = xSemaphoreCreateMutex();
BaseType_t xReturned = BaseType_t xReturned =
xTaskCreate(networkingTask, "WD", 4096, null, 5, &handleNetworkTask); xTaskCreate(networkingTask, "NetworkingTask", 4096, null, 5, &handleNetworkTask);
if (xReturned == pdPASS) { if (xReturned == pdPASS) {
Serial.println("Success create networking task"); Serial.println("Success create networking task");
} else { } else {
@ -283,7 +283,7 @@ void setup() {
if (networkOption == UseCellular) { if (networkOption == UseCellular) {
configSchedule.setPeriod(CELLULAR_SERVER_CONFIG_SYNC_INTERVAL); configSchedule.setPeriod(CELLULAR_SERVER_CONFIG_SYNC_INTERVAL);
transmissionSchedule.setPeriod(CELLULAR_TRANSMISSION_INTERVAL); transmissionSchedule.setPeriod(CELLULAR_TRANSMISSION_INTERVAL);
configSchedule.setPeriod(CELLULAR_MEASUREMENT_INTERVAL); measurementSchedule.setPeriod(CELLULAR_MEASUREMENT_INTERVAL);
} }
} }
@ -966,7 +966,7 @@ void initializeNetwork() {
#ifdef ESP8266 #ifdef ESP8266
// ota not supported // ota not supported
#else #else
// checkForFirmwareUpdate(); //! Temporary until ota cellular // checkForFirmwareUpdate(); // FIX: Temporary until ota cellular
// checkForUpdateSchedule.update(); // checkForUpdateSchedule.update();
#endif #endif
@ -1287,14 +1287,8 @@ void sendDataToServer(void) {
return; return;
} }
// TODO: Loop through measurementCycleQueue size // Aquire queue mutex to get queue size
xSemaphoreTake(mutexMeasurementCycleQueue, portMAX_DELAY);
// Aquire queue mutex
if (xSemaphoreTake(mutexMeasurementCycleQueue, portMAX_DELAY) == pdFALSE) {
// Sanity check to just release mutex
xSemaphoreGive(mutexMeasurementCycleQueue);
}
// Make sure measurement cycle available // Make sure measurement cycle available
int queueSize = measurementCycleQueue.size(); int queueSize = measurementCycleQueue.size();
@ -1304,23 +1298,36 @@ void sendDataToServer(void) {
return; return;
} }
// Get the oldest queue Serial.printf("Measurement cycle queue size %d\n", queueSize);
auto mc = measurementCycleQueue.front();
// Release before actually post measures that might takes too long
xSemaphoreGive(mutexMeasurementCycleQueue); xSemaphoreGive(mutexMeasurementCycleQueue);
delay(10); // Wait for a moment in case new measurement schedule wait for it
String payload = measurements.buildMeasurementPayload(mc, fwMode); for (int i = 1; i <= queueSize; i++) {
if (agClient->httpPostMeasures(ag->getDeviceId(), payload.c_str())) { Serial.printf("Attempt post measurement cycle from queue %d\n", i);
Serial.println(); // Aquire queue mutex to get oldest in queue
Serial.println("Online mode and isPostToAirGradient = true"); xSemaphoreTake(mutexMeasurementCycleQueue, portMAX_DELAY);
// Get the oldest queue
auto mc = measurementCycleQueue.front();
// Release before actually post measures that might takes too long
xSemaphoreGive(mutexMeasurementCycleQueue);
String payload = measurements.buildMeasurementPayload(mc, fwMode);
if (agClient->httpPostMeasures(ag->getDeviceId(), payload.c_str()) == false) {
// Consider network has a problem, retry in next schedule
Serial.println("Post measures failed, retry in next schedule");
break;
}
Serial.println(); Serial.println();
// Post success, remove the oldest queue // Post success, remove the oldest queue
if (xSemaphoreTake(mutexMeasurementCycleQueue, portMAX_DELAY) == pdTRUE) { xSemaphoreTake(mutexMeasurementCycleQueue, portMAX_DELAY);
measurementCycleQueue.erase(measurementCycleQueue.begin()); measurementCycleQueue.erase(measurementCycleQueue.begin());
xSemaphoreGive(mutexMeasurementCycleQueue); xSemaphoreGive(mutexMeasurementCycleQueue);
}
// Wait a moment before post next in queue
delay(2000);
} }
} }