Lucas Vates / Mbed OS Dot-Examples-online

Dependencies:   libmDot-mbed5 ISL29011

Fork of Dot-Examples by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers peer_to_peer_example.cpp Source File

peer_to_peer_example.cpp

00001 #include "dot_util.h"
00002 #include "RadioEvent.h"
00003  
00004 #if ACTIVE_EXAMPLE == PEER_TO_PEER_EXAMPLE
00005 
00006 /////////////////////////////////////////////////////////////////////////////
00007 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
00008 // * Because these example programs can be used for both mDot and xDot     //
00009 //     devices, the LoRa stack is not included. The libmDot library should //
00010 //     be imported if building for mDot devices. The libxDot library       //
00011 //     should be imported if building for xDot devices.                    //
00012 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
00013 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
00014 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
00015 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
00016 /////////////////////////////////////////////////////////////////////////////
00017 
00018 /////////////////////////////////////////////////////////////
00019 // * these options must match between the two devices in   //
00020 //   order for communication to be successful
00021 /////////////////////////////////////////////////////////////
00022 static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 };
00023 static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
00024 static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
00025 
00026 mDot* dot = NULL;
00027 lora::ChannelPlan* plan = NULL;
00028 
00029 Serial pc(USBTX, USBRX);
00030 
00031 #if defined(TARGET_XDOT_L151CC)
00032 I2C i2c(I2C_SDA, I2C_SCL);
00033 ISL29011 lux(i2c);
00034 #else
00035 AnalogIn lux(XBEE_AD0);
00036 #endif
00037 
00038 int main() {
00039     // Custom event handler for automatically displaying RX data
00040     RadioEvent events;
00041     uint32_t tx_frequency;
00042     uint8_t tx_datarate;
00043     uint8_t tx_power;
00044     uint8_t frequency_band;
00045 
00046     pc.baud(115200);
00047 
00048     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00049     
00050 #if CHANNEL_PLAN == CP_US915
00051     plan = new lora::ChannelPlan_US915();
00052 #elif CHANNEL_PLAN == CP_AU915
00053     plan = new lora::ChannelPlan_AU915();
00054 #elif CHANNEL_PLAN == CP_EU868
00055     plan = new lora::ChannelPlan_EU868();
00056 #elif CHANNEL_PLAN == CP_KR920
00057     plan = new lora::ChannelPlan_KR920();
00058 #elif CHANNEL_PLAN == CP_AS923
00059     plan = new lora::ChannelPlan_AS923();
00060 #elif CHANNEL_PLAN == CP_AS923_JAPAN
00061     plan = new lora::ChannelPlan_AS923_Japan();
00062 #elif CHANNEL_PLAN == CP_IN865
00063     plan = new lora::ChannelPlan_IN865();
00064 #endif
00065     assert(plan);
00066 
00067     dot = mDot::getInstance(plan);
00068     assert(dot);
00069 
00070     logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
00071 
00072     // start from a well-known state
00073     logInfo("defaulting Dot configuration");
00074     dot->resetConfig();
00075 
00076     // make sure library logging is turned on
00077     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00078 
00079     // attach the custom events handler
00080     dot->setEvents(&events);
00081 
00082     // update configuration if necessary
00083     if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
00084         logInfo("changing network join mode to PEER_TO_PEER");
00085         if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
00086             logError("failed to set network join mode to PEER_TO_PEER");
00087         }
00088     }
00089     frequency_band = dot->getFrequencyBand();
00090     switch (frequency_band) {
00091         case lora::ChannelPlan::EU868_OLD:
00092         case lora::ChannelPlan::EU868:
00093             // 250kHz channels achieve higher throughput
00094             // DR_6 : SF7 @ 250kHz
00095             // DR_0 - DR_5 (125kHz channels) available but much slower
00096             tx_frequency = 869850000;
00097             tx_datarate = lora::DR_6;
00098             // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
00099             tx_power = 4;
00100             break;
00101 
00102         case lora::ChannelPlan::US915_OLD:
00103         case lora::ChannelPlan::US915:
00104         case lora::ChannelPlan::AU915_OLD:
00105         case lora::ChannelPlan::AU915:
00106             // 500kHz channels achieve highest throughput
00107             // DR_8 : SF12 @ 500kHz
00108             // DR_9 : SF11 @ 500kHz
00109             // DR_10 : SF10 @ 500kHz
00110             // DR_11 : SF9 @ 500kHz
00111             // DR_12 : SF8 @ 500kHz
00112             // DR_13 : SF7 @ 500kHz
00113             // DR_0 - DR_3 (125kHz channels) available but much slower
00114             tx_frequency = 915500000;
00115             tx_datarate = lora::DR_13;
00116             // 915 bands have no duty cycle restrictions, set tx power to max
00117             tx_power = 20;
00118             break;
00119 
00120         case lora::ChannelPlan::AS923:
00121         case lora::ChannelPlan::AS923_JAPAN:
00122             // 250kHz channels achieve higher throughput
00123             // DR_6 : SF7 @ 250kHz
00124             // DR_0 - DR_5 (125kHz channels) available but much slower
00125             tx_frequency = 924800000;
00126             tx_datarate = lora::DR_6;
00127             tx_power = 16;
00128             break;
00129 
00130         case lora::ChannelPlan::KR920:
00131             // DR_5 : SF7 @ 125kHz
00132             tx_frequency = 922700000;
00133             tx_datarate = lora::DR_5;
00134             tx_power = 14;
00135             break;
00136 
00137         default:
00138             while (true) {
00139                 logFatal("no known channel plan in use - extra configuration is needed!");
00140                 wait(5);
00141             }
00142             break;
00143     }
00144     // in PEER_TO_PEER mode there is no join request/response transaction
00145     // as long as both Dots are configured correctly, they should be able to communicate
00146     update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power);
00147 
00148     // save changes to configuration
00149     logInfo("saving configuration");
00150     if (!dot->saveConfig()) {
00151         logError("failed to save configuration");
00152     }
00153 
00154     // display configuration
00155     display_config();
00156 
00157 #if defined(TARGET_XDOT_L151CC)
00158     // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
00159     lux.setMode(ISL29011::ALS_CONT);
00160     lux.setResolution(ISL29011::ADC_16BIT);
00161     lux.setRange(ISL29011::RNG_64000);
00162 #endif
00163 
00164     while (true) {
00165         uint16_t light;
00166         std::vector<uint8_t> tx_data;
00167 
00168         // join network if not joined
00169         if (!dot->getNetworkJoinStatus()) {
00170             join_network();
00171         }
00172 
00173 #if defined(TARGET_XDOT_L151CC)
00174         // get the latest light sample and send it to the gateway
00175         light = lux.getData();
00176         tx_data.push_back((light >> 8) & 0xFF);
00177         tx_data.push_back(light & 0xFF);
00178         logInfo("light: %lu [0x%04X]", light, light);
00179         send_data(tx_data);
00180 #else 
00181         // get some dummy data and send it to the gateway
00182         light = lux.read_u16();
00183         tx_data.push_back((light >> 8) & 0xFF);
00184         tx_data.push_back(light & 0xFF);
00185         logInfo("light: %lu [0x%04X]", light, light);
00186         send_data(tx_data);
00187 #endif
00188 
00189         // the Dot can't sleep in PEER_TO_PEER mode
00190         // it must be waiting for data from the other Dot
00191         // send data every 5 seconds
00192         logInfo("waiting for 5s");
00193         wait(5);
00194     }
00195  
00196     return 0;
00197 }
00198 
00199 #endif
00200