MultiTech mDot Test Application for Loriot.io LoRaWAN Cloud service
Dependencies: libmDot mbed-rtos mbed-src
Fork of mDot_test by
main.cpp@9:f3629c43fd23, 2016-03-28 (annotated)
- Committer:
- aheadley1967
- Date:
- Mon Mar 28 22:40:34 2016 +0000
- Revision:
- 9:f3629c43fd23
- Parent:
- 8:4b47a8973f91
mDot Test using Loriot.io LoRaWAN Cloud Server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:e17e5a07892d | 1 | #include "mbed.h" |
mfiore | 0:e17e5a07892d | 2 | #include "mDot.h" |
mfiore | 0:e17e5a07892d | 3 | #include "MTSLog.h" |
mfiore | 0:e17e5a07892d | 4 | #include "MTSText.h" |
mfiore | 0:e17e5a07892d | 5 | #include <string> |
mfiore | 0:e17e5a07892d | 6 | #include <vector> |
mfiore | 0:e17e5a07892d | 7 | |
mfiore | 0:e17e5a07892d | 8 | using namespace mts; |
mfiore | 0:e17e5a07892d | 9 | |
jknapp_smtc | 4:ee6bf074135c | 10 | /* ********************************************** */ |
jknapp_smtc | 4:ee6bf074135c | 11 | /* Create AppEUI identifier and Assigned AppKey */ |
jknapp_smtc | 4:ee6bf074135c | 12 | /* ********************************************** */ |
jknapp_smtc | 4:ee6bf074135c | 13 | |
aheadley1967 | 9:f3629c43fd23 | 14 | #define USE_OTAA 0 // 1 for OtAA and 0 for Personalized |
jknapp_smtc | 8:4b47a8973f91 | 15 | |
aheadley1967 | 9:f3629c43fd23 | 16 | static const uint8_t DEVKEY[16] = { 0x3D, 0x4B, 0x87, 0x8B, 0x56, 0xEA, 0xA8, 0x1D, 0xA9, 0x7F, 0xB1, 0x9E, 0x69, 0xF8, 0x23, 0x6B }; // Normal |
aheadley1967 | 9:f3629c43fd23 | 17 | static const uint8_t APPEUI[8] = { 0xBE, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x02, 0x83 }; // SMTC AppEUI - Most Significant Byte First |
jknapp_smtc | 7:4c9cead936d8 | 18 | |
jknapp_smtc | 7:4c9cead936d8 | 19 | //DevAddr 01DC266E |
jknapp_smtc | 7:4c9cead936d8 | 20 | //NWKSKEY C7 58 CB 09 BD F2 C7 0E 5F 4C FA 5B 7A 96 68 D8 |
jknapp_smtc | 7:4c9cead936d8 | 21 | //APPSKEY AC B3 B3 A1 B3 26 25 F7 24 3F B4 39 65 06 38 A7 |
aheadley1967 | 9:f3629c43fd23 | 22 | static const uint8_t DEV_ADDR[4] = { 0x01, 0xDC, 0x26, 0x6E }; |
aheadley1967 | 9:f3629c43fd23 | 23 | static const uint8_t NETSKEY[16] = { 0xC7, 0x58, 0xCB, 0x09, 0xBD, 0xF2, 0xC7, 0x0E, 0x5F, 0x4C, 0xFA, 0x5B, 0x7A, 0x96, 0x68, 0xD8 }; // Normal |
aheadley1967 | 9:f3629c43fd23 | 24 | static const uint8_t APPSKEY[16] = { 0xAC, 0xB3, 0xB3, 0xA1, 0xB3, 0x26, 0x25, 0xF7, 0x24, 0x3F, 0xB4, 0x39, 0x65, 0x06, 0x38, 0xA7 }; // Normal |
jknapp_smtc | 8:4b47a8973f91 | 25 | |
jknapp_smtc | 8:4b47a8973f91 | 26 | //DevAddr 01DC266E |
jknapp_smtc | 8:4b47a8973f91 | 27 | //NWKSKEY 2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c |
jknapp_smtc | 8:4b47a8973f91 | 28 | //APPSKEY 2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c |
aheadley1967 | 9:f3629c43fd23 | 29 | //static const uint8_t DEV_ADDR[4] = { 0x01, 0xDC, 0x26, 0x6E }; |
aheadley1967 | 9:f3629c43fd23 | 30 | //static const uint8_t NETSKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; // Normal |
aheadley1967 | 9:f3629c43fd23 | 31 | //static const uint8_t APPSKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; // Normal |
jknapp_smtc | 7:4c9cead936d8 | 32 | |
jknapp_smtc | 7:4c9cead936d8 | 33 | static uint8_t config_frequency_sub_band = 1; // 0 = Enable all channels, 1 = 1st eight channels |
jknapp_smtc | 4:ee6bf074135c | 34 | |
jknapp_smtc | 4:ee6bf074135c | 35 | /* ********************************************** */ |
jknapp_smtc | 3:7cc5dfd22b54 | 36 | |
jknapp_smtc | 3:7cc5dfd22b54 | 37 | static std::vector<uint8_t> AppKey (DEVKEY, DEVKEY + sizeof(DEVKEY)/sizeof(uint8_t) ); |
jknapp_smtc | 3:7cc5dfd22b54 | 38 | static std::vector<uint8_t> AppEUI (APPEUI, APPEUI + sizeof(APPEUI)/sizeof(uint8_t) ); |
jknapp_smtc | 3:7cc5dfd22b54 | 39 | |
jknapp_smtc | 7:4c9cead936d8 | 40 | static std::vector<uint8_t> DevAddr (DEV_ADDR, DEV_ADDR + sizeof(DEV_ADDR)/sizeof(uint8_t) ); |
jknapp_smtc | 7:4c9cead936d8 | 41 | static std::vector<uint8_t> NetSKey (NETSKEY, NETSKEY + sizeof(NETSKEY)/sizeof(uint8_t) ); |
jknapp_smtc | 7:4c9cead936d8 | 42 | static std::vector<uint8_t> AppSKey (APPSKEY, APPSKEY + sizeof(APPSKEY)/sizeof(uint8_t) ); |
jknapp_smtc | 7:4c9cead936d8 | 43 | |
jknapp_smtc | 7:4c9cead936d8 | 44 | |
jknapp_smtc | 7:4c9cead936d8 | 45 | |
jknapp_smtc | 3:7cc5dfd22b54 | 46 | InterruptIn button(PB_1); |
jknapp_smtc | 3:7cc5dfd22b54 | 47 | DigitalOut drive_high(PB_0); |
jknapp_smtc | 3:7cc5dfd22b54 | 48 | DigitalIn door_open(PA_7); |
jknapp_smtc | 3:7cc5dfd22b54 | 49 | |
jknapp_smtc | 3:7cc5dfd22b54 | 50 | volatile bool msg_rdy; |
jknapp_smtc | 3:7cc5dfd22b54 | 51 | |
jknapp_smtc | 3:7cc5dfd22b54 | 52 | void queue_message(){ |
jknapp_smtc | 3:7cc5dfd22b54 | 53 | msg_rdy = true; |
jknapp_smtc | 3:7cc5dfd22b54 | 54 | } |
jknapp_smtc | 3:7cc5dfd22b54 | 55 | |
mfiore | 0:e17e5a07892d | 56 | |
mfiore | 0:e17e5a07892d | 57 | int main() { |
mfiore | 0:e17e5a07892d | 58 | Serial debug(USBTX, USBRX); |
aheadley1967 | 9:f3629c43fd23 | 59 | debug.baud(115200); |
jknapp_smtc | 3:7cc5dfd22b54 | 60 | drive_high = 1; |
mfiore | 0:e17e5a07892d | 61 | |
jknapp_smtc | 3:7cc5dfd22b54 | 62 | msg_rdy = false; |
mfiore | 0:e17e5a07892d | 63 | int32_t ret; |
mfiore | 0:e17e5a07892d | 64 | int32_t next_tx; |
mfiore | 2:aadbdfb6d517 | 65 | int32_t wait_time = 2; |
mfiore | 0:e17e5a07892d | 66 | mDot* dot; |
mfiore | 0:e17e5a07892d | 67 | std::vector<uint8_t> send_data; |
mfiore | 0:e17e5a07892d | 68 | std::vector<uint8_t> recv_data; |
mfiore | 0:e17e5a07892d | 69 | uint8_t recv = 0; |
mfiore | 0:e17e5a07892d | 70 | uint8_t recv_mismatch = 0; |
mfiore | 0:e17e5a07892d | 71 | uint8_t send_failure = 0; |
mfiore | 0:e17e5a07892d | 72 | uint8_t iterations = 50; |
jknapp_smtc | 3:7cc5dfd22b54 | 73 | button.rise(&queue_message); |
mfiore | 0:e17e5a07892d | 74 | |
jknapp_smtc | 3:7cc5dfd22b54 | 75 | send_data.push_back(0x01); |
jknapp_smtc | 3:7cc5dfd22b54 | 76 | send_data.push_back(0x02); |
mfiore | 0:e17e5a07892d | 77 | |
mfiore | 0:e17e5a07892d | 78 | dot = mDot::getInstance(); |
mfiore | 0:e17e5a07892d | 79 | |
mfiore | 0:e17e5a07892d | 80 | dot->resetConfig(); |
mfiore | 1:8295b8c0d802 | 81 | |
mfiore | 1:8295b8c0d802 | 82 | dot->setLogLevel(MTSLog::TRACE_LEVEL); |
jknapp_smtc | 3:7cc5dfd22b54 | 83 | //dot->setJoinByteOrder(1); // MSB |
jknapp_smtc | 3:7cc5dfd22b54 | 84 | dot->setTxDataRate(mDot::SF_10); |
jknapp_smtc | 3:7cc5dfd22b54 | 85 | //dot->setTxPower(20); |
jknapp_smtc | 3:7cc5dfd22b54 | 86 | |
mfiore | 0:e17e5a07892d | 87 | while ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 0:e17e5a07892d | 88 | logError("failed to set frequency sub band: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:e17e5a07892d | 89 | } |
jknapp_smtc | 3:7cc5dfd22b54 | 90 | while ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
jknapp_smtc | 3:7cc5dfd22b54 | 91 | logError("failed to set Public Network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:e17e5a07892d | 92 | } |
jknapp_smtc | 3:7cc5dfd22b54 | 93 | /** Set network ID |
jknapp_smtc | 3:7cc5dfd22b54 | 94 | * for use with OTA & AUTO_OTA network join modes |
jknapp_smtc | 3:7cc5dfd22b54 | 95 | * setting network ID via this function sets network name to empty |
jknapp_smtc | 3:7cc5dfd22b54 | 96 | * @param id a vector of 8 bytes |
jknapp_smtc | 3:7cc5dfd22b54 | 97 | * @returns MDOT_OK if success |
jknapp_smtc | 3:7cc5dfd22b54 | 98 | */ |
jknapp_smtc | 8:4b47a8973f91 | 99 | #if USE_OTAA == 1 |
jknapp_smtc | 8:4b47a8973f91 | 100 | while ((ret = dot->setNetworkId(AppEUI)) != mDot::MDOT_OK) { |
jknapp_smtc | 8:4b47a8973f91 | 101 | logError("failed to set AppEUI: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
jknapp_smtc | 8:4b47a8973f91 | 102 | } |
jknapp_smtc | 8:4b47a8973f91 | 103 | while ((ret = dot->setNetworkKey(AppKey)) != mDot::MDOT_OK) { |
aheadley1967 | 9:f3629c43fd23 | 104 | logError("failed to set AppKey: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
jknapp_smtc | 8:4b47a8973f91 | 105 | } |
jknapp_smtc | 8:4b47a8973f91 | 106 | dot->setJoinMode( dot->OTA ); // 0 = Manual and 1 = OTA |
jknapp_smtc | 8:4b47a8973f91 | 107 | #else |
jknapp_smtc | 7:4c9cead936d8 | 108 | while ((ret = dot->setNetworkAddress(DevAddr)) != mDot::MDOT_OK) { |
jknapp_smtc | 7:4c9cead936d8 | 109 | logError("failed to set DevAddr: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
jknapp_smtc | 3:7cc5dfd22b54 | 110 | } |
aheadley1967 | 9:f3629c43fd23 | 111 | while ((ret = dot->setNetworkKey(AppKey)) != mDot::MDOT_OK) { |
aheadley1967 | 9:f3629c43fd23 | 112 | logError("failed to set AppKey: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
aheadley1967 | 9:f3629c43fd23 | 113 | } |
jknapp_smtc | 7:4c9cead936d8 | 114 | while ((ret = dot->setNetworkSessionKey(NetSKey)) != mDot::MDOT_OK) { |
jknapp_smtc | 7:4c9cead936d8 | 115 | logError("failed to set NetworkSessionKey: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
jknapp_smtc | 7:4c9cead936d8 | 116 | } |
jknapp_smtc | 7:4c9cead936d8 | 117 | while ((ret = dot->setDataSessionKey(AppSKey)) != mDot::MDOT_OK) { |
jknapp_smtc | 7:4c9cead936d8 | 118 | logError("failed to set App Session Key: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:e17e5a07892d | 119 | } |
jknapp_smtc | 8:4b47a8973f91 | 120 | dot->setJoinMode( dot->MANUAL ); // 0 = Manual and 1 = OtAA |
jknapp_smtc | 8:4b47a8973f91 | 121 | #endif |
mfiore | 0:e17e5a07892d | 122 | |
mfiore | 2:aadbdfb6d517 | 123 | logInfo("enabling activity LED"); |
mfiore | 2:aadbdfb6d517 | 124 | dot->setActivityLedEnable(true); |
mfiore | 2:aadbdfb6d517 | 125 | |
mfiore | 0:e17e5a07892d | 126 | logInfo("joining network"); |
mfiore | 0:e17e5a07892d | 127 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 0:e17e5a07892d | 128 | logError("failed to join network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:e17e5a07892d | 129 | wait_ms(dot->getNextTxMs() + 1); |
mfiore | 0:e17e5a07892d | 130 | } |
mfiore | 0:e17e5a07892d | 131 | logInfo("joined"); |
mfiore | 0:e17e5a07892d | 132 | |
jknapp_smtc | 7:4c9cead936d8 | 133 | //dot->setAck(3); // Use Confirmed frames and try three times |
jknapp_smtc | 3:7cc5dfd22b54 | 134 | |
jknapp_smtc | 3:7cc5dfd22b54 | 135 | while (1) { |
jknapp_smtc | 3:7cc5dfd22b54 | 136 | |
jknapp_smtc | 3:7cc5dfd22b54 | 137 | send_data[0] = 192; //door_open; |
mfiore | 0:e17e5a07892d | 138 | if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { |
mfiore | 0:e17e5a07892d | 139 | logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:e17e5a07892d | 140 | send_failure++; |
mfiore | 0:e17e5a07892d | 141 | } else { |
mfiore | 0:e17e5a07892d | 142 | logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); |
mfiore | 0:e17e5a07892d | 143 | if ((ret = dot->recv(recv_data)) != mDot::MDOT_OK) { |
mfiore | 0:e17e5a07892d | 144 | logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:e17e5a07892d | 145 | } else { |
mfiore | 0:e17e5a07892d | 146 | logInfo("recv data: %s", Text::bin2hexString(recv_data).c_str()); |
jknapp_smtc | 3:7cc5dfd22b54 | 147 | if (recv_data[0] == 0x80) { |
mfiore | 0:e17e5a07892d | 148 | recv++; |
jknapp_smtc | 3:7cc5dfd22b54 | 149 | } else if (recv_data[0] == 0xFF) { |
jknapp_smtc | 3:7cc5dfd22b54 | 150 | goto END; |
mfiore | 0:e17e5a07892d | 151 | } else { |
mfiore | 0:e17e5a07892d | 152 | recv_mismatch++; |
mfiore | 0:e17e5a07892d | 153 | } |
mfiore | 0:e17e5a07892d | 154 | } |
mfiore | 0:e17e5a07892d | 155 | recv_data.clear(); |
mfiore | 0:e17e5a07892d | 156 | } |
jknapp_smtc | 6:b1afa2592e36 | 157 | msg_rdy = true; // Set to 'true' for free running. |
jknapp_smtc | 5:25114cb5e9a7 | 158 | wait(wait_time); |
jknapp_smtc | 3:7cc5dfd22b54 | 159 | while (!msg_rdy) wait(wait_time); |
mfiore | 0:e17e5a07892d | 160 | } |
jknapp_smtc | 3:7cc5dfd22b54 | 161 | END: |
mfiore | 0:e17e5a07892d | 162 | logInfo("Version: %s", dot->getId().c_str()); |
mfiore | 0:e17e5a07892d | 163 | logInfo("Recv: %d/%d", recv, iterations); |
mfiore | 0:e17e5a07892d | 164 | logInfo("Recv Mismatch: %d/%d", recv_mismatch, iterations); |
mfiore | 0:e17e5a07892d | 165 | logInfo("Send Failure: %d/%d", send_failure, iterations); |
mfiore | 0:e17e5a07892d | 166 | logInfo("Dropped: %d/%d", iterations - (recv + recv_mismatch + send_failure), iterations); |
mfiore | 0:e17e5a07892d | 167 | |
mfiore | 0:e17e5a07892d | 168 | return 0; |
mfiore | 0:e17e5a07892d | 169 | } |