Handle LoRa Basic Function
Lora.cpp@0:f8af47d9f0cd, 2017-03-08 (annotated)
- Committer:
- biwa1400
- Date:
- Wed Mar 08 18:54:32 2017 +0000
- Revision:
- 0:f8af47d9f0cd
20170308
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
biwa1400 | 0:f8af47d9f0cd | 1 | #include "Lora.h" |
biwa1400 | 0:f8af47d9f0cd | 2 | #include "mbed.h" |
biwa1400 | 0:f8af47d9f0cd | 3 | #include "mDot.h" |
biwa1400 | 0:f8af47d9f0cd | 4 | #include "MTSLog.h" |
biwa1400 | 0:f8af47d9f0cd | 5 | #include <string> |
biwa1400 | 0:f8af47d9f0cd | 6 | #include <vector> |
biwa1400 | 0:f8af47d9f0cd | 7 | |
biwa1400 | 0:f8af47d9f0cd | 8 | Lora::Lora() |
biwa1400 | 0:f8af47d9f0cd | 9 | { |
biwa1400 | 0:f8af47d9f0cd | 10 | // get a mDot handle |
biwa1400 | 0:f8af47d9f0cd | 11 | dot = mDot::getInstance(); |
biwa1400 | 0:f8af47d9f0cd | 12 | logInfo("version: %s", dot->getId().c_str()); |
biwa1400 | 0:f8af47d9f0cd | 13 | //joinNetwork(); |
biwa1400 | 0:f8af47d9f0cd | 14 | } |
biwa1400 | 0:f8af47d9f0cd | 15 | |
biwa1400 | 0:f8af47d9f0cd | 16 | |
biwa1400 | 0:f8af47d9f0cd | 17 | |
biwa1400 | 0:f8af47d9f0cd | 18 | bool Lora::setting(string config_network_name, string config_network_pass,uint8_t config_frequency_sub_band,uint8_t config_ACK) |
biwa1400 | 0:f8af47d9f0cd | 19 | { |
biwa1400 | 0:f8af47d9f0cd | 20 | int32_t ret; |
biwa1400 | 0:f8af47d9f0cd | 21 | bool state = true; |
biwa1400 | 0:f8af47d9f0cd | 22 | //******************************************* |
biwa1400 | 0:f8af47d9f0cd | 23 | // configuration |
biwa1400 | 0:f8af47d9f0cd | 24 | //******************************************* |
biwa1400 | 0:f8af47d9f0cd | 25 | // reset to default config so we know what state we're in |
biwa1400 | 0:f8af47d9f0cd | 26 | dot->resetConfig(); |
biwa1400 | 0:f8af47d9f0cd | 27 | |
biwa1400 | 0:f8af47d9f0cd | 28 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
biwa1400 | 0:f8af47d9f0cd | 29 | |
biwa1400 | 0:f8af47d9f0cd | 30 | // set up the mDot with our network information: frequency sub band, network name, and network password |
biwa1400 | 0:f8af47d9f0cd | 31 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
biwa1400 | 0:f8af47d9f0cd | 32 | |
biwa1400 | 0:f8af47d9f0cd | 33 | // frequency sub band is only applicable in the 915 (US) frequency band |
biwa1400 | 0:f8af47d9f0cd | 34 | // 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 |
biwa1400 | 0:f8af47d9f0cd | 35 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
biwa1400 | 0:f8af47d9f0cd | 36 | |
biwa1400 | 0:f8af47d9f0cd | 37 | logInfo("setting frequency sub band"); |
biwa1400 | 0:f8af47d9f0cd | 38 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 39 | { |
biwa1400 | 0:f8af47d9f0cd | 40 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 41 | state = false; |
biwa1400 | 0:f8af47d9f0cd | 42 | } |
biwa1400 | 0:f8af47d9f0cd | 43 | |
biwa1400 | 0:f8af47d9f0cd | 44 | logInfo("setting network name"); |
biwa1400 | 0:f8af47d9f0cd | 45 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 46 | { |
biwa1400 | 0:f8af47d9f0cd | 47 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 48 | state = false; |
biwa1400 | 0:f8af47d9f0cd | 49 | } |
biwa1400 | 0:f8af47d9f0cd | 50 | |
biwa1400 | 0:f8af47d9f0cd | 51 | logInfo("setting network password"); |
biwa1400 | 0:f8af47d9f0cd | 52 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 53 | { |
biwa1400 | 0:f8af47d9f0cd | 54 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 55 | state = false; |
biwa1400 | 0:f8af47d9f0cd | 56 | } |
biwa1400 | 0:f8af47d9f0cd | 57 | |
biwa1400 | 0:f8af47d9f0cd | 58 | // a higher spreading factor allows for longer range but lower throughput |
biwa1400 | 0:f8af47d9f0cd | 59 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
biwa1400 | 0:f8af47d9f0cd | 60 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
biwa1400 | 0:f8af47d9f0cd | 61 | logInfo("setting TX spreading factor"); |
biwa1400 | 0:f8af47d9f0cd | 62 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 63 | { |
biwa1400 | 0:f8af47d9f0cd | 64 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 65 | state = false; |
biwa1400 | 0:f8af47d9f0cd | 66 | } |
biwa1400 | 0:f8af47d9f0cd | 67 | |
biwa1400 | 0:f8af47d9f0cd | 68 | // request receive confirmation of packets from the gateway |
biwa1400 | 0:f8af47d9f0cd | 69 | logInfo("enabling ACKs"); |
biwa1400 | 0:f8af47d9f0cd | 70 | if ((ret = dot->setAck(config_ACK)) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 71 | { |
biwa1400 | 0:f8af47d9f0cd | 72 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 73 | state = false; |
biwa1400 | 0:f8af47d9f0cd | 74 | } |
biwa1400 | 0:f8af47d9f0cd | 75 | |
biwa1400 | 0:f8af47d9f0cd | 76 | // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping |
biwa1400 | 0:f8af47d9f0cd | 77 | logInfo("setting join mode to AUTO_OTA"); |
biwa1400 | 0:f8af47d9f0cd | 78 | if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
biwa1400 | 0:f8af47d9f0cd | 79 | logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 80 | } |
biwa1400 | 0:f8af47d9f0cd | 81 | |
biwa1400 | 0:f8af47d9f0cd | 82 | // save this configuration to the mDot's NVM |
biwa1400 | 0:f8af47d9f0cd | 83 | logInfo("saving config"); |
biwa1400 | 0:f8af47d9f0cd | 84 | if (! dot->saveConfig()) |
biwa1400 | 0:f8af47d9f0cd | 85 | { |
biwa1400 | 0:f8af47d9f0cd | 86 | logError("failed to save configuration"); |
biwa1400 | 0:f8af47d9f0cd | 87 | state = false; |
biwa1400 | 0:f8af47d9f0cd | 88 | } |
biwa1400 | 0:f8af47d9f0cd | 89 | //******************************************* |
biwa1400 | 0:f8af47d9f0cd | 90 | // end of configuration |
biwa1400 | 0:f8af47d9f0cd | 91 | //******************************************* |
biwa1400 | 0:f8af47d9f0cd | 92 | return state; |
biwa1400 | 0:f8af47d9f0cd | 93 | } |
biwa1400 | 0:f8af47d9f0cd | 94 | |
biwa1400 | 0:f8af47d9f0cd | 95 | bool Lora::joinNetwork() |
biwa1400 | 0:f8af47d9f0cd | 96 | { |
biwa1400 | 0:f8af47d9f0cd | 97 | int32_t ret; |
biwa1400 | 0:f8af47d9f0cd | 98 | // attempt to join the network |
biwa1400 | 0:f8af47d9f0cd | 99 | if (!dot->getNetworkJoinStatus()) |
biwa1400 | 0:f8af47d9f0cd | 100 | { |
biwa1400 | 0:f8af47d9f0cd | 101 | logInfo("joining network"); |
biwa1400 | 0:f8af47d9f0cd | 102 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 103 | { |
biwa1400 | 0:f8af47d9f0cd | 104 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 105 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
biwa1400 | 0:f8af47d9f0cd | 106 | osDelay((uint32_t)dot->getNextTxMs()); |
biwa1400 | 0:f8af47d9f0cd | 107 | } |
biwa1400 | 0:f8af47d9f0cd | 108 | } |
biwa1400 | 0:f8af47d9f0cd | 109 | |
biwa1400 | 0:f8af47d9f0cd | 110 | return true; |
biwa1400 | 0:f8af47d9f0cd | 111 | } |
biwa1400 | 0:f8af47d9f0cd | 112 | |
biwa1400 | 0:f8af47d9f0cd | 113 | bool Lora::sendData_vector(const std::vector<uint8_t>& data) |
biwa1400 | 0:f8af47d9f0cd | 114 | { |
biwa1400 | 0:f8af47d9f0cd | 115 | int32_t ret; |
biwa1400 | 0:f8af47d9f0cd | 116 | if ((ret = dot->send(data)) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 117 | { |
biwa1400 | 0:f8af47d9f0cd | 118 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 119 | return false; |
biwa1400 | 0:f8af47d9f0cd | 120 | } |
biwa1400 | 0:f8af47d9f0cd | 121 | else |
biwa1400 | 0:f8af47d9f0cd | 122 | { |
biwa1400 | 0:f8af47d9f0cd | 123 | logInfo("successfully sent data to gateway"); |
biwa1400 | 0:f8af47d9f0cd | 124 | return true; |
biwa1400 | 0:f8af47d9f0cd | 125 | } |
biwa1400 | 0:f8af47d9f0cd | 126 | } |
biwa1400 | 0:f8af47d9f0cd | 127 | |
biwa1400 | 0:f8af47d9f0cd | 128 | bool Lora::sendData_string(std::string& input_data) |
biwa1400 | 0:f8af47d9f0cd | 129 | { |
biwa1400 | 0:f8af47d9f0cd | 130 | int32_t ret; |
biwa1400 | 0:f8af47d9f0cd | 131 | vector<uint8_t> sent_data; |
biwa1400 | 0:f8af47d9f0cd | 132 | for (std::string::iterator it = input_data.begin(); it != input_data.end(); it++) |
biwa1400 | 0:f8af47d9f0cd | 133 | { |
biwa1400 | 0:f8af47d9f0cd | 134 | sent_data.push_back((uint8_t) *it); |
biwa1400 | 0:f8af47d9f0cd | 135 | } |
biwa1400 | 0:f8af47d9f0cd | 136 | |
biwa1400 | 0:f8af47d9f0cd | 137 | if ((ret = dot->send(sent_data)) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 138 | { |
biwa1400 | 0:f8af47d9f0cd | 139 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 140 | } |
biwa1400 | 0:f8af47d9f0cd | 141 | else |
biwa1400 | 0:f8af47d9f0cd | 142 | { |
biwa1400 | 0:f8af47d9f0cd | 143 | logInfo("successfully sent data to gateway"); |
biwa1400 | 0:f8af47d9f0cd | 144 | } |
biwa1400 | 0:f8af47d9f0cd | 145 | return true; |
biwa1400 | 0:f8af47d9f0cd | 146 | |
biwa1400 | 0:f8af47d9f0cd | 147 | } |
biwa1400 | 0:f8af47d9f0cd | 148 | |
biwa1400 | 0:f8af47d9f0cd | 149 | bool Lora::receiveData_string(std::string& output_data) |
biwa1400 | 0:f8af47d9f0cd | 150 | { |
biwa1400 | 0:f8af47d9f0cd | 151 | int32_t ret; |
biwa1400 | 0:f8af47d9f0cd | 152 | vector<uint8_t> receive_data; |
biwa1400 | 0:f8af47d9f0cd | 153 | if ((ret = dot->recv(receive_data)) != mDot::MDOT_OK) |
biwa1400 | 0:f8af47d9f0cd | 154 | { |
biwa1400 | 0:f8af47d9f0cd | 155 | logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
biwa1400 | 0:f8af47d9f0cd | 156 | return false; |
biwa1400 | 0:f8af47d9f0cd | 157 | } |
biwa1400 | 0:f8af47d9f0cd | 158 | else |
biwa1400 | 0:f8af47d9f0cd | 159 | { |
biwa1400 | 0:f8af47d9f0cd | 160 | string str(receive_data.begin(),receive_data.end()); |
biwa1400 | 0:f8af47d9f0cd | 161 | output_data=str; |
biwa1400 | 0:f8af47d9f0cd | 162 | } |
biwa1400 | 0:f8af47d9f0cd | 163 | return true; |
biwa1400 | 0:f8af47d9f0cd | 164 | |
biwa1400 | 0:f8af47d9f0cd | 165 | } |