rssi test for 868

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_LoRa_Connect_Example by MultiTech

Revision:
7:f6d527a85c05
Parent:
6:f0b6fea36e28
--- a/main.cpp	Tue Apr 12 23:44:25 2016 +0000
+++ b/main.cpp	Tue May 09 15:45:01 2017 +0000
@@ -1,109 +1,208 @@
+#include "mbed.h"
 #include "mbed.h"
 #include "mDot.h"
 #include "MTSLog.h"
 #include <string>
 #include <vector>
 #include <algorithm>
+#include <math.h>
 
-// these options must match the settings on your Conduit
-// uncomment the following lines and edit their values to match your configuration
-static std::string config_network_name = "<lora network id>";
-static std::string config_network_pass = "<lora network key>";
-static uint8_t config_frequency_sub_band = 1;
+#define RETURN_OK              0
+#define RETURN_ERR            -1
+//define baudrate
+#define PC_BAUDRATE            115200
+
+/* functions prototype */
+
+int configNetwork(void);
+int joinNetwork(void);
+int send_data(char *str);
+
+/* Global variables */
+Serial pc       (USBTX, USBRX);  // tx, rx
+
+mDot* dot;
+
+static std::string config_network_name = "chinaiot";
+static std::string config_network_pass = "password";
+static uint8_t config_frequency_sub_band = 1; 
+
+int main() 
+{    
+    int32_t ret, counter;
+    char dataBuf[15];
+
+    pc.baud(PC_BAUDRATE);
+    pc.printf("\n\r\n\r\n\r");
+    pc.printf("============================================\n\r");
+    pc.printf("SiFOX remote card read system!\n\r");
+    pc.printf("============================================\n\r");
+    pc.printf("PC COM RS232 baudrate: %d \n\r", PC_BAUDRATE);
 
-int main() {
-    int32_t ret;
-    mDot* dot;
-    std::vector<uint8_t> data;
-    std::string data_str = "hello!";
+    //*******************************************
+    // Configurate Network
+    //*******************************************
+    ret = configNetwork();
+    if(ret != RETURN_OK)         
+        dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart
     
-    // get a mDot handle
+    //*******************************************
+    // Join Network
+    //*******************************************
+    ret = joinNetwork();
+    if(ret != RETURN_OK)         
+        dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart
+   
+    counter = 0;
+    while(1) {
+        sprintf(dataBuf, "%d", counter);
+        
+        /* Send data to gateway */
+        send_data(dataBuf);        
+        /* wait a while */
+        wait(10);
+        
+        logInfo("RSSI Last:%d", dot->getRssiStats().last);
+        logInfo("SNR Last:%d", dot->getSnrStats().last);
+
+        counter++;          
+    }
+}
+
+    
+int configNetwork(void)
+{
+    int32_t ret; 
+    
     dot = mDot::getInstance();
     
     // print library version information
-    logInfo("version: %s", dot->getId().c_str());
-
+    pc.printf("version: %s\n\r", dot->getId().c_str());
+    
     //*******************************************
     // configuration
     //*******************************************
     // reset to default config so we know what state we're in
     dot->resetConfig();
     
+    dot->setTxPower(20);
+
     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
 
     // 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()
-    
+
     // 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
-    
-//    logInfo("setting to public network");
-//    if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
-//        logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-//    }
-
-    logInfo("setting frequency sub band");    
+    //pc.printf("Setting TX frequency band as 868MHz\n\r");
+    //if ((ret = dot->setTxFrequency(mDot::FB_868)) != mDot::MDOT_OK){
+    //    pc.printf("Error:failed to set TX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+    //    return RETURN_ERR;
+    //}
+    //pc.printf("Setting RX frequency band as 868MHz\n\r");
+    //if ((ret = dot->setRxFrequency(mDot::FB_868)) != mDot::MDOT_OK){
+    //    pc.printf("Error:failed to set RX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+    //    return RETURN_ERR;
+    //}
+    pc.printf("Setting frequency sub band\n\r");
     if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
-        logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+        pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+        return RETURN_ERR;
     }
-    
-    logInfo("setting network name");
+
+    pc.printf("Setting network name\n\r");
     if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
-        logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+        pc.printf("Error:failed to set network name %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+        return RETURN_ERR;
     }
-    
-    logInfo("setting network password");
+
+    pc.printf("Setting network password\n\r");
     if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
-        logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+        pc.printf("Error:failed to set network password %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+        return RETURN_ERR;
     }
     
     // a higher spreading factor allows for longer range but lower throughput
     // in the 915 (US) frequency band, spreading factors 7 - 10 are available
     // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
-    logInfo("setting TX spreading factor");
+    pc.printf("Setting TX spreading factor\n\r");
     if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
-        logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+        pc.printf("Error:failed to set TX datarate %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+        return RETURN_ERR;
     }
-    
+
     // request receive confirmation of packets from the gateway
-    logInfo("enabling ACKs");
+    pc.printf("Enabling ACKs\n\r");
     if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
-        logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+        pc.printf("Error:failed to enable ACKs %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+        return RETURN_OK;
+    }
+
+    // save this configuration to the mDot's NVM
+    pc.printf("Saving config\n\r");
+    if (! dot->saveConfig()) {
+        pc.printf("Error:failed to save configuration\n\r");
     }
     
-    // save this configuration to the mDot's NVM
-    logInfo("saving config");
-    if (! dot->saveConfig()) {
-        logError("failed to save configuration");
-    }
-    //*******************************************
-    // end of configuration
-    //*******************************************
+    return RETURN_OK;
+}   // end of configuration
+
 
+int joinNetwork(void)
+{    
+    int32_t ret,i; 
+    std::vector<uint8_t> sendData;
+    char _header[] = "Reset!";
+    
     // attempt to join the network
-    logInfo("joining network");
+    pc.printf("Joining network...\n\r");
+    
     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
-        logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+        pc.printf("Error: failed to join network %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
         // 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()));
+        return RETURN_ERR;
     }
 
+    sendData.clear(); 
     // format data for sending to the gateway
-    for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
-        data.push_back((uint8_t) *it);
+    for( i=0; i< strlen(_header); i++ )
+               sendData.push_back( _header[i] );
+               
+    // send the data to the gateway
+    pc.printf("Send header to the gateway\n\r");
+
+    if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
+        pc.printf("Error: failed to send %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+        return RETURN_ERR;
+    } else {
+        pc.printf("Successfully sent data to gateway\n\r");
+    } 
+    
+    return RETURN_OK;
+}
 
-    while (true) {
-        // send the data to the gateway
-        if ((ret = dot->send(data)) != mDot::MDOT_OK) {
-            logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
-        } else {
-            logInfo("successfully sent data to gateway");
-        }
+int send_data(char *str)
+{
+    int32_t i, ret;
+    std::vector<uint8_t> sendData;
+    
+    //Send the data to Gateway
+    sendData.clear();   
+    // probably not the most efficent way to do this
+    for(i=0; i< strlen(str); i++ )
+        sendData.push_back(str[i] );
+        
+    // send the data to the gateway
+    pc.printf("Send %s to Gateway \n\r", str); 
+    
+    if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
+        pc.printf("Error:failed to send\n\r", ret, mDot::getReturnCodeString(ret).c_str());
+        return RETURN_ERR;
+    } else {            
+        pc.printf("Sent data to gateway successfully!\n\r");
+    }
+    return RETURN_OK;
+}
 
-        // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
-        osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
-    }
 
-    return 0;
-}