rssi test for 868

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_LoRa_Connect_Example by MultiTech

Committer:
Dengjj
Date:
Tue May 09 15:45:01 2017 +0000
Revision:
7:f6d527a85c05
Parent:
6:f0b6fea36e28
release for 868 rssi test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Dengjj 7:f6d527a85c05 1 #include "mbed.h"
mfiore 0:09250cd371d2 2 #include "mbed.h"
mfiore 0:09250cd371d2 3 #include "mDot.h"
mfiore 4:36e214ebfa56 4 #include "MTSLog.h"
mfiore 0:09250cd371d2 5 #include <string>
mfiore 0:09250cd371d2 6 #include <vector>
mfiore 4:36e214ebfa56 7 #include <algorithm>
Dengjj 7:f6d527a85c05 8 #include <math.h>
mfiore 0:09250cd371d2 9
Dengjj 7:f6d527a85c05 10 #define RETURN_OK 0
Dengjj 7:f6d527a85c05 11 #define RETURN_ERR -1
Dengjj 7:f6d527a85c05 12 //define baudrate
Dengjj 7:f6d527a85c05 13 #define PC_BAUDRATE 115200
Dengjj 7:f6d527a85c05 14
Dengjj 7:f6d527a85c05 15 /* functions prototype */
Dengjj 7:f6d527a85c05 16
Dengjj 7:f6d527a85c05 17 int configNetwork(void);
Dengjj 7:f6d527a85c05 18 int joinNetwork(void);
Dengjj 7:f6d527a85c05 19 int send_data(char *str);
Dengjj 7:f6d527a85c05 20
Dengjj 7:f6d527a85c05 21 /* Global variables */
Dengjj 7:f6d527a85c05 22 Serial pc (USBTX, USBRX); // tx, rx
Dengjj 7:f6d527a85c05 23
Dengjj 7:f6d527a85c05 24 mDot* dot;
Dengjj 7:f6d527a85c05 25
Dengjj 7:f6d527a85c05 26 static std::string config_network_name = "chinaiot";
Dengjj 7:f6d527a85c05 27 static std::string config_network_pass = "password";
Dengjj 7:f6d527a85c05 28 static uint8_t config_frequency_sub_band = 1;
Dengjj 7:f6d527a85c05 29
Dengjj 7:f6d527a85c05 30 int main()
Dengjj 7:f6d527a85c05 31 {
Dengjj 7:f6d527a85c05 32 int32_t ret, counter;
Dengjj 7:f6d527a85c05 33 char dataBuf[15];
Dengjj 7:f6d527a85c05 34
Dengjj 7:f6d527a85c05 35 pc.baud(PC_BAUDRATE);
Dengjj 7:f6d527a85c05 36 pc.printf("\n\r\n\r\n\r");
Dengjj 7:f6d527a85c05 37 pc.printf("============================================\n\r");
Dengjj 7:f6d527a85c05 38 pc.printf("SiFOX remote card read system!\n\r");
Dengjj 7:f6d527a85c05 39 pc.printf("============================================\n\r");
Dengjj 7:f6d527a85c05 40 pc.printf("PC COM RS232 baudrate: %d \n\r", PC_BAUDRATE);
mfiore 0:09250cd371d2 41
Dengjj 7:f6d527a85c05 42 //*******************************************
Dengjj 7:f6d527a85c05 43 // Configurate Network
Dengjj 7:f6d527a85c05 44 //*******************************************
Dengjj 7:f6d527a85c05 45 ret = configNetwork();
Dengjj 7:f6d527a85c05 46 if(ret != RETURN_OK)
Dengjj 7:f6d527a85c05 47 dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart
mfiore 2:6e2c378339d9 48
Dengjj 7:f6d527a85c05 49 //*******************************************
Dengjj 7:f6d527a85c05 50 // Join Network
Dengjj 7:f6d527a85c05 51 //*******************************************
Dengjj 7:f6d527a85c05 52 ret = joinNetwork();
Dengjj 7:f6d527a85c05 53 if(ret != RETURN_OK)
Dengjj 7:f6d527a85c05 54 dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart
Dengjj 7:f6d527a85c05 55
Dengjj 7:f6d527a85c05 56 counter = 0;
Dengjj 7:f6d527a85c05 57 while(1) {
Dengjj 7:f6d527a85c05 58 sprintf(dataBuf, "%d", counter);
Dengjj 7:f6d527a85c05 59
Dengjj 7:f6d527a85c05 60 /* Send data to gateway */
Dengjj 7:f6d527a85c05 61 send_data(dataBuf);
Dengjj 7:f6d527a85c05 62 /* wait a while */
Dengjj 7:f6d527a85c05 63 wait(10);
Dengjj 7:f6d527a85c05 64
Dengjj 7:f6d527a85c05 65 logInfo("RSSI Last:%d", dot->getRssiStats().last);
Dengjj 7:f6d527a85c05 66 logInfo("SNR Last:%d", dot->getSnrStats().last);
Dengjj 7:f6d527a85c05 67
Dengjj 7:f6d527a85c05 68 counter++;
Dengjj 7:f6d527a85c05 69 }
Dengjj 7:f6d527a85c05 70 }
Dengjj 7:f6d527a85c05 71
Dengjj 7:f6d527a85c05 72
Dengjj 7:f6d527a85c05 73 int configNetwork(void)
Dengjj 7:f6d527a85c05 74 {
Dengjj 7:f6d527a85c05 75 int32_t ret;
Dengjj 7:f6d527a85c05 76
mfiore 0:09250cd371d2 77 dot = mDot::getInstance();
mfiore 2:6e2c378339d9 78
mfiore 2:6e2c378339d9 79 // print library version information
Dengjj 7:f6d527a85c05 80 pc.printf("version: %s\n\r", dot->getId().c_str());
Dengjj 7:f6d527a85c05 81
mfiore 2:6e2c378339d9 82 //*******************************************
mfiore 2:6e2c378339d9 83 // configuration
mfiore 2:6e2c378339d9 84 //*******************************************
mfiore 0:09250cd371d2 85 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 86 dot->resetConfig();
mfiore 0:09250cd371d2 87
Dengjj 7:f6d527a85c05 88 dot->setTxPower(20);
Dengjj 7:f6d527a85c05 89
mfiore 4:36e214ebfa56 90 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
mfiore 0:09250cd371d2 91
mfiore 2:6e2c378339d9 92 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 93 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
Dengjj 7:f6d527a85c05 94
mfiore 4:36e214ebfa56 95 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 96 // 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
mfiore 4:36e214ebfa56 97 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
Dengjj 7:f6d527a85c05 98 //pc.printf("Setting TX frequency band as 868MHz\n\r");
Dengjj 7:f6d527a85c05 99 //if ((ret = dot->setTxFrequency(mDot::FB_868)) != mDot::MDOT_OK){
Dengjj 7:f6d527a85c05 100 // pc.printf("Error:failed to set TX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 101 // return RETURN_ERR;
Dengjj 7:f6d527a85c05 102 //}
Dengjj 7:f6d527a85c05 103 //pc.printf("Setting RX frequency band as 868MHz\n\r");
Dengjj 7:f6d527a85c05 104 //if ((ret = dot->setRxFrequency(mDot::FB_868)) != mDot::MDOT_OK){
Dengjj 7:f6d527a85c05 105 // pc.printf("Error:failed to set RX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 106 // return RETURN_ERR;
Dengjj 7:f6d527a85c05 107 //}
Dengjj 7:f6d527a85c05 108 pc.printf("Setting frequency sub band\n\r");
mfiore 0:09250cd371d2 109 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
Dengjj 7:f6d527a85c05 110 pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 111 return RETURN_ERR;
mfiore 2:6e2c378339d9 112 }
Dengjj 7:f6d527a85c05 113
Dengjj 7:f6d527a85c05 114 pc.printf("Setting network name\n\r");
mfiore 2:6e2c378339d9 115 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
Dengjj 7:f6d527a85c05 116 pc.printf("Error:failed to set network name %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 117 return RETURN_ERR;
mfiore 0:09250cd371d2 118 }
Dengjj 7:f6d527a85c05 119
Dengjj 7:f6d527a85c05 120 pc.printf("Setting network password\n\r");
mfiore 2:6e2c378339d9 121 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
Dengjj 7:f6d527a85c05 122 pc.printf("Error:failed to set network password %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 123 return RETURN_ERR;
mfiore 0:09250cd371d2 124 }
mfiore 4:36e214ebfa56 125
mfiore 4:36e214ebfa56 126 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 127 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 128 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
Dengjj 7:f6d527a85c05 129 pc.printf("Setting TX spreading factor\n\r");
mfiore 4:36e214ebfa56 130 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
Dengjj 7:f6d527a85c05 131 pc.printf("Error:failed to set TX datarate %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 132 return RETURN_ERR;
mfiore 4:36e214ebfa56 133 }
Dengjj 7:f6d527a85c05 134
mfiore 4:36e214ebfa56 135 // request receive confirmation of packets from the gateway
Dengjj 7:f6d527a85c05 136 pc.printf("Enabling ACKs\n\r");
mfiore 4:36e214ebfa56 137 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
Dengjj 7:f6d527a85c05 138 pc.printf("Error:failed to enable ACKs %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 139 return RETURN_OK;
Dengjj 7:f6d527a85c05 140 }
Dengjj 7:f6d527a85c05 141
Dengjj 7:f6d527a85c05 142 // save this configuration to the mDot's NVM
Dengjj 7:f6d527a85c05 143 pc.printf("Saving config\n\r");
Dengjj 7:f6d527a85c05 144 if (! dot->saveConfig()) {
Dengjj 7:f6d527a85c05 145 pc.printf("Error:failed to save configuration\n\r");
mfiore 4:36e214ebfa56 146 }
mfiore 4:36e214ebfa56 147
Dengjj 7:f6d527a85c05 148 return RETURN_OK;
Dengjj 7:f6d527a85c05 149 } // end of configuration
Dengjj 7:f6d527a85c05 150
mfiore 0:09250cd371d2 151
Dengjj 7:f6d527a85c05 152 int joinNetwork(void)
Dengjj 7:f6d527a85c05 153 {
Dengjj 7:f6d527a85c05 154 int32_t ret,i;
Dengjj 7:f6d527a85c05 155 std::vector<uint8_t> sendData;
Dengjj 7:f6d527a85c05 156 char _header[] = "Reset!";
Dengjj 7:f6d527a85c05 157
mfiore 0:09250cd371d2 158 // attempt to join the network
Dengjj 7:f6d527a85c05 159 pc.printf("Joining network...\n\r");
Dengjj 7:f6d527a85c05 160
mfiore 0:09250cd371d2 161 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
Dengjj 7:f6d527a85c05 162 pc.printf("Error: failed to join network %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 163 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
Dengjj 7:f6d527a85c05 164 return RETURN_ERR;
mfiore 0:09250cd371d2 165 }
mfiore 0:09250cd371d2 166
Dengjj 7:f6d527a85c05 167 sendData.clear();
mfiore 0:09250cd371d2 168 // format data for sending to the gateway
Dengjj 7:f6d527a85c05 169 for( i=0; i< strlen(_header); i++ )
Dengjj 7:f6d527a85c05 170 sendData.push_back( _header[i] );
Dengjj 7:f6d527a85c05 171
Dengjj 7:f6d527a85c05 172 // send the data to the gateway
Dengjj 7:f6d527a85c05 173 pc.printf("Send header to the gateway\n\r");
Dengjj 7:f6d527a85c05 174
Dengjj 7:f6d527a85c05 175 if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
Dengjj 7:f6d527a85c05 176 pc.printf("Error: failed to send %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 177 return RETURN_ERR;
Dengjj 7:f6d527a85c05 178 } else {
Dengjj 7:f6d527a85c05 179 pc.printf("Successfully sent data to gateway\n\r");
Dengjj 7:f6d527a85c05 180 }
Dengjj 7:f6d527a85c05 181
Dengjj 7:f6d527a85c05 182 return RETURN_OK;
Dengjj 7:f6d527a85c05 183 }
mfiore 0:09250cd371d2 184
Dengjj 7:f6d527a85c05 185 int send_data(char *str)
Dengjj 7:f6d527a85c05 186 {
Dengjj 7:f6d527a85c05 187 int32_t i, ret;
Dengjj 7:f6d527a85c05 188 std::vector<uint8_t> sendData;
Dengjj 7:f6d527a85c05 189
Dengjj 7:f6d527a85c05 190 //Send the data to Gateway
Dengjj 7:f6d527a85c05 191 sendData.clear();
Dengjj 7:f6d527a85c05 192 // probably not the most efficent way to do this
Dengjj 7:f6d527a85c05 193 for(i=0; i< strlen(str); i++ )
Dengjj 7:f6d527a85c05 194 sendData.push_back(str[i] );
Dengjj 7:f6d527a85c05 195
Dengjj 7:f6d527a85c05 196 // send the data to the gateway
Dengjj 7:f6d527a85c05 197 pc.printf("Send %s to Gateway \n\r", str);
Dengjj 7:f6d527a85c05 198
Dengjj 7:f6d527a85c05 199 if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
Dengjj 7:f6d527a85c05 200 pc.printf("Error:failed to send\n\r", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 7:f6d527a85c05 201 return RETURN_ERR;
Dengjj 7:f6d527a85c05 202 } else {
Dengjj 7:f6d527a85c05 203 pc.printf("Sent data to gateway successfully!\n\r");
Dengjj 7:f6d527a85c05 204 }
Dengjj 7:f6d527a85c05 205 return RETURN_OK;
Dengjj 7:f6d527a85c05 206 }
mfiore 0:09250cd371d2 207
mfiore 0:09250cd371d2 208