Bryce Himebaugh / Mbed OS xdot_reference

Dependencies:   libxDot-mbed5 TSL2561

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 int main() {
00032     // Custom event handler for automatically displaying RX data
00033     RadioEvent events;
00034     uint32_t tx_frequency;
00035     uint8_t tx_datarate;
00036     uint8_t tx_power;
00037     uint8_t frequency_band;
00038 
00039     pc.baud(115200);
00040 
00041     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00042     
00043 #if CHANNEL_PLAN == CP_US915
00044     plan = new lora::ChannelPlan_US915();
00045 #elif CHANNEL_PLAN == CP_AU915
00046     plan = new lora::ChannelPlan_AU915();
00047 #elif CHANNEL_PLAN == CP_EU868
00048     plan = new lora::ChannelPlan_EU868();
00049 #elif CHANNEL_PLAN == CP_KR920
00050     plan = new lora::ChannelPlan_KR920();
00051 #elif CHANNEL_PLAN == CP_AS923
00052     plan = new lora::ChannelPlan_AS923();
00053 #elif CHANNEL_PLAN == CP_AS923_JAPAN
00054     plan = new lora::ChannelPlan_AS923_Japan();
00055 #elif CHANNEL_PLAN == CP_IN865
00056     plan = new lora::ChannelPlan_IN865();
00057 #endif
00058     assert(plan);
00059 
00060     dot = mDot::getInstance(plan);
00061     assert(dot);
00062 
00063     logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
00064 
00065     // start from a well-known state
00066     logInfo("defaulting Dot configuration");
00067     dot->resetConfig();
00068 
00069     // make sure library logging is turned on
00070     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00071 
00072     // attach the custom events handler
00073     dot->setEvents(&events);
00074 
00075     // update configuration if necessary
00076     if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
00077         logInfo("changing network join mode to PEER_TO_PEER");
00078         if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
00079             logError("failed to set network join mode to PEER_TO_PEER");
00080         }
00081     }
00082     frequency_band = dot->getFrequencyBand();
00083     switch (frequency_band) {
00084         case lora::ChannelPlan::EU868_OLD:
00085         case lora::ChannelPlan::EU868:
00086             // 250kHz channels achieve higher throughput
00087             // DR_6 : SF7 @ 250kHz
00088             // DR_0 - DR_5 (125kHz channels) available but much slower
00089             tx_frequency = 869850000;
00090             tx_datarate = lora::DR_6;
00091             // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
00092             tx_power = 4;
00093             break;
00094 
00095         case lora::ChannelPlan::US915_OLD:
00096         case lora::ChannelPlan::US915:
00097         case lora::ChannelPlan::AU915_OLD:
00098         case lora::ChannelPlan::AU915:
00099             // 500kHz channels achieve highest throughput
00100             // DR_8 : SF12 @ 500kHz
00101             // DR_9 : SF11 @ 500kHz
00102             // DR_10 : SF10 @ 500kHz
00103             // DR_11 : SF9 @ 500kHz
00104             // DR_12 : SF8 @ 500kHz
00105             // DR_13 : SF7 @ 500kHz
00106             // DR_0 - DR_3 (125kHz channels) available but much slower
00107             tx_frequency = 915500000;
00108             tx_datarate = lora::DR_13;
00109             // 915 bands have no duty cycle restrictions, set tx power to max
00110             tx_power = 20;
00111             break;
00112 
00113         case lora::ChannelPlan::AS923:
00114         case lora::ChannelPlan::AS923_JAPAN:
00115             // 250kHz channels achieve higher throughput
00116             // DR_6 : SF7 @ 250kHz
00117             // DR_0 - DR_5 (125kHz channels) available but much slower
00118             tx_frequency = 924800000;
00119             tx_datarate = lora::DR_6;
00120             tx_power = 16;
00121             break;
00122 
00123         case lora::ChannelPlan::KR920:
00124             // DR_5 : SF7 @ 125kHz
00125             tx_frequency = 922700000;
00126             tx_datarate = lora::DR_5;
00127             tx_power = 14;
00128             break;
00129 
00130         default:
00131             while (true) {
00132                 logFatal("no known channel plan in use - extra configuration is needed!");
00133                 wait(5);
00134             }
00135             break;
00136     }
00137     // in PEER_TO_PEER mode there is no join request/response transaction
00138     // as long as both Dots are configured correctly, they should be able to communicate
00139     update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power);
00140 
00141     // save changes to configuration
00142     logInfo("saving configuration");
00143     if (!dot->saveConfig()) {
00144         logError("failed to save configuration");
00145     }
00146 
00147     // display configuration
00148     display_config();
00149 
00150     uint8_t counter = 0;
00151     while (true) {
00152         std::vector<uint8_t> tx_data;
00153 
00154         // join network if not joined
00155         if (!dot->getNetworkJoinStatus()) {
00156             join_network();
00157         }
00158 
00159         tx_data.push_back(++counter);
00160         logInfo("sending uplink with data = %d", counter);
00161         send_data(tx_data);
00162 
00163         // the Dot can't sleep in PEER_TO_PEER mode
00164         // it must be waiting for data from the other Dot
00165         // send data every 5 seconds
00166         logInfo("waiting for 5s");
00167         wait(5);
00168     }
00169  
00170     return 0;
00171 }
00172 
00173 #endif