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 picojson ISL29011
Revision 0:a151a6350d7f, committed 2016-10-05
- Comitter:
- mfiore
- Date:
- Wed Oct 05 21:07:50 2016 +0000
- Child:
- 1:c4915e00d2ce
- Commit message:
- initial commit of OTA and AUTO_OTA examples
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/example_config.h Wed Oct 05 21:07:50 2016 +0000 @@ -0,0 +1,13 @@ +#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 +#define ACTIVE_EXAMPLE OTA_EXAMPLE + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/inc/dot_util.h Wed Oct 05 21:07:50 2016 +0000 @@ -0,0 +1,27 @@ +#ifndef __DOT_UTIL_H__ +#define __DOT_UTIL_H__ + +#include "mbed.h" +#include "mDot.h" +#include "MTSLog.h" +#include "MTSText.h" +#include "ISL29011.h" +#include "example_config.h" + +extern mDot* dot; + +void display_config(); + +void update_ota_config(std::string network_name, std::string network_passphrase, uint8_t frequency_sub_band, bool public_network, uint8_t ack); + +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 send_data(std::vector<uint8_t> data); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/lib/ISL29011.lib Wed Oct 05 21:07:50 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/Multi-Hackers/code/ISL29011/#f46f74107f9e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/src/auto_ota_example.cpp Wed Oct 05 21:07:50 2016 +0000
@@ -0,0 +1,96 @@
+#include "dot_util.h"
+
+#if ACTIVE_EXAMPLE == AUTO_OTA_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 std::string network_name = "mikes_lora_network";
+static std::string network_passphrase = "password_123";
+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 = false;
+
+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, then display it
+ // 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");
+ }
+ }
+ update_ota_config(network_name, network_passphrase, frequency_sub_band, public_network, ack);
+ 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/dot_util.cpp Wed Oct 05 21:07:50 2016 +0000
@@ -0,0 +1,186 @@
+#include "dot_util.h"
+
+void display_config() {
+ // display configuration and library version information
+ logInfo("version: %s", dot->getId().c_str());
+ logInfo("general configuration");
+ logInfo("---------------------");
+ logInfo("\tdevice ID/EUI: %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str());
+ logInfo("\tfrequency band: %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str());
+ logInfo("\tfrequency sub band: %u", dot->getFrequencySubBand());
+ 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 join mode: %s", mDot::JoinModeStr(dot->getJoinMode()).c_str());
+ logInfo("communication parameters");
+ logInfo("------------------------");
+ logInfo("\tacks: %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck());
+ logInfo("\tTX datarate: %s", mDot::DataRateStr(dot->getTxDataRate()).c_str());
+ logInfo("\tTX power: %lu dBm", dot->getTxPower());
+ logInfo("\tatnenna gain: %u dBm", dot->getAntennaGain());
+}
+
+void update_ota_config(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 (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;
+
+ // 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 < 2) {
+ 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");
+
+ // go to sleep/deepsleep for delay_s seconds and wake using the RTC alarm
+ dot->sleep(delay_s, mDot::RTC_ALARM, deepsleep);
+}
+
+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 rising edge 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 rising edge 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");
+
+ // 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);
+}
+
+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 rising edge 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 rising edge 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");
+
+ // 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);
+}
+
+void send_data(std::vector<uint8_t> data) {
+ uint32_t ret;
+
+ ret = dot->send(data);
+ if (ret != mDot::MDOT_OK) {
+ logError("failed to send light data to gateway [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
+ } else {
+ logInfo("successfully sent light data to gateway");
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/src/ota_example.cpp Wed Oct 05 21:07:50 2016 +0000
@@ -0,0 +1,109 @@
+#include "dot_util.h"
+
+#if ACTIVE_EXAMPLE == OTA_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 std::string network_name = "mikes_lora_network";
+static std::string network_passphrase = "password_123";
+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 = false;
+
+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, then display it
+ 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");
+ }
+ }
+ update_ota_config(network_name, network_passphrase, frequency_sub_band, public_network, ack);
+ display_config();
+
+ // restore the saved session if the dot woke from deepsleep mode
+ // useful to use with deepsleep because RAM session info is otherwise lost when the dot enters deepsleep
+ if (dot->getStandbyFlag()) {
+ logInfo("restoring network session from NVM");
+ dot->restoreNetworkSession();
+ }
+
+ 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
+
+ // 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/mbed-os.lib Wed Oct 05 21:07:50 2016 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#670b0984ebf1207771ef5afbc1c64f85af349da1