Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / examples / OICSensorBoard / sensors.h
index 44ad441..aecbf4c 100644 (file)
@@ -25,6 +25,7 @@
 #define ONBOARD_LED_PIN 13
 #define TEMPERATURE_AIO_PIN 0
 #define LIGHT_SENSOR_AIO_PIN 2
+#define SAMPLE_NUM 5
 
 namespace Sensors
 {
@@ -60,20 +61,40 @@ inline void SetOnboardLed(int on)
         mraa_gpio_write(led_gpio, on); // Writes into GPIO
 }
 
-inline float GetTemperatureInC()
+inline float GetAverageTemperatureRaw()
 {
-    float ret = 0;
     if (tmp_aio == NULL)
     {
         tmp_aio = mraa_aio_init(TEMPERATURE_AIO_PIN); // initialize pin 0
     }
-    if (tmp_aio != NULL)
-    {
-        uint16_t adc_value = mraa_aio_read(tmp_aio); // read the raw value
-        //convert reading to temperature
-        float beta = 4090.0; //the beta of the thermistor, magic number
-        ret = beta / (log((4095.0 * 10 / adc_value - 10) / 10) + beta / 298.0) - 273.0;
-    }
+    
+    uint16_t adc_value = 0;
+    for (int i=0; i< SAMPLE_NUM; i++)
+        adc_value += mraa_aio_read(tmp_aio);           // read the raw value
+    
+    float average = (float)adc_value/SAMPLE_NUM;
+    cout << "Temperature reading raw ..."  << average << endl;
+    
+    return average;
+}
+
+inline float GetTemperatureInC()
+{
+    // Temperature calculation using simpilfy Steinhart-Hart equation
+    //
+    //          1/T = 1/T0 + 1/beta*ln (R/R0)
+    //
+    // where T0 = 25C room temp, R0 = 10000 ohms
+    //
+    float beta = 4090.0;            //the beta of the thermistor, magic number
+    float t_raw = GetAverageTemperatureRaw();
+    float R = 1023.0/t_raw -1;      // 
+    R = 10000.0/R;                  // 10K resistor divider circuit
+        
+    float T1 = log(R/10000.0)/beta; // natural log 
+    float T2 = T1 + 1.0/298.15;     // room temp 25C= 298.15K
+    float ret = 1.0/T2 - 273.0;
     return ret;
 }