Display the value of the three sensors on the OLED.

Dependencies:   SSD1308_128x64_I2C Grove_temperature

Files at this revision

API Documentation at this revision

Comitter:
dcj001
Date:
Wed Dec 11 16:50:10 2019 +0000
Commit message:
Display the value from the three sensors on the OLED.

Changed in this revision

DHT/DHT.cpp Show annotated file Show diff for this revision Revisions of this file
DHT/DHT.h Show annotated file Show diff for this revision Revisions of this file
Grove_temperature.lib Show annotated file Show diff for this revision Revisions of this file
SSD1308_128x64_I2C.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4dacc9be0df2 DHT/DHT.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT/DHT.cpp	Wed Dec 11 16:50:10 2019 +0000
@@ -0,0 +1,227 @@
+/*
+ *  DHT Library for  Digital-output Humidity and Temperature sensors
+ *
+ *  Works with DHT11, DHT22
+ *             SEN11301P,  Grove - Temperature&Humidity Sensor     (Seeed Studio)
+ *             SEN51035P,  Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
+ *             AM2302   ,  temperature-humidity sensor
+ *             HM2303   ,  Digital-output humidity and temperature sensor
+ *
+ *  Copyright (C) Wim De Roeve
+ *                based on DHT22 sensor library by HO WING KIT
+ *                Arduino DHT11 library
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documnetation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to  whom the Software is
+ * furished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "DHT.h"
+
+#define DHT_DATA_BIT_COUNT 40
+
+DHT::DHT(PinName pin, eType DHTtype)
+{
+    _pin = pin;
+    _DHTtype = DHTtype;
+    _firsttime = true;
+}
+
+DHT::~DHT()
+{
+    
+}
+
+eError DHT::stall(DigitalInOut &io, int const level, int const max_time)
+{
+    int cnt = 0;
+    while (level == io) {
+        if (cnt > max_time) {
+            return ERROR_NO_PATIENCE;
+        }
+        cnt++;
+        wait_us(1);
+    }
+    return ERROR_NONE;
+}
+
+eError DHT::readData()
+{
+    uint8_t i = 0, j = 0, b = 0, data_valid = 0;
+    uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0};
+
+    eError err = ERROR_NONE;
+    time_t currentTime = time(NULL);
+
+    DigitalInOut DHT_io(_pin);
+
+    // IO must be in hi state to start
+    if (ERROR_NONE != stall(DHT_io, 0, 250)) {
+        return BUS_BUSY;
+    }
+
+    // start the transfer
+    DHT_io.output();
+    DHT_io = 0;
+    // only 500uS for DHT22 but 18ms for DHT11
+    (_DHTtype == 11) ? wait_ms(18) : wait(0.0005);
+    DHT_io = 1;
+    wait_us(30);
+    DHT_io.input();
+    // wait till the sensor grabs the bus
+    if (ERROR_NONE != stall(DHT_io, 1, 40)) {
+        return ERROR_NOT_PRESENT;
+    }
+    // sensor should signal low 80us and then hi 80us
+    if (ERROR_NONE != stall(DHT_io, 0, 100)) {
+        return ERROR_SYNC_TIMEOUT;
+    }
+    if (ERROR_NONE != stall(DHT_io, 1, 100)) {
+        return ERROR_NO_PATIENCE;
+    }
+    // capture the data
+    for (i = 0; i < 5; i++) {
+        for (j = 0; j < 8; j++) {
+            if (ERROR_NONE != stall(DHT_io, 0, 75)) {
+                return ERROR_DATA_TIMEOUT;
+            }
+            // logic 0 is 28us max, 1 is 70us
+            wait_us(40);
+            bit_value[i*8+j] = DHT_io;
+            if (ERROR_NONE != stall(DHT_io, 1, 50)) {
+                return ERROR_DATA_TIMEOUT;
+            }
+        }
+    }
+    // store the data
+    for (i = 0; i < 5; i++) {
+        b=0;
+        for (j=0; j<8; j++) {
+            if (bit_value[i*8+j] == 1) {
+                b |= (1 << (7-j));
+            }
+        }
+        DHT_data[i]=b;
+    }
+
+    // uncomment to see the checksum error if it exists
+    //printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]);
+    data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3];
+    if (DHT_data[4] == data_valid) {
+        _lastReadTime = currentTime;
+        _lastTemperature = CalcTemperature();
+        _lastHumidity = CalcHumidity();
+
+    } else {
+        err = ERROR_CHECKSUM;
+    }
+
+    return err;
+
+}
+
+float DHT::CalcTemperature()
+{
+    int v;
+
+    switch (_DHTtype) {
+        case DHT11:
+            v = DHT_data[2];
+            return float(v);
+        case DHT22:
+            v = DHT_data[2] & 0x7F;
+            v *= 256;
+            v += DHT_data[3];
+            v /= 10;
+            if (DHT_data[2] & 0x80)
+                v *= -1;
+            return float(v);
+    }
+    return 0;
+}
+
+float DHT::ReadHumidity()
+{
+    return _lastHumidity+30;
+}
+
+float DHT::ConvertCelciustoFarenheit(float const celsius)
+{
+    return celsius * 9 / 5 + 32;
+}
+
+float DHT::ConvertCelciustoKelvin(float const celsius)
+{
+    return celsius + 273.15;
+}
+
+// dewPoint function NOAA
+// reference: http://wahiduddin.net/calc/density_algorithms.htm
+float DHT::CalcdewPoint(float const celsius, float const humidity)
+{
+    float A0= 373.15/(273.15 + celsius);
+    float SUM = -7.90298 * (A0-1);
+    SUM += 5.02808 * log10(A0);
+    SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
+    SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
+    SUM += log10(1013.246);
+    float VP = pow(10, SUM-3) * (humidity);
+    float T = log(VP/0.61078);   // temp var
+    return (241.88 * T) / (17.558-T);
+}
+
+// delta max = 0.6544 wrt dewPoint()
+// 5x faster than dewPoint()
+// reference: http://en.wikipedia.org/wiki/Dew_point
+/*float DHT::CalcdewPointFast(float const celsius, float const humidity)
+{
+    float a = 17.271;
+    float b = 237.7;
+    float temp = (a * celsius) / (b + celsius) + log(humidity/100);
+    float Td = (b * temp) / (a - temp);
+    return Td;
+}*/
+
+float DHT::ReadTemperature(eScale Scale)
+{
+    if (Scale == FARENHEIT)
+        return ConvertCelciustoFarenheit(_lastTemperature);
+    else if (Scale == KELVIN)
+        return ConvertCelciustoKelvin(_lastTemperature);
+    else
+        return _lastTemperature;
+}
+
+float DHT::CalcHumidity()
+{
+    int v;
+
+    switch (_DHTtype) {
+        case DHT11:
+            v = DHT_data[0];
+            return float(v);
+        case DHT22:
+            v = DHT_data[0];
+            v *= 256;
+            v += DHT_data[1];
+            v /= 10;
+            return float(v);
+    }
+    return 0;
+}
+
+
diff -r 000000000000 -r 4dacc9be0df2 DHT/DHT.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT/DHT.h	Wed Dec 11 16:50:10 2019 +0000
@@ -0,0 +1,101 @@
+/*
+ *  DHT Library for  Digital-output Humidity and Temperature sensors
+ *
+ *  Works with DHT11, DHT21, DHT22
+ *             SEN11301P,  Grove - Temperature&Humidity Sensor     (Seeed Studio)
+ *             SEN51035P,  Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
+ *             AM2302   ,  temperature-humidity sensor
+ *             RHT01,RHT02, RHT03    ,  Humidity and Temperature Sensor         (Sparkfun)
+ *
+ *  Copyright (C) Wim De Roeve
+ *                based on DHT22 sensor library by HO WING KIT
+ *                Arduino DHT11 library
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documnetation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to  whom the Software is
+ * furished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_DHT_H
+#define MBED_DHT_H
+
+#include "mbed.h"
+
+
+enum eType {
+    DHT11     = 11,
+    SEN11301P = 11,
+    RHT01     = 11,
+    DHT22     = 22,
+    AM2302    = 22,
+    SEN51035P = 22,
+    RHT02     = 22,
+    RHT03     = 22
+};
+typedef enum eType eType;
+
+
+enum eError {
+    ERROR_NONE = 0,
+    BUS_BUSY,
+    ERROR_NOT_PRESENT,
+    ERROR_ACK_TOO_LONG,
+    ERROR_SYNC_TIMEOUT,
+    ERROR_DATA_TIMEOUT,
+    ERROR_CHECKSUM,
+    ERROR_NO_PATIENCE
+};
+typedef enum eError eError;
+
+
+enum eScale {
+    CELCIUS = 0,
+    FARENHEIT,
+    KELVIN
+};
+typedef enum eScale eScale;
+
+class DHT
+{
+
+public:
+
+    DHT(PinName pin, eType DHTtype);
+    ~DHT();
+    eError readData(void);
+    float ReadHumidity(void);
+    float ReadTemperature(eScale const Scale);
+    float CalcdewPoint(float const celsius, float const humidity);
+    float CalcdewPointFast(float const celsius, float const humidity);
+
+private:
+    time_t  _lastReadTime;
+    float _lastTemperature;
+    float _lastHumidity;
+    PinName _pin;
+    bool _firsttime;
+    eType _DHTtype;
+    uint8_t DHT_data[5];
+    float CalcTemperature();
+    float CalcHumidity();
+    float ConvertCelciustoFarenheit(float const);
+    float ConvertCelciustoKelvin(float const);
+    eError stall(DigitalInOut &io, int const level, int const max_time);
+
+};
+
+#endif
diff -r 000000000000 -r 4dacc9be0df2 Grove_temperature.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Grove_temperature.lib	Wed Dec 11 16:50:10 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/MACRUM/code/Grove_temperature/#aee37a51ccbb
diff -r 000000000000 -r 4dacc9be0df2 SSD1308_128x64_I2C.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SSD1308_128x64_I2C.lib	Wed Dec 11 16:50:10 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/wim/code/SSD1308_128x64_I2C/#e564cde8e03e
diff -r 000000000000 -r 4dacc9be0df2 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 11 16:50:10 2019 +0000
@@ -0,0 +1,131 @@
+#include "mbed.h"
+#include "Grove_temperature.h"
+#include "DHT.h"
+#include "SSD1308.h"
+
+DigitalOut myled(LED2);           //Create a DigitalOut object named "myled", it's connected to Pin LED2.
+Grove_temperature temp_obj(A1);   //Create a Crove_temperature object named "temp_obj", it's connected to Pin  A1.
+DHT dht11(D6,DHT11);              //Create a DHT object named "dht11", it's connected to Port D6.   
+AnalogIn potar(A3);               //Create a varialbe resisitor object named "potar", it's connected to Pin A3.
+I2C i2c(PB_9,PB_8);               //OLED is connected to the L073RZ's I2C port.
+SSD1308 oled(&i2c, SSD1308_SA0);  //Create a SSD1308 object named "oled".    
+
+int main(void)
+{
+    
+    while (true) {
+/****************************************** For Class 1 and Class 2 **************************************************/        
+        
+//        /* Part1: LED test */
+//        printf("*** Part1: LED test ***\n");
+//        myled = !myled;
+//        if(myled == 1)
+//            printf("Led2 is on!\n\n");
+//        else
+//            printf("Led2 is off!\n\n");
+//        wait(1);
+//        
+//        /* Part2: NTC temperature sensor */
+//        printf("*** Part2: NTC temperature sensor ***\n");
+//        printf("temperature = %2.2f\n\n", temp_obj.getTemperature());
+//        wait(1);
+//        
+//        /* Part3: Variable resistor */
+//        printf("*** Part3: Variable resistor ***\n");
+//        printf("Percentage value: %3.3f%%\n", potar.read()*100.0f);
+//        printf("Normal value(0-1): %3.3f\n\n", potar.read_u16()/65535.0);
+//        wait(1);
+//        
+//        /* Part4: DHT11 temperature and humidity sensor */
+//        printf("*** Part4: DHT11 temperature and humidity sensor ***\n");
+//        int err;
+//        float temperature;
+//        float humidite;
+//        float point_rose;
+//        
+//        err = dht11.readData();   // Get the data from the sensor and the return value of the function is to judge if any error happened.
+//        if (err == 0) {           // No errors happened
+//            temperature = dht11.ReadTemperature(CELCIUS);
+//            humidite = dht11.ReadHumidity();
+//            point_rose = dht11.CalcdewPoint(dht11.ReadTemperature(CELCIUS), dht11.ReadHumidity());
+//            printf("Temperature : %4.2f C \n",temperature); // send the data to the serial port connected to PC(9600bauds).
+//            printf("Humidite : %4.2f %% \n",humidite);
+//            printf("Point rose : %4.2f C \n\n",point_rose);
+//        } else
+//            printf("\r\nErreur %i \n\n",err);
+//        
+//        wait(1);
+//        
+//        /* Part5: OLED SSD1308 */
+//        printf("*** Part5: OLED SSD1308 ***\n\n");
+//        oled.writeString(0, 0, "Hello mbed !");
+//        wait(1);
+//        oled.clearDisplay();
+//        
+//        wait(1);
+
+/****************************************** For Class 3 ****************************************************************/
+
+        
+        /* Part6: The valus of the three sensors will be displayed on the OLED */
+        printf("*** Part6: The valus of the three sensors will be displayed on the OLED ***\n");
+        
+        //
+        // title
+        oled.writeString(0,2,"SmartCampus");
+        
+        //
+        // public variables
+        char str[10];         //store the conversion( float-to-char ) result.
+        
+        //
+        // NTC value
+        oled.writeString(2,0,"NTC:");
+        oled.writeString(2,15,"C");
+      
+        float ntc_temp = temp_obj.getTemperature();   
+        sprintf(str,"%4.3f",ntc_temp);      //data type conversion.
+        oled.writeString(2,4,str);
+        
+        //
+        // variable resister
+        oled.writeString(3,0,"VarRes:");
+
+        float var_res = potar.read();
+        sprintf(str,"%4.3f",var_res);       //data type conversion.
+        oled.writeString(3,7,str);
+        
+        //
+        // DHT11
+        oled.writeString(4,0,"DHT11");
+        oled.writeString(5,0,"Temp:");
+        oled.writeString(5,15,"C");
+        oled.writeString(6,0,"Humi:");   
+        oled.writeString(6,15,"%");    
+        
+        int err = 0;
+        float temperature;
+        float humidity;
+        
+        err = dht11.readData();   // Get the data from the sensor and the return value of the function is to judge if any error happened.
+        if (err == 0)             // No errors happened
+        {           
+            temperature = dht11.ReadTemperature(CELCIUS);
+            humidity = dht11.ReadHumidity();
+            
+            sprintf(str,"%4.3f",temperature);       //data type conversion.
+            oled.writeString(5,5,str);
+            sprintf(str,"%4.3f",humidity);          //data type conversion.
+            oled.writeString(6,5,str);
+            
+            oled.writeString(7,0,"                ");   //If errors have been solved ,then clear the errors.         
+        } else
+        {
+            oled.writeString(7,0,"DHT11 Error");     
+            printf("%d\n",err);
+        }
+               
+        wait(1);
+    }
+    
+}
diff -r 000000000000 -r 4dacc9be0df2 mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Wed Dec 11 16:50:10 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#485bdeee150e2bc8ed75e27d936060fb63a7a7d1