rssi test for 915

Dependencies:   libmDot915 mbed-rtos mbed

Fork of rssi_test by wireless sensor

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 = 2; 
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, "Send:%d", counter);
00059         /* Send data to gateway */
00060         send_data(dataBuf);        
00061         /* wait a while */
00062         wait(10);
00063 
00064         counter++;          
00065     }
00066 }
00067 
00068     
00069 int configNetwork(void)
00070 {
00071     int32_t ret; 
00072     
00073     dot = mDot::getInstance();
00074     
00075     // print library version information
00076     pc.printf("version: %s\n\r", dot->getId().c_str());
00077     
00078     //*******************************************
00079     // configuration
00080     //*******************************************
00081     // reset to default config so we know what state we're in
00082     dot->resetConfig();
00083     
00084     dot->setTxPower(20);
00085 
00086     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00087 
00088     // set up the mDot with our network information: frequency sub band, network name, and network password
00089     // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
00090 
00091     // frequency sub band is only applicable in the 915 (US) frequency band
00092     // 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
00093     // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
00094     //pc.printf("Setting TX frequency band as 868MHz\n\r");
00095     //if ((ret = dot->setTxFrequency(mDot::FB_868)) != mDot::MDOT_OK){
00096     //    pc.printf("Error:failed to set TX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00097     //    return RETURN_ERR;
00098     //}
00099     //pc.printf("Setting RX frequency band as 868MHz\n\r");
00100     //if ((ret = dot->setRxFrequency(mDot::FB_868)) != mDot::MDOT_OK){
00101     //    pc.printf("Error:failed to set RX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00102     //    return RETURN_ERR;
00103     //}
00104     pc.printf("Setting frequency sub band\n\r");
00105     if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00106         pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00107         return RETURN_ERR;
00108     }
00109 
00110     pc.printf("Setting network name\n\r");
00111     if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
00112         pc.printf("Error:failed to set network name %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00113         return RETURN_ERR;
00114     }
00115 
00116     pc.printf("Setting network password\n\r");
00117     if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
00118         pc.printf("Error:failed to set network password %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00119         return RETURN_ERR;
00120     }
00121     
00122     // a higher spreading factor allows for longer range but lower throughput
00123     // in the 915 (US) frequency band, spreading factors 7 - 10 are available
00124     // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
00125     pc.printf("Setting TX spreading factor\n\r");
00126     if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
00127         pc.printf("Error:failed to set TX datarate %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00128         return RETURN_ERR;
00129     }
00130 
00131     // request receive confirmation of packets from the gateway
00132     pc.printf("Enabling ACKs\n\r");
00133     if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
00134         pc.printf("Error:failed to enable ACKs %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00135         return RETURN_OK;
00136     }
00137 
00138     // save this configuration to the mDot's NVM
00139     pc.printf("Saving config\n\r");
00140     if (! dot->saveConfig()) {
00141         pc.printf("Error:failed to save configuration\n\r");
00142     }
00143     
00144     return RETURN_OK;
00145 }   // end of configuration
00146 
00147 
00148 int joinNetwork(void)
00149 {    
00150     int32_t ret,i; 
00151     std::vector<uint8_t> sendData;
00152     char _header[] = "Reset!";
00153     
00154     // attempt to join the network
00155     pc.printf("Joining network...\n\r");
00156     
00157     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00158         pc.printf("Error: failed to join network %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00159         // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
00160         return RETURN_ERR;
00161     }
00162 
00163     sendData.clear(); 
00164     // format data for sending to the gateway
00165     for( i=0; i< strlen(_header); i++ )
00166                sendData.push_back( _header[i] );
00167                
00168     // send the data to the gateway
00169     pc.printf("Send header to the gateway\n\r");
00170 
00171     if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
00172         pc.printf("Error: failed to send %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00173         return RETURN_ERR;
00174     } else {
00175         pc.printf("Successfully sent data to gateway\n\r");
00176     } 
00177     
00178     return RETURN_OK;
00179 }
00180 
00181 int send_data(char *str)
00182 {
00183     int32_t i, ret;
00184     std::vector<uint8_t> sendData;
00185     
00186     //Send the data to Gateway
00187     sendData.clear();   
00188     // probably not the most efficent way to do this
00189     for(i=0; i< strlen(str); i++ )
00190         sendData.push_back(str[i] );
00191         
00192     // send the data to the gateway
00193     pc.printf("Send %s to Gateway \n\r", str); 
00194     
00195     if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
00196         pc.printf("Error:failed to send\n\r", ret, mDot::getReturnCodeString(ret).c_str());
00197         return RETURN_ERR;
00198     } else {            
00199         pc.printf("Sent data to gateway successfully!\n\r");
00200     }
00201     return RETURN_OK;
00202 }
00203 
00204