Harry Chen / Mbed OS Dot-Examples

Dependencies:   libmDot-mbed5 ISL29011

Revision:
39:1f8558902454
Parent:
33:15ea8f985c54
Child:
40:71d8df33ef7d
--- a/examples/src/ota_example.cpp	Thu May 02 20:47:12 2019 +0000
+++ b/examples/src/ota_example.cpp	Fri Jan 03 07:18:09 2020 +0000
@@ -1,190 +1,134 @@
 #include "dot_util.h"
 #include "RadioEvent.h"
- 
-#if ACTIVE_EXAMPLE == OTA_EXAMPLE
-
-/////////////////////////////////////////////////////////////////////////////
-// -------------------- DOT LIBRARY REQUIRED ------------------------------//
-// * Because these example programs can be used for both mDot and xDot     //
-//     devices, the LoRa stack is not included. The libmDot library should //
-//     be imported if building for mDot devices. The libxDot library       //
-//     should be imported if building for xDot devices.                    //
-// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
-// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
-// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
-// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
-/////////////////////////////////////////////////////////////////////////////
 
-/////////////////////////////////////////////////////////////
-// * these options must match the settings on your gateway //
-// * edit their values to match your configuration         //
-// * frequency sub band is only relevant for the 915 bands //
-// * either the network name and passphrase can be used or //
-//     the network ID (8 bytes) and KEY (16 bytes)         //
-/////////////////////////////////////////////////////////////
-static std::string network_name = "MultiTech";
-static std::string network_passphrase = "MultiTech";
-static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
-static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
-static uint8_t frequency_sub_band = 0;
-static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
-static uint8_t join_delay = 5;
-static uint8_t ack = 0;
-static bool adr = true;
+#include <string>
+#include <vector>
+#include <algorithm>
+#include <iostream>
+#include <sstream> 
+using namespace std;
+AnalogIn in(PA_5);
+static uint8_t config_app_eui[] = {0x86,0xe4,0xef,0xc7,0x10,0x4f,0x68,0x29 };
+static uint8_t config_app_key[] = { 0xa3,0x46,0xb6,0xfa,0xef,0x2b,0xd3,0x3c,0x16,0xfe,0x9b,0x1d,0x8d,0x47,0xa1,0x1d};
 
-// deepsleep consumes slightly less current than sleep
-// in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
-// in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
-// if deep_sleep == true, device will enter deepsleep mode
-static bool deep_sleep = false;
-
-mDot* dot = NULL;
+// use the same subband as gateway that gateway can listen to you
+static uint8_t config_frequency_sub_band = 1;
+mDot *dot; 
 lora::ChannelPlan* plan = NULL;
 
