#define MAX_FRAGMENTS_PER_BATCH 2
#define MAX_PAYLOAD_SIZE 51
#define GEO_PORT 3
uint8_t currentFragment = 0;
uint8_t totalFragments = 0;
String geofenceDataString = "";
bool isSendingGeofenceData = false;
bool geofenceTransmissionInProgress = false;
void prepareGeofenceDataString() {
geofenceDataString = "";
for (size_t i = 0; i < geoFence.vertices.size(); i++) {
String lat = String(geoFence.vertices[i].y, 6);
String lng = String(geoFence.vertices[i].x, 6);
geofenceDataString += lat + "," + lng;
if (i < geoFence.vertices.size() - 1) {
geofenceDataString += ";";
}
}
totalFragments = (geofenceDataString.length() + MAX_PAYLOAD_SIZE - 1) / MAX_PAYLOAD_SIZE;
}
void resetGeofenceTransmissionState() {
currentFragment = 0;
isSendingGeofenceData = false;
geofenceTransmissionInProgress = false; // Reset flag when transmission is done
}
void sendGeofenceFragment() {
if (currentFragment < totalFragments) {
uint8_t fragmentPayload[MAX_PAYLOAD_SIZE] = {0};
uint8_t startIdx = currentFragment * MAX_PAYLOAD_SIZE;
uint8_t fragmentSize = min(static_cast<unsigned int>(MAX_PAYLOAD_SIZE), geofenceDataString.length() - startIdx);
String fragment = geofenceDataString.substring(startIdx, startIdx + fragmentSize);
fragment.getBytes(fragmentPayload, fragmentSize + 1);
Serial.printf("Preparing to send fragment %d of %d\n", currentFragment + 1, totalFragments);
for (int i = 0; i < fragmentSize; i++) {
Serial.printf("%02X ", fragmentPayload[i]);
}
Serial.println();
prepareTxFrame(fragmentPayload, fragmentSize, GEO_PORT);
LoRaWAN.send();
currentFragment++;
Serial.printf("Sent fragment %d of %d\n", currentFragment, totalFragments);
delay(3000);
if (currentFragment >= totalFragments) {
Serial.println("All fragments sent successfully.");
resetGeofenceTransmissionState();
deviceState = DEVICE_STATE_CYCLE;
} else {
deviceState = DEVICE_STATE_CYCLE;
}
} else {
Serial.println("No more fragments to send or all fragments sent.");
}
}
void sendAllGeofenceFragments() {
Serial.println("Starting sendAllGeofenceFragments...");
geofenceTransmissionInProgress = true; // Set flag to indicate geofence transmission is in progress
prepareGeofenceDataString();
currentFragment = 0;
isSendingGeofenceData = true;
sendGeofenceFragment();
}
void sendGeofenceUplink() {
switch (deviceState) {
case DEVICE_STATE_INIT: {
Serial.println("Initializing LoRaWAN...");
LoRaWAN.init(loraWanClass, loraWanRegion);
deviceState = DEVICE_STATE_JOIN;
break;
}
case DEVICE_STATE_JOIN: {
Serial.println("Joining LoRaWAN network...");
LoRaWAN.join();
break;
}
case DEVICE_STATE_SEND: {
if (!isSendingGeofenceData) {
sendAllGeofenceFragments();
} else {
sendGeofenceFragment();
}
break;
}
case DEVICE_STATE_CYCLE: {
Serial.println("Cycling before continuing with the next fragment...");
txDutyCycleTime = 3000;
LoRaWAN.cycle(txDutyCycleTime);
deviceState = DEVICE_STATE_SEND;
break;
}
case DEVICE_STATE_SLEEP: {
Serial.println("Entering sleep mode...");
LoRaWAN.sleep();
break;
}
default: {
deviceState = DEVICE_STATE_INIT;
break;
}
}
}
Then in my loop I have:
if (isSendingGeofenceData) {
sendGeofenceUplink(); // Continue sending geofence fragments
} else {}
I know that at this point my code is very overcomplicated, but I’ve been trying everything now. Thank you for your help!