Jjun Deng / Mbed 2 deprecated WDR_additional_counter

Dependencies:   libmDot mbed-rtos mbed

Fork of CardRead_20170613 by wireless sensor

Revision:
18:12be4d4c8fc2
Parent:
17:b60549bc8626
Child:
19:a0e9128dec64
--- a/main.cpp	Thu Feb 16 17:34:37 2017 +0000
+++ b/main.cpp	Mon Jun 12 17:01:42 2017 +0000
@@ -28,10 +28,13 @@
 
 unsigned int inIndex  = 0;
 unsigned int outIndex = 0;
+unsigned int pwr_cnt  = 0;
+
 
 /* functions prototype */
 void readCardID(void);
 void sendCardID(void);
+void sendPwrStat(void);
 
 void my_strcpy(char *dst, char *src);
 void my_strclear(char *str, unsigned int len);
@@ -43,12 +46,16 @@
 /* Global variables */
 Serial pc       (USBTX, USBRX);  // tx, rx
 Serial cm_rs485 (PA_2,PA_3);     // tx, rx
+DigitalIn pwr_stat (PA_6);       //pwr_status pin on/off
+string pwr_stat_str;
+std::string pwr_on_str =  "ON";
+std::string pwr_off_str = "OFF";
 
 mDot* dot;
 
 static std::string config_network_name = "chinaiot";
 static std::string config_network_pass = "password";
-static uint8_t config_frequency_sub_band = 2; 
+static uint8_t config_frequency_sub_band = 8; 
 
 int main() 
 {    
@@ -89,7 +96,18 @@
         sendCardID();
         
         /* wait a while */
-        wait(1);          
+        wait(1); 
+        if (pwr_cnt == 31){       
+             pwr_cnt = 0;
+             if(pwr_stat == 1){
+                send_data("ON");
+             }else{
+                send_data("OFF");    
+             }    
+        }     
+        else {
+            pwr_cnt++;
+        }         
     }
 }
 
@@ -117,11 +135,14 @@
     // frequency sub band is only applicable in the 915 (US) frequency band
     // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
     // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
-    pc.printf("Setting frequency sub band\n\r");
-    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
-        pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
-        return RETURN_ERR;
-    }
+    int32_t setTxFrequency(const uint32_t& FB_868);
+    
+  
+    //pc.printf("Setting frequency sub band\n\r");
+    //if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
+    //    pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+    //    return RETURN_ERR;
+    //}
 
     pc.printf("Setting network name\n\r");
     if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
@@ -141,7 +162,7 @@
     pc.printf("Setting TX spreading factor\n\r");
     if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
         pc.printf("Error:failed to set TX datarate %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
-        return RETURN_ERR;
+        return RETURN_ERR;//
     }
 
     // request receive confirmation of packets from the gateway
@@ -341,4 +362,43 @@
         //wait(10);
     }
     
-}
\ No newline at end of file
+}
+
+
+void sendPwrStat(void)
+{
+    int32_t ret;
+    
+    //pc.printf("Send Tick!\n\r");
+    /*send the data */
+    if(ringBuffer[outIndex][0] != 0) //not empty
+    {
+        pc.printf("Send dada[%s] to Gateway!\n\r", ringBuffer[outIndex]);
+      #ifndef NO_MULTITECH_GATEWAY  
+        /* t.b.d : send data though LoRA */
+        if(send_data(ringBuffer[outIndex]) != RETURN_OK)
+        {
+           pc.printf("Failed to send data to netwotk!\n\r");
+           pc.printf("Rejoin network and try one more time!\n\r");
+                       // attempt to rejoin the network
+           pc.printf("Attemp to rejoin network....\n\r");
+           if ((ret = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
+                pc.printf("Failed to rejoin network! %d:%s \n\r", ret, mDot::getReturnCodeString(ret).c_str());
+                return;
+           }else{
+                pc.printf("Rejoin network successfully!");
+                if(send_data(ringBuffer[outIndex]) != RETURN_OK) return;  //abort to send       
+           } 
+        }
+       #endif 
+        //clear the buffer
+        my_strclear(ringBuffer[outIndex], RESPONSE_LENGTH);
+        outIndex++;
+        if(outIndex >= RINGBUFFER_SIZE)outIndex = 0;
+    }else{ //empty
+        pc.printf("Nothing to send!\n\r");
+        //wait(10);
+    }
+    
+}
+