Demo of DHT11->mDot->TTN

Dependencies:   DHT11 libmDot mbed-rtos mbed

Revision:
9:086351e54b57
Parent:
8:8070e9d660e4
Child:
10:717a3d909c6b
--- a/main.cpp	Tue Nov 10 21:46:38 2015 +0000
+++ b/main.cpp	Sat Nov 14 20:15:20 2015 +0000
@@ -1,6 +1,6 @@
 /** 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.
+ * It used the MANUAL join mode with parameters for The Things Network.
+ * 
  *
  * Uses MultiTech mDot developer board http://www.multitech.com/models/94558010LF
  * Requires a MultiTech MultiConnect Conduit http://www.multitech.com/models/94557203LF
@@ -29,8 +29,7 @@
 // Network session key
 uint8_t NwkSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
 
-// Network Address
-//uint8_t NetworkAddr[4]= {0x02,0x01,0x2A,0x00};      // Dev board
+// Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace
 uint8_t NetworkAddr[4]= {0x02,0x01,0x2A,0x01};      // Temperature sensor/OxFloodNet sensor
 
 
@@ -42,9 +41,6 @@
 // Ignoring sub band for EU modules.
 //static uint8_t config_frequency_sub_band = 1;
 
-// mDot/dev board activity LED
-//#define ACTIVITY_LED PA_0
-
 // DS18B20 OneWire pin
 // D13 on Dev Board, pin 18 on mDot, Compatible with Oxford Flood Network PCB temperature sensor.
 #define DATA_PIN     PA_5
@@ -54,16 +50,12 @@
 // A2 - input to reset LoRaWAN config. Pin 15 om mDot.
 #define CONFIG_RESET PC_1
 
-// Config Reset intput
-DigitalIn configReset(CONFIG_RESET);
-
 // Temperature sensor object
 DS1820 probe(DATA_PIN);
 
 // Serial via USB for debugging only
 Serial pc(USBTX,USBRX);
 
-
 int main()
 {
     int32_t ret;
@@ -85,53 +77,30 @@
     // get a mDot handle
     dot = mDot::getInstance();
 
-//    dot->setLogLevel(MTSLog::WARNING_LEVEL);
-    dot->setLogLevel(MTSLog::TRACE_LEVEL);
+    dot->setLogLevel(MTSLog::WARNING_LEVEL);
+//    dot->setLogLevel(MTSLog::TRACE_LEVEL);
 
     logInfo("Checking Config");
 
     // Test if we've already saved the config
     std::string configNetworkName = dot->getNetworkName();
 
-//    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++);
-
     it = NetworkAddr;
     for (uint8_t i = 0; i<4; i++)
         networkAddr.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 ) {
-//    if( !configReset ) {
-        // Config Reset link present
-        logInfo("Resetting Config");
-        // reset to default config so we know what state we're in
-        dot->resetConfig();
-//    }
+    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
 //    dot->setJoinByteOrder(mDot::LSB);
     dot->setJoinByteOrder(mDot::MSB);       // This is default for > 1.0.30 Conduit
 
-//    dot->setCrc(true);
-
-    // 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
-//    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.
     logInfo("Set SF");
@@ -181,22 +150,11 @@
         logError("Failed to set Network 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->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
-//            logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-//        }
-
     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();
@@ -207,7 +165,6 @@
     pc.printf("Data Session Key: ");
     pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
 
-
     pc.printf("Device ID ");
     std::vector<uint8_t> deviceId;
     deviceId = dot->getDeviceId();
@@ -216,7 +173,7 @@
     pc.printf("\r\n");
 
     std::vector<uint8_t> netAddress;
-    
+
     pc.printf("Network Address ");
     netAddress = dot->getNetworkAddress();
     for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
@@ -240,23 +197,22 @@
     pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
     pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N")  );
 
-//    logInfo("Joining Network");
+    logInfo("Joining Network");
 
-//    while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
-//        logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
-//        wait_ms(dot->getNextTxMs() + 1);
-//    }
+    while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
+        logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
+        wait_ms(dot->getNextTxMs() + 1);
+    }
 
-//    logInfo("Joined Network");
+    logInfo("Joined Network");
 
+    // Set the Temperature sesnor resolution, 9 bits is enough and makes it faster to provide a reading.
     probe.setResolution(9);
 
     char dataBuf[50];
     while( 1 ) {
-        // This takes upto 750mS, way too long. Change to 9 bit resolution if not already used.
-
-        probe.convertTemperature(true, DS1820::all_devices);         //Start temperature conversion, wait until ready
-//        printf("It is %3.1fC\r\n", probe.temperature());
+        //Start temperature conversion, wait until ready
+        probe.convertTemperature(true, DS1820::all_devices);
         // Output data as JSON e.g. {"t":21.3}
         temperature = probe.temperature();
         sprintf(dataBuf, "{\"t\":%3.1f}", temperature );
@@ -277,7 +233,6 @@
 
         // go to sleep and wake up automatically sleep_time seconds later
         dot->sleep(sleep_time, mDot::RTC_ALARM);
-//        wait_ms(sleep_time * 1000);
     }
 
     return 0;