Demo of LoRA send/receive

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Connect_IoTClub_one_wire by wireless sensor

Revision:
13:c966b64b227f
Parent:
12:b31e43c9fb15
--- a/main.cpp	Mon Aug 15 14:48:41 2016 +0000
+++ b/main.cpp	Sat Aug 20 08:31:14 2016 +0000
@@ -4,26 +4,14 @@
 #include <string>
 #include <vector>
 #include <algorithm>
-#include "AnalogPH.h"
 
-//defined for mDot SVB debug, comment it if applying for whole system
-//#define NO_MULTITECH_GATEWAY
-
-//Define the number of channels
-#define NUM_OF_CH     4
- 
-AnalogPHSensor CH0_PH(PB_0, 0.0, 0.0);
+const char TURNON[] = "turnon";
+const char TURNOFF[] = "turnoff";
 
-#if (NUM_OF_CH >1)
-  AnalogPHSensor CH1_PH(PB_1, 0.0, 0.0);
-#endif
+const char TURN_ON_ALARM[] = "TurnOnAlarm";
+const char TURN_OFF_ALARM[] = "TurnOffAlarm";
 
-#if (NUM_OF_CH >2)
-  AnalogPHSensor CH2_PH(PA_5, 0.0, 0.0);
-#endif
-#if (NUM_OF_CH >3)
-  AnalogPHSensor CH3_PH(PA_4, 0.0, 0.0);
-#endif
+const char _header[] = "PH Alarm";
 
 // these options must match the settings on your Conduit
 // uncomment the following lines and edit their values to match your configuration
@@ -31,15 +19,24 @@
 static std::string config_network_pass = "password";
 static uint8_t config_frequency_sub_band = 2; 
 
+bool strCmp(char* str1, const char* str2, int len)
+{
+    int i;
+    for (i=0; i<len; i++)
+    {
+        if(str1[i]!=str2[i])return false;
+    }
+    return true;    
+}
+
+
 int main() {
     int32_t ret; 
     mDot* dot;
-    std::vector<uint8_t> data;
-    float phValue;
-    char _header[] = "PH Sensors";
-    char dataBuf[11];
-    int  i, k;
-    int send_failed;
+    std::vector<uint8_t> sendData, recvData;
+    std::vector<uint8_t> turnonData,turnoffData;
+    char sendBuf[11],recvBuf[30];
+    int  i;
 
     // get a mDot handle
     dot = mDot::getInstance();
@@ -54,7 +51,7 @@
     dot->resetConfig();
 
     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
-#ifndef NO_MULTITECH_GATEWAY
+
     // set up the mDot with our network information: frequency sub band, network name, and network password
     // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
 
@@ -106,76 +103,59 @@
         // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
         osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
     }
-
     // format data for sending to the gateway
-    for( int i=0; i< strlen(_header); i++ )
-               data.push_back( _header[i] );
+    for(i=0; i< strlen(TURNON); i++ )
+               turnonData.push_back( TURNON[i] );
+    
+    for(i=0; i< strlen(TURNOFF); i++ )
+               turnoffData.push_back( TURNOFF[i] );                    
+    // format data for sending to the gateway
+    for(i=0; i< strlen(_header); i++ )
+               sendData.push_back( _header[i] );
                
     // send the data to the gateway
-    if ((ret = dot->send(data)) != mDot::MDOT_OK) {
+    if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
         logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
     } else {
             logInfo("successfully sent data to gateway");
     } 
-#else
-  logInfo("%s", _header);
-#endif 
     while (true) {
-        send_failed = 0;
-        // Read the PH values
-        for(k=0; k<NUM_OF_CH; k++){
-           switch(k){
-              case 0:  phValue = CH0_PH; break;
-            #if (NUM_OF_CH >1)
-              case 1:  phValue = CH1_PH; break;
-            #endif
-            #if (NUM_OF_CH >2)
-              case 2:  phValue = CH2_PH; break;
-            #endif  
-            #if (NUM_OF_CH >3)
-              case 3:  phValue = CH3_PH; break;
-            #endif
-              default: 
-                   break;
-            }           
-            sprintf(dataBuf, "CH%d:%5.4f", k, phValue);
-            logInfo("%s", dataBuf);
-            
-         #ifndef NO_MULTITECH_GATEWAY
-            //Send the data to Gateway
-            data.clear();   
-            // probably not the most efficent way to do this
-            for( int i=0; i< strlen(dataBuf); i++ )
-               data.push_back( dataBuf[i] );
-        
-            // send the data to the gateway
-            if ((ret = dot->send(data)) != mDot::MDOT_OK) {
-               logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
-               send_failed++;
-               osDelay(5000);         
-            } else {            
-                logInfo("successfully sent data to gateway");
-            }
-          #endif
-        } 
-        //for
-        if(send_failed>=3){
-           // attempt to rejoin the network
-           logInfo("Attemp to rejoin network....");
-           if ((ret = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
-                logError("Failed to rejoin network!"); // %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-           }else{
-                logInfo("Rejoin network successfully!");    
-           } 
-           osDelay(5000);
-        }
-        //printf("++++++++++++++++++++++\t"); 
-        // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
-     #ifndef NO_MULTITECH_GATEWAY
+       for(i=0;i<30;i++)recvBuf[i]=0;
+       if ((ret = dot->send(turnonData)) != mDot::MDOT_OK) {
+          logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
+       } else {
+          logInfo("successfully sent data to gateway");
+          recvData.clear();
+          if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
+             logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
+          } else {
+             logInfo("datasize = %d", recvData.size());
+             for( int i=0; i< recvData.size(); i++ )
+                recvBuf[i] = recvData[i];
+             logInfo("%s", recvBuf);
+             if(strCmp(recvBuf, TURN_ON_ALARM, recvData.size()))logInfo("Turn on the alarm");
+             if(strCmp(recvBuf, TURN_OFF_ALARM, recvData.size()))logInfo("Turn off the alarm");
+          } 
+       } 
+       osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
+       for(i=0;i<30;i++)recvBuf[i]=0;
+       if ((ret = dot->send(turnoffData)) != mDot::MDOT_OK) {
+          logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
+       } else {
+          logInfo("successfully sent data to gateway");
+          recvData.clear();
+          if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
+             logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
+          } else {
+             logInfo("datasize = %d", recvData.size());
+             for( int i=0; i< recvData.size(); i++ )
+                recvBuf[i] = recvData[i];
+             logInfo("%s", recvBuf);
+             if(strCmp(recvBuf, TURN_ON_ALARM, recvData.size()))logInfo("Turn on the alarm");
+             if(strCmp(recvBuf, TURN_OFF_ALARM, recvData.size()))logInfo("Turn off the alarm");
+          } 
+       } 
         osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
-     #else
-        osDelay(1000);
-     #endif
     }
 
 }