This a domestic weather station that measures temperature, humidity and light and stores the data in a .csv file in SD card.
Dependencies: DS1302 SDFileSystem mbed
Fork of temp_humid_light_time_DS1302_LM35_DHT11_LDR by
main.cpp@2:02668481aebc, 2016-03-27 (annotated)
- Committer:
- nivmukka
- Date:
- Sun Mar 27 04:18:07 2016 +0000
- Revision:
- 2:02668481aebc
- Parent:
- 1:4b3e952a1406
Domestic weather station that measures temperature, humidity and light.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Clovis | 0:8f0934e41a57 | 1 | #include "mbed.h" |
Clovis | 0:8f0934e41a57 | 2 | #include "SDFileSystem.h" |
Clovis | 0:8f0934e41a57 | 3 | #include "DHT.h" |
Clovis | 0:8f0934e41a57 | 4 | #include "DS1302.h" |
Clovis | 0:8f0934e41a57 | 5 | |
Clovis | 0:8f0934e41a57 | 6 | // Defines for the DS1302 timer module |
Clovis | 0:8f0934e41a57 | 7 | #define SCLK PTC1 |
Clovis | 0:8f0934e41a57 | 8 | #define IO PTB19 |
Clovis | 0:8f0934e41a57 | 9 | #define CE PTB18 |
Clovis | 0:8f0934e41a57 | 10 | |
Clovis | 0:8f0934e41a57 | 11 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //MOSI, MISO, SCLK, SSEL. (SD Card) Tested on K64F, correct pins. |
Clovis | 0:8f0934e41a57 | 12 | AnalogIn LM35(PTB2); |
Clovis | 0:8f0934e41a57 | 13 | AnalogIn LDR(PTB3); |
Clovis | 1:4b3e952a1406 | 14 | AnalogIn SolarPanel(PTB10); |
Clovis | 0:8f0934e41a57 | 15 | int count=0; |
Clovis | 0:8f0934e41a57 | 16 | float ldrcalc= 0.00; |
Clovis | 0:8f0934e41a57 | 17 | DigitalOut RedLed(LED1); // Error indication |
Clovis | 0:8f0934e41a57 | 18 | DigitalOut GreenLed(LED2); // LM35 read indication |
Clovis | 0:8f0934e41a57 | 19 | DigitalOut Blueled(LED3); // DHT11 read indication |
Clovis | 0:8f0934e41a57 | 20 | DHT sensor(PTB20,DHT11); // Use the DHT11 sensor, on pin |
Clovis | 0:8f0934e41a57 | 21 | DS1302 clk(SCLK, IO, PTB18); // ports for the DS1302 time keeper |
Clovis | 0:8f0934e41a57 | 22 | |
nivmukka | 2:02668481aebc | 23 | int main() |
nivmukka | 2:02668481aebc | 24 | { |
Clovis | 0:8f0934e41a57 | 25 | // the 6 lines below are for the time keeper chip |
Clovis | 0:8f0934e41a57 | 26 | #ifdef INITIAL_RUN |
Clovis | 1:4b3e952a1406 | 27 | clk.set_time(1406017928); |
Clovis | 0:8f0934e41a57 | 28 | #endif |
Clovis | 0:8f0934e41a57 | 29 | |
nivmukka | 2:02668481aebc | 30 | char storedByte = clk.recallByte(0); |
Clovis | 0:8f0934e41a57 | 31 | clk.storeByte(0, storedByte + 1); |
nivmukka | 2:02668481aebc | 32 | |
Clovis | 0:8f0934e41a57 | 33 | int err; |
Clovis | 0:8f0934e41a57 | 34 | wait(1); // wait 1 second for DHT11 to stabilyze |
Clovis | 0:8f0934e41a57 | 35 | |
Clovis | 0:8f0934e41a57 | 36 | |
Clovis | 0:8f0934e41a57 | 37 | mkdir("/sd/dados", 0777); |
Clovis | 0:8f0934e41a57 | 38 | |
Clovis | 1:4b3e952a1406 | 39 | FILE *fp = fopen("/sd/dados/data004.csv", "a"); |
nivmukka | 2:02668481aebc | 40 | if(fp == NULL) |
nivmukka | 2:02668481aebc | 41 | { |
Clovis | 0:8f0934e41a57 | 42 | error("Could not open file for write\n"); |
nivmukka | 2:02668481aebc | 43 | RedLed= 0; |
nivmukka | 2:02668481aebc | 44 | } |
nivmukka | 2:02668481aebc | 45 | |
nivmukka | 2:02668481aebc | 46 | fprintf(fp, "%s,", "--------------\n"); |
Clovis | 1:4b3e952a1406 | 47 | fclose(fp); |
Clovis | 1:4b3e952a1406 | 48 | |
nivmukka | 2:02668481aebc | 49 | while (1) |
nivmukka | 2:02668481aebc | 50 | { |
Clovis | 0:8f0934e41a57 | 51 | |
nivmukka | 2:02668481aebc | 52 | Blueled= 1; |
nivmukka | 2:02668481aebc | 53 | RedLed= 1; |
nivmukka | 2:02668481aebc | 54 | GreenLed= 1; |
Clovis | 0:8f0934e41a57 | 55 | |
nivmukka | 2:02668481aebc | 56 | if (count < 5000000) //around 10 seconds before it reaches the count of 12000000 |
nivmukka | 2:02668481aebc | 57 | { |
nivmukka | 2:02668481aebc | 58 | count++; |
nivmukka | 2:02668481aebc | 59 | } |
nivmukka | 2:02668481aebc | 60 | else |
nivmukka | 2:02668481aebc | 61 | { |
nivmukka | 2:02668481aebc | 62 | err = sensor.readData(); |
nivmukka | 2:02668481aebc | 63 | if (err == 0) |
nivmukka | 2:02668481aebc | 64 | { |
nivmukka | 2:02668481aebc | 65 | GreenLed= 0; |
nivmukka | 2:02668481aebc | 66 | printf("\r\nTemperature in celsius is %4.2f C \r\n",sensor.ReadTemperature(CELCIUS)); |
nivmukka | 2:02668481aebc | 67 | printf("Temperature in farenheit is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT)); |
nivmukka | 2:02668481aebc | 68 | printf("Temperature in kelvin is %4.2f K \r\n",sensor.ReadTemperature(KELVIN)); |
nivmukka | 2:02668481aebc | 69 | printf("Humidity is %4.2f \r\n",sensor.ReadHumidity()); |
nivmukka | 2:02668481aebc | 70 | printf("Dew point is %4.2f \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); |
nivmukka | 2:02668481aebc | 71 | printf("Dew point (fast) is %4.2f \r\n",sensor.CalcdewPointFast(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); |
nivmukka | 2:02668481aebc | 72 | } |
nivmukka | 2:02668481aebc | 73 | else |
nivmukka | 2:02668481aebc | 74 | { |
nivmukka | 2:02668481aebc | 75 | printf("\nErr %i \r\n",err); |
nivmukka | 2:02668481aebc | 76 | } |
nivmukka | 2:02668481aebc | 77 | |
nivmukka | 2:02668481aebc | 78 | FILE *fp = fopen("/sd/dados/data004.csv", "a"); |
Clovis | 0:8f0934e41a57 | 79 | |
nivmukka | 2:02668481aebc | 80 | if(fp == NULL) |
nivmukka | 2:02668481aebc | 81 | { |
nivmukka | 2:02668481aebc | 82 | error("Could not open file for write\n"); |
nivmukka | 2:02668481aebc | 83 | } |
Clovis | 0:8f0934e41a57 | 84 | |
nivmukka | 2:02668481aebc | 85 | ldrcalc= LDR.read(); |
nivmukka | 2:02668481aebc | 86 | ldrcalc= (1/ldrcalc)-1; //Transforms the LDR value into a 0-5 signal (integer) |
nivmukka | 2:02668481aebc | 87 | time_t seconds = clk.time(NULL); |
nivmukka | 2:02668481aebc | 88 | fprintf(fp, "%s\r,%f,%f,%f\n", "LM35", "DHT11", "Humid", "Month", "Day", "Hour", "Year"); |
nivmukka | 2:02668481aebc | 89 | fprintf(fp, "%s\r,%f,%f,%f,%f,%f", ctime(&seconds), 333.333*LM35.read(), SolarPanel.read(), sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity(), ldrcalc); |
nivmukka | 2:02668481aebc | 90 | fclose(fp); |
nivmukka | 2:02668481aebc | 91 | printf("\n--------------------------------------------------\n"); |
nivmukka | 2:02668481aebc | 92 | |
nivmukka | 2:02668481aebc | 93 | count=0; |
nivmukka | 2:02668481aebc | 94 | Blueled= 0; |
nivmukka | 2:02668481aebc | 95 | } |
Clovis | 0:8f0934e41a57 | 96 | } |
nivmukka | 2:02668481aebc | 97 | } |