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-src
Fork of mDot_test_rx by
main.cpp@5:25114cb5e9a7, 2015-08-01 (annotated)
- Committer:
- jknapp_smtc
- Date:
- Sat Aug 01 01:17:43 2015 +0000
- Revision:
- 5:25114cb5e9a7
- Parent:
- 4:ee6bf074135c
- Child:
- 6:b1afa2592e36
Added wait_time to stop packets from being sent at 100% duty cycle.;
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 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 14 | static const uint8_t DEVKEY[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; // Normal |
| jknapp_smtc | 4:ee6bf074135c | 15 | static const uint8_t APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // SMTC AppEUI - Most Significant Byte First |
| jknapp_smtc | 4:ee6bf074135c | 16 | static uint8_t config_frequency_sub_band = 0; // 0 = Enable all channels, 1 = 1st eight channels |
| jknapp_smtc | 4:ee6bf074135c | 17 | |
| jknapp_smtc | 4:ee6bf074135c | 18 | /* ********************************************** */ |
| jknapp_smtc | 3:7cc5dfd22b54 | 19 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 20 | static std::vector<uint8_t> AppKey (DEVKEY, DEVKEY + sizeof(DEVKEY)/sizeof(uint8_t) ); |
| jknapp_smtc | 3:7cc5dfd22b54 | 21 | static std::vector<uint8_t> AppEUI (APPEUI, APPEUI + sizeof(APPEUI)/sizeof(uint8_t) ); |
| jknapp_smtc | 3:7cc5dfd22b54 | 22 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 23 | InterruptIn button(PB_1); |
| jknapp_smtc | 3:7cc5dfd22b54 | 24 | DigitalOut drive_high(PB_0); |
| jknapp_smtc | 3:7cc5dfd22b54 | 25 | DigitalIn door_open(PA_7); |
| jknapp_smtc | 3:7cc5dfd22b54 | 26 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 27 | volatile bool msg_rdy; |
| jknapp_smtc | 3:7cc5dfd22b54 | 28 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 29 | void queue_message(){ |
| jknapp_smtc | 3:7cc5dfd22b54 | 30 | msg_rdy = true; |
| jknapp_smtc | 3:7cc5dfd22b54 | 31 | } |
| jknapp_smtc | 3:7cc5dfd22b54 | 32 | |
| mfiore | 0:e17e5a07892d | 33 | |
| mfiore | 0:e17e5a07892d | 34 | int main() { |
| mfiore | 0:e17e5a07892d | 35 | Serial debug(USBTX, USBRX); |
| mfiore | 0:e17e5a07892d | 36 | debug.baud(460800); |
| jknapp_smtc | 3:7cc5dfd22b54 | 37 | drive_high = 1; |
| mfiore | 0:e17e5a07892d | 38 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 39 | msg_rdy = false; |
| mfiore | 0:e17e5a07892d | 40 | int32_t ret; |
| mfiore | 0:e17e5a07892d | 41 | int32_t next_tx; |
| mfiore | 2:aadbdfb6d517 | 42 | int32_t wait_time = 2; |
| mfiore | 0:e17e5a07892d | 43 | mDot* dot; |
| mfiore | 0:e17e5a07892d | 44 | std::vector<uint8_t> send_data; |
| mfiore | 0:e17e5a07892d | 45 | std::vector<uint8_t> recv_data; |
| mfiore | 0:e17e5a07892d | 46 | uint8_t recv = 0; |
| mfiore | 0:e17e5a07892d | 47 | uint8_t recv_mismatch = 0; |
| mfiore | 0:e17e5a07892d | 48 | uint8_t send_failure = 0; |
| mfiore | 0:e17e5a07892d | 49 | uint8_t iterations = 50; |
| jknapp_smtc | 3:7cc5dfd22b54 | 50 | button.rise(&queue_message); |
| mfiore | 0:e17e5a07892d | 51 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 52 | send_data.push_back(0x01); |
| jknapp_smtc | 3:7cc5dfd22b54 | 53 | send_data.push_back(0x02); |
| mfiore | 0:e17e5a07892d | 54 | |
| mfiore | 0:e17e5a07892d | 55 | dot = mDot::getInstance(); |
| mfiore | 0:e17e5a07892d | 56 | |
| mfiore | 0:e17e5a07892d | 57 | dot->resetConfig(); |
| mfiore | 1:8295b8c0d802 | 58 | |
| mfiore | 1:8295b8c0d802 | 59 | dot->setLogLevel(MTSLog::TRACE_LEVEL); |
| jknapp_smtc | 3:7cc5dfd22b54 | 60 | //dot->setJoinByteOrder(1); // MSB |
| jknapp_smtc | 3:7cc5dfd22b54 | 61 | dot->setTxDataRate(mDot::SF_10); |
| jknapp_smtc | 3:7cc5dfd22b54 | 62 | //dot->setTxPower(20); |
| jknapp_smtc | 3:7cc5dfd22b54 | 63 | |
| mfiore | 0:e17e5a07892d | 64 | while ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
| mfiore | 0:e17e5a07892d | 65 | logError("failed to set frequency sub band: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:e17e5a07892d | 66 | } |
| jknapp_smtc | 3:7cc5dfd22b54 | 67 | while ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
| jknapp_smtc | 3:7cc5dfd22b54 | 68 | logError("failed to set Public Network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:e17e5a07892d | 69 | } |
| jknapp_smtc | 3:7cc5dfd22b54 | 70 | /** Set network ID |
| jknapp_smtc | 3:7cc5dfd22b54 | 71 | * for use with OTA & AUTO_OTA network join modes |
| jknapp_smtc | 3:7cc5dfd22b54 | 72 | * setting network ID via this function sets network name to empty |
| jknapp_smtc | 3:7cc5dfd22b54 | 73 | * @param id a vector of 8 bytes |
| jknapp_smtc | 3:7cc5dfd22b54 | 74 | * @returns MDOT_OK if success |
| jknapp_smtc | 3:7cc5dfd22b54 | 75 | */ |
| jknapp_smtc | 3:7cc5dfd22b54 | 76 | while ((ret = dot->setNetworkId(AppEUI)) != mDot::MDOT_OK) { |
| jknapp_smtc | 3:7cc5dfd22b54 | 77 | logError("failed to set AppEUI: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| jknapp_smtc | 3:7cc5dfd22b54 | 78 | } |
| jknapp_smtc | 3:7cc5dfd22b54 | 79 | while ((ret = dot->setNetworkKey(AppKey)) != mDot::MDOT_OK) { |
| jknapp_smtc | 3:7cc5dfd22b54 | 80 | logError("failed to set AppEUI: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:e17e5a07892d | 81 | } |
| mfiore | 0:e17e5a07892d | 82 | |
| mfiore | 2:aadbdfb6d517 | 83 | logInfo("enabling activity LED"); |
| mfiore | 2:aadbdfb6d517 | 84 | dot->setActivityLedEnable(true); |
| mfiore | 2:aadbdfb6d517 | 85 | |
| mfiore | 0:e17e5a07892d | 86 | logInfo("joining network"); |
| mfiore | 0:e17e5a07892d | 87 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
| mfiore | 0:e17e5a07892d | 88 | logError("failed to join network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:e17e5a07892d | 89 | wait_ms(dot->getNextTxMs() + 1); |
| mfiore | 0:e17e5a07892d | 90 | } |
| mfiore | 0:e17e5a07892d | 91 | logInfo("joined"); |
| mfiore | 0:e17e5a07892d | 92 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 93 | dot->setAck(3); // Use Confirmed frames and try three times |
| jknapp_smtc | 3:7cc5dfd22b54 | 94 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 95 | while (1) { |
| jknapp_smtc | 3:7cc5dfd22b54 | 96 | |
| jknapp_smtc | 3:7cc5dfd22b54 | 97 | send_data[0] = 192; //door_open; |
| mfiore | 0:e17e5a07892d | 98 | if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { |
| mfiore | 0:e17e5a07892d | 99 | logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:e17e5a07892d | 100 | send_failure++; |
| mfiore | 0:e17e5a07892d | 101 | } else { |
| mfiore | 0:e17e5a07892d | 102 | logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); |
| mfiore | 0:e17e5a07892d | 103 | if ((ret = dot->recv(recv_data)) != mDot::MDOT_OK) { |
| mfiore | 0:e17e5a07892d | 104 | logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:e17e5a07892d | 105 | } else { |
| mfiore | 0:e17e5a07892d | 106 | logInfo("recv data: %s", Text::bin2hexString(recv_data).c_str()); |
| jknapp_smtc | 3:7cc5dfd22b54 | 107 | if (recv_data[0] == 0x80) { |
| mfiore | 0:e17e5a07892d | 108 | recv++; |
| jknapp_smtc | 3:7cc5dfd22b54 | 109 | } else if (recv_data[0] == 0xFF) { |
| jknapp_smtc | 3:7cc5dfd22b54 | 110 | goto END; |
| mfiore | 0:e17e5a07892d | 111 | } else { |
| mfiore | 0:e17e5a07892d | 112 | recv_mismatch++; |
| mfiore | 0:e17e5a07892d | 113 | } |
| mfiore | 0:e17e5a07892d | 114 | } |
| mfiore | 0:e17e5a07892d | 115 | recv_data.clear(); |
| mfiore | 0:e17e5a07892d | 116 | } |
| jknapp_smtc | 4:ee6bf074135c | 117 | msg_rdy = false; // Set to 'true' for free running. |
| jknapp_smtc | 5:25114cb5e9a7 | 118 | wait(wait_time); |
| jknapp_smtc | 3:7cc5dfd22b54 | 119 | while (!msg_rdy) wait(wait_time); |
| mfiore | 0:e17e5a07892d | 120 | } |
| jknapp_smtc | 3:7cc5dfd22b54 | 121 | END: |
| mfiore | 0:e17e5a07892d | 122 | logInfo("Version: %s", dot->getId().c_str()); |
| mfiore | 0:e17e5a07892d | 123 | logInfo("Recv: %d/%d", recv, iterations); |
| mfiore | 0:e17e5a07892d | 124 | logInfo("Recv Mismatch: %d/%d", recv_mismatch, iterations); |
| mfiore | 0:e17e5a07892d | 125 | logInfo("Send Failure: %d/%d", send_failure, iterations); |
| mfiore | 0:e17e5a07892d | 126 | logInfo("Dropped: %d/%d", iterations - (recv + recv_mismatch + send_failure), iterations); |
| mfiore | 0:e17e5a07892d | 127 | |
| mfiore | 0:e17e5a07892d | 128 | return 0; |
| mfiore | 0:e17e5a07892d | 129 | } |
