Demo of DHT11->mDot->TTN

Dependencies:   DHT11 libmDot mbed-rtos mbed

Revision:
5:48eb9245a914
Parent:
4:f649ab1b61d1
Child:
6:0a7760eeaba9
--- a/main.cpp	Thu Oct 15 20:34:52 2015 +0000
+++ b/main.cpp	Tue Oct 20 14:48:11 2015 +0000
@@ -1,16 +1,11 @@
-/** mDot_DS18B20 - Simple mDot temperature sensor using Dallas Semiconductors DS18B20 OneWire temperature sensor.
+/** mDot_TTN_DS18B20 - Simple mDot temperature sensor using Dallas Semiconductors DS18B20 OneWire temperature sensor.
  * It used the OTA_AUTO join mode using saved parameters. If the config is to be reset then pin A2 on the
  * dev board must be held low during a reset or power up.
  *
  * Uses MultiTech mDot developer board http://www.multitech.com/models/94558010LF
  * Requires a MultiTech MultiConnect Conduit http://www.multitech.com/models/94557203LF
- *
- * Example JSON received on Conduit:
- { "chan": 5, "codr": "4/5", "datr": "SF9BW125", "freq": "869.5",
- "lsnr": "8.8", "modu": "LORA", "rfch": 1, "rssi": -41, "seqn": 13,
- "size": 12, "timestamp": "2015-07-22T21:19:11Z", "tmst": 517590990,
- "payload": "{\"tmp\":21.3}", "eui": "00:80:00:00:00:00:9a:63", "_msg
- id": "73bcd8dd.8c4328" }
+ * http://www.multitech.net/developer/software/lora/conduit-mlinux-convert-to-basic-packet-forwarder/
+ * http://forum.thethingsnetwork.org/t/setting-up-multitech-conduit-gateway-for-ttn/216/35
  *
  */
 
@@ -29,8 +24,17 @@
 
 // these options must match the settings on your Conduit in
 // /var/config/lora/lora-network-server.conf
-static std::string config_network_name = "ThingInnovations";
-static std::string config_network_pass = "donkey123";
+//static std::string config_network_name = "ThingInnovations";
+//static std::string config_network_pass = "donkey123";
+//
+//const uint8_t AppKey[16]={0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
+// Application session key
+uint8_t AppSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
+// Network session key
+uint8_t NwkSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
+// Dev addr
+uint8_t NodeAddr[4]= {0x23,0x05,0x19,0x67};
+
 // Ignoring sub band for EU modules.
 //static uint8_t config_frequency_sub_band = 1;
 
@@ -62,6 +66,8 @@
     mDot* dot;
     std::vector<uint8_t> send_data;
     std::vector<uint8_t> recv_data;
+    std::vector<uint8_t> nwkSKey;
+    std::vector<uint8_t> nodeAddr;
 
     float temperature = 0.0;
 
@@ -69,7 +75,7 @@
     configReset.mode(PullUp);
 
     pc.baud(115200);
-    pc.printf("mDot LoRa Temperature sensor\n\r");
+    pc.printf("TTN mDot LoRa Temperature sensor\n\r");
 
     // get a mDot handle
     dot = mDot::getInstance();
@@ -81,38 +87,71 @@
 
     // Test if we've already saved the config
     std::string configNetworkName = dot->getNetworkName();
+//   config_network_name = std::string(NwkSKey,16);
+//   config_network_pass = std::string(NwkSKey,16);
+
+//    pc.printf("Name: %s\r\n",config_network_name.c_str());
+    uint8_t *it = NwkSKey;
+    for (uint8_t i = 0; i<16; i++)
+        nwkSKey.push_back((uint8_t) *it++);
+
+    it = NodeAddr;
+    for (uint8_t i = 0; i<4; i++)
+        nodeAddr.push_back((uint8_t) *it++);
 
     // Reset config if network name is different or pin is low then reset config.
