mdot-examples triage

Dependencies:   libmDot-dev-mbed5-deprecated ISL29011

Committer:
Maciej Pijanowski
Date:
Thu Jun 29 19:54:41 2017 +0200
Revision:
0:f3d9c16b08d7
manual single channel example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Maciej Pijanowski 0:f3d9c16b08d7 1 #include "dot_util.h"
Maciej Pijanowski 0:f3d9c16b08d7 2 #include "RadioEvent.h"
Maciej Pijanowski 0:f3d9c16b08d7 3
Maciej Pijanowski 0:f3d9c16b08d7 4 #if ACTIVE_EXAMPLE == CLASS_C_EXAMPLE
Maciej Pijanowski 0:f3d9c16b08d7 5
Maciej Pijanowski 0:f3d9c16b08d7 6 /////////////////////////////////////////////////////////////////////////////
Maciej Pijanowski 0:f3d9c16b08d7 7 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
Maciej Pijanowski 0:f3d9c16b08d7 8 // * Because these example programs can be used for both mDot and xDot //
Maciej Pijanowski 0:f3d9c16b08d7 9 // devices, the LoRa stack is not included. The libmDot library should //
Maciej Pijanowski 0:f3d9c16b08d7 10 // be imported if building for mDot devices. The libxDot library //
Maciej Pijanowski 0:f3d9c16b08d7 11 // should be imported if building for xDot devices. //
Maciej Pijanowski 0:f3d9c16b08d7 12 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
Maciej Pijanowski 0:f3d9c16b08d7 13 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
Maciej Pijanowski 0:f3d9c16b08d7 14 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
Maciej Pijanowski 0:f3d9c16b08d7 15 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
Maciej Pijanowski 0:f3d9c16b08d7 16 /////////////////////////////////////////////////////////////////////////////
Maciej Pijanowski 0:f3d9c16b08d7 17
Maciej Pijanowski 0:f3d9c16b08d7 18 /////////////////////////////////////////////////////////////
Maciej Pijanowski 0:f3d9c16b08d7 19 // * these options must match the settings on your gateway //
Maciej Pijanowski 0:f3d9c16b08d7 20 // * edit their values to match your configuration //
Maciej Pijanowski 0:f3d9c16b08d7 21 // * frequency sub band is only relevant for the 915 bands //
Maciej Pijanowski 0:f3d9c16b08d7 22 // * either the network name and passphrase can be used or //
Maciej Pijanowski 0:f3d9c16b08d7 23 // the network ID (8 bytes) and KEY (16 bytes) //
Maciej Pijanowski 0:f3d9c16b08d7 24 /////////////////////////////////////////////////////////////
Maciej Pijanowski 0:f3d9c16b08d7 25 static std::string network_name = "MultiTech";
Maciej Pijanowski 0:f3d9c16b08d7 26 static std::string network_passphrase = "MultiTech";
Maciej Pijanowski 0:f3d9c16b08d7 27 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
Maciej Pijanowski 0:f3d9c16b08d7 28 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
Maciej Pijanowski 0:f3d9c16b08d7 29 static uint8_t frequency_sub_band = 0;
Maciej Pijanowski 0:f3d9c16b08d7 30 static bool public_network = false;
Maciej Pijanowski 0:f3d9c16b08d7 31 static uint8_t ack = 1;
Maciej Pijanowski 0:f3d9c16b08d7 32 static bool adr = true;
Maciej Pijanowski 0:f3d9c16b08d7 33
Maciej Pijanowski 0:f3d9c16b08d7 34 mDot* dot = NULL;
Maciej Pijanowski 0:f3d9c16b08d7 35 lora::ChannelPlan* plan = NULL;
Maciej Pijanowski 0:f3d9c16b08d7 36
Maciej Pijanowski 0:f3d9c16b08d7 37 Serial pc(USBTX, USBRX);
Maciej Pijanowski 0:f3d9c16b08d7 38
Maciej Pijanowski 0:f3d9c16b08d7 39 #if defined(TARGET_XDOT_L151CC)
Maciej Pijanowski 0:f3d9c16b08d7 40 I2C i2c(I2C_SDA, I2C_SCL);
Maciej Pijanowski 0:f3d9c16b08d7 41 ISL29011 lux(i2c);
Maciej Pijanowski 0:f3d9c16b08d7 42 #else
Maciej Pijanowski 0:f3d9c16b08d7 43 AnalogIn lux(XBEE_AD0);
Maciej Pijanowski 0:f3d9c16b08d7 44 #endif
Maciej Pijanowski 0:f3d9c16b08d7 45
Maciej Pijanowski 0:f3d9c16b08d7 46 int main() {
Maciej Pijanowski 0:f3d9c16b08d7 47 // Custom event handler for automatically displaying RX data
Maciej Pijanowski 0:f3d9c16b08d7 48 RadioEvent events;
Maciej Pijanowski 0:f3d9c16b08d7 49
Maciej Pijanowski 0:f3d9c16b08d7 50 pc.baud(115200);
Maciej Pijanowski 0:f3d9c16b08d7 51
Maciej Pijanowski 0:f3d9c16b08d7 52 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Maciej Pijanowski 0:f3d9c16b08d7 53
Maciej Pijanowski 0:f3d9c16b08d7 54 #if CHANNEL_PLAN == CP_US915
Maciej Pijanowski 0:f3d9c16b08d7 55 plan = new lora::ChannelPlan_US915();
Maciej Pijanowski 0:f3d9c16b08d7 56 #elif CHANNEL_PLAN == CP_AU915
Maciej Pijanowski 0:f3d9c16b08d7 57 plan = new lora::ChannelPlan_AU915();
Maciej Pijanowski 0:f3d9c16b08d7 58 #elif CHANNEL_PLAN == CP_EU868
Maciej Pijanowski 0:f3d9c16b08d7 59 plan = new lora::ChannelPlan_EU868();
Maciej Pijanowski 0:f3d9c16b08d7 60 #elif CHANNEL_PLAN == CP_KR920
Maciej Pijanowski 0:f3d9c16b08d7 61 plan = new lora::ChannelPlan_KR920();
Maciej Pijanowski 0:f3d9c16b08d7 62 #elif CHANNEL_PLAN == CP_AS923
Maciej Pijanowski 0:f3d9c16b08d7 63 plan = new lora::ChannelPlan_AS923();
Maciej Pijanowski 0:f3d9c16b08d7 64 #elif CHANNEL_PLAN == CP_AS923_JAPAN
Maciej Pijanowski 0:f3d9c16b08d7 65 plan = new lora::ChannelPlan_AS923_Japan();
Maciej Pijanowski 0:f3d9c16b08d7 66 #endif
Maciej Pijanowski 0:f3d9c16b08d7 67 assert(plan);
Maciej Pijanowski 0:f3d9c16b08d7 68
Maciej Pijanowski 0:f3d9c16b08d7 69 dot = mDot::getInstance(plan);
Maciej Pijanowski 0:f3d9c16b08d7 70 assert(dot);
Maciej Pijanowski 0:f3d9c16b08d7 71
Maciej Pijanowski 0:f3d9c16b08d7 72 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Maciej Pijanowski 0:f3d9c16b08d7 73
Maciej Pijanowski 0:f3d9c16b08d7 74 // start from a well-known state
Maciej Pijanowski 0:f3d9c16b08d7 75 logInfo("defaulting Dot configuration");
Maciej Pijanowski 0:f3d9c16b08d7 76 dot->resetConfig();
Maciej Pijanowski 0:f3d9c16b08d7 77 dot->resetNetworkSession();
Maciej Pijanowski 0:f3d9c16b08d7 78
Maciej Pijanowski 0:f3d9c16b08d7 79 // make sure library logging is turned on
Maciej Pijanowski 0:f3d9c16b08d7 80 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
Maciej Pijanowski 0:f3d9c16b08d7 81
Maciej Pijanowski 0:f3d9c16b08d7 82 // attach the custom events handler
Maciej Pijanowski 0:f3d9c16b08d7 83 dot->setEvents(&events);
Maciej Pijanowski 0:f3d9c16b08d7 84
Maciej Pijanowski 0:f3d9c16b08d7 85 // update configuration if necessary
Maciej Pijanowski 0:f3d9c16b08d7 86 if (dot->getJoinMode() != mDot::OTA) {
Maciej Pijanowski 0:f3d9c16b08d7 87 logInfo("changing network join mode to OTA");
Maciej Pijanowski 0:f3d9c16b08d7 88 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
Maciej Pijanowski 0:f3d9c16b08d7 89 logError("failed to set network join mode to OTA");
Maciej Pijanowski 0:f3d9c16b08d7 90 }
Maciej Pijanowski 0:f3d9c16b08d7 91 }
Maciej Pijanowski 0:f3d9c16b08d7 92 // 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
Maciej Pijanowski 0:f3d9c16b08d7 93 // only one method or the other should be used!
Maciej Pijanowski 0:f3d9c16b08d7 94 // network ID = crc64(network name)
Maciej Pijanowski 0:f3d9c16b08d7 95 // network KEY = cmac(network passphrase)
Maciej Pijanowski 0:f3d9c16b08d7 96 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
Maciej Pijanowski 0:f3d9c16b08d7 97 //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
Maciej Pijanowski 0:f3d9c16b08d7 98
Maciej Pijanowski 0:f3d9c16b08d7 99 // configure the Dot for class C operation
Maciej Pijanowski 0:f3d9c16b08d7 100 // the Dot must also be configured on the gateway for class C
Maciej Pijanowski 0:f3d9c16b08d7 101 // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
Maciej Pijanowski 0:f3d9c16b08d7 102 // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation
Maciej Pijanowski 0:f3d9c16b08d7 103 logInfo("changing network mode to class C");
Maciej Pijanowski 0:f3d9c16b08d7 104 if (dot->setClass("C") != mDot::MDOT_OK) {
Maciej Pijanowski 0:f3d9c16b08d7 105 logError("failed to set network mode to class C");
Maciej Pijanowski 0:f3d9c16b08d7 106 }
Maciej Pijanowski 0:f3d9c16b08d7 107
Maciej Pijanowski 0:f3d9c16b08d7 108 // enable or disable Adaptive Data Rate
Maciej Pijanowski 0:f3d9c16b08d7 109 dot->setAdr(adr);
Maciej Pijanowski 0:f3d9c16b08d7 110
Maciej Pijanowski 0:f3d9c16b08d7 111 // save changes to configuration
Maciej Pijanowski 0:f3d9c16b08d7 112 logInfo("saving configuration");
Maciej Pijanowski 0:f3d9c16b08d7 113 if (!dot->saveConfig()) {
Maciej Pijanowski 0:f3d9c16b08d7 114 logError("failed to save configuration");
Maciej Pijanowski 0:f3d9c16b08d7 115 }
Maciej Pijanowski 0:f3d9c16b08d7 116
Maciej Pijanowski 0:f3d9c16b08d7 117 // display configuration
Maciej Pijanowski 0:f3d9c16b08d7 118 display_config();
Maciej Pijanowski 0:f3d9c16b08d7 119
Maciej Pijanowski 0:f3d9c16b08d7 120 #if defined(TARGET_XDOT_L151CC)
Maciej Pijanowski 0:f3d9c16b08d7 121 // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
Maciej Pijanowski 0:f3d9c16b08d7 122 lux.setMode(ISL29011::ALS_CONT);
Maciej Pijanowski 0:f3d9c16b08d7 123 lux.setResolution(ISL29011::ADC_16BIT);
Maciej Pijanowski 0:f3d9c16b08d7 124 lux.setRange(ISL29011::RNG_64000);
Maciej Pijanowski 0:f3d9c16b08d7 125 #endif
Maciej Pijanowski 0:f3d9c16b08d7 126
Maciej Pijanowski 0:f3d9c16b08d7 127 while (true) {
Maciej Pijanowski 0:f3d9c16b08d7 128 uint16_t light;
Maciej Pijanowski 0:f3d9c16b08d7 129 std::vector<uint8_t> tx_data;
Maciej Pijanowski 0:f3d9c16b08d7 130
Maciej Pijanowski 0:f3d9c16b08d7 131 // join network if not joined
Maciej Pijanowski 0:f3d9c16b08d7 132 if (!dot->getNetworkJoinStatus()) {
Maciej Pijanowski 0:f3d9c16b08d7 133 join_network();
Maciej Pijanowski 0:f3d9c16b08d7 134 }
Maciej Pijanowski 0:f3d9c16b08d7 135
Maciej Pijanowski 0:f3d9c16b08d7 136 #if defined(TARGET_XDOT_L151CC)
Maciej Pijanowski 0:f3d9c16b08d7 137 // get the latest light sample and send it to the gateway
Maciej Pijanowski 0:f3d9c16b08d7 138 light = lux.getData();
Maciej Pijanowski 0:f3d9c16b08d7 139 tx_data.push_back((light >> 8) & 0xFF);
Maciej Pijanowski 0:f3d9c16b08d7 140 tx_data.push_back(light & 0xFF);
Maciej Pijanowski 0:f3d9c16b08d7 141 logInfo("light: %lu [0x%04X]", light, light);
Maciej Pijanowski 0:f3d9c16b08d7 142 send_data(tx_data);
Maciej Pijanowski 0:f3d9c16b08d7 143 #else
Maciej Pijanowski 0:f3d9c16b08d7 144 // get some dummy data and send it to the gateway
Maciej Pijanowski 0:f3d9c16b08d7 145 light = lux.read_u16();
Maciej Pijanowski 0:f3d9c16b08d7 146 tx_data.push_back((light >> 8) & 0xFF);
Maciej Pijanowski 0:f3d9c16b08d7 147 tx_data.push_back(light & 0xFF);
Maciej Pijanowski 0:f3d9c16b08d7 148 logInfo("light: %lu [0x%04X]", light, light);
Maciej Pijanowski 0:f3d9c16b08d7 149 send_data(tx_data);
Maciej Pijanowski 0:f3d9c16b08d7 150 #endif
Maciej Pijanowski 0:f3d9c16b08d7 151
Maciej Pijanowski 0:f3d9c16b08d7 152 // the Dot can't sleep in class C mode
Maciej Pijanowski 0:f3d9c16b08d7 153 // it must be waiting for data from the gateway
Maciej Pijanowski 0:f3d9c16b08d7 154 // send data every 30s
Maciej Pijanowski 0:f3d9c16b08d7 155 logInfo("waiting for 30s");
Maciej Pijanowski 0:f3d9c16b08d7 156 wait(30);
Maciej Pijanowski 0:f3d9c16b08d7 157 }
Maciej Pijanowski 0:f3d9c16b08d7 158
Maciej Pijanowski 0:f3d9c16b08d7 159 return 0;
Maciej Pijanowski 0:f3d9c16b08d7 160 }
Maciej Pijanowski 0:f3d9c16b08d7 161
Maciej Pijanowski 0:f3d9c16b08d7 162 #endif
Maciej Pijanowski 0:f3d9c16b08d7 163