1 step for smart green house

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
belloula
Date:
Wed May 22 22:28:10 2019 +0000
Commit message:
for smart green house

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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fcce3b1e1dec DHT/DHT.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT/DHT.cpp	Wed May 22 22:28: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(1);
+    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;
+}
+
+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 fcce3b1e1dec DHT/DHT.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT/DHT.h	Wed May 22 22:28:10 2019 +0000
@@ -0,0 +1,99 @@
+/*
+ *  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"
+
+typedef enum eType eType;
+enum eType {
+    DHT11     = 11,
+    SEN11301P = 11,
+    RHT01     = 11,
+    DHT22     = 22,
+    AM2302    = 22,
+    SEN51035P = 22,
+    RHT02     = 22,
+    RHT03     = 22
+};
+
+typedef enum eError eError;
+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 eScale eScale;
+enum eScale {
+    CELCIUS = 0,
+    FARENHEIT,
+    KELVIN
+};
+
+
+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 fcce3b1e1dec main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 22 22:28:10 2019 +0000
@@ -0,0 +1,240 @@
+#include "mbed.h"          // MBED LIBRARY
+#include "string.h"
+#include "DHT.h"
+
+// SETUP (ASSIGN) SERIAL COMMUNICATION PINS ON MBED
+Serial pc(USBTX, USBRX);  // PC SERIAL OVER USB PORT ON MBED
+Serial HC06(p9, p10);
+DHT sensor(p23, DHT11);
+
+DigitalOut Relais1(p17);
+DigitalOut Relais2(p18);
+DigitalOut Relais3(p16);
+DigitalOut Relais4(p20);
+
+
+#define SBIT_CLKEN     0    /* RTC Clock Enable*/
+#define SBIT_CTCRST    1    /* RTC Clock Reset */
+#define SBIT_CCALEN    4    /* RTC Calibration counter enable */
+
+
+int32_t TempsUnix = 0;
+int32_t TempsArrosage = 5;// en seconde
+
+uint8_t AlarmHOUR   = 8;   // Alarme hour value
+uint8_t AlarmMIN    = 1;   // Alarme min value
+
+bool alarmstate = false;// Determines whether the alarm is set or not
+bool ringflag = false;  // Determines if alarm is ringing
+
+
+Ticker myTicker;
+
+
+
+
+
+
+void setclock()
+{
+
+    pc.printf("Entrer Temps Unix actuel from https://www.timestampconvert.com/:\r\n");
+    pc.scanf("%u", &TempsUnix);//https://www.timestampconvert.com/
+    pc.printf("Temps Unix actuel %d:\r\n",TempsUnix);
+    set_time(TempsUnix); // Set time to actual time
+}
+
+
+
+void readHC06()
+{
+
+    int a = HC06.getc();
+    pc.printf("Le chifre est: %d \n\r",a);
+
+    switch(a) {
+        case '1':
+            Relais1=0;
+            break;
+
+        case '2':
+            Relais2=0;
+            break;
+
+        case '3':
+            Relais3=0;
+            break;
+
+        case '4':
+            Relais4=0;
+            break;
+        case 'A':
+            Relais1=1;
+            break;
+
+        case 'B':
+            Relais2=1;
+            break;
+
+        case 'C':
+            Relais3=1;
+            break;
+
+        case 'D':
+            Relais4=1;
+            break;
+        case '9': // A changer pour le rendre normalement ouvert
+            Relais1=0;
+            Relais2=0;
+            Relais3=0;
+            Relais4=0;
+            break;
+
+        case 'I':
+            Relais1=1;
+            Relais2=1;
+            Relais3=1;
+            Relais4=1;
+            break;
+        // operator doesn't match any case constant (+, -, *, /)
+        default:
+            printf("Error! operator is not correct");
+    }
+}
+void ResetRelais()
+{
+    Relais1=1;
+    Relais2=1;
+    Relais3=1;
+    Relais4=1;
+}
+
+
+/******************************************************************
+ * Arrosage
+ *
+ * This function rings while alarm by power pompe
+ *****************************************************************/
+void alarm_ring()
+{
+    Relais1=0;//pompe en marche
+    ringflag = true;//indicateur arrossage active
+    --TempsArrosage; //decremettion temps d'arrosage de 1 secaonde
+    if(TempsArrosage == 0 ) {
+        Relais1=1; //Arret de la pompe
+        myTicker.detach();// blocage des interruptions de la larme
+    }
+
+
+}
+
+/******************************************************************
+ * Alarm_check
+ *
+ * This function compares the alarm time vs the current time. Once
+ * alarm time and real time match it begins ringing the alarm.
+ * Once the times differ then it turns off the alarm.
+ *****************************************************************/
+void alarm_check()
+{
+    if ((LPC_RTC->HOUR == AlarmHOUR) && (LPC_RTC->MIN == AlarmMIN)) {
+        if ((alarmstate == true) && (ringflag == false)) {
+            myTicker.attach_us(&alarm_ring,1000000.0f); // run every 40us, Could use .attach(&onTick,0.00004) if you prefer.
+        }
+    } else {
+
+
+    }
+}
+
+int main()
+{
+
+
+    uint16_t year;
+    uint8_t hour, min, sec, date, month;
+    int error = 0;
+    float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
+
+    pc.baud(115200);
+    HC06.baud(9600);
+    ResetRelais();
+    pc.printf("Start Programme\n\r");
+
+
+    /* Disable RTC clock, reset clock, Enable RTC calibration */
+    LPC_RTC->CCR = ((1 << SBIT_CTCRST ) | (1 << SBIT_CCALEN));
+    LPC_RTC->CALIBRATION = 0x00;
+    LPC_RTC->CCR = (1 << SBIT_CLKEN);    /* Enable the clock for RTC */
+
+    //  setclock();
+
+    /* Set Date and Time only once, comment these lines after setting the time and date */
+    // Set Date 23th Mai 2019
+    LPC_RTC->DOM    = 23;   // Update date value
+    LPC_RTC->MONTH  = 05;   // Update month value
+    LPC_RTC->YEAR   = 2019; // Update year value
+
+    // Set Time 10:40:25 AM
+    LPC_RTC->HOUR   = 8;   // Update hour value
+    LPC_RTC->MIN    = 00;   // Update min value
+    LPC_RTC->SEC    = 00;   // Update sec value
+
+
+    //set_time(1558477269); // Set time to Wed, 28 Oct 2009 11:35:37
+
+    alarmstate = true;
+    HC06.attach(readHC06);
+
+
+
+
+//      time_t seconds = time(NULL);
+//    printf ("Time as seconds since January 1, 1970 = %d\n", seconds);        //https://time.is/Unix_time_now
+//    printf ("Time as a basic string = %s", ctime(&seconds));
+//    char buffer [32];
+//    strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
+//    printf ("Time as a custom formatted string = %s", buffer);
+
+
+
+    while(1) {
+
+        wait(1.0f);
+        /* Read Time */
+        hour = LPC_RTC->HOUR;
+        min  = LPC_RTC->MIN;
+        sec  = LPC_RTC->SEC;
+
+        /* Read Date */
+        date  = LPC_RTC->DOM;
+        month = LPC_RTC->MONTH;
+        year  = LPC_RTC->YEAR;
+
+
+        pc.printf("Date: %2d/%2d/%4u\n\r",date,month,year);
+        pc.printf("Time: %2d:%2d:%2d\n\r",hour,min,sec);
+
+        error = sensor.readData();
+
+        if (0 == error) {
+            c   = sensor.ReadTemperature(CELCIUS);
+            f   = sensor.ReadTemperature(FARENHEIT);
+            k   = sensor.ReadTemperature(KELVIN);
+            h   = sensor.ReadHumidity();
+            dp  = sensor.CalcdewPoint(c, h);
+            dpf = sensor.CalcdewPointFast(c, h);
+            printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
+            printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n\r\n\r", h, dp, dpf);
+
+
+        } else {
+            printf("Error: %d\n", error);
+        }
+        /* Check to see if the alarm should be started/stopped */
+        alarm_check();
+
+
+    }
+
+}
diff -r 000000000000 -r fcce3b1e1dec mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed May 22 22:28:10 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file