L-Tek / Mbed OS Dot-Examples

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