-    if( config_network_name.compare(configNetworkName) != 0 || !configReset ) {
-        // Not saved config, reset
-        logInfo("Setting Config");
-
+//    if( config_network_name.compare(configNetworkName) != 0 || !configReset ) {
+    if( !configReset ) {
+        // Config Reset link present
+        logInfo("Resetting Config");
         // reset to default config so we know what state we're in
         dot->resetConfig();
+    }
 
-        // Set byte order - AEP less than 1.0.30
+    // Set byte order - AEP less than 1.0.30
 //    dot->setJoinByteOrder(mDot::MSB);
-        dot->setJoinByteOrder(mDot::LSB);
+    dot->setJoinByteOrder(mDot::LSB);
 
-        logInfo("setting Join mode");
-        if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
-            logError("failed to set Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-        }
+    logInfo("Set Public mode");
+    if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
+        logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
 
-        // If on developer board then you can enable activity LED
-        // Currently no spare pins that LEDs are connected too.
+    logInfo("Set MANUAL Join mode");
+//        if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
+    if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
+        logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+
+    // If on developer board then you can enable activity LED
+    // Currently no spare pins that LEDs are connected too.
 //    dot->setActivityLedPin( ACTIVITY_LED );
 //    dot->setActivityLedEnable(false);
 
-        // Have a decent nubmer of retries in connecting to LoRaWAN
-        dot->setJoinRetries( 3 );
+    // Have a decent nubmer of retries in connecting to LoRaWAN
+    logInfo("Set Join retries");
+    if((ret = dot->setJoinRetries( 3 )) != mDot::MDOT_OK) {
+        logError("Failed to set Join retries %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
 
-        // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
-        // Lower is higher data rate, larger packets and shorter range.
-//        dot->setTxDataRate( mDot::SF_9 );
-        dot->setTxDataRate( mDot::SF_12 );
-        dot->setTxPower( 14 );
-        dot->setAck( 0 );       // 1 retries on Ack, 0 to disable
+    // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
+    // Lower is higher data rate, larger packets and shorter range.
+    logInfo("Set SF");
+//    if((ret = dot->setTxDataRate( mDot::SF_9 )) != mDot::MDOT_OK) {
+    if((ret = dot->setTxDataRate( mDot::SF_12 )) != mDot::MDOT_OK) {
+        logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+
+    logInfo("Set TxPower");
+    if((ret = dot->setTxPower( 14 )) != mDot::MDOT_OK) {
+        logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+
+    logInfo("Set Ack");
+    // 1 retries on Ack, 0 to disable
+    if((ret = dot->setAck( 1 )) != mDot::MDOT_OK) {
+        logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
 
 //    Not applicable for 868MHz in EU
 //    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
@@ -120,27 +159,46 @@
 //        logError(dot, "failed to set frequency sub band", ret);
 //    }
 
-        if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
-            logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-        }
+    logInfo("Set Network Key");
+    if ((ret = dot->setNetworkKey(nwkSKey)) != mDot::MDOT_OK) {
+        logError("Failed to set Network  Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
 
-        if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
-            logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-        }
+    logInfo("Set Network Session Key");
+    if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) {
+        logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+
+    logInfo("Set Data Session Key");
+    if ((ret = dot->setDataSessionKey(nwkSKey)) != mDot::MDOT_OK) {
+        logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
+
+//        if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
+//            logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+//        }
 
-        if ((ret = dot->setJoinMode( mDot::AUTO_OTA )) != mDot::MDOT_OK) {
-            logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-        }
-
-        logInfo("Saving Config");
+//        if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
+//            logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+//        }
 
-        // Save config
-        if (! dot->saveConfig()) {
-            logError("failed to save configuration");
-        }
-    } else {
-        logInfo("Using existing Config");
+    logInfo("Saving Config");
+    // Save config
+    if (! dot->saveConfig()) {
+        logError("failed to save configuration");
     }
+//   } else {
+//       logInfo("Using existing Config");
+//   }
+
+    // Display what is set
+    std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
+    pc.printf("Network Session Key: ");
+    pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
+
+    tmp = dot->getDataSessionKey();
+    pc.printf("Data Session Key: ");
+    pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
 
     logInfo("Joining Network");
 
@@ -151,6 +209,36 @@
 
     logInfo("Joined Network");
 
+
+    pc.printf("Net ID ");
+    std::vector<uint8_t> nodeAddress;
+    nodeAddress = dot->getNetworkId();
+    for (std::vector<uint8_t>::iterator it = nodeAddress.begin() ; it != nodeAddress.end(); ++it)
+        pc.printf("%2.2x",*it );
+
+    pc.printf("\r\n");
+    pc.printf("Node ID ");
+    nodeAddress = dot->getNetworkAddress();
+    for (std::vector<uint8_t>::iterator it = nodeAddress.begin() ; it != nodeAddress.end(); ++it)
+        pc.printf("%2.2x",*it );
+
+    pc.printf("\r\n");
+
+    // Display LoRa parameters
+    // Display label and values in different colours, show pretty values not numeric values where applicable
+    pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") );
+    pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() );
+    pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() );
+    pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() );
+    pc.printf("Join Retries: %d\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") );
+    pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() );
+    pc.printf("Link Check Thold: %s\r\n", dot->getLinkCheckThreshold() );
+    pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() );
+    pc.printf("Tx Power: %d\r\n", dot->getTxPower() );
+    pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" );
+    pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
+    pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N")  );
+
     probe.setResolution(9);
 
     char dataBuf[50];
@@ -178,8 +266,9 @@
         logInfo("going to sleep for %d seconds", sleep_time);
 
         // go to sleep and wake up automatically sleep_time seconds later
-        dot->sleep(sleep_time, mDot::RTC_ALARM);
+//        dot->sleep(sleep_time, mDot::RTC_ALARM);
 
+        wait_ms(sleep_time * 1000);
     }
 
     return 0;