Basic xdot code to check how many memory is available for user APP after initializing libxdot lorawan stack
examples/src/class_b_example.cpp@42:20f6b29a9903, 2021-05-25 (annotated)
- Committer:
- Taylor Heck
- Date:
- Tue May 25 09:54:33 2021 -0500
- Revision:
- 42:20f6b29a9903
- Parent:
- 33:15ea8f985c54
Target mbed-os 6 and Dot Library version 4.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jreiss | 33:15ea8f985c54 | 1 | #include "dot_util.h" |
jreiss | 33:15ea8f985c54 | 2 | #include "RadioEvent.h" |
jreiss | 33:15ea8f985c54 | 3 | |
jreiss | 33:15ea8f985c54 | 4 | #if ACTIVE_EXAMPLE == CLASS_B_EXAMPLE |
jreiss | 33:15ea8f985c54 | 5 | |
jreiss | 33:15ea8f985c54 | 6 | ///////////////////////////////////////////////////////////////////////////// |
jreiss | 33:15ea8f985c54 | 7 | // -------------------- DOT LIBRARY REQUIRED ------------------------------// |
jreiss | 33:15ea8f985c54 | 8 | // * Because these example programs can be used for both mDot and xDot // |
jreiss | 33:15ea8f985c54 | 9 | // devices, the LoRa stack is not included. The libmDot library should // |
jreiss | 33:15ea8f985c54 | 10 | // be imported if building for mDot devices. The libxDot library // |
jreiss | 33:15ea8f985c54 | 11 | // should be imported if building for xDot devices. // |
jreiss | 33:15ea8f985c54 | 12 | // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // |
jreiss | 33:15ea8f985c54 | 13 | // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // |
jreiss | 33:15ea8f985c54 | 14 | // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // |
jreiss | 33:15ea8f985c54 | 15 | // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // |
jreiss | 33:15ea8f985c54 | 16 | ///////////////////////////////////////////////////////////////////////////// |
jreiss | 33:15ea8f985c54 | 17 | |
jreiss | 33:15ea8f985c54 | 18 | ///////////////////////////////////////////////////////////// |
jreiss | 33:15ea8f985c54 | 19 | // * these options must match the settings on your gateway // |
jreiss | 33:15ea8f985c54 | 20 | // * edit their values to match your configuration // |
jreiss | 33:15ea8f985c54 | 21 | // * frequency sub band is only relevant for the 915 bands // |
jreiss | 33:15ea8f985c54 | 22 | // * either the network name and passphrase can be used or // |
jreiss | 33:15ea8f985c54 | 23 | // the network ID (8 bytes) and KEY (16 bytes) // |
jreiss | 33:15ea8f985c54 | 24 | ///////////////////////////////////////////////////////////// |
jreiss | 33:15ea8f985c54 | 25 | static std::string network_name = "MultiTech"; |
jreiss | 33:15ea8f985c54 | 26 | static std::string network_passphrase = "MultiTech"; |
jreiss | 33:15ea8f985c54 | 27 | static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; |
jreiss | 33:15ea8f985c54 | 28 | static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; |
jreiss | 33:15ea8f985c54 | 29 | static uint8_t frequency_sub_band = 1; |
jreiss | 33:15ea8f985c54 | 30 | static lora::NetworkType public_network = lora::PUBLIC_LORAWAN; |
jreiss | 33:15ea8f985c54 | 31 | static uint8_t join_delay = 5; |
jreiss | 33:15ea8f985c54 | 32 | static uint8_t ack = 3; |
jreiss | 33:15ea8f985c54 | 33 | static bool adr = true; |
jreiss | 33:15ea8f985c54 | 34 | |
jreiss | 33:15ea8f985c54 | 35 | // Number of ping slots to open per beacon interval - see mDot.h |
jreiss | 33:15ea8f985c54 | 36 | static uint8_t ping_periodicity = 4; |
jreiss | 33:15ea8f985c54 | 37 | |
jreiss | 33:15ea8f985c54 | 38 | mDot* dot = NULL; |
jreiss | 33:15ea8f985c54 | 39 | lora::ChannelPlan* plan = NULL; |
jreiss | 33:15ea8f985c54 | 40 | |
Taylor Heck |
42:20f6b29a9903 | 41 | mbed::UnbufferedSerial pc(USBTX, USBRX); |
jreiss | 33:15ea8f985c54 | 42 | |
jreiss | 33:15ea8f985c54 | 43 | #if defined(TARGET_XDOT_L151CC) |
jreiss | 33:15ea8f985c54 | 44 | I2C i2c(I2C_SDA, I2C_SCL); |
jreiss | 33:15ea8f985c54 | 45 | ISL29011 lux(i2c); |
jreiss | 33:15ea8f985c54 | 46 | #else |
jreiss | 33:15ea8f985c54 | 47 | AnalogIn lux(XBEE_AD0); |
jreiss | 33:15ea8f985c54 | 48 | #endif |
jreiss | 33:15ea8f985c54 | 49 | |
jreiss | 33:15ea8f985c54 | 50 | int main() { |
jreiss | 33:15ea8f985c54 | 51 | // Custom event handler for automatically displaying RX data |
jreiss | 33:15ea8f985c54 | 52 | RadioEvent events; |
jreiss | 33:15ea8f985c54 | 53 | |
jreiss | 33:15ea8f985c54 | 54 | pc.baud(115200); |
jreiss | 33:15ea8f985c54 | 55 | |
jreiss | 33:15ea8f985c54 | 56 | #if defined(TARGET_XDOT_L151CC) |
jreiss | 33:15ea8f985c54 | 57 | i2c.frequency(400000); |
jreiss | 33:15ea8f985c54 | 58 | #endif |
jreiss | 33:15ea8f985c54 | 59 | |
jreiss | 33:15ea8f985c54 | 60 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Taylor Heck |
42:20f6b29a9903 | 61 | |
Taylor Heck |
42:20f6b29a9903 | 62 | // Create channel plan |
Taylor Heck |
42:20f6b29a9903 | 63 | plan = create_channel_plan(); |
jreiss | 33:15ea8f985c54 | 64 | assert(plan); |
jreiss | 33:15ea8f985c54 | 65 | |
jreiss | 33:15ea8f985c54 | 66 | dot = mDot::getInstance(plan); |
jreiss | 33:15ea8f985c54 | 67 | assert(dot); |
jreiss | 33:15ea8f985c54 | 68 | |
jreiss | 33:15ea8f985c54 | 69 | logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
jreiss | 33:15ea8f985c54 | 70 | |
jreiss | 33:15ea8f985c54 | 71 | // start from a well-known state |
jreiss | 33:15ea8f985c54 | 72 | logInfo("defaulting Dot configuration"); |
jreiss | 33:15ea8f985c54 | 73 | dot->resetConfig(); |
jreiss | 33:15ea8f985c54 | 74 | dot->resetNetworkSession(); |
jreiss | 33:15ea8f985c54 | 75 | |
jreiss | 33:15ea8f985c54 | 76 | // make sure library logging is turned on |
jreiss | 33:15ea8f985c54 | 77 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
jreiss | 33:15ea8f985c54 | 78 | |
jreiss | 33:15ea8f985c54 | 79 | // attach the custom events handler |
jreiss | 33:15ea8f985c54 | 80 | dot->setEvents(&events); |
jreiss | 33:15ea8f985c54 | 81 | |
jreiss | 33:15ea8f985c54 | 82 | // update configuration if necessary |
jreiss | 33:15ea8f985c54 | 83 | if (dot->getJoinMode() != mDot::OTA) { |
jreiss | 33:15ea8f985c54 | 84 | logInfo("changing network join mode to OTA"); |
jreiss | 33:15ea8f985c54 | 85 | if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { |
jreiss | 33:15ea8f985c54 | 86 | logError("failed to set network join mode to OTA"); |
jreiss | 33:15ea8f985c54 | 87 | } |
jreiss | 33:15ea8f985c54 | 88 | } |
jreiss | 33:15ea8f985c54 | 89 | // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY |
jreiss | 33:15ea8f985c54 | 90 | // only one method or the other should be used! |
jreiss | 33:15ea8f985c54 | 91 | // network ID = crc64(network name) |
jreiss | 33:15ea8f985c54 | 92 | // network KEY = cmac(network passphrase) |
jreiss | 33:15ea8f985c54 | 93 | update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); |
jreiss | 33:15ea8f985c54 | 94 | //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack); |
jreiss | 33:15ea8f985c54 | 95 | |
jreiss | 33:15ea8f985c54 | 96 | // enable or disable Adaptive Data Rate |
jreiss | 33:15ea8f985c54 | 97 | dot->setAdr(adr); |
jreiss | 33:15ea8f985c54 | 98 | |
jreiss | 33:15ea8f985c54 | 99 | // Configure the join delay |
jreiss | 33:15ea8f985c54 | 100 | dot->setJoinDelay(join_delay); |
jreiss | 33:15ea8f985c54 | 101 | |
jreiss | 33:15ea8f985c54 | 102 | // Configure the class B ping periodicity |
jreiss | 33:15ea8f985c54 | 103 | dot->setPingPeriodicity(ping_periodicity); |
jreiss | 33:15ea8f985c54 | 104 | |
jreiss | 33:15ea8f985c54 | 105 | // save changes to configuration |
jreiss | 33:15ea8f985c54 | 106 | logInfo("saving configuration"); |
jreiss | 33:15ea8f985c54 | 107 | if (!dot->saveConfig()) { |
jreiss | 33:15ea8f985c54 | 108 | logError("failed to save configuration"); |
jreiss | 33:15ea8f985c54 | 109 | } |
jreiss | 33:15ea8f985c54 | 110 | |
jreiss | 33:15ea8f985c54 | 111 | // display configuration |
jreiss | 33:15ea8f985c54 | 112 | display_config(); |
jreiss | 33:15ea8f985c54 | 113 | |
jreiss | 33:15ea8f985c54 | 114 | // join the network - must do this before attempting to switch to class B |
jreiss | 33:15ea8f985c54 | 115 | join_network(); |
jreiss | 33:15ea8f985c54 | 116 | |
jreiss | 33:15ea8f985c54 | 117 | // configure the Dot for class B operation |
jreiss | 33:15ea8f985c54 | 118 | // the Dot must also be configured on the gateway for class B |
jreiss | 33:15ea8f985c54 | 119 | // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/ |
jreiss | 33:15ea8f985c54 | 120 | // to provision your Dot for class B operation with a 3rd party gateway, see the gateway or network provider documentation |
jreiss | 33:15ea8f985c54 | 121 | // Note: we won't actually switch to class B until we receive a beacon (mDotEvent::BeaconRx fires) |
jreiss | 33:15ea8f985c54 | 122 | logInfo("changing network mode to class B"); |
jreiss | 33:15ea8f985c54 | 123 | |
jreiss | 33:15ea8f985c54 | 124 | if (dot->setClass("B") != mDot::MDOT_OK) { |
jreiss | 33:15ea8f985c54 | 125 | logError("Failed to set network mode to class B"); |
jreiss | 33:15ea8f985c54 | 126 | logInfo("Reset the MCU to try again"); |
jreiss | 33:15ea8f985c54 | 127 | return 0; |
jreiss | 33:15ea8f985c54 | 128 | } |
jreiss | 33:15ea8f985c54 | 129 | |
jreiss | 33:15ea8f985c54 | 130 | // Start a timer to check the beacon was acquired |
jreiss | 33:15ea8f985c54 | 131 | LowPowerTimer bcn_timer; |
jreiss | 33:15ea8f985c54 | 132 | bcn_timer.start(); |
jreiss | 33:15ea8f985c54 | 133 | |
jreiss | 33:15ea8f985c54 | 134 | while (true) { |
jreiss | 33:15ea8f985c54 | 135 | std::vector<uint8_t> tx_data; |
jreiss | 33:15ea8f985c54 | 136 | static bool send_uplink = true; |
jreiss | 33:15ea8f985c54 | 137 | |
jreiss | 33:15ea8f985c54 | 138 | // Check if we locked the beacon yet and send an uplink to notify the network server |
jreiss | 33:15ea8f985c54 | 139 | // To receive data from the gateway in class B ping slots, we must have received a beacon |
jreiss | 33:15ea8f985c54 | 140 | // already, and sent one uplink to signal to the network server that we are in class B mode |
jreiss | 33:15ea8f985c54 | 141 | if (events.BeaconLocked && send_uplink) { |
jreiss | 33:15ea8f985c54 | 142 | logInfo("Acquired a beacon lock"); |
jreiss | 33:15ea8f985c54 | 143 | |
jreiss | 33:15ea8f985c54 | 144 | // Add a random delay before trying the uplink to avoid collisions w/ other motes |
jreiss | 33:15ea8f985c54 | 145 | srand(dot->getRadioRandom()); |
jreiss | 33:15ea8f985c54 | 146 | uint32_t rand_delay = rand() % 5000; |
jreiss | 33:15ea8f985c54 | 147 | logInfo("Applying a random delay of %d ms before class notification uplink", rand_delay); |
Taylor Heck |
42:20f6b29a9903 | 148 | ThisThread::sleep_for(std::chrono::milliseconds(rand_delay)); |
jreiss | 33:15ea8f985c54 | 149 | |
jreiss | 33:15ea8f985c54 | 150 | // Ensure the link is idle before trying to transmit |
jreiss | 33:15ea8f985c54 | 151 | while (!dot->getIsIdle()) { |
Taylor Heck |
42:20f6b29a9903 | 152 | ThisThread::sleep_for(10ms); |
jreiss | 33:15ea8f985c54 | 153 | } |
jreiss | 33:15ea8f985c54 | 154 | |
jreiss | 33:15ea8f985c54 | 155 | if (send_data(tx_data) != mDot::MDOT_OK) { |
jreiss | 33:15ea8f985c54 | 156 | logError("Failed to inform the network server we are in class B"); |
jreiss | 33:15ea8f985c54 | 157 | logInfo("Reset the MCU to try again"); |
jreiss | 33:15ea8f985c54 | 158 | return 0; |
jreiss | 33:15ea8f985c54 | 159 | } |
jreiss | 33:15ea8f985c54 | 160 | |
jreiss | 33:15ea8f985c54 | 161 | logInfo("Enqueued packets may now be scheduled on class B ping slots"); |
jreiss | 33:15ea8f985c54 | 162 | send_uplink = false; |
jreiss | 33:15ea8f985c54 | 163 | bcn_timer.stop(); |
jreiss | 33:15ea8f985c54 | 164 | } else if (!events.BeaconLocked) { |
jreiss | 33:15ea8f985c54 | 165 | logInfo("Waiting to receive a beacon.."); |
jreiss | 33:15ea8f985c54 | 166 | |
jreiss | 33:15ea8f985c54 | 167 | if (bcn_timer.read() > lora::DEFAULT_BEACON_PERIOD) { |
jreiss | 33:15ea8f985c54 | 168 | if (dot->setClass("B") != mDot::MDOT_OK) { |
jreiss | 33:15ea8f985c54 | 169 | logError("Failed to set network mode to class B"); |
jreiss | 33:15ea8f985c54 | 170 | logInfo("Reset the MCU to try again"); |
jreiss | 33:15ea8f985c54 | 171 | return 0; |
jreiss | 33:15ea8f985c54 | 172 | } |
jreiss | 33:15ea8f985c54 | 173 | |
jreiss | 33:15ea8f985c54 | 174 | bcn_timer.reset(); |
jreiss | 33:15ea8f985c54 | 175 | } |
jreiss | 33:15ea8f985c54 | 176 | } |
Taylor Heck |
42:20f6b29a9903 | 177 | ThisThread::sleep_for(10s); |
jreiss | 33:15ea8f985c54 | 178 | } |
jreiss | 33:15ea8f985c54 | 179 | |
jreiss | 33:15ea8f985c54 | 180 | return 0; |
jreiss | 33:15ea8f985c54 | 181 | } |
jreiss | 33:15ea8f985c54 | 182 | |
jreiss | 33:15ea8f985c54 | 183 | #endif |