1 step for smart green house

Dependencies:   mbed

Committer:
belloula
Date:
Wed May 22 22:28:10 2019 +0000
Revision:
0:fcce3b1e1dec
for smart green house

Who changed what in which revision?

UserRevisionLine numberNew contents of line
belloula 0:fcce3b1e1dec 1 #include "mbed.h" // MBED LIBRARY
belloula 0:fcce3b1e1dec 2 #include "string.h"
belloula 0:fcce3b1e1dec 3 #include "DHT.h"
belloula 0:fcce3b1e1dec 4
belloula 0:fcce3b1e1dec 5 // SETUP (ASSIGN) SERIAL COMMUNICATION PINS ON MBED
belloula 0:fcce3b1e1dec 6 Serial pc(USBTX, USBRX); // PC SERIAL OVER USB PORT ON MBED
belloula 0:fcce3b1e1dec 7 Serial HC06(p9, p10);
belloula 0:fcce3b1e1dec 8 DHT sensor(p23, DHT11);
belloula 0:fcce3b1e1dec 9
belloula 0:fcce3b1e1dec 10 DigitalOut Relais1(p17);
belloula 0:fcce3b1e1dec 11 DigitalOut Relais2(p18);
belloula 0:fcce3b1e1dec 12 DigitalOut Relais3(p16);
belloula 0:fcce3b1e1dec 13 DigitalOut Relais4(p20);
belloula 0:fcce3b1e1dec 14
belloula 0:fcce3b1e1dec 15
belloula 0:fcce3b1e1dec 16 #define SBIT_CLKEN 0 /* RTC Clock Enable*/
belloula 0:fcce3b1e1dec 17 #define SBIT_CTCRST 1 /* RTC Clock Reset */
belloula 0:fcce3b1e1dec 18 #define SBIT_CCALEN 4 /* RTC Calibration counter enable */
belloula 0:fcce3b1e1dec 19
belloula 0:fcce3b1e1dec 20
belloula 0:fcce3b1e1dec 21 int32_t TempsUnix = 0;
belloula 0:fcce3b1e1dec 22 int32_t TempsArrosage = 5;// en seconde
belloula 0:fcce3b1e1dec 23
belloula 0:fcce3b1e1dec 24 uint8_t AlarmHOUR = 8; // Alarme hour value
belloula 0:fcce3b1e1dec 25 uint8_t AlarmMIN = 1; // Alarme min value
belloula 0:fcce3b1e1dec 26
belloula 0:fcce3b1e1dec 27 bool alarmstate = false;// Determines whether the alarm is set or not
belloula 0:fcce3b1e1dec 28 bool ringflag = false; // Determines if alarm is ringing
belloula 0:fcce3b1e1dec 29
belloula 0:fcce3b1e1dec 30
belloula 0:fcce3b1e1dec 31 Ticker myTicker;
belloula 0:fcce3b1e1dec 32
belloula 0:fcce3b1e1dec 33
belloula 0:fcce3b1e1dec 34
belloula 0:fcce3b1e1dec 35
belloula 0:fcce3b1e1dec 36
belloula 0:fcce3b1e1dec 37
belloula 0:fcce3b1e1dec 38 void setclock()
belloula 0:fcce3b1e1dec 39 {
belloula 0:fcce3b1e1dec 40
belloula 0:fcce3b1e1dec 41 pc.printf("Entrer Temps Unix actuel from https://www.timestampconvert.com/:\r\n");
belloula 0:fcce3b1e1dec 42 pc.scanf("%u", &TempsUnix);//https://www.timestampconvert.com/
belloula 0:fcce3b1e1dec 43 pc.printf("Temps Unix actuel %d:\r\n",TempsUnix);
belloula 0:fcce3b1e1dec 44 set_time(TempsUnix); // Set time to actual time
belloula 0:fcce3b1e1dec 45 }
belloula 0:fcce3b1e1dec 46
belloula 0:fcce3b1e1dec 47
belloula 0:fcce3b1e1dec 48
belloula 0:fcce3b1e1dec 49 void readHC06()
belloula 0:fcce3b1e1dec 50 {
belloula 0:fcce3b1e1dec 51
belloula 0:fcce3b1e1dec 52 int a = HC06.getc();
belloula 0:fcce3b1e1dec 53 pc.printf("Le chifre est: %d \n\r",a);
belloula 0:fcce3b1e1dec 54
belloula 0:fcce3b1e1dec 55 switch(a) {
belloula 0:fcce3b1e1dec 56 case '1':
belloula 0:fcce3b1e1dec 57 Relais1=0;
belloula 0:fcce3b1e1dec 58 break;
belloula 0:fcce3b1e1dec 59
belloula 0:fcce3b1e1dec 60 case '2':
belloula 0:fcce3b1e1dec 61 Relais2=0;
belloula 0:fcce3b1e1dec 62 break;
belloula 0:fcce3b1e1dec 63
belloula 0:fcce3b1e1dec 64 case '3':
belloula 0:fcce3b1e1dec 65 Relais3=0;
belloula 0:fcce3b1e1dec 66 break;
belloula 0:fcce3b1e1dec 67
belloula 0:fcce3b1e1dec 68 case '4':
belloula 0:fcce3b1e1dec 69 Relais4=0;
belloula 0:fcce3b1e1dec 70 break;
belloula 0:fcce3b1e1dec 71 case 'A':
belloula 0:fcce3b1e1dec 72 Relais1=1;
belloula 0:fcce3b1e1dec 73 break;
belloula 0:fcce3b1e1dec 74
belloula 0:fcce3b1e1dec 75 case 'B':
belloula 0:fcce3b1e1dec 76 Relais2=1;
belloula 0:fcce3b1e1dec 77 break;
belloula 0:fcce3b1e1dec 78
belloula 0:fcce3b1e1dec 79 case 'C':
belloula 0:fcce3b1e1dec 80 Relais3=1;
belloula 0:fcce3b1e1dec 81 break;
belloula 0:fcce3b1e1dec 82
belloula 0:fcce3b1e1dec 83 case 'D':
belloula 0:fcce3b1e1dec 84 Relais4=1;
belloula 0:fcce3b1e1dec 85 break;
belloula 0:fcce3b1e1dec 86 case '9': // A changer pour le rendre normalement ouvert
belloula 0:fcce3b1e1dec 87 Relais1=0;
belloula 0:fcce3b1e1dec 88 Relais2=0;
belloula 0:fcce3b1e1dec 89 Relais3=0;
belloula 0:fcce3b1e1dec 90 Relais4=0;
belloula 0:fcce3b1e1dec 91 break;
belloula 0:fcce3b1e1dec 92
belloula 0:fcce3b1e1dec 93 case 'I':
belloula 0:fcce3b1e1dec 94 Relais1=1;
belloula 0:fcce3b1e1dec 95 Relais2=1;
belloula 0:fcce3b1e1dec 96 Relais3=1;
belloula 0:fcce3b1e1dec 97 Relais4=1;
belloula 0:fcce3b1e1dec 98 break;
belloula 0:fcce3b1e1dec 99 // operator doesn't match any case constant (+, -, *, /)
belloula 0:fcce3b1e1dec 100 default:
belloula 0:fcce3b1e1dec 101 printf("Error! operator is not correct");
belloula 0:fcce3b1e1dec 102 }
belloula 0:fcce3b1e1dec 103 }
belloula 0:fcce3b1e1dec 104 void ResetRelais()
belloula 0:fcce3b1e1dec 105 {
belloula 0:fcce3b1e1dec 106 Relais1=1;
belloula 0:fcce3b1e1dec 107 Relais2=1;
belloula 0:fcce3b1e1dec 108 Relais3=1;
belloula 0:fcce3b1e1dec 109 Relais4=1;
belloula 0:fcce3b1e1dec 110 }
belloula 0:fcce3b1e1dec 111
belloula 0:fcce3b1e1dec 112
belloula 0:fcce3b1e1dec 113 /******************************************************************
belloula 0:fcce3b1e1dec 114 * Arrosage
belloula 0:fcce3b1e1dec 115 *
belloula 0:fcce3b1e1dec 116 * This function rings while alarm by power pompe
belloula 0:fcce3b1e1dec 117 *****************************************************************/
belloula 0:fcce3b1e1dec 118 void alarm_ring()
belloula 0:fcce3b1e1dec 119 {
belloula 0:fcce3b1e1dec 120 Relais1=0;//pompe en marche
belloula 0:fcce3b1e1dec 121 ringflag = true;//indicateur arrossage active
belloula 0:fcce3b1e1dec 122 --TempsArrosage; //decremettion temps d'arrosage de 1 secaonde
belloula 0:fcce3b1e1dec 123 if(TempsArrosage == 0 ) {
belloula 0:fcce3b1e1dec 124 Relais1=1; //Arret de la pompe
belloula 0:fcce3b1e1dec 125 myTicker.detach();// blocage des interruptions de la larme
belloula 0:fcce3b1e1dec 126 }
belloula 0:fcce3b1e1dec 127
belloula 0:fcce3b1e1dec 128
belloula 0:fcce3b1e1dec 129 }
belloula 0:fcce3b1e1dec 130
belloula 0:fcce3b1e1dec 131 /******************************************************************
belloula 0:fcce3b1e1dec 132 * Alarm_check
belloula 0:fcce3b1e1dec 133 *
belloula 0:fcce3b1e1dec 134 * This function compares the alarm time vs the current time. Once
belloula 0:fcce3b1e1dec 135 * alarm time and real time match it begins ringing the alarm.
belloula 0:fcce3b1e1dec 136 * Once the times differ then it turns off the alarm.
belloula 0:fcce3b1e1dec 137 *****************************************************************/
belloula 0:fcce3b1e1dec 138 void alarm_check()
belloula 0:fcce3b1e1dec 139 {
belloula 0:fcce3b1e1dec 140 if ((LPC_RTC->HOUR == AlarmHOUR) && (LPC_RTC->MIN == AlarmMIN)) {
belloula 0:fcce3b1e1dec 141 if ((alarmstate == true) && (ringflag == false)) {
belloula 0:fcce3b1e1dec 142 myTicker.attach_us(&alarm_ring,1000000.0f); // run every 40us, Could use .attach(&onTick,0.00004) if you prefer.
belloula 0:fcce3b1e1dec 143 }
belloula 0:fcce3b1e1dec 144 } else {
belloula 0:fcce3b1e1dec 145
belloula 0:fcce3b1e1dec 146
belloula 0:fcce3b1e1dec 147 }
belloula 0:fcce3b1e1dec 148 }
belloula 0:fcce3b1e1dec 149
belloula 0:fcce3b1e1dec 150 int main()
belloula 0:fcce3b1e1dec 151 {
belloula 0:fcce3b1e1dec 152
belloula 0:fcce3b1e1dec 153
belloula 0:fcce3b1e1dec 154 uint16_t year;
belloula 0:fcce3b1e1dec 155 uint8_t hour, min, sec, date, month;
belloula 0:fcce3b1e1dec 156 int error = 0;
belloula 0:fcce3b1e1dec 157 float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
belloula 0:fcce3b1e1dec 158
belloula 0:fcce3b1e1dec 159 pc.baud(115200);
belloula 0:fcce3b1e1dec 160 HC06.baud(9600);
belloula 0:fcce3b1e1dec 161 ResetRelais();
belloula 0:fcce3b1e1dec 162 pc.printf("Start Programme\n\r");
belloula 0:fcce3b1e1dec 163
belloula 0:fcce3b1e1dec 164
belloula 0:fcce3b1e1dec 165 /* Disable RTC clock, reset clock, Enable RTC calibration */
belloula 0:fcce3b1e1dec 166 LPC_RTC->CCR = ((1 << SBIT_CTCRST ) | (1 << SBIT_CCALEN));
belloula 0:fcce3b1e1dec 167 LPC_RTC->CALIBRATION = 0x00;
belloula 0:fcce3b1e1dec 168 LPC_RTC->CCR = (1 << SBIT_CLKEN); /* Enable the clock for RTC */
belloula 0:fcce3b1e1dec 169
belloula 0:fcce3b1e1dec 170 // setclock();
belloula 0:fcce3b1e1dec 171
belloula 0:fcce3b1e1dec 172 /* Set Date and Time only once, comment these lines after setting the time and date */
belloula 0:fcce3b1e1dec 173 // Set Date 23th Mai 2019
belloula 0:fcce3b1e1dec 174 LPC_RTC->DOM = 23; // Update date value
belloula 0:fcce3b1e1dec 175 LPC_RTC->MONTH = 05; // Update month value
belloula 0:fcce3b1e1dec 176 LPC_RTC->YEAR = 2019; // Update year value
belloula 0:fcce3b1e1dec 177
belloula 0:fcce3b1e1dec 178 // Set Time 10:40:25 AM
belloula 0:fcce3b1e1dec 179 LPC_RTC->HOUR = 8; // Update hour value
belloula 0:fcce3b1e1dec 180 LPC_RTC->MIN = 00; // Update min value
belloula 0:fcce3b1e1dec 181 LPC_RTC->SEC = 00; // Update sec value
belloula 0:fcce3b1e1dec 182
belloula 0:fcce3b1e1dec 183
belloula 0:fcce3b1e1dec 184 //set_time(1558477269); // Set time to Wed, 28 Oct 2009 11:35:37
belloula 0:fcce3b1e1dec 185
belloula 0:fcce3b1e1dec 186 alarmstate = true;
belloula 0:fcce3b1e1dec 187 HC06.attach(readHC06);
belloula 0:fcce3b1e1dec 188
belloula 0:fcce3b1e1dec 189
belloula 0:fcce3b1e1dec 190
belloula 0:fcce3b1e1dec 191
belloula 0:fcce3b1e1dec 192 // time_t seconds = time(NULL);
belloula 0:fcce3b1e1dec 193 // printf ("Time as seconds since January 1, 1970 = %d\n", seconds); //https://time.is/Unix_time_now
belloula 0:fcce3b1e1dec 194 // printf ("Time as a basic string = %s", ctime(&seconds));
belloula 0:fcce3b1e1dec 195 // char buffer [32];
belloula 0:fcce3b1e1dec 196 // strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
belloula 0:fcce3b1e1dec 197 // printf ("Time as a custom formatted string = %s", buffer);
belloula 0:fcce3b1e1dec 198
belloula 0:fcce3b1e1dec 199
belloula 0:fcce3b1e1dec 200
belloula 0:fcce3b1e1dec 201 while(1) {
belloula 0:fcce3b1e1dec 202
belloula 0:fcce3b1e1dec 203 wait(1.0f);
belloula 0:fcce3b1e1dec 204 /* Read Time */
belloula 0:fcce3b1e1dec 205 hour = LPC_RTC->HOUR;
belloula 0:fcce3b1e1dec 206 min = LPC_RTC->MIN;
belloula 0:fcce3b1e1dec 207 sec = LPC_RTC->SEC;
belloula 0:fcce3b1e1dec 208
belloula 0:fcce3b1e1dec 209 /* Read Date */
belloula 0:fcce3b1e1dec 210 date = LPC_RTC->DOM;
belloula 0:fcce3b1e1dec 211 month = LPC_RTC->MONTH;
belloula 0:fcce3b1e1dec 212 year = LPC_RTC->YEAR;
belloula 0:fcce3b1e1dec 213
belloula 0:fcce3b1e1dec 214
belloula 0:fcce3b1e1dec 215 pc.printf("Date: %2d/%2d/%4u\n\r",date,month,year);
belloula 0:fcce3b1e1dec 216 pc.printf("Time: %2d:%2d:%2d\n\r",hour,min,sec);
belloula 0:fcce3b1e1dec 217
belloula 0:fcce3b1e1dec 218 error = sensor.readData();
belloula 0:fcce3b1e1dec 219
belloula 0:fcce3b1e1dec 220 if (0 == error) {
belloula 0:fcce3b1e1dec 221 c = sensor.ReadTemperature(CELCIUS);
belloula 0:fcce3b1e1dec 222 f = sensor.ReadTemperature(FARENHEIT);
belloula 0:fcce3b1e1dec 223 k = sensor.ReadTemperature(KELVIN);
belloula 0:fcce3b1e1dec 224 h = sensor.ReadHumidity();
belloula 0:fcce3b1e1dec 225 dp = sensor.CalcdewPoint(c, h);
belloula 0:fcce3b1e1dec 226 dpf = sensor.CalcdewPointFast(c, h);
belloula 0:fcce3b1e1dec 227 printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
belloula 0:fcce3b1e1dec 228 printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n\r\n\r", h, dp, dpf);
belloula 0:fcce3b1e1dec 229
belloula 0:fcce3b1e1dec 230
belloula 0:fcce3b1e1dec 231 } else {
belloula 0:fcce3b1e1dec 232 printf("Error: %d\n", error);
belloula 0:fcce3b1e1dec 233 }
belloula 0:fcce3b1e1dec 234 /* Check to see if the alarm should be started/stopped */
belloula 0:fcce3b1e1dec 235 alarm_check();
belloula 0:fcce3b1e1dec 236
belloula 0:fcce3b1e1dec 237
belloula 0:fcce3b1e1dec 238 }
belloula 0:fcce3b1e1dec 239
belloula 0:fcce3b1e1dec 240 }