-Serial pc(USBTX, USBRX);
-
-#if defined(TARGET_XDOT_L151CC)
-I2C i2c(I2C_SDA, I2C_SCL);
-ISL29011 lux(i2c);
-#else
-AnalogIn lux(XBEE_AD0);
-#endif
+#if ACTIVE_EXAMPLE == OTA_EXAMPLE
+void config(mDot *dot,lora::ChannelPlan *plan)
+{
+    uint8_t ret;
+    //  reset to default config so we know what state we're in  
+    dot->resetConfig();
+    //  set how many log info will be show 
+    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
+    // set subband frequency the same as gateway so gateway can listen to you 
+    logInfo("setting frequency sub band\r\n");
+    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
+        logError("failed to set frequency sub band %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+    // lora has private network and public network here we use public network
+    logInfo("setting public network mode");    
+    if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
+        logError("failed to public network mode");
+    }
+    std::vector<uint8_t> temp;
+    
+    for (int i = 0; i < 8; i++) {
+        temp.push_back(config_app_eui[i]);
+    }
+    // set network id 
+    logInfo("setting app eui\r\n");
+    if ((ret = dot->setNetworkId(temp)) != mDot::MDOT_OK) {
+        logError("failed to set app eui %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+    temp.clear();
+    for (int i = 0; i < 16; i++) {
+        temp.push_back(config_app_key[i]);
+    }
+    // set network key
+    logInfo("setting app key\r\n");
+    if ((ret = dot->setNetworkKey(temp)) != mDot::MDOT_OK) {
+        logError("failed to set app key %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+    // a higher spreading factor allows for longer range but lower throughput
+    // in the 915 (US) frequency band, spreading factors 7 - 10 are available
+    logInfo("setting TX spreading factor\r\n");
+    if ((ret = dot->setTxDataRate(mDot::DR10)) != mDot::MDOT_OK) {
+        logError("failed to set TX datarate %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+    // request receive confirmation of packets from the gateway
+    logInfo("enabling ACKs\r\n");
+    if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
+        logError("failed to enable ACKs %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+    // Set Tx Power
+    logInfo("enabling Tx Power\r\n");
+    if ((ret = dot->setTxPower(20)) != mDot::MDOT_OK) {
+        logError("failed to enable Tx Power %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+    // request receive confirmation of packets from the gateway
+    logInfo("enabling Tx Data Rate\r\n");
+    if ((ret = dot->setTxDataRate(3)) != mDot::MDOT_OK) {
+        logError("failed to enable Tx Data Rate %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+    // save this configuration to the mDot's NVM
+    logInfo("saving config\r\n");
+    if (! dot->saveConfig()) {
+        logError("failed to save configuration\r\n");
+    }
+    logInfo("joining network\r\n");
+    while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
+        logError("failed to join network %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+        osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
+    }
+    return;
+}
+int main()
+{
+    // object to control the debug board
+       
+    plan= new lora::ChannelPlan_AS923();
+    
+    dot = mDot::getInstance(plan);
+    
+    // set network
+    config(dot,plan);    
 
-int main() {
-    // Custom event handler for automatically displaying RX data
-    RadioEvent events;
+    while (true) {
+        int tmp,ret;
+        std::vector<uint8_t> data;
+        stringstream ss;
+        std::string data_str ;
+        
+        tmp = in.read_u16();
+        printf("%d\r\n",tmp);
+        
+        // format data for sending to the gateway
+        ss << tmp;
+        ss << "@105502522/105502511/105502512/105502531/105502546/105502528";
+        ss >> data_str;
+        for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
+            data.push_back((uint8_t) *it);
+        
+        // send the data to the gateway
+        if ((ret = dot->send(data)) != mDot::MDOT_OK) {
+            logError("failed to send\r\n", ret, mDot::getReturnCodeString(ret).c_str());
+        } else {
+            logInfo("successfully sent data to gateway\r\n");
+        }
 
-    pc.baud(115200);
+        // we use US but in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
+        osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
+    }
 
-#if defined(TARGET_XDOT_L151CC)
-    i2c.frequency(400000);
+    return 0;
+
+}
+
+
+
 #endif
 
-    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
-    
-#if CHANNEL_PLAN == CP_US915
-    plan = new lora::ChannelPlan_US915();
-#elif CHANNEL_PLAN == CP_AU915
-    plan = new lora::ChannelPlan_AU915();
-#elif CHANNEL_PLAN == CP_EU868
-    plan = new lora::ChannelPlan_EU868();
-#elif CHANNEL_PLAN == CP_KR920
-    plan = new lora::ChannelPlan_KR920();
-#elif CHANNEL_PLAN == CP_AS923
-    plan = new lora::ChannelPlan_AS923();
-#elif CHANNEL_PLAN == CP_AS923_JAPAN
-    plan = new lora::ChannelPlan_AS923_Japan();
-#elif CHANNEL_PLAN == CP_IN865
-    plan = new lora::ChannelPlan_IN865();
-#endif
-    assert(plan);
-
-    dot = mDot::getInstance(plan);
-    assert(dot);
-
-    // attach the custom events handler
-    dot->setEvents(&events);
-
-    if (!dot->getStandbyFlag()) {
-        logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
-
-        // start from a well-known state
-        logInfo("defaulting Dot configuration");
-        dot->resetConfig();
-        dot->resetNetworkSession();
-
-        // make sure library logging is turned on
-        dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
-
-        // update configuration if necessary
-        if (dot->getJoinMode() != mDot::OTA) {
-            logInfo("changing network join mode to OTA");
-            if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
-                logError("failed to set network join mode to OTA");
-            }
-        }
-        // 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
-        // only one method or the other should be used!
-        // network ID = crc64(network name)
-        // network KEY = cmac(network passphrase)
-        update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack);
-        //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
-
-        // configure network link checks
-        // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
-        // check the link every count packets
-        // declare the Dot disconnected after threshold failed link checks
-        // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
-        update_network_link_check_config(3, 5);
-
-        // enable or disable Adaptive Data Rate
-        dot->setAdr(adr);
-
-        // Configure the join delay
-        dot->setJoinDelay(join_delay);
-
-        // save changes to configuration
-        logInfo("saving configuration");
-        if (!dot->saveConfig()) {
-            logError("failed to save configuration");
-        }
-
-        // display configuration
-        display_config();
-    } else {
-        // restore the saved session if the dot woke from deepsleep mode
-        // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
-        logInfo("restoring network session from NVM");
-        dot->restoreNetworkSession();
-    }
-
-    while (true) {
-        uint16_t light;
-        std::vector<uint8_t> tx_data;
-
-        // join network if not joined
-        if (!dot->getNetworkJoinStatus()) {
-            join_network();
-        }
-
-#if defined(TARGET_XDOT_L151CC)
-        // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
-        lux.setMode(ISL29011::ALS_CONT);
-        lux.setResolution(ISL29011::ADC_16BIT);
-        lux.setRange(ISL29011::RNG_64000);
-
-        // get the latest light sample and send it to the gateway
-        light = lux.getData();
-        tx_data.push_back((light >> 8) & 0xFF);
-        tx_data.push_back(light & 0xFF);
-        logInfo("light: %lu [0x%04X]", light, light);
-        send_data(tx_data);
-
-        // put the LSL29011 ambient light sensor into a low power state
-        lux.setMode(ISL29011::PWR_DOWN);
-#else 
-        // get some dummy data and send it to the gateway
-        light = lux.read_u16();
-        tx_data.push_back((light >> 8) & 0xFF);
-        tx_data.push_back(light & 0xFF);
-        logInfo("light: %lu [0x%04X]", light, light);
-        send_data(tx_data);
-#endif
-
-        // if going into deepsleep mode, save the session so we don't need to join again after waking up
-        // not necessary if going into sleep mode since RAM is retained
-        if (deep_sleep) {
-            logInfo("saving network session to NVM");
-            dot->saveNetworkSession();
-        }
-
-        // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
-        //sleep_wake_rtc_only(deep_sleep);
-        //sleep_wake_interrupt_only(deep_sleep);
-        sleep_wake_rtc_or_interrupt(deep_sleep);
-    }
- 
-    return 0;
-}
-
-#endif