wireless sensor / Mbed 2 deprecated rssi_test_868

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_LoRa_Connect_Example by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mbed.h"
00003 #include "mDot.h"
00004 #include "MTSLog.h"
00005 #include <string>
00006 #include <vector>
00007 #include <algorithm>
00008 #include <math.h>
00009 
00010 #define RETURN_OK              0
00011 #define RETURN_ERR            -1
00012 //define baudrate
00013 #define PC_BAUDRATE            115200
00014 
00015 /* functions prototype */
00016 
00017 int configNetwork(void);
00018 int joinNetwork(void);
00019 int send_data(char *str);
00020 
00021 /* Global variables */
00022 Serial pc       (USBTX, USBRX);  // tx, rx
00023 
00024 mDot* dot;
00025 
00026 static std::string config_network_name = "chinaiot";
00027 static std::string config_network_pass = "password";
00028 static uint8_t config_frequency_sub_band = 1; 
00029 
00030 int main() 
00031 {    
00032     int32_t ret, counter;
00033     char dataBuf[15];
00034 
00035     pc.baud(PC_BAUDRATE);
00036     pc.printf("\n\r\n\r\n\r");
00037     pc.printf("============================================\n\r");
00038     pc.printf("SiFOX remote card read system!\n\r");
00039     pc.printf("============================================\n\r");
00040     pc.printf("PC COM RS232 baudrate: %d \n\r", PC_BAUDRATE);
00041 
00042     //*******************************************
00043     // Configurate Network
00044     //*******************************************
00045     ret = configNetwork();
00046     if(ret != RETURN_OK)         
00047         dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart
00048     
00049     //*******************************************
00050     // Join Network
00051     //*******************************************
00052     ret = joinNetwork();
00053     if(ret != RETURN_OK)         
00054         dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart
00055    
00056     counter = 0;
00057     while(1) {
00058         sprintf(dataBuf, "%d", counter);
00059         
00060         /* Send data to gateway */
00061         send_data(dataBuf);        
00062         /* wait a while */
00063         wait(10);
00064         
00065         logInfo("RSSI Last:%d", dot->getRssiStats().last);
00066         logInfo("SNR Last:%d", dot->getSnrStats().last);
00067 
00068         counter++;          
00069     }
00070 }
00071 
00072     
00073 int configNetwork(void)
00074 {
00075     int32_t ret; 
00076     
00077     dot = mDot::getInstance();
00078     
00079     // print library version information
00080     pc.printf("version: %s\n\r", dot->getId().c_str());
00081     
00082     //*******************************************
00083     // configuration
00084     //*******************************************
00085     // reset to default config so we know what state we're in
00086     dot->resetConfig();
00087     
00088     dot->setTxPower(20);
00089 
00090     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00091 
00092     // set up the mDot with our network information: frequency sub band, network name, and network password
00093     // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
00094 
00095     // frequency sub band is only applicable in the 915 (US) frequency band
00096     // 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
00097     // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
00098     //pc.printf("Setting TX frequency band as 868MHz\n\r");
00099     //if ((ret = dot->setTxFrequency(mDot::FB_868)) != mDot::MDOT_OK){
00100     //    pc.printf("Error:failed to set TX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00101     //    return RETURN_ERR;
00102     //}
00103     //pc.printf("Setting RX frequency band as 868MHz\n\r");
00104     //if ((ret = dot->setRxFrequency(mDot::FB_868)) != mDot::MDOT_OK){
00105     //    pc.printf("Error:failed to set RX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00106     //    return RETURN_ERR;
00107     //}
00108     pc.printf("Setting frequency sub band\n\r");
00109     if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00110         pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00111         return RETURN_ERR;
00112     }
00113 
00114     pc.printf("Setting network name\n\r");
00115     if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
00116         pc.printf("Error:failed to set network name %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00117         return RETURN_ERR;
00118     }
00119 
00120     pc.printf("Setting network password\n\r");
00121     if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
00122         pc.printf("Error:failed to set network password %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00123         return RETURN_ERR;
00124     }
00125     
00126     // a higher spreading factor allows for longer range but lower throughput
00127     // in the 915 (US) frequency band, spreading factors 7 - 10 are available
00128     // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
00129     pc.printf("Setting TX spreading factor\n\r");
00130     if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
00131         pc.printf("Error:failed to set TX datarate %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00132         return RETURN_ERR;
00133     }
00134 
00135     // request receive confirmation of packets from the gateway
00136     pc.printf("Enabling ACKs\n\r");
00137     if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
00138         pc.printf("Error:failed to enable ACKs %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00139         return RETURN_OK;
00140     }
00141 
00142     // save this configuration to the mDot's NVM
00143     pc.printf("Saving config\n\r");
00144     if (! dot->saveConfig()) {
00145         pc.printf("Error:failed to save configuration\n\r");
00146     }
00147     
00148     return RETURN_OK;
00149 }   // end of configuration
00150 
00151 
00152 int joinNetwork(void)
00153 {    
00154     int32_t ret,i; 
00155     std::vector<uint8_t> sendData;
00156     char _header[] = "Reset!";
00157     
00158     // attempt to join the network
00159     pc.printf("Joining network...\n\r");
00160     
00161     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00162         pc.printf("Error: failed to join network %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00163         // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
00164         return RETURN_ERR;
00165     }
00166 
00167     sendData.clear(); 
00168     // format data for sending to the gateway
00169     for( i=0; i< strlen(_header); i++ )
00170                sendData.push_back( _header[i] );
00171                
00172     // send the data to the gateway
00173     pc.printf("Send header to the gateway\n\r");
00174 
00175     if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
00176         pc.printf("Error: failed to send %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00177         return RETURN_ERR;
00178     } else {
00179         pc.printf("Successfully sent data to gateway\n\r");
00180     } 
00181     
00182     return RETURN_OK;
00183 }
00184 
00185 int send_data(char *str)
00186 {
00187     int32_t i, ret;
00188     std::vector<uint8_t> sendData;
00189     
00190     //Send the data to Gateway
00191     sendData.clear();   
00192     // probably not the most efficent way to do this
00193     for(i=0; i< strlen(str); i++ )
00194         sendData.push_back(str[i] );
00195         
00196     // send the data to the gateway
00197     pc.printf("Send %s to Gateway \n\r", str); 
00198     
00199     if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
00200         pc.printf("Error:failed to send\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00201         return RETURN_ERR;
00202     } else {            
00203         pc.printf("Sent data to gateway successfully!\n\r");
00204     }
00205     return RETURN_OK;
00206 }
00207 
00208