Datalogger that reads temperature, humidity and light level every couple of seconds, and store the data in a microSD card (.CSV file).
Dependencies: DS1302 SDFileSystem mbed
Fork of temp_humid_time_DS1302_LM35_DHT11 by
This is a weather station/datalogger using a Freescale FRDM-K64F dev board, a LM35 (temperature sensor), a DHT11 (temperature and humidity sensor), a DS1302 (timekeeper module) and a LDR (light level sensor). All the data is periodically stored on a microSDHC card; The acquisition period is adjustable via software (by setting the value of a counter).
There is also (commented) code for sending all these information via serial to a terminal on PC.
Full explanation, details and schematics can be found here: http://embedded-clovis.blogspot.ca/2014/07/temperature-datalogger-with-arm-cortex.html
main.cpp@1:4b3e952a1406, 2014-07-24 (annotated)
- Committer:
- Clovis
- Date:
- Thu Jul 24 18:44:48 2014 +0000
- Revision:
- 1:4b3e952a1406
- Parent:
- 0:8f0934e41a57
Initial release for the FRDM-K64F group
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 | |
Clovis | 0:8f0934e41a57 | 23 | int main() { |
Clovis | 0:8f0934e41a57 | 24 | // the 6 lines below are for the time keeper chip |
Clovis | 0:8f0934e41a57 | 25 | #ifdef INITIAL_RUN |
Clovis | 1:4b3e952a1406 | 26 | clk.set_time(1406017928); |
Clovis | 0:8f0934e41a57 | 27 | #endif |
Clovis | 0:8f0934e41a57 | 28 | |
Clovis | 0:8f0934e41a57 | 29 | char storedByte = clk.recallByte(0); |
Clovis | 0:8f0934e41a57 | 30 | //printf("\r\nStored byte was %d, now increasing by one\r\n", storedByte); |
Clovis | 0:8f0934e41a57 | 31 | clk.storeByte(0, storedByte + 1); |
Clovis | 0:8f0934e41a57 | 32 | // |
Clovis | 0:8f0934e41a57 | 33 | |
Clovis | 0:8f0934e41a57 | 34 | int err; |
Clovis | 0:8f0934e41a57 | 35 | wait(1); // wait 1 second for DHT11 to stabilyze |
Clovis | 0:8f0934e41a57 | 36 | |
Clovis | 0:8f0934e41a57 | 37 | //printf("Hello World!\n"); |
Clovis | 0:8f0934e41a57 | 38 | |
Clovis | 0:8f0934e41a57 | 39 | mkdir("/sd/dados", 0777); |
Clovis | 0:8f0934e41a57 | 40 | |
Clovis | 1:4b3e952a1406 | 41 | FILE *fp = fopen("/sd/dados/data004.csv", "a"); |
Clovis | 0:8f0934e41a57 | 42 | if(fp == NULL) { |
Clovis | 0:8f0934e41a57 | 43 | error("Could not open file for write\n"); |
Clovis | 0:8f0934e41a57 | 44 | RedLed= 0; |
Clovis | 0:8f0934e41a57 | 45 | |
Clovis | 0:8f0934e41a57 | 46 | } |
Clovis | 1:4b3e952a1406 | 47 | fprintf(fp, "%s\r,", "--------------"); |
Clovis | 1:4b3e952a1406 | 48 | fclose(fp); |
Clovis | 1:4b3e952a1406 | 49 | |
Clovis | 0:8f0934e41a57 | 50 | while (1) { |
Clovis | 0:8f0934e41a57 | 51 | |
Clovis | 0:8f0934e41a57 | 52 | Blueled= 1; |
Clovis | 0:8f0934e41a57 | 53 | RedLed= 1; |
Clovis | 0:8f0934e41a57 | 54 | GreenLed= 1; |
Clovis | 0:8f0934e41a57 | 55 | |
Clovis | 1:4b3e952a1406 | 56 | if (count < 5000000){ //around 10 seconds before it reaches the count of 12000000 |
Clovis | 0:8f0934e41a57 | 57 | count++; |
Clovis | 0:8f0934e41a57 | 58 | } else{ |
Clovis | 0:8f0934e41a57 | 59 | // -------------------------- |
Clovis | 0:8f0934e41a57 | 60 | |
Clovis | 0:8f0934e41a57 | 61 | err = sensor.readData(); |
Clovis | 0:8f0934e41a57 | 62 | if (err == 0) { |
Clovis | 0:8f0934e41a57 | 63 | GreenLed= 0; |
Clovis | 0:8f0934e41a57 | 64 | printf("Temperature is %4.2f \r\n",sensor.ReadTemperature(CELCIUS)); |
Clovis | 0:8f0934e41a57 | 65 | //printf("Temperature is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT)); |
Clovis | 0:8f0934e41a57 | 66 | //printf("Temperature is %4.2f K \r\n",sensor.ReadTemperature(KELVIN)); |
Clovis | 0:8f0934e41a57 | 67 | printf("Humidity is %4.2f \r\n",sensor.ReadHumidity()); |
Clovis | 0:8f0934e41a57 | 68 | //printf("Dew point is %4.2f \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); |
Clovis | 0:8f0934e41a57 | 69 | //printf("Dew point (fast) is %4.2f \r\n",sensor.CalcdewPointFast(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); |
Clovis | 0:8f0934e41a57 | 70 | |
Clovis | 0:8f0934e41a57 | 71 | } else |
Clovis | 0:8f0934e41a57 | 72 | printf("\r\nErr %i \n",err); |
Clovis | 0:8f0934e41a57 | 73 | |
Clovis | 0:8f0934e41a57 | 74 | // ---------------------------- |
Clovis | 1:4b3e952a1406 | 75 | FILE *fp = fopen("/sd/dados/data004.csv", "a"); |
Clovis | 0:8f0934e41a57 | 76 | if(fp == NULL) { |
Clovis | 0:8f0934e41a57 | 77 | error("Could not open file for write\n"); |
Clovis | 0:8f0934e41a57 | 78 | |
Clovis | 0:8f0934e41a57 | 79 | } |
Clovis | 0:8f0934e41a57 | 80 | |
Clovis | 0:8f0934e41a57 | 81 | ldrcalc= LDR.read(); |
Clovis | 1:4b3e952a1406 | 82 | ldrcalc= (1/ldrcalc)-1; //Transforms the LDR value into a 0-5 signal (integer) |
Clovis | 0:8f0934e41a57 | 83 | time_t seconds = clk.time(NULL); |
Clovis | 0:8f0934e41a57 | 84 | //fprintf(fp, "%s\r,%f,%f,%f\n", "LM35", "DHT11", "Humid", "Month", "Day", "Hour", "Year"); |
Clovis | 1:4b3e952a1406 | 85 | fprintf(fp, "%s\r,%f,%f,%f,%f,%f", ctime(&seconds), 333.333*LM35.read(), SolarPanel.read(), sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity(), ldrcalc); |
Clovis | 0:8f0934e41a57 | 86 | fclose(fp); |
Clovis | 0:8f0934e41a57 | 87 | //printf("Goodbye World!\n"); |
Clovis | 0:8f0934e41a57 | 88 | count=0; |
Clovis | 0:8f0934e41a57 | 89 | Blueled= 0; |
Clovis | 0:8f0934e41a57 | 90 | } |
Clovis | 0:8f0934e41a57 | 91 | } |
Clovis | 0:8f0934e41a57 | 92 | } |