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-mbed5 ISL29011
Revision 8:e667f4a507b1, committed 2016-10-07
- Comitter:
- Mike Fiore
- Date:
- Fri Oct 07 15:31:03 2016 -0500
- Parent:
- 7:724cb82a113e
- Child:
- 9:72d3203279b2
- Commit message:
- add manual join mode example
Changed in this revision
--- a/examples/inc/dot_util.h Fri Oct 07 12:45:23 2016 -0500 +++ b/examples/inc/dot_util.h Fri Oct 07 15:31:03 2016 -0500 @@ -16,6 +16,8 @@ void update_ota_config_id_key(uint8_t *network_id, uint8_t *network_key, uint8_t frequency_sub_band, bool public_network, uint8_t ack); +void update_manual_config(uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key, uint8_t frequency_sub_band, bool public_network, uint8_t ack); + void join_network(); void sleep_wake_rtc_only(bool deepsleep);
--- a/examples/src/dot_util.cpp Fri Oct 07 12:45:23 2016 -0500
+++ b/examples/src/dot_util.cpp Fri Oct 07 15:31:03 2016 -0500
@@ -25,11 +25,17 @@
logInfo("\tpublic network: %s", dot->getPublicNetwork() == true ? "on" : "off");
logInfo("credentials configuration");
logInfo("-------------------------");
- logInfo("\tnetwork name: %s", dot->getNetworkName().c_str());
- logInfo("\tnetwork phrase: %s", dot->getNetworkPassphrase().c_str());
- logInfo("\tnetwork EUI: %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str());
- logInfo("\tnetwork KEY: %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str());
logInfo("\tnetwork join mode: %s", mDot::JoinModeStr(dot->getJoinMode()).c_str());
+ if (dot->getJoinMode() == mDot::MANUAL) {
+ logInfo("\tnetwork address: %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str());
+ logInfo("\tnetwork session key: %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str());
+ logInfo("\tdata session key: %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str());
+ } else {
+ logInfo("\tnetwork name: %s", dot->getNetworkName().c_str());
+ logInfo("\tnetwork phrase: %s", dot->getNetworkPassphrase().c_str());
+ logInfo("\tnetwork EUI: %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str());
+ logInfo("\tnetwork KEY: %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str());
+ }
logInfo("communication parameters");
logInfo("------------------------");
logInfo("\tacks: %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck());
@@ -127,6 +133,61 @@
}
}
+void update_manual_config(uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key, uint8_t frequency_sub_band, bool public_network, uint8_t ack) {
+ std::vector<uint8_t> current_network_address = dot->getNetworkAddress();
+ std::vector<uint8_t> current_network_session_key = dot->getNetworkSessionKey();
+ std::vector<uint8_t> current_data_session_key = dot->getDataSessionKey();
+ uint8_t current_frequency_sub_band = dot->getFrequencySubBand();
+ bool current_public_network = dot->getPublicNetwork();
+ uint8_t current_ack = dot->getAck();
+
+ std::vector<uint8_t> network_address_vector(network_address, network_address + 4);
+ std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16);
+ std::vector<uint8_t> data_session_key_vector(data_session_key, data_session_key + 16);
+
+ if (current_network_address != network_address_vector) {
+ logInfo("changing network address from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_address).c_str(), mts::Text::bin2hexString(network_address_vector).c_str());
+ if (dot->setNetworkAddress(network_address_vector) != mDot::MDOT_OK) {
+ logError("failed to set network address to \"%s\"", mts::Text::bin2hexString(network_address_vector).c_str());
+ }
+ }
+
+ if (current_network_session_key != network_session_key_vector) {
+ logInfo("changing network session key from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_session_key).c_str(), mts::Text::bin2hexString(network_session_key_vector).c_str());
+ if (dot->setNetworkSessionKey(network_session_key_vector) != mDot::MDOT_OK) {
+ logError("failed to set network session key to \"%s\"", mts::Text::bin2hexString(network_session_key_vector).c_str());
+ }
+ }
+
+ if (current_data_session_key != data_session_key_vector) {
+ logInfo("changing data session key from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_data_session_key).c_str(), mts::Text::bin2hexString(data_session_key_vector).c_str());
+ if (dot->setDataSessionKey(data_session_key_vector) != mDot::MDOT_OK) {
+ logError("failed to set data session key to \"%s\"", mts::Text::bin2hexString(data_session_key_vector).c_str());
+ }
+ }
+
+ if (current_frequency_sub_band != frequency_sub_band) {
+ logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band);
+ if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) {
+ logError("failed to set frequency sub band to %u", frequency_sub_band);
+ }
+ }
+
+ if (current_public_network != public_network) {
+ logInfo("changing public network from %s to %s", current_public_network ? "true" : "false", public_network ? "true" : "false");
+ if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
+ logError("failed to set public network to %s", public_network ? "true" : "false");
+ }
+ }
+
+ if (current_ack != ack) {
+ logInfo("changing acks from %u to %u", current_ack, ack);
+ if (dot->setAck(ack) != mDot::MDOT_OK) {
+ logError("failed to set acks to %u", ack);
+ }
+ }
+}
+
void join_network() {
int32_t j_attempts = 0;
int32_t ret = mDot::MDOT_ERROR;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/src/manual_example.cpp Fri Oct 07 15:31:03 2016 -0500
@@ -0,0 +1,118 @@
+#include "dot_util.h"
+
+#if ACTIVE_EXAMPLE == MANUAL_EXAMPLE
+
+/////////////////////////////////////////////////////////////
+// * these options must match the settings on your gateway //
+// * edit their values to match your configuration //
+// * frequency sub band is only relevant for the 915 bands //
+/////////////////////////////////////////////////////////////
+static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 };
+static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
+static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
+static uint8_t frequency_sub_band = 6;
+static bool public_network = false;
+static uint8_t ack = 1;
+
+// deepsleep consumes slightly less current than sleep
+// in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
+// in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
+// if deep_sleep == true, device will enter deepsleep mode
+static bool deep_sleep = true;
+
+mDot* dot = NULL;
+
+Serial pc(USBTX, USBRX);
+
+#if defined(TARGET_XDOT_L151CC)
+I2C i2c(I2C_SDA, I2C_SCL);
+ISL29011 lux(i2c);
+#else
+AnalogIn lux(XBEE_AD0);
+#endif
+
+int main() {
+ pc.baud(115200);
+
+ mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
+
+ dot = mDot::getInstance();
+
+ // make sure library logging is turned on
+ dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
+
+ // update configuration if necessary
+ if (dot->getJoinMode() != mDot::MANUAL) {
+ logInfo("changing network join mode to MANUAL");
+ if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) {
+ logError("failed to set network join mode to MANUAL");
+ }
+ }
+ // in MANUAL join mode there is no join request/response transaction
+ // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate
+ // network address - 4 bytes (00000001 - FFFFFFFE)
+ // network session key - 16 bytes
+ // data session key - 16 bytes
+ // to provision your Dot with a Conduit gateway, follow the following steps
+ // * ssh into the Conduit
+ // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/
+ // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
+ // * if you change the network address, network session key, or data session key, make sure you update them on the gateway
+ // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
+ update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack);
+
+ // restore the saved session if the dot woke from deepsleep mode
+ // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
+ if (dot->getStandbyFlag()) {
+ logInfo("restoring network session from NVM");
+ dot->restoreNetworkSession();
+ }
+
+ // display configuration
+ display_config();
+
+ while (true) {
+ uint16_t light;
+ std::vector<uint8_t> tx_data;
+
+#if defined(TARGET_XDOT_L151CC)
+ // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
+ lux.setMode(ISL29011::ALS_CONT);
+ lux.setResolution(ISL29011::ADC_16BIT);
+ lux.setRange(ISL29011::RNG_64000);
+
+ // get the latest light sample and send it to the gateway
+ light = lux.getData();
+ tx_data.push_back((light >> 8) & 0xFF);
+ tx_data.push_back(light & 0xFF);
+ logInfo("light: %lu [0x%04X]", light, light);
+ send_data(tx_data);
+
+ // put the LSL29011 ambient light sensor into a low power state
+ lux.setMode(ISL29011::PWR_DOWN);
+#else
+ // get some dummy data and send it to the gateway
+ light = lux.read_u16();
+ tx_data.push_back((light >> 8) & 0xFF);
+ tx_data.push_back(light & 0xFF);
+ logInfo("light: %lu [0x%04X]", light, light);
+ send_data(tx_data);
+#endif
+
+ // if going into deepsleep mode, save the session so we don't need to join again after waking up
+ // not necessary if going into sleep mode since RAM is retained
+ if (deep_sleep) {
+ logInfo("saving network session to NVM");
+ dot->saveNetworkSession();
+ }
+
+ // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
+ //sleep_wake_rtc_only(deep_sleep);
+ //sleep_wake_interrupt_only(deep_sleep);
+ sleep_wake_rtc_or_interrupt(deep_sleep);
+ }
+
+ return 0;
+}
+
+#endif