Code to talk to the mdot

Dependencies:   libmDot-mbed5 ISL29011

Committer:
bhimebau
Date:
Thu Feb 08 18:45:41 2018 +0000
Revision:
0:34da43c761f8
Initial push;

Who changed what in which revision?

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