Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 setPublicNetwork(); 00027 setFrequencySubBand(); 00028 setNetworkName(); 00029 setNetworkPassphrase(); 00030 setSpreadingFactor(); 00031 setAckRetries(); 00032 setTxPower(); 00033 00034 saveConfig(); 00035 00036 if(config->isActiv()){ 00037 joinNetwork(); 00038 } 00039 00040 } 00041 00042 int32_t LoRa::setPublicNetwork(){ 00043 int32_t ret; 00044 bool publicity = config->isPublic(); 00045 00046 if ((ret = dot->setPublicNetwork(publicity)) != mDot::MDOT_OK) { 00047 debugSerial->printf("failed to set public network %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00048 } 00049 00050 return ret; 00051 } 00052 00053 int32_t LoRa::setFrequencySubBand(){ 00054 int32_t ret; 00055 uint8_t subBand = config->getFrequencySubBand(); 00056 00057 debugSerial->printf("setting frequency sub band\n"); 00058 00059 if ((ret = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) { 00060 debugSerial->printf("failed to set frequency sub band %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00061 } 00062 00063 return ret; 00064 } 00065 00066 int32_t LoRa::setNetworkName(){ 00067 int32_t ret; 00068 std::string networkName = config->getNetworkName(); 00069 00070 debugSerial->printf("setting network name\n"); 00071 00072 if ((ret = dot->setNetworkName(networkName)) != mDot::MDOT_OK) { 00073 debugSerial->printf("failed to set network name %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00074 } 00075 00076 return ret; 00077 } 00078 00079 int32_t LoRa::setNetworkPassphrase(){ 00080 int32_t ret; 00081 std::string networkPassphrase = config->getNetworkPassphrase(); 00082 debugSerial->printf("setting network password\n"); 00083 00084 if ((ret = dot->setNetworkPassphrase(networkPassphrase)) != mDot::MDOT_OK) { 00085 debugSerial->printf("failed to set network password %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00086 } 00087 00088 return ret; 00089 } 00090 00091 int32_t LoRa::setSpreadingFactor(){ 00092 int32_t ret; 00093 00094 spreadingFactor = config->getSpreadingFactor(); 00095 mDot::DataRates spreadingFactorMdot; 00096 00097 switch(spreadingFactor){ 00098 case LORA_SPREADING_FACTOR_7: 00099 spreadingFactorMdot = mDot::SF_7; 00100 break; 00101 00102 case LORA_SPREADING_FACTOR_8: 00103 spreadingFactorMdot = mDot::SF_8; 00104 break; 00105 00106 case LORA_SPREADING_FACTOR_9: 00107 spreadingFactorMdot = mDot::SF_9; 00108 break; 00109 00110 case LORA_SPREADING_FACTOR_10: 00111 spreadingFactorMdot = mDot::SF_10; 00112 break; 00113 00114 case LORA_SPREADING_FACTOR_11: 00115 spreadingFactorMdot = mDot::SF_11; 00116 break; 00117 00118 case LORA_SPREADING_FACTOR_12: 00119 spreadingFactorMdot = mDot::SF_12; 00120 break; 00121 00122 default: 00123 spreadingFactorMdot = mDot::SF_12; 00124 break; 00125 } 00126 00127 debugSerial->printf("setting TX spreading factor\n"); 00128 if ((ret = dot->setTxDataRate(spreadingFactorMdot)) != mDot::MDOT_OK) { 00129 debugSerial->printf("failed to set TX datarate %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00130 } 00131 00132 return ret; 00133 } 00134 00135 int32_t LoRa::setTxPower(){ 00136 txPowerdBm = config->getTxPowerdBm(); 00137 dot->setTxPower(txPowerdBm); 00138 } 00139 00140 int32_t LoRa::setAckRetries(){ 00141 int32_t ret; 00142 uint8_t retries = config->getAcknowledgeRetries(); 00143 00144 debugSerial->printf("enabling ACKs\n"); 00145 if ((ret = dot->setAck(retries)) != mDot::MDOT_OK) { 00146 debugSerial->printf("failed to enable ACKs %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00147 } 00148 00149 return ret; 00150 } 00151 00152 void LoRa::resetConfig(){ 00153 dot->resetConfig(); 00154 } 00155 00156 void LoRa::saveConfig(){ 00157 debugSerial->printf("saving config\n"); 00158 if (! dot->saveConfig()) { 00159 debugSerial->printf("failed to save configuration\n"); 00160 } 00161 } 00162 00163 00164 int32_t LoRa::send(std::vector<uint8_t>& data){ 00165 int32_t ret = mDot::MDOT_NOT_JOINED; 00166 00167 if(config->isActiv()){ 00168 ret=dot->send(data); 00169 if (ret != mDot::MDOT_OK) { 00170 debugSerial->printf("failed to send\n"); 00171 debugSerial->printf(mDot::getReturnCodeString(ret).c_str()); 00172 debugSerial->printf("\n"); 00173 } else { 00174 debugSerial->printf("successfully sent data to gateway\n"); 00175 } 00176 } 00177 00178 return ret; 00179 } 00180 00181 int32_t LoRa::recv(std::vector<uint8_t>& data){ 00182 int32_t ret = mDot::MDOT_NOT_JOINED; 00183 if(config->isActiv()){ 00184 ret = dot->recv(data); 00185 } 00186 return ret; 00187 } 00188 00189 int16_t LoRa::getLastRssi(){ 00190 return dot->getRssiStats().last; 00191 } 00192 00193 int16_t LoRa::getLastSnr(){ 00194 return snr; 00195 } 00196 00197 uint8_t LoRa::getSpreadingFactor(){ 00198 return spreadingFactor; 00199 } 00200 00201 uint8_t LoRa::getTxPowerdBm(){ 00202 return txPowerdBm; 00203 } 00204 00205 void LoRa::ping(){ 00206 mDot::ping_response response = dot->ping(); 00207 } 00208 00209 00210 void LoRa::joinNetwork(){ 00211 int32_t ret; 00212 // attempt to join the network 00213 debugSerial->printf("joining network\n"); 00214 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { 00215 debugSerial->printf("failed to join network %d:%s\n", ret, mDot::getReturnCodeString(ret).c_str()); 00216 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00217 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); 00218 } 00219 debugSerial->printf("Network joined\n"); 00220 } 00221
Generated on Tue Jul 12 2022 20:10:02 by
1.7.2
