DOT_Example_16_10_2018
Dependencies: libmDot-mbed5 ISL29011
Revision 0:6a6c127cf398, committed 2018-10-16
- Comitter:
- nguyenhoang9x5555
- Date:
- Tue Oct 16 08:05:27 2018 +0000
- Commit message:
- DOT_EXample_16_10_2018
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,6 @@ +mbed-os +.build +/mdot/ +/mdot-library/ +/BUILD/ +/.git/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,22 @@ +Instructions for building Dot-Examples + +1. Import dot-examples +$ mbed import http://os.mbed.com/teams/MultiTech/code/Dot-Examples/ + +2. cd to the Dot-Examples/examples and import the dot library stack +Choose either the stable or dev library for your dot device +e.g. to get the latest development library for the xDot +$ mbed add http://os.mbed.com/teams/MultiTech/code/libxDot-dev-mbed5/ + +3. Update mbed-os revision to match that of the dot library you just imported. +This information can be found in the library's commit history. +e.g. +$ cd Dot-Examples/mbed-os +$ mbed update mbed-os-5.7.6 + +4. Modify the Dot-Examples/examples/example_config.h to select the channel plan and which example to build +By default, the OTA example is selected to build with the US channel plan + +5. Once the example is selected, modify the example source file to match the configuration of your gateway. +Make sure the network_name, network_passphrase, frequency_sub_band (US), public_network, and join_delay settings match that of your gateway +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/example_config.h Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,28 @@ +#ifndef __EXAMPLE__CONFIG_H__ +#define __EXAMPLE__CONFIG_H__ + +#define OTA_EXAMPLE 1 // see ota_example.cpp +#define AUTO_OTA_EXAMPLE 2 // see auto_ota_example.cpp +#define MANUAL_EXAMPLE 3 // see manual_example.cpp +#define PEER_TO_PEER_EXAMPLE 4 // see peer_to_peer_example.cpp +#define CLASS_C_EXAMPLE 5 // see class_c_example.cpp + +// the active example is the one that will be compiled +#if !defined(ACTIVE_EXAMPLE) +#define ACTIVE_EXAMPLE OTA_EXAMPLE +#endif + +// the active channel plan is the one that will be compiled +// options are : +// CP_US915 +// CP_AU915 +// CP_EU868 +// CP_KR920 +// CP_AS923 +// CP_AS923_JAPAN +// CP_IN865 +#if !defined(CHANNEL_PLAN) +#define CHANNEL_PLAN CP_AS923 +#endif + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/inc/RadioEvent.h Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,75 @@ +#ifndef __RADIO_EVENT_H__ +#define __RADIO_EVENT_H__ + +#include "dot_util.h" +#include "mDotEvent.h" + +class RadioEvent : public mDotEvent +{ + +public: + RadioEvent() {} + + virtual ~RadioEvent() {} + + /*! + * MAC layer event callback prototype. + * + * \param [IN] flags Bit field indicating the MAC events occurred + * \param [IN] info Details about MAC events occurred + */ + virtual void MacEvent(LoRaMacEventFlags* flags, LoRaMacEventInfo* info) { + + if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) { + std::string msg = "OK"; + switch (info->Status) { + case LORAMAC_EVENT_INFO_STATUS_ERROR: + msg = "ERROR"; + break; + case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT: + msg = "TX_TIMEOUT"; + break; + case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT: + msg = "RX_TIMEOUT"; + break; + case LORAMAC_EVENT_INFO_STATUS_RX_ERROR: + msg = "RX_ERROR"; + break; + case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL: + msg = "JOIN_FAIL"; + break; + case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL: + msg = "DOWNLINK_FAIL"; + break; + case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL: + msg = "ADDRESS_FAIL"; + break; + case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL: + msg = "MIC_FAIL"; + break; + default: + break; + } + logTrace("Event: %s", msg.c_str()); + + logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d", + flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept); + logTrace("Info: Status: %d ACK: %d Retries: %d TxDR: %d RxPort: %d RxSize: %d RSSI: %d SNR: %d Energy: %d Margin: %d Gateways: %d", + info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize, + info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways); + } + + if (flags->Bits.Rx) { + + logDebug("Rx %d bytes", info->RxBufferSize); + if (info->RxBufferSize > 0) { + // print RX data as string and hexadecimal + std::string rx((const char*)info->RxBuffer, info->RxBufferSize); + printf("Rx data: %s [%s]\r\n", rx.c_str(), mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str()); + } + } + } +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/inc/dot_util.h Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,42 @@ +#ifndef __DOT_UTIL_H__ +#define __DOT_UTIL_H__ + +#include "mbed.h" +#include "mDot.h" +#include "ChannelPlans.h" +#include "MTSLog.h" +#include "MTSText.h" +#include "ISL29011.h" +#include "example_config.h" + +extern mDot* dot; + +void display_config(); + +void update_ota_config_name_phrase(std::string network_name, std::string network_passphrase, uint8_t frequency_sub_band, bool public_network, uint8_t ack); + +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 update_peer_to_peer_config(uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key, uint32_t tx_frequency, uint8_t tx_datarate, uint8_t tx_power); + +void update_network_link_check_config(uint8_t link_check_count, uint8_t link_check_threshold); + +void join_network(); + +void sleep_wake_rtc_only(bool deepsleep); + +void sleep_wake_interrupt_only(bool deepsleep); + +void sleep_wake_rtc_or_interrupt(bool deepsleep); + +void sleep_save_io(); + +void sleep_configure_io(); + +void sleep_restore_io(); + +void send_data(std::vector<uint8_t> data); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/lib/ISL29011.lib Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/Multi-Hackers/code/ISL29011/#953adca17438
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/src/auto_ota_example.cpp Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,179 @@ +#include "dot_util.h" +#include "RadioEvent.h" + +#if ACTIVE_EXAMPLE == AUTO_OTA_EXAMPLE + +///////////////////////////////////////////////////////////////////////////// +// -------------------- DOT LIBRARY REQUIRED ------------------------------// +// * Because these example programs can be used for both mDot and xDot // +// devices, the LoRa stack is not included. The libmDot library should // +// be imported if building for mDot devices. The libxDot library // +// should be imported if building for xDot devices. // +// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // +///////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////////////////// +// * 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 // +// * either the network name and passphrase can be used or // +// the network ID (8 bytes) and KEY (16 bytes) // +///////////////////////////////////////////////////////////// +static std::string network_name = "MultiTech"; +static std::string network_passphrase = "MultiTech"; +static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; +static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; +static uint8_t frequency_sub_band = 0; +static bool public_network = true; +static uint8_t join_delay = 5; +static uint8_t ack = 0; +static bool adr = true; + +// 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; +lora::ChannelPlan* plan = 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() { + // Custom event handler for automatically displaying RX data + RadioEvent events; + + pc.baud(115200); + +#if defined(TARGET_XDOT_L151CC) + i2c.frequency(400000); +#endif + + mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); + +#if CHANNEL_PLAN == CP_US915 + plan = new lora::ChannelPlan_US915(); +#elif CHANNEL_PLAN == CP_AU915 + plan = new lora::ChannelPlan_AU915(); +#elif CHANNEL_PLAN == CP_EU868 + plan = new lora::ChannelPlan_EU868(); +#elif CHANNEL_PLAN == CP_KR920 + plan = new lora::ChannelPlan_KR920(); +#elif CHANNEL_PLAN == CP_AS923 + plan = new lora::ChannelPlan_AS923(); +#elif CHANNEL_PLAN == CP_AS923_JAPAN + plan = new lora::ChannelPlan_AS923_Japan(); +#elif CHANNEL_PLAN == CP_IN865 + plan = new lora::ChannelPlan_IN865(); +#endif + assert(plan); + + dot = mDot::getInstance(plan); + assert(dot); + + // attach the custom events handler + dot->setEvents(&events); + + if (!dot->getStandbyFlag()) { + logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); + + // start from a well-known state + logInfo("defaulting Dot configuration"); + dot->resetConfig(); + dot->resetNetworkSession(); + + // make sure library logging is turned on + dot->setLogLevel(mts::MTSLog::INFO_LEVEL); + + // update configuration if necessary + // in AUTO_OTA mode the session is automatically saved, so saveNetworkSession and restoreNetworkSession are not needed + if (dot->getJoinMode() != mDot::AUTO_OTA) { + logInfo("changing network join mode to AUTO_OTA"); + if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) { + logError("failed to set network join mode to AUTO_OTA"); + } + } + // 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 + // only one method or the other should be used! + // network ID = crc64(network name) + // network KEY = cmac(network passphrase) + update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); + //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack); + + // configure network link checks + // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots + // check the link every count packets + // declare the Dot disconnected after threshold failed link checks + // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row + update_network_link_check_config(3, 5); + + // enable or disable Adaptive Data Rate + dot->setAdr(adr); + + // Configure the join delay + dot->setJoinDelay(join_delay); + + // save changes to configuration + logInfo("saving configuration"); + if (!dot->saveConfig()) { + logError("failed to save configuration"); + } + + // display configuration + display_config(); + } + + while (true) { + uint16_t light; + std::vector<uint8_t> tx_data; + + // join network if not joined + if (!dot->getNetworkJoinStatus()) { + join_network(); + } + +#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 + + // 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/src/class_c_example.cpp Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,174 @@ +#include "dot_util.h" +#include "RadioEvent.h" + +#if ACTIVE_EXAMPLE == CLASS_C_EXAMPLE + +///////////////////////////////////////////////////////////////////////////// +// -------------------- DOT LIBRARY REQUIRED ------------------------------// +// * Because these example programs can be used for both mDot and xDot // +// devices, the LoRa stack is not included. The libmDot library should // +// be imported if building for mDot devices. The libxDot library // +// should be imported if building for xDot devices. // +// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // +///////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////////////////// +// * 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 // +// * either the network name and passphrase can be used or // +// the network ID (8 bytes) and KEY (16 bytes) // +///////////////////////////////////////////////////////////// +static std::string network_name = "MultiTech"; +static std::string network_passphrase = "MultiTech"; +static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; +static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; +static uint8_t frequency_sub_band = 0; +static bool public_network = true; +static uint8_t join_delay = 5; +static uint8_t ack = 1; +static bool adr = true; + +mDot* dot = NULL; +lora::ChannelPlan* plan = 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() { + // Custom event handler for automatically displaying RX data + RadioEvent events; + + pc.baud(115200); + +#if defined(TARGET_XDOT_L151CC) + i2c.frequency(400000); +#endif + + mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); + +#if CHANNEL_PLAN == CP_US915 + plan = new lora::ChannelPlan_US915(); +#elif CHANNEL_PLAN == CP_AU915 + plan = new lora::ChannelPlan_AU915(); +#elif CHANNEL_PLAN == CP_EU868 + plan = new lora::ChannelPlan_EU868(); +#elif CHANNEL_PLAN == CP_KR920 + plan = new lora::ChannelPlan_KR920(); +#elif CHANNEL_PLAN == CP_AS923 + plan = new lora::ChannelPlan_AS923(); +#elif CHANNEL_PLAN == CP_AS923_JAPAN + plan = new lora::ChannelPlan_AS923_Japan(); +#elif CHANNEL_PLAN == CP_IN865 + plan = new lora::ChannelPlan_IN865(); +#endif + assert(plan); + + dot = mDot::getInstance(plan); + assert(dot); + + logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); + + // start from a well-known state + logInfo("defaulting Dot configuration"); + dot->resetConfig(); + dot->resetNetworkSession(); + + // make sure library logging is turned on + dot->setLogLevel(mts::MTSLog::INFO_LEVEL); + + // attach the custom events handler + dot->setEvents(&events); + + // update configuration if necessary + if (dot->getJoinMode() != mDot::OTA) { + logInfo("changing network join mode to OTA"); + if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { + logError("failed to set network join mode to OTA"); + } + } + // 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 + // only one method or the other should be used! + // network ID = crc64(network name) + // network KEY = cmac(network passphrase) + update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); + //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack); + + // configure the Dot for class C operation + // the Dot must also be configured on the gateway for class C + // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/ + // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation + logInfo("changing network mode to class C"); + if (dot->setClass("C") != mDot::MDOT_OK) { + logError("failed to set network mode to class C"); + } + + // enable or disable Adaptive Data Rate + dot->setAdr(adr); + + // Configure the join delay + dot->setJoinDelay(join_delay); + + // save changes to configuration + logInfo("saving configuration"); + if (!dot->saveConfig()) { + logError("failed to save configuration"); + } + + // display configuration + display_config(); + + while (true) { + uint16_t light; + std::vector<uint8_t> tx_data; + + // join network if not joined + if (!dot->getNetworkJoinStatus()) { + join_network(); + } + +#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 + + // the Dot can't sleep in class C mode + // it must be waiting for data from the gateway + // send data every 30s + logInfo("waiting for 30s"); + wait(30); + } + + return 0; +} + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/src/dot_util.cpp Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,717 @@ +#include "dot_util.h" +#if defined(TARGET_XDOT_L151CC) +#include "xdot_low_power.h" +#endif + +#if defined(TARGET_MTS_MDOT_F411RE) +uint32_t portA[6]; +uint32_t portB[6]; +uint32_t portC[6]; +uint32_t portD[6]; +uint32_t portH[6]; +#endif + + +void display_config() { + // display configuration and library version information + logInfo("====================="); + logInfo("general configuration"); + logInfo("====================="); + logInfo("version ------------------ %s", dot->getId().c_str()); + logInfo("device ID/EUI ------------ %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); + logInfo("default channel plan ----- %s", mDot::FrequencyBandStr(dot->getDefaultFrequencyBand()).c_str()); + logInfo("current channel plan ----- %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str()); + if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) { + logInfo("frequency sub band ------- %u", dot->getFrequencySubBand()); + } + logInfo("public network ----------- %s", dot->getPublicNetwork() ? "on" : "off"); + logInfo("========================="); + logInfo("credentials configuration"); + logInfo("========================="); + logInfo("device class ------------- %s", dot->getClass().c_str()); + logInfo("network join mode -------- %s", mDot::JoinModeStr(dot->getJoinMode()).c_str()); + if (dot->getJoinMode() == mDot::MANUAL || dot->getJoinMode() == mDot::PEER_TO_PEER) { + logInfo("network address ---------- %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str()); + logInfo("network session key------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str()); + logInfo("data session key---------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str()); + } else { + logInfo("network name ------------- %s", dot->getNetworkName().c_str()); + logInfo("network phrase ----------- %s", dot->getNetworkPassphrase().c_str()); + logInfo("network EUI -------------- %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str()); + logInfo("network KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str()); + } + logInfo("========================"); + logInfo("communication parameters"); + logInfo("========================"); + if (dot->getJoinMode() == mDot::PEER_TO_PEER) + { + logInfo("TX frequency ------------- %lu", dot->getTxFrequency()); + } + else + { + logInfo("acks --------------------- %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck()); + } + logInfo("TX datarate -------------- %s", mDot::DataRateStr(dot->getTxDataRate()).c_str()); + logInfo("TX power ----------------- %lu dBm", dot->getTxPower()); + logInfo("antenna gain ------------- %u dBm", dot->getAntennaGain()); + logInfo("LBT ---------------------- %s", dot->getLbtTimeUs() ? "on" : "off"); + if (dot->getLbtTimeUs()) { + logInfo("LBT time ----------------- %lu us", dot->getLbtTimeUs()); + logInfo("LBT threshold ------------ %d dBm", dot->getLbtThreshold()); + } +} + +void update_ota_config_name_phrase(std::string network_name, std::string network_passphrase, uint8_t frequency_sub_band, bool public_network, uint8_t ack) { + std::string current_network_name = dot->getNetworkName(); + std::string current_network_passphrase = dot->getNetworkPassphrase(); + uint8_t current_frequency_sub_band = dot->getFrequencySubBand(); + bool current_public_network = dot->getPublicNetwork(); + uint8_t current_ack = dot->getAck(); + + if (current_network_name != network_name) { + logInfo("changing network name from \"%s\" to \"%s\"", current_network_name.c_str(), network_name.c_str()); + if (dot->setNetworkName(network_name) != mDot::MDOT_OK) { + logError("failed to set network name to \"%s\"", network_name.c_str()); + } + } + + if (current_network_passphrase != network_passphrase) { + logInfo("changing network passphrase from \"%s\" to \"%s\"", current_network_passphrase.c_str(), network_passphrase.c_str()); + if (dot->setNetworkPassphrase(network_passphrase) != mDot::MDOT_OK) { + logError("failed to set network passphrase to \"%s\"", network_passphrase.c_str()); + } + } + + if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) { + 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 ? "on" : "off", public_network ? "on" : "off"); + if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) { + logError("failed to set public network to %s", public_network ? "on" : "off"); + } + } + + 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 update_ota_config_id_key(uint8_t *network_id, uint8_t *network_key, uint8_t frequency_sub_band, bool public_network, uint8_t ack) { + std::vector<uint8_t> current_network_id = dot->getNetworkId(); + std::vector<uint8_t> current_network_key = dot->getNetworkKey(); + 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_id_vector(network_id, network_id + 8); + std::vector<uint8_t> network_key_vector(network_key, network_key + 16); + + if (current_network_id != network_id_vector) { + logInfo("changing network ID from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_id).c_str(), mts::Text::bin2hexString(network_id_vector).c_str()); + if (dot->setNetworkId(network_id_vector) != mDot::MDOT_OK) { + logError("failed to set network ID to \"%s\"", mts::Text::bin2hexString(network_id_vector).c_str()); + } + } + + if (current_network_key != network_key_vector) { + logInfo("changing network KEY from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_key).c_str(), mts::Text::bin2hexString(network_key_vector).c_str()); + if (dot->setNetworkKey(network_key_vector) != mDot::MDOT_OK) { + logError("failed to set network KEY to \"%s\"", mts::Text::bin2hexString(network_key_vector).c_str()); + } + } + + if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) { + 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 ? "on" : "off", public_network ? "on" : "off"); + if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) { + logError("failed to set public network to %s", public_network ? "on" : "off"); + } + } + + 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 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 ? "on" : "off", public_network ? "on" : "off"); + if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) { + logError("failed to set public network to %s", public_network ? "on" : "off"); + } + } + + 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 update_peer_to_peer_config(uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key, uint32_t tx_frequency, uint8_t tx_datarate, uint8_t tx_power) { + 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(); + uint32_t current_tx_frequency = dot->getTxFrequency(); + uint8_t current_tx_datarate = dot->getTxDataRate(); + uint8_t current_tx_power = dot->getTxPower(); + + 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_tx_frequency != tx_frequency) { + logInfo("changing TX frequency from %lu to %lu", current_tx_frequency, tx_frequency); + if (dot->setTxFrequency(tx_frequency) != mDot::MDOT_OK) { + logError("failed to set TX frequency to %lu", tx_frequency); + } + } + + if (current_tx_datarate != tx_datarate) { + logInfo("changing TX datarate from %u to %u", current_tx_datarate, tx_datarate); + if (dot->setTxDataRate(tx_datarate) != mDot::MDOT_OK) { + logError("failed to set TX datarate to %u", tx_datarate); + } + } + + if (current_tx_power != tx_power) { + logInfo("changing TX power from %u to %u", current_tx_power, tx_power); + if (dot->setTxPower(tx_power) != mDot::MDOT_OK) { + logError("failed to set TX power to %u", tx_power); + } + } +} + +void update_network_link_check_config(uint8_t link_check_count, uint8_t link_check_threshold) { + uint8_t current_link_check_count = dot->getLinkCheckCount(); + uint8_t current_link_check_threshold = dot->getLinkCheckThreshold(); + + if (current_link_check_count != link_check_count) { + logInfo("changing link check count from %u to %u", current_link_check_count, link_check_count); + if (dot->setLinkCheckCount(link_check_count) != mDot::MDOT_OK) { + logError("failed to set link check count to %u", link_check_count); + } + } + + if (current_link_check_threshold != link_check_threshold) { + logInfo("changing link check threshold from %u to %u", current_link_check_threshold, link_check_threshold); + if (dot->setLinkCheckThreshold(link_check_threshold) != mDot::MDOT_OK) { + logError("failed to set link check threshold to %u", link_check_threshold); + } + } +} + +void join_network() { + int32_t j_attempts = 0; + int32_t ret = mDot::MDOT_ERROR; + + // attempt to join the network + while (ret != mDot::MDOT_OK) { + logInfo("attempt %d to join network", ++j_attempts); + ret = dot->joinNetwork(); + if (ret != mDot::MDOT_OK) { + logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + // in some frequency bands we need to wait until another channel is available before transmitting again + uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1; + if (delay_s < 5) { + logInfo("waiting %lu s until next free channel", delay_s); + wait(delay_s); + } else { + logInfo("sleeping %lu s until next free channel", delay_s); + dot->sleep(delay_s, mDot::RTC_ALARM, false); + } + } + } +} + +void sleep_wake_rtc_only(bool deepsleep) { + // in some frequency bands we need to wait until another channel is available before transmitting again + // wait at least 10s between transmissions + uint32_t delay_s = dot->getNextTxMs() / 1000; + if (delay_s < 10) { + delay_s = 10; + } + + logInfo("%ssleeping %lus", deepsleep ? "deep" : "", delay_s); + logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume"); + + // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors + // the library handles all internal IOs automatically, but the external IOs are the application's responsibility + // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption + // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE + // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1 + // steps are: + // * save IO configuration + // * configure IOs to reduce current consumption + // * sleep + // * restore IO configuration + if (! deepsleep) { + // save the GPIO state. + sleep_save_io(); + + // configure GPIOs for lowest current + sleep_configure_io(); + } + + // go to sleep/deepsleep for delay_s seconds and wake using the RTC alarm + dot->sleep(delay_s, mDot::RTC_ALARM, deepsleep); + + if (! deepsleep) { + // restore the GPIO state. + sleep_restore_io(); + } +} + +void sleep_wake_interrupt_only(bool deepsleep) { +#if defined (TARGET_XDOT_L151CC) + if (deepsleep) { + // for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep + // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call + } else { + // configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes + // other pins can be confgured instead: GPIO0-3 or UART_RX + dot->setWakePin(WAKE); + } + + logInfo("%ssleeping until interrupt on %s pin", deepsleep ? "deep" : "", deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str()); +#else + + if (deepsleep) { + // for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep + // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call + } else { + // configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes + // other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN + dot->setWakePin(XBEE_DIO7); + } + + logInfo("%ssleeping until interrupt on %s pin", deepsleep ? "deep" : "", deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str()); +#endif + + logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume"); + + // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors + // the library handles all internal IOs automatically, but the external IOs are the application's responsibility + // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption + // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE + // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1 + // steps are: + // * save IO configuration + // * configure IOs to reduce current consumption + // * sleep + // * restore IO configuration + if (! deepsleep) { + // save the GPIO state. + sleep_save_io(); + + // configure GPIOs for lowest current + sleep_configure_io(); + } + + // go to sleep/deepsleep and wake on rising edge of configured wake pin (only the WAKE pin in deepsleep) + // since we're not waking on the RTC alarm, the interval is ignored + dot->sleep(0, mDot::INTERRUPT, deepsleep); + + if (! deepsleep) { + // restore the GPIO state. + sleep_restore_io(); + } +} + +void sleep_wake_rtc_or_interrupt(bool deepsleep) { + // in some frequency bands we need to wait until another channel is available before transmitting again + // wait at least 10s between transmissions + uint32_t delay_s = dot->getNextTxMs() / 1000; + if (delay_s < 10) { + delay_s = 10; + } + +#if defined (TARGET_XDOT_L151CC) + if (deepsleep) { + // for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep + // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call + } else { + // configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes + // other pins can be confgured instead: GPIO0-3 or UART_RX + dot->setWakePin(WAKE); + } + + logInfo("%ssleeping %lus or until interrupt on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str()); +#else + if (deepsleep) { + // for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep + // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call + } else { + // configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes + // other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN + dot->setWakePin(XBEE_DIO7); + } + + logInfo("%ssleeping %lus or until interrupt on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str()); +#endif + + logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume"); + + // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors + // the library handles all internal IOs automatically, but the external IOs are the application's responsibility + // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption + // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE + // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1 + // steps are: + // * save IO configuration + // * configure IOs to reduce current consumption + // * sleep + // * restore IO configuration + if (! deepsleep) { + // save the GPIO state. + sleep_save_io(); + + // configure GPIOs for lowest current + sleep_configure_io(); + } + + // go to sleep/deepsleep and wake using the RTC alarm after delay_s seconds or rising edge of configured wake pin (only the WAKE pin in deepsleep) + // whichever comes first will wake the xDot + dot->sleep(delay_s, mDot::RTC_ALARM_OR_INTERRUPT, deepsleep); + + if (! deepsleep) { + // restore the GPIO state. + sleep_restore_io(); + } +} + +void sleep_save_io() { +#if defined(TARGET_XDOT_L151CC) + xdot_save_gpio_state(); +#else + portA[0] = GPIOA->MODER; + portA[1] = GPIOA->OTYPER; + portA[2] = GPIOA->OSPEEDR; + portA[3] = GPIOA->PUPDR; + portA[4] = GPIOA->AFR[0]; + portA[5] = GPIOA->AFR[1]; + + portB[0] = GPIOB->MODER; + portB[1] = GPIOB->OTYPER; + portB[2] = GPIOB->OSPEEDR; + portB[3] = GPIOB->PUPDR; + portB[4] = GPIOB->AFR[0]; + portB[5] = GPIOB->AFR[1]; + + portC[0] = GPIOC->MODER; + portC[1] = GPIOC->OTYPER; + portC[2] = GPIOC->OSPEEDR; + portC[3] = GPIOC->PUPDR; + portC[4] = GPIOC->AFR[0]; + portC[5] = GPIOC->AFR[1]; + + portD[0] = GPIOD->MODER; + portD[1] = GPIOD->OTYPER; + portD[2] = GPIOD->OSPEEDR; + portD[3] = GPIOD->PUPDR; + portD[4] = GPIOD->AFR[0]; + portD[5] = GPIOD->AFR[1]; + + portH[0] = GPIOH->MODER; + portH[1] = GPIOH->OTYPER; + portH[2] = GPIOH->OSPEEDR; + portH[3] = GPIOH->PUPDR; + portH[4] = GPIOH->AFR[0]; + portH[5] = GPIOH->AFR[1]; +#endif +} + +void sleep_configure_io() { +#if defined(TARGET_XDOT_L151CC) + // GPIO Ports Clock Enable + __GPIOA_CLK_ENABLE(); + __GPIOB_CLK_ENABLE(); + __GPIOC_CLK_ENABLE(); + __GPIOH_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct; + + // UART1_TX, UART1_RTS & UART1_CTS to analog nopull - RX could be a wakeup source + GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + // I2C_SDA & I2C_SCL to analog nopull + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + // SPI_MOSI, SPI_MISO, SPI_SCK, & SPI_NSS to analog nopull + GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + // iterate through potential wake pins - leave the configured wake pin alone if one is needed + if (dot->getWakePin() != WAKE || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_0; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + if (dot->getWakePin() != GPIO0 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + if (dot->getWakePin() != GPIO1 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + if (dot->getWakePin() != GPIO2 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_0; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + } + if (dot->getWakePin() != GPIO3 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + } + if (dot->getWakePin() != UART1_RX || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } +#else + /* GPIO Ports Clock Enable */ + __GPIOA_CLK_ENABLE(); + __GPIOB_CLK_ENABLE(); + __GPIOC_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct; + + // XBEE_DOUT, XBEE_DIN, XBEE_DO8, XBEE_RSSI, USBTX, USBRX, PA_12, PA_13, PA_14 & PA_15 to analog nopull + GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_6 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 + | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + // PB_0, PB_1, PB_3 & PB_4 to analog nopull + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + // PC_9 & PC_13 to analog nopull + GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_13; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + // iterate through potential wake pins - leave the configured wake pin alone if one is needed + // XBEE_DIN - PA3 + // XBEE_DIO2 - PA5 + // XBEE_DIO3 - PA4 + // XBEE_DIO4 - PA7 + // XBEE_DIO5 - PC1 + // XBEE_DIO6 - PA1 + // XBEE_DIO7 - PA0 + // XBEE_SLEEPRQ - PA11 + + if (dot->getWakePin() != XBEE_DIN || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + + if (dot->getWakePin() != XBEE_DIO2 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + + if (dot->getWakePin() != XBEE_DIO3 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + + if (dot->getWakePin() != XBEE_DIO4 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + + if (dot->getWakePin() != XBEE_DIO5 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + } + + if (dot->getWakePin() != XBEE_DIO6 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + + if (dot->getWakePin() != XBEE_DIO7 || dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_0; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + + if (dot->getWakePin() != XBEE_SLEEPRQ|| dot->getWakeMode() == mDot::RTC_ALARM) { + GPIO_InitStruct.Pin = GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } +#endif +} + +void sleep_restore_io() { +#if defined(TARGET_XDOT_L151CC) + xdot_restore_gpio_state(); +#else + GPIOA->MODER = portA[0]; + GPIOA->OTYPER = portA[1]; + GPIOA->OSPEEDR = portA[2]; + GPIOA->PUPDR = portA[3]; + GPIOA->AFR[0] = portA[4]; + GPIOA->AFR[1] = portA[5]; + + GPIOB->MODER = portB[0]; + GPIOB->OTYPER = portB[1]; + GPIOB->OSPEEDR = portB[2]; + GPIOB->PUPDR = portB[3]; + GPIOB->AFR[0] = portB[4]; + GPIOB->AFR[1] = portB[5]; + + GPIOC->MODER = portC[0]; + GPIOC->OTYPER = portC[1]; + GPIOC->OSPEEDR = portC[2]; + GPIOC->PUPDR = portC[3]; + GPIOC->AFR[0] = portC[4]; + GPIOC->AFR[1] = portC[5]; + + GPIOD->MODER = portD[0]; + GPIOD->OTYPER = portD[1]; + GPIOD->OSPEEDR = portD[2]; + GPIOD->PUPDR = portD[3]; + GPIOD->AFR[0] = portD[4]; + GPIOD->AFR[1] = portD[5]; + + GPIOH->MODER = portH[0]; + GPIOH->OTYPER = portH[1]; + GPIOH->OSPEEDR = portH[2]; + GPIOH->PUPDR = portH[3]; + GPIOH->AFR[0] = portH[4]; + GPIOH->AFR[1] = portH[5]; +#endif +} + +void send_data(std::vector<uint8_t> data) { + int32_t ret; + + ret = dot->send(data); + if (ret != mDot::MDOT_OK) { + logError("failed to send data to %s [%d][%s]", dot->getJoinMode() == mDot::PEER_TO_PEER ? "peer" : "gateway", ret, mDot::getReturnCodeString(ret).c_str()); + } else { + logInfo("successfully sent data to %s", dot->getJoinMode() == mDot::PEER_TO_PEER ? "peer" : "gateway"); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/src/manual_example.cpp Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,181 @@ +#include "dot_util.h" +#include "RadioEvent.h" + +#if ACTIVE_EXAMPLE == MANUAL_EXAMPLE + +///////////////////////////////////////////////////////////////////////////// +// -------------------- DOT LIBRARY REQUIRED ------------------------------// +// * Because these example programs can be used for both mDot and xDot // +// devices, the LoRa stack is not included. The libmDot library should // +// be imported if building for mDot devices. The libxDot library // +// should be imported if building for xDot devices. // +// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // +///////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////////////////// +// * 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 = true; +static uint8_t join_delay = 5; +static uint8_t ack = 1; +static bool adr = true; + +// 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; +lora::ChannelPlan* plan = 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() { + // Custom event handler for automatically displaying RX data + RadioEvent events; + + pc.baud(115200); + +#if defined(TARGET_XDOT_L151CC) + i2c.frequency(400000); +#endif + + mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); + +#if CHANNEL_PLAN == CP_US915 + plan = new lora::ChannelPlan_US915(); +#elif CHANNEL_PLAN == CP_AU915 + plan = new lora::ChannelPlan_AU915(); +#elif CHANNEL_PLAN == CP_EU868 + plan = new lora::ChannelPlan_EU868(); +#elif CHANNEL_PLAN == CP_KR920 + plan = new lora::ChannelPlan_KR920(); +#elif CHANNEL_PLAN == CP_AS923 + plan = new lora::ChannelPlan_AS923(); +#elif CHANNEL_PLAN == CP_AS923_JAPAN + plan = new lora::ChannelPlan_AS923_Japan(); +#elif CHANNEL_PLAN == CP_IN865 + plan = new lora::ChannelPlan_IN865(); +#endif + assert(plan); + + dot = mDot::getInstance(plan); + assert(dot); + + // attach the custom events handler + dot->setEvents(&events); + + if (!dot->getStandbyFlag()) { + logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); + + // start from a well-known state + logInfo("defaulting Dot configuration"); + dot->resetConfig(); + dot->resetNetworkSession(); + + // 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); + + // enable or disable Adaptive Data Rate + dot->setAdr(adr); + + // Configure the join delay + dot->setJoinDelay(join_delay); + + // save changes to configuration + logInfo("saving configuration"); + if (!dot->saveConfig()) { + logError("failed to save configuration"); + } + + // display configuration + display_config(); + } else { + // 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 + logInfo("restoring network session from NVM"); + dot->restoreNetworkSession(); + } + + 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/src/ota_example.cpp Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,234 @@ +#include "dot_util.h" +#include "RadioEvent.h" +#include "string.h" +#if ACTIVE_EXAMPLE == OTA_EXAMPLE + +static std::string network_name = "MultiTech"; +static std::string network_passphrase = "MultiTech"; +static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; +static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; +static uint8_t frequency_sub_band = 0; +static bool public_network = true; +static uint8_t join_delay = 5; +static uint8_t ack = 1; +static bool adr = true; + +static bool deep_sleep = false; + +mDot* dot = NULL; +lora::ChannelPlan* plan = NULL; + +Serial pc(USBTX, USBRX); + +char buffer_data[20]; + +int main() { + // Custom event handler for automatically displaying RX data + RadioEvent events; + pc.baud(115200); + + int _array_Tx[7] = {2,5,8,11,14,17,20}; + // Caution : at here convert position follow position of _array_Tx + int _min_Tx = 3; // ~ Tx = 11 dBm = _array_Tx[3] + int _max_Tx = 3; // ~ Tx = 11 dBm + + int _array_DR[8] = {0,1,2,3,4,5,6,7}; + int _min_DR = 2; + int _max_DR = 2; + wait(2); + + /////////////////////////////// function + + mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); + + #if CHANNEL_PLAN == CP_US915 + plan = new lora::ChannelPlan_US915(); + #elif CHANNEL_PLAN == CP_AS923 + plan = new lora::ChannelPlan_AS923(); + #endif + assert(plan); + + dot = mDot::getInstance(plan); + assert(dot); + + // attach the custom events handler + dot->setEvents(&events); + + if (!dot->getStandbyFlag()) + { + logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); + + // start from a well-known state + logInfo("defaulting Dot configuration"); + dot->resetConfig(); + dot->resetNetworkSession(); + + // make sure library logging is turned on + dot->setLogLevel(mts::MTSLog::INFO_LEVEL); + + // update configuration if necessary + if (dot->getJoinMode() != mDot::OTA) + { + logInfo("changing network join mode to OTA"); + if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) + { + logError("failed to set network join mode to OTA"); + } + } + // 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 + // only one method or the other should be used! + // network ID = crc64(network name) + // network KEY = cmac(network passphrase) + update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); + //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack); + + // configure network link checks + // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots + // check the link every count packets + // declare the Dot disconnected after threshold failed link checks + // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row + update_network_link_check_config(3, 5); + + // enable or disable Adaptive Data Rate + // dot->setAdr(adr); + // dot->setTxDataRate(2); + + // dot->setTxPower(5); + // logInfo(" du lieu TxPower %u",dot->getTxPower()); + // Configure the join delay + dot->setJoinDelay(join_delay); + + // save changes to configuration + logInfo("saving configuration"); + if (!dot->saveConfig()) { + logError("failed to save configuration"); + } + // display configuration + display_config(); + } + else + { + // 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 + logInfo("restoring network session from NVM"); + dot->restoreNetworkSession(); + } + /////////////////////////////////////////// end function + + while(true) // intial infinite loop from here + { + int _value_loop = 10; + + pc.printf(" START begin now - please press button OPEN on APP SERIAL to BEGIN --- "); + // fix at here + while (_value_loop) + { + + pc.scanf("%s",&buffer_data); + pc.printf(" DATA received %s \n ", buffer_data); + if(buffer_data[0] == 'T' && buffer_data[1] =='x' && buffer_data[6] =='P') + { + _value_loop = 0; + break; + } + } + + // check string config TxDR00PWR11TO17OK + if(buffer_data[0] != 'T' && buffer_data[1] !='x' && buffer_data[6] !='P') + return 0; + + // process string config + int _array_get_config[6] ={0,0,0,0,0,0}; + int _count_config = 0; + for(int run = 0; run < sizeof(buffer_data); run++) + { + if( buffer_data[run] >='0' && buffer_data[run] <= '9') + { + _array_get_config[_count_config] = int (buffer_data[run] -48); + _count_config ++; + } + } + + wait(2); + // set DR and Tx + pc.printf(" Get value Done \n "); + _min_DR = _array_get_config[0]; + _max_DR = _array_get_config[1]; + _min_Tx = _array_get_config[2]; + _max_Tx = _array_get_config[3]; + // wait for stable + wait(5); + + // function mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); ===>> dot->restoreNetworkSession(); + + int _count_while = 1; + while (_count_while-- >0) + { + std::vector<uint8_t> tx_data; + tx_data.clear(); + tx_data.push_back(0x1D); + tx_data.push_back(0x1A); + tx_data.push_back(0x1B); + tx_data.push_back(0xBB); + tx_data.push_back(0xCC); + tx_data.push_back(0xDD); + tx_data.push_back(0xEE); + tx_data.push_back(0xFF); + tx_data.push_back(0x1D); + + // join network if not joined + + for(int x = _min_Tx ; x <= _max_Tx ; x++) + { + + //dot->setTxPower(_array_Tx[x]); + if (dot->setTxPower(_array_Tx[x]) != mDot::MDOT_OK) { + logError("failed to set TX power to %u", _array_Tx[x]); + } + else{ + logInfo("\n Data with POWER TRANSMITION %d ============= TESTING ==== \n",_array_Tx[x]); + } + wait(3); + + for(int i = _min_DR; i <= _max_DR ; i++) + { + + //dot->setTxDataRate(_array_DR[i]); + if (dot->setTxDataRate(_array_DR[i]) != mDot::MDOT_OK) { + logError("failed to set TX datarate to %u", _array_DR[i]); + } + else{ + logInfo(" === Data with DR %d ============= TESTING == \n",i); + } + + if (!dot->getNetworkJoinStatus()) + { + join_network(); + } + + for(int i = 0; i <3; i++ ) + { + + send_data(tx_data); + wait(3); + pc.printf(" SENDING data test ========== \n"); + pc.printf(" RSSI =%d and SNR =%d >>>> OK Done \n",(dot->getRssiStats()).last,(dot->getSnrStats()).last); + } + + } + }//for + + } + + logInfo("\n Data with POWER TRANSMITION %u DR ; va %u PWR \n",dot->getTxDataRate(),dot->getTxPower()); + pc.printf("@@@@@@@@@@ THE END test signal @@@@@@@@@@@@@@@@@@@@@@@@ \n \n \n "); + + buffer_data[0] ='N'; + buffer_data[1] ='A'; + _value_loop = 10; + + } // end from infinite loop + //return 0; +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/src/peer_to_peer_example.cpp Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,204 @@ +#include "dot_util.h" +#include "RadioEvent.h" + +#if ACTIVE_EXAMPLE == PEER_TO_PEER_EXAMPLE + +///////////////////////////////////////////////////////////////////////////// +// -------------------- DOT LIBRARY REQUIRED ------------------------------// +// * Because these example programs can be used for both mDot and xDot // +// devices, the LoRa stack is not included. The libmDot library should // +// be imported if building for mDot devices. The libxDot library // +// should be imported if building for xDot devices. // +// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // +// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // +///////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////////////////// +// * these options must match between the two devices in // +// order for communication to be successful +///////////////////////////////////////////////////////////// +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 }; + +mDot* dot = NULL; +lora::ChannelPlan* plan = 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() { + // Custom event handler for automatically displaying RX data + RadioEvent events; + uint32_t tx_frequency; + uint8_t tx_datarate; + uint8_t tx_power; + uint8_t frequency_band; + + pc.baud(115200); + +#if defined(TARGET_XDOT_L151CC) + i2c.frequency(400000); +#endif + + mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); + +#if CHANNEL_PLAN == CP_US915 + plan = new lora::ChannelPlan_US915(); +#elif CHANNEL_PLAN == CP_AU915 + plan = new lora::ChannelPlan_AU915(); +#elif CHANNEL_PLAN == CP_EU868 + plan = new lora::ChannelPlan_EU868(); +#elif CHANNEL_PLAN == CP_KR920 + plan = new lora::ChannelPlan_KR920(); +#elif CHANNEL_PLAN == CP_AS923 + plan = new lora::ChannelPlan_AS923(); +#elif CHANNEL_PLAN == CP_AS923_JAPAN + plan = new lora::ChannelPlan_AS923_Japan(); +#elif CHANNEL_PLAN == CP_IN865 + plan = new lora::ChannelPlan_IN865(); +#endif + assert(plan); + + dot = mDot::getInstance(plan); + assert(dot); + + logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); + + // start from a well-known state + logInfo("defaulting Dot configuration"); + dot->resetConfig(); + + // make sure library logging is turned on + dot->setLogLevel(mts::MTSLog::INFO_LEVEL); + + // attach the custom events handler + dot->setEvents(&events); + + // update configuration if necessary + if (dot->getJoinMode() != mDot::PEER_TO_PEER) { + logInfo("changing network join mode to PEER_TO_PEER"); + if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) { + logError("failed to set network join mode to PEER_TO_PEER"); + } + } + frequency_band = dot->getFrequencyBand(); + switch (frequency_band) { + case lora::ChannelPlan::EU868_OLD: + case lora::ChannelPlan::EU868: + // 250kHz channels achieve higher throughput + // DR_6 : SF7 @ 250kHz + // DR_0 - DR_5 (125kHz channels) available but much slower + tx_frequency = 869850000; + tx_datarate = lora::DR_6; + // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7 + tx_power = 4; + break; + + case lora::ChannelPlan::US915_OLD: + case lora::ChannelPlan::US915: + case lora::ChannelPlan::AU915_OLD: + case lora::ChannelPlan::AU915: + // 500kHz channels achieve highest throughput + // DR_8 : SF12 @ 500kHz + // DR_9 : SF11 @ 500kHz + // DR_10 : SF10 @ 500kHz + // DR_11 : SF9 @ 500kHz + // DR_12 : SF8 @ 500kHz + // DR_13 : SF7 @ 500kHz + // DR_0 - DR_3 (125kHz channels) available but much slower + tx_frequency = 915500000; + tx_datarate = lora::DR_13; + // 915 bands have no duty cycle restrictions, set tx power to max + tx_power = 20; + break; + + case lora::ChannelPlan::AS923: + case lora::ChannelPlan::AS923_JAPAN: + // 250kHz channels achieve higher throughput + // DR_6 : SF7 @ 250kHz + // DR_0 - DR_5 (125kHz channels) available but much slower + tx_frequency = 924800000; + tx_datarate = lora::DR_6; + tx_power = 16; + break; + + case lora::ChannelPlan::KR920: + // DR_5 : SF7 @ 125kHz + tx_frequency = 922700000; + tx_datarate = lora::DR_5; + tx_power = 14; + break; + + default: + while (true) { + logFatal("no known channel plan in use - extra configuration is needed!"); + wait(5); + } + break; + } + // in PEER_TO_PEER mode there is no join request/response transaction + // as long as both Dots are configured correctly, they should be able to communicate + update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power); + + // save changes to configuration + logInfo("saving configuration"); + if (!dot->saveConfig()) { + logError("failed to save configuration"); + } + + // display configuration + display_config(); + + while (true) { + uint16_t light; + std::vector<uint8_t> tx_data; + + // join network if not joined + if (!dot->getNetworkJoinStatus()) { + join_network(); + } + +#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 + + // the Dot can't sleep in PEER_TO_PEER mode + // it must be waiting for data from the other Dot + // send data every 5 seconds + logInfo("waiting for 5s"); + wait(5); + } + + return 0; +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmDot-mbed5.lib Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/teams/MultiTech/code/libmDot-mbed5/#ed37629c03cd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#fc1836545dcc2fc86f03b01292b62bf2089f67c3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.sh Tue Oct 16 08:05:27 2018 +0000 @@ -0,0 +1,3 @@ +if [ ! -d ISL29011 ]; then + hg clone https://developer.mbed.org/teams/Multi-Hackers/code/ISL29011/ +fi