Shoalhaven Water / Mbed OS Cloudtracker

Dependencies:   libmDot-mbed5 ISL29011

Revision:
47:2bc515eeaa18
Parent:
44:89b8ed62c32c
--- a/examples/inc/RadioEvent.h	Mon Mar 01 02:47:26 2021 +0000
+++ b/examples/inc/RadioEvent.h	Sat May 15 02:59:15 2021 +0000
@@ -4,6 +4,12 @@
 #include "dot_util.h"
 #include "mDotEvent.h"
 #include "Fota.h"
+#include <string.h>
+
+extern bool rejoin;
+extern uint16_t rain_total;
+extern float uplinkInterval;
+extern uint8_t downlink_msg;
 
 class RadioEvent : public mDotEvent
 {
@@ -14,8 +20,8 @@
     virtual ~RadioEvent() {}
 
     virtual void PacketRx(uint8_t port, uint8_t *payload, uint16_t size, int16_t rssi, int16_t snr, lora::DownlinkControl ctrl, uint8_t slot, uint8_t retries, uint32_t address, bool dupRx) {
-        //mDotEvent::PacketRx(port, payload, size, rssi, snr, ctrl, slot, retries, address, dupRx);
-
+        mDotEvent::PacketRx(port, payload, size, rssi, snr, ctrl, slot, retries, address, dupRx);
+        
 #if ACTIVE_EXAMPLE == FOTA_EXAMPLE
         if(port == 200 || port == 201 || port == 202) {
             Fota::getInstance()->processCmd(payload, port, size);
@@ -74,11 +80,92 @@
 
             logDebug("Rx %d bytes", info->RxBufferSize);
             if (info->RxBufferSize > 0) {
-#if ACTIVE_EXAMPLE != FOTA_EXAMPLE
+//#if ACTIVE_EXAMPLE != FOTA_EXAMPLE
                 // print RX data as string and hexadecimal
                 std::string rx((const char*)info->RxBuffer, info->RxBufferSize);
-                printf("Rx data: %s [%s]\r\n", rx.c_str(), mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str());
-#endif
+                //printf("Rx data: %s [%s]\r\n", rx.c_str(), mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str());
+                
+/// - SECTION FOR DOWNLINK HANDLING - ////////////////////////////////////////////////////////////////////////////////////////////////////
+                std::string rxData = mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str();
+                logInfo("Rx data: [%s]\r", mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str());
+                
+                if(rxData.compare("01") == 0){
+                    logInfo("Command received: [01]");
+                    logInfo("Soft reset of the system");
+                    dot->resetCpu();
+                }
+                else if(rxData.compare("02") == 0){
+                    logInfo("Command received: [02]");
+                    logInfo("Setting rain_total to 0");
+                    rain_total = 0;
+                    downlink_msg = 0x02;
+                }
+                else if(rxData.compare("03") == 0){
+                    logInfo("Command received: [03]");
+                    logInfo("Setting uplinkInterval to 15mins");
+                    uplinkInterval = 15;
+                    downlink_msg = 0x03;
+                }
+                else if(rxData.compare("04") == 0){
+                    logInfo("Command received: [04]");
+                    logInfo("Setting uplinkInterval to 30mins");
+                    uplinkInterval = 30;
+                    downlink_msg = 0x04;
+                }
+                else if(rxData.compare("05") == 0){
+                    logInfo("Command received: [05]");
+                    logInfo("Setting uplinkInterval to 60mins");
+                    uplinkInterval = 60;
+                    downlink_msg = 0x05;
+                }
+                else if(rxData.compare("06") == 0){
+                    logInfo("Command received: [06]");
+                    logInfo("Setting uplinkInterval to 1min (for debugging)");
+                    uplinkInterval = 1;
+                    downlink_msg = 0x06;
+                }
+                else if(rxData.compare("07") == 0){
+                    logInfo("Command received: [07]");
+                    logInfo("Setting ACK to: 0 (off)");
+                    if (dot->setAck(0) != mDot::MDOT_OK) {
+                        logError("failed to set acks to %u", 0);
+                    }
+                }
+                else if(rxData.compare("08") == 0){
+                    logInfo("Command received: [08]");
+                    logInfo("Setting ACK to: 1");
+                    if (dot->setAck(1) != mDot::MDOT_OK) {
+                        logError("failed to set acks to %u", 1);
+                    }
+                }
+                else if(rxData.compare("09") == 0){
+                    logInfo("Command received: [09]");
+                    logInfo("Setting ACK to: 3");
+                    if (dot->setAck(3) != mDot::MDOT_OK) {
+                        logError("failed to set acks to %u", 3);
+                    }
+                }
+                else if(rxData.compare("0a") == 0){
+                    logInfo("Command received: [0a]");
+                    logInfo("Setting ACK to: 5");
+                    if (dot->setAck(5) != mDot::MDOT_OK) {
+                        logError("failed to set acks to %u", 5);
+                    }
+                }
+                else if(rxData.compare("0b") == 0){
+                    logInfo("Command received: [0b]");
+                    logInfo("Setting ACK to: 8 (max)");
+                    if (dot->setAck(8) != mDot::MDOT_OK) {
+                        logError("failed to set acks to %u", 8);
+                    }
+                }
+                else{
+                    logInfo("Unknown downlink command received");
+                    downlink_msg = 0xFF;
+                }
+                
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//#endif
             }
         }
     }