SRK Version of mDot LoRa_Sensormode_SRK
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Sensornode by
LoRa.cpp
00001 /* 00002 * LoRa.cpp 00003 * 00004 * Created on: May 31, 2016 00005 * Author: Adrian 00006 */ 00007 00008 #include "LoRa.h " 00009 00010 LoRa::LoRa(mDot* dot,RawSerial* debugSerial) { 00011 this->dot = dot; 00012 this->debugSerial = debugSerial; 00013 this->config = new LoRaConfig(); 00014 this->dot->setActivityLedPin(PA_0); 00015 this->dot->setActivityLedEnable(true); 00016 } 00017 00018 LoRa::~LoRa() { 00019 // TODO Auto-generated destructor stub 00020 } 00021 00022 void LoRa::init(LORA_MODE desiredMode){ 00023 config->build(desiredMode); 00024 00025 resetConfig(); 00026 00027 setNetworkName(); 00028 setNetworkPassphrase(); 00029 setAppEUI(); 00030 setAppKey(); 00031 setFrequencySubBand(); 00032 setPublicNetwork(); 00033 setSpreadingFactor(); 00034 setAckRetries(); 00035 setTxPower(); 00036 setJoinMode(); 00037 00038 saveConfig(); 00039 00040 if(config->isActiv()){ 00041 joinNetwork(); 00042 } 00043 00044 } 00045 00046 int32_t LoRa::setPublicNetwork(){ 00047 int32_t ret; 00048 bool publicity = config->isPublic(); 00049 00050 if ((ret = dot->setPublicNetwork(publicity)) != mDot::MDOT_OK) { 00051 debugSerial->printf("failed to set public network %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00052 } 00053 00054 return ret; 00055 } 00056 00057 int32_t LoRa::setFrequencySubBand(){ 00058 int32_t ret; 00059 uint8_t subBand = config->getFrequencySubBand(); 00060 00061 debugSerial->printf("setting frequency sub band\n"); 00062 00063 if ((ret = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) { 00064 debugSerial->printf("failed to set frequency sub band %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00065 } 00066 00067 return ret; 00068 } 00069 00070 int32_t LoRa::setNetworkName(){ 00071 int32_t ret; 00072 std::string networkName = config->getNetworkName(); 00073 00074 debugSerial->printf("setting network name\n"); 00075 00076 if ((ret = dot->setNetworkName(networkName)) != mDot::MDOT_OK) { 00077 debugSerial->printf("failed to set network name %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00078 } 00079 00080 return ret; 00081 } 00082 00083 int32_t LoRa::setNetworkPassphrase(){ 00084 int32_t ret; 00085 std::string networkPassphrase = config->getNetworkPassphrase(); 00086 debugSerial->printf("setting network password\n"); 00087 00088 if ((ret = dot->setNetworkPassphrase(networkPassphrase)) != mDot::MDOT_OK) { 00089 debugSerial->printf("failed to set network password %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00090 } 00091 00092 return ret; 00093 } 00094 00095 int32_t LoRa::setAppEUI(){ 00096 int32_t ret; 00097 std::vector<uint8_t> appEUI = config->getAppEUI(); 00098 00099 debugSerial->printf("setting App EUI\n"); 00100 00101 if ((ret = dot->setNetworkId(appEUI)) != mDot::MDOT_OK) { 00102 debugSerial->printf("failed to set App EUI %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00103 } 00104 00105 return ret; 00106 00107 } 00108 00109 int32_t LoRa::setAppKey(){ 00110 int32_t ret; 00111 std::vector<uint8_t> appKey = config->getAppKey(); 00112 00113 debugSerial->printf("setting App Key\n"); 00114 00115 if ((ret = dot->setNetworkKey(appKey)) != mDot::MDOT_OK) { 00116 debugSerial->printf("failed to set App Key %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00117 } 00118 00119 return ret; 00120 } 00121 00122 00123 00124 int32_t LoRa::setJoinMode(){ 00125 int32_t ret; 00126 JOIN_MODE joinMode = config->getJOIN_MODE(); 00127 00128 debugSerial->printf("setting join mode\n"); 00129 00130 if(joinMode == OTA){ 00131 if ((ret = dot->setJoinMode(mDot::OTA)) != mDot::MDOT_OK) { 00132 debugSerial->printf("failed to set network address %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00133 } 00134 } 00135 else if(joinMode == ABP){ 00136 if ((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) { 00137 debugSerial->printf("failed to set network address %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00138 } 00139 } 00140 00141 return ret; 00142 } 00143 00144 int32_t LoRa::setSpreadingFactor(){ 00145 int32_t ret; 00146 00147 spreadingFactor = config->getSpreadingFactor(); 00148 mDot::DataRates spreadingFactorMdot; 00149 00150 switch(spreadingFactor){ 00151 case LORA_SPREADING_FACTOR_7: 00152 spreadingFactorMdot = mDot::SF_7; 00153 break; 00154 00155 case LORA_SPREADING_FACTOR_8: 00156 spreadingFactorMdot = mDot::SF_8; 00157 break; 00158 00159 case LORA_SPREADING_FACTOR_9: 00160 spreadingFactorMdot = mDot::SF_9; 00161 break; 00162 00163 case LORA_SPREADING_FACTOR_10: 00164 spreadingFactorMdot = mDot::SF_10; 00165 break; 00166 00167 case LORA_SPREADING_FACTOR_11: 00168 spreadingFactorMdot = mDot::SF_11; 00169 break; 00170 00171 case LORA_SPREADING_FACTOR_12: 00172 spreadingFactorMdot = mDot::SF_12; 00173 break; 00174 00175 default: 00176 spreadingFactorMdot = mDot::SF_12; 00177 break; 00178 } 00179 00180 debugSerial->printf("setting TX spreading factor\n"); 00181 if ((ret = dot->setTxDataRate(spreadingFactorMdot)) != mDot::MDOT_OK) { 00182 debugSerial->printf("failed to set TX datarate %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00183 } 00184 00185 return ret; 00186 } 00187 00188 int32_t LoRa::setTxPower(){ 00189 txPowerdBm = config->getTxPowerdBm(); 00190 dot->setTxPower(txPowerdBm); 00191 } 00192 00193 int32_t LoRa::setAckRetries(){ 00194 int32_t ret; 00195 uint8_t retries = config->getAcknowledgeRetries(); 00196 00197 debugSerial->printf("enabling ACKs\n"); 00198 if ((ret = dot->setAck(retries)) != mDot::MDOT_OK) { 00199 debugSerial->printf("failed to enable ACKs %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00200 } 00201 00202 return ret; 00203 } 00204 00205 void LoRa::resetConfig(){ 00206 dot->resetConfig(); 00207 } 00208 00209 void LoRa::saveConfig(){ 00210 debugSerial->printf("saving config\n"); 00211 if (! dot->saveConfig()) { 00212 debugSerial->printf("failed to save configuration\n"); 00213 } 00214 } 00215 00216 00217 int32_t LoRa::send(std::vector<uint8_t>& data){ 00218 int32_t ret = mDot::MDOT_NOT_JOINED; 00219 00220 if(config->isActiv()){ 00221 ret=dot->send(data); 00222 if (ret != mDot::MDOT_OK) { 00223 debugSerial->printf("failed to send\n"); 00224 debugSerial->printf(mDot::getReturnCodeString(ret).c_str()); 00225 debugSerial->printf("\n"); 00226 } else { 00227 debugSerial->printf("successfully sent data to gateway\n"); 00228 } 00229 } 00230 00231 return ret; 00232 } 00233 00234 int32_t LoRa::recv(std::vector<uint8_t>& data){ 00235 int32_t ret = mDot::MDOT_NOT_JOINED; 00236 if(config->isActiv()){ 00237 ret = dot->recv(data); 00238 } 00239 return ret; 00240 } 00241 00242 int16_t LoRa::getLastRssi(){ 00243 return dot->getRssiStats().last; 00244 } 00245 00246 int16_t LoRa::getLastSnr(){ 00247 return snr; 00248 } 00249 00250 uint8_t LoRa::getSpreadingFactor(){ 00251 return spreadingFactor; 00252 } 00253 00254 uint8_t LoRa::getTxPowerdBm(){ 00255 return txPowerdBm; 00256 } 00257 00258 void LoRa::ping(){ 00259 mDot::ping_response response = dot->ping(); 00260 } 00261 00262 uint32_t LoRa::getNextTxMs(){ 00263 return dot->getNextTxMs(); 00264 } 00265 00266 00267 void LoRa::joinNetwork(){ 00268 int32_t ret; 00269 // attempt to join the network 00270 debugSerial->printf("joining network\n"); 00271 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { 00272 debugSerial->printf("failed to join network %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00273 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00274 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); 00275 } 00276 debugSerial->printf("Network joined\n"); 00277 } 00278
Generated on Wed Jul 13 2022 09:23:47 by 1.7.2