Abdelmalek BELLOULA / Mbed 2 deprecated SGRHouse

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"          // MBED LIBRARY
00002 #include "string.h"
00003 #include "DHT.h"
00004 
00005 // SETUP (ASSIGN) SERIAL COMMUNICATION PINS ON MBED
00006 Serial pc(USBTX, USBRX);  // PC SERIAL OVER USB PORT ON MBED
00007 Serial HC06(p9, p10);
00008 DHT sensor(p23, DHT11);
00009 
00010 DigitalOut Relais1(p17);
00011 DigitalOut Relais2(p18);
00012 DigitalOut Relais3(p16);
00013 DigitalOut Relais4(p20);
00014 
00015 
00016 #define SBIT_CLKEN     0    /* RTC Clock Enable*/
00017 #define SBIT_CTCRST    1    /* RTC Clock Reset */
00018 #define SBIT_CCALEN    4    /* RTC Calibration counter enable */
00019 
00020 
00021 int32_t TempsUnix = 0;
00022 int32_t TempsArrosage = 5;// en seconde
00023 
00024 uint8_t AlarmHOUR   = 8;   // Alarme hour value
00025 uint8_t AlarmMIN    = 1;   // Alarme min value
00026 
00027 bool alarmstate = false;// Determines whether the alarm is set or not
00028 bool ringflag = false;  // Determines if alarm is ringing
00029 
00030 
00031 Ticker myTicker;
00032 
00033 
00034 
00035 
00036 
00037 
00038 void setclock()
00039 {
00040 
00041     pc.printf("Entrer Temps Unix actuel from https://www.timestampconvert.com/:\r\n");
00042     pc.scanf("%u", &TempsUnix);//https://www.timestampconvert.com/
00043     pc.printf("Temps Unix actuel %d:\r\n",TempsUnix);
00044     set_time(TempsUnix); // Set time to actual time
00045 }
00046 
00047 
00048 
00049 void readHC06()
00050 {
00051 
00052     int a = HC06.getc();
00053     pc.printf("Le chifre est: %d \n\r",a);
00054 
00055     switch(a) {
00056         case '1':
00057             Relais1=0;
00058             break;
00059 
00060         case '2':
00061             Relais2=0;
00062             break;
00063 
00064         case '3':
00065             Relais3=0;
00066             break;
00067 
00068         case '4':
00069             Relais4=0;
00070             break;
00071         case 'A':
00072             Relais1=1;
00073             break;
00074 
00075         case 'B':
00076             Relais2=1;
00077             break;
00078 
00079         case 'C':
00080             Relais3=1;
00081             break;
00082 
00083         case 'D':
00084             Relais4=1;
00085             break;
00086         case '9': // A changer pour le rendre normalement ouvert
00087             Relais1=0;
00088             Relais2=0;
00089             Relais3=0;
00090             Relais4=0;
00091             break;
00092 
00093         case 'I':
00094             Relais1=1;
00095             Relais2=1;
00096             Relais3=1;
00097             Relais4=1;
00098             break;
00099         // operator doesn't match any case constant (+, -, *, /)
00100         default:
00101             printf("Error! operator is not correct");
00102     }
00103 }
00104 void ResetRelais()
00105 {
00106     Relais1=1;
00107     Relais2=1;
00108     Relais3=1;
00109     Relais4=1;
00110 }
00111 
00112 
00113 /******************************************************************
00114  * Arrosage
00115  *
00116  * This function rings while alarm by power pompe
00117  *****************************************************************/
00118 void alarm_ring()
00119 {
00120     Relais1=0;//pompe en marche
00121     ringflag = true;//indicateur arrossage active
00122     --TempsArrosage; //decremettion temps d'arrosage de 1 secaonde
00123     if(TempsArrosage == 0 ) {
00124         Relais1=1; //Arret de la pompe
00125         myTicker.detach();// blocage des interruptions de la larme
00126     }
00127 
00128 
00129 }
00130 
00131 /******************************************************************
00132  * Alarm_check
00133  *
00134  * This function compares the alarm time vs the current time. Once
00135  * alarm time and real time match it begins ringing the alarm.
00136  * Once the times differ then it turns off the alarm.
00137  *****************************************************************/
00138 void alarm_check()
00139 {
00140     if ((LPC_RTC->HOUR == AlarmHOUR) && (LPC_RTC->MIN == AlarmMIN)) {
00141         if ((alarmstate == true) && (ringflag == false)) {
00142             myTicker.attach_us(&alarm_ring,1000000.0f); // run every 40us, Could use .attach(&onTick,0.00004) if you prefer.
00143         }
00144     } else {
00145 
00146 
00147     }
00148 }
00149 
00150 int main()
00151 {
00152 
00153 
00154     uint16_t year;
00155     uint8_t hour, min, sec, date, month;
00156     int error = 0;
00157     float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
00158 
00159     pc.baud(115200);
00160     HC06.baud(9600);
00161     ResetRelais();
00162     pc.printf("Start Programme\n\r");
00163 
00164 
00165     /* Disable RTC clock, reset clock, Enable RTC calibration */
00166     LPC_RTC->CCR = ((1 << SBIT_CTCRST ) | (1 << SBIT_CCALEN));
00167     LPC_RTC->CALIBRATION = 0x00;
00168     LPC_RTC->CCR = (1 << SBIT_CLKEN);    /* Enable the clock for RTC */
00169 
00170     //  setclock();
00171 
00172     /* Set Date and Time only once, comment these lines after setting the time and date */
00173     // Set Date 23th Mai 2019
00174     LPC_RTC->DOM    = 23;   // Update date value
00175     LPC_RTC->MONTH  = 05;   // Update month value
00176     LPC_RTC->YEAR   = 2019; // Update year value
00177 
00178     // Set Time 10:40:25 AM
00179     LPC_RTC->HOUR   = 8;   // Update hour value
00180     LPC_RTC->MIN    = 00;   // Update min value
00181     LPC_RTC->SEC    = 00;   // Update sec value
00182 
00183 
00184     //set_time(1558477269); // Set time to Wed, 28 Oct 2009 11:35:37
00185 
00186     alarmstate = true;
00187     HC06.attach(readHC06);
00188 
00189 
00190 
00191 
00192 //      time_t seconds = time(NULL);
00193 //    printf ("Time as seconds since January 1, 1970 = %d\n", seconds);        //https://time.is/Unix_time_now
00194 //    printf ("Time as a basic string = %s", ctime(&seconds));
00195 //    char buffer [32];
00196 //    strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
00197 //    printf ("Time as a custom formatted string = %s", buffer);
00198 
00199 
00200 
00201     while(1) {
00202 
00203         wait(1.0f);
00204         /* Read Time */
00205         hour = LPC_RTC->HOUR;
00206         min  = LPC_RTC->MIN;
00207         sec  = LPC_RTC->SEC;
00208 
00209         /* Read Date */
00210         date  = LPC_RTC->DOM;
00211         month = LPC_RTC->MONTH;
00212         year  = LPC_RTC->YEAR;
00213 
00214 
00215         pc.printf("Date: %2d/%2d/%4u\n\r",date,month,year);
00216         pc.printf("Time: %2d:%2d:%2d\n\r",hour,min,sec);
00217 
00218         error = sensor.readData();
00219 
00220         if (0 == error) {
00221             c   = sensor.ReadTemperature(CELCIUS);
00222             f   = sensor.ReadTemperature(FARENHEIT);
00223             k   = sensor.ReadTemperature(KELVIN);
00224             h   = sensor.ReadHumidity();
00225             dp  = sensor.CalcdewPoint(c, h);
00226             dpf = sensor.CalcdewPointFast(c, h);
00227             printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
00228             printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n\r\n\r", h, dp, dpf);
00229 
00230 
00231         } else {
00232             printf("Error: %d\n", error);
00233         }
00234         /* Check to see if the alarm should be started/stopped */
00235         alarm_check();
00236 
00237 
00238     }
00239 
00240 }