Temperature reading demo

Dependencies:   DHT libmDot mbed-rtos mbed

Revision:
13:d42337b32b1e
Parent:
12:e82913ee9c61
diff -r e82913ee9c61 -r d42337b32b1e main.cpp
--- a/main.cpp	Sat Nov 14 20:58:36 2015 +0000
+++ b/main.cpp	Thu Nov 19 21:28:11 2015 +0000
@@ -10,7 +10,7 @@
  */
 
 #include "mbed.h"
-#include "DS1820.h"
+#include "DHT.h"
 #include "mDot.h"
 #include "MTSLog.h"
 #include "MTSText.h"
@@ -30,16 +30,16 @@
 uint8_t NwkSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
 
 // Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace
-uint8_t NetworkAddr[4]= {0x02,0x01,0x00,0x00};      // Our Network address or Node ID
+uint8_t NetworkAddr[4]= {0x02,0x01,0x2D,0x00};      // Our Network address or Node ID
 
 
 // Some defines for the LoRa configuration
-#define LORA_SF mDot::SF_12
+#define LORA_SF mDot::SF_9
 #define LORA_ACK 0
 #define LORA_TXPOWER 14
 
 // Ignoring sub band for EU modules.
-//static uint8_t config_frequency_sub_band = 1;
+static uint8_t config_frequency_sub_band = 7;
 
 // DS18B20 OneWire pin
 // D13 on Dev Board, pin 18 on mDot, Compatible with Oxford Flood Network PCB temperature sensor.
@@ -48,13 +48,16 @@
 //#define DATA_PIN     PB_1
 
 // Temperature sensor object
-DS1820 probe(DATA_PIN);
+#define DHTTYPE DHT22
+
+DHT sensor(DATA_PIN, DHTTYPE);
 
 // Serial via USB for debugging only
 Serial pc(USBTX,USBRX);
 
 int main()
 {
+    int initStatus;
     int32_t ret;
     mDot* dot;
     std::vector<uint8_t> send_data;
@@ -124,10 +127,10 @@
     }
 
 //    Not applicable for 868MHz in EU
-//    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
-//        initStatus = false;
-//        logError(dot, "failed to set frequency sub band", ret);
-//    }
+    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
+        initStatus = false;
+        logError("failed to set frequency sub band", ret, mDot::getReturnCodeString(ret).c_str());
+    }
 
     logInfo("Set Network Address");
     if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) {
@@ -201,28 +204,37 @@
     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 ) {
-        //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 );
-        send_data.clear();
-        // probably not the most efficent way to do this
-        for( int i=0; i< strlen(dataBuf); i++ )
-            send_data.push_back( dataBuf[i] );
+    int err;
+        err = sensor.readData();        
+ //       if (err == 0) {
+            // Output data as JSON e.g. {"t":21.3}
+            temperature = sensor.ReadTemperature(CELCIUS);        
+            sprintf(dataBuf, "{\"t\":%4.2f C}", temperature );
+            send_data.clear();
+            // probably not the most efficent way to do this
+            for( int i=0; i< strlen(dataBuf); i++ )
+                send_data.push_back( dataBuf[i] );
+    
+            if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
+                logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
+            } else {
+                logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
+                logError("send data: %d %s", err, Text::bin2hexString(send_data).c_str());   
+            }
+//        } else {
+//                logError("failed to read Temp: [%d][%s]", err, mDot::getReturnCodeString(err).c_str());
+       
+ //       }       
 
-        if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
-            logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
-        } else {
-            logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
-        }
+
 
-        // Should  sleep here and wakeup after a set 10 minute interval.
-        uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), 600);
+        // Should  sleep here and wakeup after a set 5 minute interval.
+        uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), 60);
         logInfo("going to sleep for %d seconds", sleep_time);
 
         // go to sleep and wake up automatically sleep_time seconds later