Skip to content
Snippets Groups Projects

Catchup

Merged Callum Inglis requested to merge DEV into update-sensor-config-from-api
1 file
+ 26
5
Compare changes
  • Side-by-side
  • Inline
@@ -45,10 +45,13 @@
#define DEBUG 1
#define MIN_RAND_DELAY 500 // ms
#define MAX_RAND_DELAY 1250 // ms
#define POLLING_FREQUENCY 2500 // ms Take value every POLLING_FREQUENCY ms
#define MAX_TRANSMISSION_RETRIES 5 // No. Retries before recordings new values then retry
#define TX_AFTER_N_READINGS 10 // No. of samples, after which Transmit Avg Readings
#define TX_RESERVATION_TIME 1 // How long do we require use of TX for sending packets? (Seconds)
// Variable Values - May be updated by Gateway
int TX_AFTER_N_READINGS = 4; // No. of samples, after which Transmit Avg Readings
int POLLING_FREQUENCY = 30000; // ms Take value every POLLING_FREQUENCY ms
int pollEventCount = 0; // Number of times data has been sampled in this recording period
// Lora Config
@@ -155,6 +158,7 @@ bool setupLoRa() {
LoRa.setSpreadingFactor(loraSpreadingFactor);
LoRa.setSignalBandwidth(loraSignalBandwidth);
LoRa.setTxPower(20);
Serial.println("[+] LoRa Initialized OK!");
return true;
@@ -360,6 +364,7 @@ StaticJsonDocument<1024> listenForAndConsumeMessage(int listenDuration) {
}
}
Serial.println(incoming);
// Deserialize - TODO Error Handling https://arduinojson.org/v6/api/json/deserializejson/
deserializeJson(json, incoming);
break;
@@ -445,7 +450,22 @@ boolean listenForTxPayloadAccept(int listenDuration, int messageID) {
Serial.println("[+] Transmit - Payload - Ack Recieved: " + String(ackStatus) + "\n");
if (ackStatus) { return true; } // It all worked :)
if (ackStatus) { // It all worked :)
// Check for any updated config values
const int newTxAfterNReadings = json["txAfterNReadings"];
if (newTxAfterNReadings != NULL && newTxAfterNReadings != TX_AFTER_N_READINGS) {
TX_AFTER_N_READINGS = newTxAfterNReadings;
Serial.println("[+] Tx After N Readings Updated, now: " + String(TX_AFTER_N_READINGS) + " samples");
}
const int newPollingFrequency = json["pollingFrequency"];
if (newPollingFrequency != NULL && newPollingFrequency != POLLING_FREQUENCY) {
POLLING_FREQUENCY = newPollingFrequency;
Serial.println("[+] Polling Frequency Updated, now: " + String(POLLING_FREQUENCY) + "ms");
}
return true;
}
// TODO Retransmit, recover, etc
Serial.println("[-] Transmit - Payload - Ack Failed - TODO Setup Retransmission");
@@ -483,7 +503,7 @@ boolean transmitData(DynamicJsonDocument payload) {
sendTxHello(msgCount);
// Await TX Hello Auth - Expect: Timeout | Not Auth | Accept + Clear To Transmit
if (!listenForTxHelloAccept(TX_RESERVATION_TIME * 2, msgCount)) {
if (!listenForTxHelloAccept(TX_RESERVATION_TIME * 4, msgCount)) {
return false; // Can't transmit just now, we will retry
}
@@ -584,11 +604,12 @@ void loop() {
// Transmission failed, handle re-tries
int numRetries = 1;
waitRandomDelay(TX_RESERVATION_TIME*2); // Introduce random delay to avoid another collision
while (!transmitData(sensorData) && numRetries < MAX_TRANSMISSION_RETRIES){
numRetries++;
Serial.println("[-] Failed to send packet, retrying. Attempt " + String(numRetries) + " of " + String(MAX_TRANSMISSION_RETRIES) + "\n");
waitRandomDelay(TX_RESERVATION_TIME); // Introduce random delay to avoid another collision
waitRandomDelay(TX_RESERVATION_TIME*2); // Introduce random delay to avoid another collision
}
// We were able to transmit after retries, values reset, now record new values
Loading