![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
chemical dosing section of the rig
Fork of mbed_plant_rig by
main.cpp@0:790a9e0285d6, 2017-07-10 (annotated)
- Committer:
- Demonthorn
- Date:
- Mon Jul 10 15:23:02 2017 +0000
- Revision:
- 0:790a9e0285d6
- Child:
- 1:d022bdae586b
Version 1.1.05 full sequential logging and operation check for environmental control.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Demonthorn | 0:790a9e0285d6 | 1 | //////////////////////////////////////////////////////////////// |
Demonthorn | 0:790a9e0285d6 | 2 | // Plant Research Rig V1.1.05 // |
Demonthorn | 0:790a9e0285d6 | 3 | // Michael Steel // |
Demonthorn | 0:790a9e0285d6 | 4 | //////////////////////////////////////////////////////////////// |
Demonthorn | 0:790a9e0285d6 | 5 | |
Demonthorn | 0:790a9e0285d6 | 6 | // Libraries |
Demonthorn | 0:790a9e0285d6 | 7 | #include "mbed.h" |
Demonthorn | 0:790a9e0285d6 | 8 | #include "Dht11.h" |
Demonthorn | 0:790a9e0285d6 | 9 | #include "ConfigFile.h" |
Demonthorn | 0:790a9e0285d6 | 10 | // Define I/O and constants |
Demonthorn | 0:790a9e0285d6 | 11 | |
Demonthorn | 0:790a9e0285d6 | 12 | Serial pc(USBTX, USBRX); |
Demonthorn | 0:790a9e0285d6 | 13 | Dht11 sensor(p5); |
Demonthorn | 0:790a9e0285d6 | 14 | DigitalIn LOG_SWITCH(p20); |
Demonthorn | 0:790a9e0285d6 | 15 | DigitalOut TEMP_BLED(p7); |
Demonthorn | 0:790a9e0285d6 | 16 | DigitalOut TEMP_GLED(p8); |
Demonthorn | 0:790a9e0285d6 | 17 | DigitalOut TEMP_RLED(p9); |
Demonthorn | 0:790a9e0285d6 | 18 | DigitalOut FAN(p10); |
Demonthorn | 0:790a9e0285d6 | 19 | DigitalOut HEAT(p11); |
Demonthorn | 0:790a9e0285d6 | 20 | |
Demonthorn | 0:790a9e0285d6 | 21 | LocalFileSystem local("local"); |
Demonthorn | 0:790a9e0285d6 | 22 | ConfigFile cfg; |
Demonthorn | 0:790a9e0285d6 | 23 | float TEMP; |
Demonthorn | 0:790a9e0285d6 | 24 | float TEMPHH; |
Demonthorn | 0:790a9e0285d6 | 25 | float TEMPH; |
Demonthorn | 0:790a9e0285d6 | 26 | float TEMPL; |
Demonthorn | 0:790a9e0285d6 | 27 | float TEMPLL; |
Demonthorn | 0:790a9e0285d6 | 28 | int TCHECK; |
Demonthorn | 0:790a9e0285d6 | 29 | int YEAR; |
Demonthorn | 0:790a9e0285d6 | 30 | |
Demonthorn | 0:790a9e0285d6 | 31 | |
Demonthorn | 0:790a9e0285d6 | 32 | int main() |
Demonthorn | 0:790a9e0285d6 | 33 | { |
Demonthorn | 0:790a9e0285d6 | 34 | // Determins Input file variables |
Demonthorn | 0:790a9e0285d6 | 35 | char *key1 = "TEMPHH"; |
Demonthorn | 0:790a9e0285d6 | 36 | char *key2 = "TEMPH"; |
Demonthorn | 0:790a9e0285d6 | 37 | char *key3 = "TEMPL"; |
Demonthorn | 0:790a9e0285d6 | 38 | char *key4 = "TEMPLL"; |
Demonthorn | 0:790a9e0285d6 | 39 | char *key5 = "YEAR"; |
Demonthorn | 0:790a9e0285d6 | 40 | char value[BUFSIZ]; |
Demonthorn | 0:790a9e0285d6 | 41 | |
Demonthorn | 0:790a9e0285d6 | 42 | // Read the configuration file from the mbed |
Demonthorn | 0:790a9e0285d6 | 43 | cfg.read("/local/input.cfg"); |
Demonthorn | 0:790a9e0285d6 | 44 | if (cfg.getValue(key5, &value[0], sizeof(value))) { |
Demonthorn | 0:790a9e0285d6 | 45 | YEAR = atoi(value) ; |
Demonthorn | 0:790a9e0285d6 | 46 | } |
Demonthorn | 0:790a9e0285d6 | 47 | pc.printf("\r\n Plant Research Rig V1.0 \r\n Resart occured loading variables \r\n"); |
Demonthorn | 0:790a9e0285d6 | 48 | // Check correct time and get the current time from the terminal if needed |
Demonthorn | 0:790a9e0285d6 | 49 | struct tm t; |
Demonthorn | 0:790a9e0285d6 | 50 | time_t seconds = time(NULL); |
Demonthorn | 0:790a9e0285d6 | 51 | char buffer[21]; |
Demonthorn | 0:790a9e0285d6 | 52 | strftime(buffer, 21, "%Y\n", localtime(&seconds)); |
Demonthorn | 0:790a9e0285d6 | 53 | TCHECK = atoi(buffer) ; |
Demonthorn | 0:790a9e0285d6 | 54 | if (TCHECK != YEAR ) { |
Demonthorn | 0:790a9e0285d6 | 55 | printf("Enter current date and time:\n"); |
Demonthorn | 0:790a9e0285d6 | 56 | printf("YYYY MM DD HH MM SS[enter]\n"); |
Demonthorn | 0:790a9e0285d6 | 57 | scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday |
Demonthorn | 0:790a9e0285d6 | 58 | , &t.tm_hour, &t.tm_min, &t.tm_sec); |
Demonthorn | 0:790a9e0285d6 | 59 | |
Demonthorn | 0:790a9e0285d6 | 60 | // adjust for tm structure required values |
Demonthorn | 0:790a9e0285d6 | 61 | t.tm_year = t.tm_year - 1900; |
Demonthorn | 0:790a9e0285d6 | 62 | t.tm_mon = t.tm_mon - 1; |
Demonthorn | 0:790a9e0285d6 | 63 | |
Demonthorn | 0:790a9e0285d6 | 64 | // set the time |
Demonthorn | 0:790a9e0285d6 | 65 | set_time(mktime(&t)); |
Demonthorn | 0:790a9e0285d6 | 66 | |
Demonthorn | 0:790a9e0285d6 | 67 | // display the time |
Demonthorn | 0:790a9e0285d6 | 68 | time_t seconds = time(NULL); |
Demonthorn | 0:790a9e0285d6 | 69 | } |
Demonthorn | 0:790a9e0285d6 | 70 | //printf("\r\n Time as a basic string = %s \r\n", ctime(&seconds)); |
Demonthorn | 0:790a9e0285d6 | 71 | wait(1); |
Demonthorn | 0:790a9e0285d6 | 72 | if (cfg.getValue(key1, &value[0], sizeof(value))) { |
Demonthorn | 0:790a9e0285d6 | 73 | pc.printf("'%s'='%s'\r\n", key1, value); |
Demonthorn | 0:790a9e0285d6 | 74 | TEMPHH = atof(value) ; |
Demonthorn | 0:790a9e0285d6 | 75 | } else { |
Demonthorn | 0:790a9e0285d6 | 76 | pc.printf("Failure to get a configuration. Please check config file"); |
Demonthorn | 0:790a9e0285d6 | 77 | } |
Demonthorn | 0:790a9e0285d6 | 78 | if (cfg.getValue(key2, &value[0], sizeof(value))) { |
Demonthorn | 0:790a9e0285d6 | 79 | pc.printf("'%s'='%s'\r\n", key2, value); |
Demonthorn | 0:790a9e0285d6 | 80 | TEMPH = atof(value) ; |
Demonthorn | 0:790a9e0285d6 | 81 | } |
Demonthorn | 0:790a9e0285d6 | 82 | if (cfg.getValue(key3, &value[0], sizeof(value))) { |
Demonthorn | 0:790a9e0285d6 | 83 | pc.printf("'%s'='%s'\r\n", key3, value); |
Demonthorn | 0:790a9e0285d6 | 84 | TEMPL = atof(value) ; |
Demonthorn | 0:790a9e0285d6 | 85 | } |
Demonthorn | 0:790a9e0285d6 | 86 | if (cfg.getValue(key4, &value[0], sizeof(value))) { |
Demonthorn | 0:790a9e0285d6 | 87 | pc.printf("'%s'='%s'\r\n", key4, value); |
Demonthorn | 0:790a9e0285d6 | 88 | TEMPLL = atof(value) ; |
Demonthorn | 0:790a9e0285d6 | 89 | } |
Demonthorn | 0:790a9e0285d6 | 90 | pc.printf("Begin logging \r\n"); |
Demonthorn | 0:790a9e0285d6 | 91 | while(1) { |
Demonthorn | 0:790a9e0285d6 | 92 | while(LOG_SWITCH == 1) { |
Demonthorn | 0:790a9e0285d6 | 93 | |
Demonthorn | 0:790a9e0285d6 | 94 | sensor.read() ; |
Demonthorn | 0:790a9e0285d6 | 95 | time_t seconds = time(NULL); |
Demonthorn | 0:790a9e0285d6 | 96 | strftime(buffer, 21,"%X, %x\n", localtime(&seconds)); |
Demonthorn | 0:790a9e0285d6 | 97 | FILE *fp = fopen("/local/out.txt", "a"); // Open "out.txt" on the local file system for writing |
Demonthorn | 0:790a9e0285d6 | 98 | fprintf(fp, "\r\n T: %f, H: %f, %s,",sensor.getCelsius(),sensor.getHumidity(),buffer); |
Demonthorn | 0:790a9e0285d6 | 99 | fclose(fp); |
Demonthorn | 0:790a9e0285d6 | 100 | |
Demonthorn | 0:790a9e0285d6 | 101 | pc.printf("\r\n T: %f, H: %f, %s,",sensor.getCelsius(),sensor.getHumidity(),buffer); |
Demonthorn | 0:790a9e0285d6 | 102 | TEMP = sensor.getCelsius(); |
Demonthorn | 0:790a9e0285d6 | 103 | |
Demonthorn | 0:790a9e0285d6 | 104 | if (TEMP >= TEMPHH) { |
Demonthorn | 0:790a9e0285d6 | 105 | FAN = 1; |
Demonthorn | 0:790a9e0285d6 | 106 | HEAT = 0; |
Demonthorn | 0:790a9e0285d6 | 107 | TEMP_RLED = 1; |
Demonthorn | 0:790a9e0285d6 | 108 | TEMP_GLED = 0; |
Demonthorn | 0:790a9e0285d6 | 109 | TEMP_BLED = 0; |
Demonthorn | 0:790a9e0285d6 | 110 | pc.printf(" Temperature in High Alarm"); |
Demonthorn | 0:790a9e0285d6 | 111 | } else if (TEMP >= TEMPH) { |
Demonthorn | 0:790a9e0285d6 | 112 | FAN = 1; |
Demonthorn | 0:790a9e0285d6 | 113 | HEAT = 0; |
Demonthorn | 0:790a9e0285d6 | 114 | TEMP_RLED = 0; |
Demonthorn | 0:790a9e0285d6 | 115 | TEMP_GLED = 1; |
Demonthorn | 0:790a9e0285d6 | 116 | TEMP_BLED = 0; |
Demonthorn | 0:790a9e0285d6 | 117 | } else if (TEMP >= TEMPL) { |
Demonthorn | 0:790a9e0285d6 | 118 | FAN = 0; |
Demonthorn | 0:790a9e0285d6 | 119 | HEAT = 0; |
Demonthorn | 0:790a9e0285d6 | 120 | TEMP_RLED = 0; |
Demonthorn | 0:790a9e0285d6 | 121 | TEMP_GLED = 1; |
Demonthorn | 0:790a9e0285d6 | 122 | TEMP_BLED = 0; |
Demonthorn | 0:790a9e0285d6 | 123 | } else if (TEMP >= TEMPLL) { |
Demonthorn | 0:790a9e0285d6 | 124 | FAN = 0; |
Demonthorn | 0:790a9e0285d6 | 125 | HEAT = 1; |
Demonthorn | 0:790a9e0285d6 | 126 | TEMP_RLED = 0; |
Demonthorn | 0:790a9e0285d6 | 127 | TEMP_GLED = 1; |
Demonthorn | 0:790a9e0285d6 | 128 | TEMP_BLED = 0; |
Demonthorn | 0:790a9e0285d6 | 129 | } else { |
Demonthorn | 0:790a9e0285d6 | 130 | FAN = 0; |
Demonthorn | 0:790a9e0285d6 | 131 | HEAT = 1; |
Demonthorn | 0:790a9e0285d6 | 132 | TEMP_RLED = 0; |
Demonthorn | 0:790a9e0285d6 | 133 | TEMP_GLED = 0; |
Demonthorn | 0:790a9e0285d6 | 134 | TEMP_BLED = 1; |
Demonthorn | 0:790a9e0285d6 | 135 | pc.printf(" Temperature in Low Alarm"); |
Demonthorn | 0:790a9e0285d6 | 136 | } |
Demonthorn | 0:790a9e0285d6 | 137 | wait(1); |
Demonthorn | 0:790a9e0285d6 | 138 | |
Demonthorn | 0:790a9e0285d6 | 139 | } |
Demonthorn | 0:790a9e0285d6 | 140 | } |
Demonthorn | 0:790a9e0285d6 | 141 | } |