a Hello world to connect to The Things Network
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
main.cpp@0:09250cd371d2, 2015-06-24 (annotated)
- Committer:
- mfiore
- Date:
- Wed Jun 24 22:22:07 2015 +0000
- Revision:
- 0:09250cd371d2
- Child:
- 2:6e2c378339d9
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:09250cd371d2 | 1 | #include "mbed.h" |
mfiore | 0:09250cd371d2 | 2 | #include "mDot.h" |
mfiore | 0:09250cd371d2 | 3 | #include <string> |
mfiore | 0:09250cd371d2 | 4 | #include <vector> |
mfiore | 0:09250cd371d2 | 5 | |
mfiore | 0:09250cd371d2 | 6 | // these options must match the settings on your Conduit in |
mfiore | 0:09250cd371d2 | 7 | // /var/config/lora/lora-network-server.conf |
mfiore | 0:09250cd371d2 | 8 | static std::string config_network_name = "<lora network id>"; |
mfiore | 0:09250cd371d2 | 9 | static std::string config_network_pass = "<lora network key>"; |
mfiore | 0:09250cd371d2 | 10 | static uint8_t config_frequency_sub_band = 1; |
mfiore | 0:09250cd371d2 | 11 | |
mfiore | 0:09250cd371d2 | 12 | void log_error(mDot* dot, const char* msg, int32_t retval); |
mfiore | 0:09250cd371d2 | 13 | |
mfiore | 0:09250cd371d2 | 14 | int main() { |
mfiore | 0:09250cd371d2 | 15 | int32_t ret; |
mfiore | 0:09250cd371d2 | 16 | mDot* dot; |
mfiore | 0:09250cd371d2 | 17 | std::vector<uint8_t> data; |
mfiore | 0:09250cd371d2 | 18 | std::string data_str = "hello world!"; |
mfiore | 0:09250cd371d2 | 19 | |
mfiore | 0:09250cd371d2 | 20 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 21 | dot = mDot::getInstance(); |
mfiore | 0:09250cd371d2 | 22 | |
mfiore | 0:09250cd371d2 | 23 | // reset to default config so we know what state we're in |
mfiore | 0:09250cd371d2 | 24 | dot->resetConfig(); |
mfiore | 0:09250cd371d2 | 25 | |
mfiore | 0:09250cd371d2 | 26 | // print library version information |
mfiore | 0:09250cd371d2 | 27 | printf("version: %s\r\n", dot->getId().c_str()); |
mfiore | 0:09250cd371d2 | 28 | |
mfiore | 0:09250cd371d2 | 29 | // set up the mDot with our network information |
mfiore | 0:09250cd371d2 | 30 | printf("setting frequency sub band\r\n"); |
mfiore | 0:09250cd371d2 | 31 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 0:09250cd371d2 | 32 | log_error(dot, "failed to set frequency sub band", ret); |
mfiore | 0:09250cd371d2 | 33 | } |
mfiore | 0:09250cd371d2 | 34 | printf("setting network name\r\n"); |
mfiore | 0:09250cd371d2 | 35 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 0:09250cd371d2 | 36 | log_error(dot, "failed to set network name", ret); |
mfiore | 0:09250cd371d2 | 37 | } |
mfiore | 0:09250cd371d2 | 38 | printf("setting network password\r\n"); |
mfiore | 0:09250cd371d2 | 39 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 0:09250cd371d2 | 40 | log_error(dot, "failed to set network password", ret); |
mfiore | 0:09250cd371d2 | 41 | } |
mfiore | 0:09250cd371d2 | 42 | |
mfiore | 0:09250cd371d2 | 43 | // attempt to join the network |
mfiore | 0:09250cd371d2 | 44 | printf("joining network\r\n"); |
mfiore | 0:09250cd371d2 | 45 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 0:09250cd371d2 | 46 | log_error(dot, "failed to join network", ret); |
mfiore | 0:09250cd371d2 | 47 | wait(2); |
mfiore | 0:09250cd371d2 | 48 | } |
mfiore | 0:09250cd371d2 | 49 | |
mfiore | 0:09250cd371d2 | 50 | // format data for sending to the gateway |
mfiore | 0:09250cd371d2 | 51 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
mfiore | 0:09250cd371d2 | 52 | data.push_back((uint8_t) *it); |
mfiore | 0:09250cd371d2 | 53 | |
mfiore | 0:09250cd371d2 | 54 | while (true) { |
mfiore | 0:09250cd371d2 | 55 | // send the data |
mfiore | 0:09250cd371d2 | 56 | // ACKs are enabled by default, so we're expecting to get one back |
mfiore | 0:09250cd371d2 | 57 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 0:09250cd371d2 | 58 | log_error(dot, "failed to send", ret); |
mfiore | 0:09250cd371d2 | 59 | } else { |
mfiore | 0:09250cd371d2 | 60 | printf("successfully sent data to gateway\r\n"); |
mfiore | 0:09250cd371d2 | 61 | } |
mfiore | 0:09250cd371d2 | 62 | |
mfiore | 0:09250cd371d2 | 63 | wait(5); |
mfiore | 0:09250cd371d2 | 64 | } |
mfiore | 0:09250cd371d2 | 65 | |
mfiore | 0:09250cd371d2 | 66 | return 0; |
mfiore | 0:09250cd371d2 | 67 | } |
mfiore | 0:09250cd371d2 | 68 | |
mfiore | 0:09250cd371d2 | 69 | void log_error(mDot* dot, const char* msg, int32_t retval) { |
mfiore | 0:09250cd371d2 | 70 | printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); |
mfiore | 0:09250cd371d2 | 71 | } |