Esta es la prueba con LoRa y el sensor de luz integrado en la placa.

Dependencies:   libxDot-mbed5 ISL29011

Files at this revision

API Documentation at this revision

Comitter:
Mike Fiore
Date:
Mon Oct 10 15:04:22 2016 -0500
Parent:
9:72d3203279b2
Child:
11:d2e31743433a
Commit message:
add class C example, clean up configuration display

Changed in this revision

examples/src/class_c_example.cpp Show annotated file Show diff for this revision Revisions of this file
examples/src/dot_util.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/src/class_c_example.cpp	Mon Oct 10 15:04:22 2016 -0500
@@ -0,0 +1,194 @@
+#include "dot_util.h"
+#include "mDotEvent.h"
+ 
+#if ACTIVE_EXAMPLE == CLASS_C_EXAMPLE
+
+/////////////////////////////////////////////////////////////
+// * 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 bool public_network = false;
+static uint8_t ack = 1;
+
+mDot* dot = 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
+
+// Custom event handler for receiving Class C packets
+class RadioEvent : public mDotEvent
+{
+ 
+public:
+    RadioEvent() {}
+ 
+    virtual ~RadioEvent() {}
+ 
+    /*!
+     * MAC layer event callback prototype.
+     *
+     * \param [IN] flags Bit field indicating the MAC events occurred
+     * \param [IN] info  Details about MAC events occurred
+     */
+    virtual void MacEvent(LoRaMacEventFlags* flags, LoRaMacEventInfo* info) {
+ 
+        if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) {
+            std::string msg = "OK";
+            switch (info->Status) {
+                case LORAMAC_EVENT_INFO_STATUS_ERROR:
+                    msg = "ERROR";
+                    break;
+                case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT:
+                    msg = "TX_TIMEOUT";
+                    break;
+                case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT:
+                    msg = "RX_TIMEOUT";
+                    break;
+                case LORAMAC_EVENT_INFO_STATUS_RX_ERROR:
+                    msg = "RX_ERROR";
+                    break;
+                case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL:
+                    msg = "JOIN_FAIL";
+                    break;
+                case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL:
+                    msg = "DOWNLINK_FAIL";
+                    break;
+                case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL:
+                    msg = "ADDRESS_FAIL";
+                    break;
+                case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL:
+                    msg = "MIC_FAIL";
+                    break;
+                default:
+                    break;
+            }
+            logTrace("Event: %s", msg.c_str());
+ 
+            logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d",
+                     flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept);
+            logTrace("Info: Status: %d ACK: %d Retries: %d TxDR: %d RxPort: %d RxSize: %d RSSI: %d SNR: %d Energy: %d Margin: %d Gateways: %d",
+                     info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize,
+                     info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways);
+        }
+ 
+        if (flags->Bits.Rx) {
+            
+            logDebug("Rx %d bytes", info->RxBufferSize);
+            if (info->RxBufferSize > 0) {
+                // print RX data as hexadecimal
+                //printf("Rx data: %s\r\n", mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str());
+
+                // print RX data as string
+                pc.printf("Rx data: ");
+                for (int i = 0; i < info->RxBufferSize; i++) {
+                    pc.putc(info->RxBuffer[i]);
+                }
+                pc.printf("\r\n");
+            }
+        }
+    }
+};
+
+int main() {
+    RadioEvent events;
+
+    pc.baud(115200);
+
+    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
+    
+    dot = mDot::getInstance();
+
+    // make sure library logging is turned on
+    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
+
+    // attach the custom events handler
+    dot->setEvents(&events);
+
+    // 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 EUI 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, public_network, ack);
+    //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
+
+    // configure the Dot for class C operation
+    // the Dot must also be configured on the gateway for class C
+    // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
+    // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation
+    logInfo("changing network mode to class C");
+    if (dot->setClass("C") != mDot::MDOT_OK) {
+        logError("failed to set network mode to class C");
+    }
+    
+    // save changes to configuration
+    logInfo("saving configuration");
+    if (!dot->saveConfig()) {
+        logError("failed to save configuration");
+    }
+
+    // display configuration
+    display_config();
+
+    // 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);
+
+    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)
+        // 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);
+#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
+
+        // the Dot can't sleep in class C mode
+        // it must be waiting for data from the gateway
+        // send data every 30s
+        logInfo("waiting for 30s");
+        wait(30);
+    }
+ 
+    return 0;
+}
+
+#endif
+
--- a/examples/src/dot_util.cpp	Fri Oct 07 15:48:23 2016 -0500
+++ b/examples/src/dot_util.cpp	Mon Oct 10 15:04:22 2016 -0500
@@ -14,34 +14,38 @@
 
 void display_config() {
     // display configuration and library version information
-    logInfo("version: %s", dot->getId().c_str());
+    logInfo("=====================");
     logInfo("general configuration");
-    logInfo("---------------------");
-    logInfo("\tdevice ID/EUI:           %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str());
-    logInfo("\tfrequency band:          %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str());
+    logInfo("=====================");
+    logInfo("version ------------------ %s", dot->getId().c_str());
+    logInfo("device ID/EUI ------------ %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str());
+    logInfo("frequency band ----------- %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str());
     if (dot->getFrequencySubBand() != mDot::FB_EU868) {
-        logInfo("\tfrequency sub band:      %u", dot->getFrequencySubBand());
+        logInfo("frequency sub band ------- %u", dot->getFrequencySubBand());
     }
-    logInfo("\tpublic network:          %s", dot->getPublicNetwork() == true ? "on" : "off");
+    logInfo("public network ----------- %s", dot->getPublicNetwork() ? "on" : "off");
+    logInfo("=========================");
     logInfo("credentials configuration");
-    logInfo("-------------------------");
-    logInfo("\tnetwork join mode:       %s", mDot::JoinModeStr(dot->getJoinMode()).c_str());
+    logInfo("=========================");
+    logInfo("device class ------------- %s", dot->getClass().c_str());
+    logInfo("network join mode -------- %s", mDot::JoinModeStr(dot->getJoinMode()).c_str());
     if (dot->getJoinMode() == mDot::MANUAL) {
-	logInfo("\tnetwork address:         %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str());
-	logInfo("\tnetwork session key:     %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str());
-	logInfo("\tdata session key:        %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str());
+	logInfo("network address ---------- %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str());
+	logInfo("network session key------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str());
+	logInfo("data session key---------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str());
     } else {
-	logInfo("\tnetwork name:            %s", dot->getNetworkName().c_str());
-	logInfo("\tnetwork phrase:          %s", dot->getNetworkPassphrase().c_str());
-	logInfo("\tnetwork EUI:             %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str());
-	logInfo("\tnetwork KEY:             %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str());
+	logInfo("network name ------------- %s", dot->getNetworkName().c_str());
+	logInfo("network phrase ----------- %s", dot->getNetworkPassphrase().c_str());
+	logInfo("network EUI -------------- %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str());
+	logInfo("network KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str());
     }
+    logInfo("========================");
     logInfo("communication parameters");
-    logInfo("------------------------");
-    logInfo("\tacks:                    %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck());
-    logInfo("\tTX datarate:             %s", mDot::DataRateStr(dot->getTxDataRate()).c_str());
-    logInfo("\tTX power:                %lu dBm", dot->getTxPower());
-    logInfo("\tatnenna gain:            %u dBm", dot->getAntennaGain());
+    logInfo("========================");
+    logInfo("acks --------------------- %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck());
+    logInfo("TX datarate -------------- %s", mDot::DataRateStr(dot->getTxDataRate()).c_str());
+    logInfo("TX power ----------------- %lu dBm", dot->getTxPower());
+    logInfo("atnenna gain ------------- %u dBm", dot->getAntennaGain());
 }
 
 void update_ota_config_name_phrase(std::string network_name, std::string network_passphrase, uint8_t frequency_sub_band, bool public_network, uint8_t ack) {
@@ -73,9 +77,9 @@
     }
     
     if (current_public_network != public_network) {
-        logInfo("changing public network from %s to %s", current_public_network ? "true" : "false", public_network ? "true" : "false");
+        logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off");
         if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
-            logError("failed to set public network to %s", public_network ? "true" : "false");
+            logError("failed to set public network to %s", public_network ? "on" : "off");
         }
     }
     
@@ -119,9 +123,9 @@
     }
     
     if (current_public_network != public_network) {
-        logInfo("changing public network from %s to %s", current_public_network ? "true" : "false", public_network ? "true" : "false");
+        logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off");
         if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
-            logError("failed to set public network to %s", public_network ? "true" : "false");
+            logError("failed to set public network to %s", public_network ? "on" : "off");
         }
     }
     
@@ -174,9 +178,9 @@
     }
     
     if (current_public_network != public_network) {
-        logInfo("changing public network from %s to %s", current_public_network ? "true" : "false", public_network ? "true" : "false");
+        logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off");
         if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
-            logError("failed to set public network to %s", public_network ? "true" : "false");
+            logError("failed to set public network to %s", public_network ? "on" : "off");
         }
     }