Demo of DHT11->mDot->TTN

Dependencies:   DHT11 libmDot mbed-rtos mbed

Revision:
3:367aa95f9771
Parent:
2:9db840d12557
Child:
4:f649ab1b61d1
--- a/main.cpp	Mon Sep 21 18:45:15 2015 +0000
+++ b/main.cpp	Tue Sep 22 13:58:59 2015 +0000
@@ -1,4 +1,6 @@
 /** mDot_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
@@ -24,23 +26,30 @@
 
 // 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 = "<network name>";
+static std::string config_network_pass = "<network password>";
+// 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 x on mDot
+// D13 on Dev Board, pin 18 on mDot
 #define DATA_PIN     PA_5
-// A0 on Dev Board, pin x on mDot
+// A0 on Dev Board, pin 20 on mDot
 //#define DATA_PIN     PB_1
 
+// 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);
 
-//void log_error(mDot* dot, const char* msg, int32_t retval);
-
+// Serial via USB for debugging only
 Serial pc(USBTX,USBRX);
 
 
@@ -53,26 +62,25 @@
 
     float temperature = 0.0;
 
+    // Enable internal pullup on input pin
+    configReset.mode(PullUp);
+
     pc.baud(115200);
     pc.printf("mDot LoRa Temperature sensor\n\r");
 
     // get a mDot handle
     dot = mDot::getInstance();
 
-    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();
 
-    // Check pin, if low then reset config.
-//       if ((ret = dot->setJoinMode( mDot::AUTO_OTA )) != mDot::MDOT_OK) {
-//            logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-//        }
-
-
-    if( config_network_name.compare(configNetworkName) != 0 ) {
+    // 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");
 
@@ -99,10 +107,10 @@
         // 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->setTxDataRate( mDot::SF_12 );
         dot->setTxPower( 14 );
         dot->setAck( 0 );       // 1 retries on Ack, 0 to disable
- 
+
 //    Not applicable for 868MHz in EU
 //    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
 //        initStatus = false;
@@ -110,12 +118,10 @@
 //    }
 
         if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
-//        initStatus = false;
             logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
         }
 
         if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
-//        initStatus = false;
             logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
         }
 
@@ -135,20 +141,18 @@
 
     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
         logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
-        //wait(2);
         wait_ms(dot->getNextTxMs() + 1);
     }
 
     probe.setResolution(9);
 
     char dataBuf[50];
-//    for (uint8_t i = 0; i < iterations; i++) {
     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());
-        // Output data as JSON e.g. {"temperature":"21.3"}
+        // Output data as JSON e.g. {"tmp":21.3}
         temperature = probe.temperature();
         sprintf(dataBuf, "{\"tmp\":%3.1f}", temperature );
         send_data.clear();
@@ -169,13 +173,6 @@
         // go to sleep and wake up automatically sleep_time seconds later
         dot->sleep(sleep_time, mDot::RTC_ALARM);
 
-        /*
-                next_tx = dot->getNextTxMs() + 1;
-                logInfo("waiting %ld ms to transmit again", next_tx);
-                wait_ms(next_tx);
-                logInfo("waiting another %d seconds", wait_time);
-                wait(wait_time);
-        */
     }
 
     return 0;