![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Clock with sensors for temperature, humidity and air pressure. TFT 2.2inch 320x240 SPI with SDCard socket. Sensors: DHT22 aka AM2302, SHT11, BMP180 RTC: DS3231 with 24C32 EEPROM on board.
Dependencies: BMP180 DS3231 RHT03 SDFileSystem SPI_TFT_ILI9341 _24LCXXX mbed SHTx
Clock and environmental display
Used a Nucleo L152RE, cheap display with QVGA 320x240 resolution and SPI bus plus SDCARD socket and a bunch of cheap board from Chinese dealers:
- DS3231 RTC with on board EEPROM 4k Byte
- BMP180 pressure sensor
- SHT11 temperature and humidity sensor
- TTP224 capacitive switch buttons with 4 pads
Simple weather forecast regarding the pressure. Not very reliable. Still searching for infos.
Data will be stored every ten minutes for statistics into EEPROM and on SDCARD EEPROM data is used for statistics display. The text file on SDCARD can be used for importing into some programs on PC.
Backlit control is done by a Pololu 3.3V step-down converter (I was to lazy to search for a transistor ;-) ).
SHT11 uses not the normal I2C protocol. I set it up for second I2C port (PB_11, PB_10). I used a AM2302 sensor, but sensor or lib were not very reliable and the update was hanging several hours. These combined temperature and humidity sensors with digital interface are not very cheap.
Update 1st June 2014
Some major changes. Now using SHT11 temperature humidity sensor. Code for AM2302 and DHT22 still available. Display changed to landscape orientation and inverted to white letters on black background. I use a TTP224 touch module with 4 buttons. Backlit control with a Pololu 3.3V step down converter (#2097) using the SHDN pin. A little bit bigger weather icons (95*85 pixel)
Normal display
Stats display
New media files for icons on black background:
------------------------------
Text for previous version
Now my little clock is running a few day and shows a little issue. It seems, the DHT22/AM2302 sensor is not very reliable and sometime the program hangs getting the data. I will show up with an workaround next days. Later on it is planned to change the sensor. I already ordered a SHT11 sensor which has I²C bus.
Weather symbols converted to RGB565 bmp files with size of 72x72. Put these files on a SDcard
May, 18: Now guessing weather. First splash screen disabled. Changed the reading of temp sensor. If after 5 tries no luck, take old values. The AM2302 reading is not very reliable.
Not really happy with the layout, but a good beginning. :)
Diff: main.cpp
- Revision:
- 1:5c0b32a77708
- Parent:
- 0:7e22a0e87f8e
- Child:
- 2:47de2d58ecc7
--- a/main.cpp Mon May 12 21:04:00 2014 +0000 +++ b/main.cpp Sat May 17 22:59:43 2014 +0000 @@ -1,24 +1,30 @@ /************************************************************************ * Simple indoor weather station with TFT display * - * Sensors: AM2302 aka DHT22 hygrometer thermometer + * Sensors: AM2302 aka DHT22 hygrometer thermometer * BMP180 barometer atmospheric pressure - * + * * Real time clock: DS3231 * Storage: 24c32 (on rtc board) - * + * * Board: ST NUCLEO-L152RE - * cheap 2.2inch 320x240 TFT with SPI-Interface and SDCard socket. + * cheap 2.2inch 320x240 TFT with SPI-Interface and SDCard socket. * - * Data stored every 10 minutes into file and 8K byte EEPROM + * Data stored every 10 minutes into file and 8K byte EEPROM * - * EEPROM storage: - * 576 values of temp, hygro and barometer (about 4 days) + * EEPROM storage: + * 576 values of temp, hygro and barometer (about 4 days) * Format: day, hour, min, temp, humidity, preasure - * pressure will cover 940hpa to 1060hpa + * pressure will cover 940hpa to 1060hpa * * Similar program exists for a E-Paper display and Seeduino Arch Pro. * will release this code later. + * + * Changes: + * May, 18.: Now guessing weather. First splash screen disabled. + * Changed the reading of temp sensor. If after 5 tries + * no luck, take old values. The AM2302 reading is not + * very reliable. ************************************************************************/ //#define NO_DMA @@ -67,6 +73,11 @@ #define max_datastorage 576 // 576 * 6 = 3456 #define COUNTERADDRESS 4000 // The counter for ring buffer will be stored here +#define RAIN 1000 +#define CHANGE 1010 +#define FAIR 1020 +#define SUNNY 1030 // hPa + // the TFT is connected to SPI SPI_TFT_ILI9341 TFT(PA_7, PA_6, PA_5, PB_6, PA_9, PC_7,"TFT"); // NUCLEO mosi, miso, sclk, cs, reset, dc @@ -86,6 +97,7 @@ int main() { char Control; + int err; //unsigned int y; // Test if SD-Card works @@ -98,24 +110,31 @@ TFT.claim(stderr); // send stderr to the TFT display TFT.background(White); // set background to black TFT.foreground(Black); // set chars to white + TFT.set_font((unsigned char*) Arial12x12); TFT.cls(); // clear the screen - //y= 0; - //TFT.background(Black); - //TFT.cls(); - - TFT.set_font((unsigned char*) Arial12x12); - int err = TFT.BMP_16(9,12,"/sd/Carina.bmp"); // load test.bmp from external SD-card + /* + err = TFT.BMP_16(9,12,"/sd/Carina.bmp"); // load test.bmp from external SD-card TFT.locate(10,120); if (err != 1) TFT.printf(" - Err: %d",err); wait(2); - TFT.cls(); // clear the screen + */ + TFT.locate(0,0); printf(" Hello Mbed "); TFT.Bitmap(20,20,172,55,p1); - - // set the DS3231 for alarm every minute. + err = TFT.BMP_16(2,80,"/sd/rain.bmp"); // load test.bmp from external SD-card + if (err != 1) TFT.printf(" - Err: %d",err); + err = TFT.BMP_16(82,80,"/sd/cloud.bmp"); // load test.bmp from external SD-card + if (err != 1) TFT.printf(" - Err: %d",err); + err = TFT.BMP_16(2,160,"/sd/suncloud.bmp"); // load test.bmp from external SD-card + if (err != 1) TFT.printf(" - Err: %d",err); + err = TFT.BMP_16(82,160,"/sd/sun.bmp"); // load test.bmp from external SD-card + if (err != 1) TFT.printf(" - Err: %d",err); + + + // set the DS3231 for alarm every minute. rtc.writeRegister(DS3231_Alarm2_Minutes,0x80); rtc.writeRegister(DS3231_Alarm2_Hours,0x80); rtc.writeRegister(DS3231_Alarm_2_Day_Date,0x80); @@ -151,7 +170,7 @@ TFT.cls(); TFT.fillrect(237,317,239,319,Red); // Just for status: during update there is a small red dot at the bottom right corner - + drawClock(); drawAnalogTime(); drawDigitalTime(); @@ -161,7 +180,7 @@ drawTemp(); storeData(); - + TFT.fillrect(237,317,239,319,White); // Reset clock alarm @@ -205,7 +224,7 @@ store_address += 6; eeprom.nbyte_write(COUNTERADDRESS,&store_address,sizeof(int)); - + // Store into file FILE *fp = fopen("/sd/stored_data.txt", "a"); fprintf(fp, "%02i %02i %02i:%02i %02i %cC %02i %% %04i hPa\n",month, date, hour, minute, store_temp, 176, store_humidity, (store_pressure+1880) * 50 / 100); @@ -343,17 +362,32 @@ // draw temp and humidity void drawTemp(void) { - int done=0; + int done=0, i; float temp,hum; + + for (i=0; i<5;i++) { //try to read 5 times (10secs) + wait(2.2); //Needed to make sure the sensor has time to initalise and so its not polled too quickly + if(humtemp.readData() == RHT_ERROR_NONE) { //Request data from the RHT03 + done=1; + i=5; + } + } + /* while(!done) { //Loop keeps running until RHT03 is read succesfully wait(2.2); //Needed to make sure the sensor has time to initalise and so its not polled too quickly if(humtemp.readData() == RHT_ERROR_NONE) done=1; //Request data from the RHT03 } + */ - temp = humtemp.getTemperatureC(); //Gets the current temperature in centigrade - store_temp = temp; - hum = humtemp.getHumidity(); //Gets the current humidity in percentage - store_humidity = hum; + if(done) { // only if read was successful + temp = humtemp.getTemperatureC(); //Gets the current temperature in centigrade + store_temp = temp; + hum = humtemp.getHumidity(); //Gets the current humidity in percentage + store_humidity = hum; + } else { // read was not successful, take old values + temp = store_temp; + hum = store_humidity; + } TFT.set_font((unsigned char*) Times_New_Roman28x27); // select the font TFT.locate(156,0); @@ -372,23 +406,30 @@ store_pressure = char((Pressure / 50) - 1880); + Pressure /= 100; // convert to hPa + TFT.set_font((unsigned char*) Times_New_Roman28x27); // select the font TFT.locate(0,138); - printf("%4dhPa",Pressure / 100); + printf("%4dhPa",Pressure); TFT.set_font((unsigned char*) Courier9x15); TFT.locate(0,168); printf("%i,%1i%cC",BMP180_Temperature/10,BMP180_Temperature%10,127); // Draw some weather icons for test int err ; - err = TFT.BMP_16(2,190,"/sd/rain.bmp"); // load test.bmp from external SD-card - if (err != 1) TFT.printf(" - Err: %d",err); - - err = TFT.BMP_16(76,190,"/sd/cloud.bmp"); // load test.bmp from external SD-card - if (err != 1) TFT.printf(" - Err: %d",err); - - err = TFT.BMP_16(150,190,"/sd/sun.bmp"); // load test.bmp from external SD-card - if (err != 1) TFT.printf(" - Err: %d",err); + if ( Pressure < RAIN ) { + err = TFT.BMP_16(2,190,"/sd/rain.bmp"); // load test.bmp from external SD-card + if (err != 1) TFT.printf(" - Err: %d",err); + } else if ( Pressure < CHANGE ) { + err = TFT.BMP_16(2,190,"/sd/cloud.bmp"); // load test.bmp from external SD-card + if (err != 1) TFT.printf(" - Err: %d",err); + } else if ( Pressure < FAIR ) { + err = TFT.BMP_16(2,190,"/sd/suncloud.bmp"); // load test.bmp from external SD-card + if (err != 1) TFT.printf(" - Err: %d",err); + } else { + err = TFT.BMP_16(2,190,"/sd/sun.bmp"); // load test.bmp from external SD-card + if (err != 1) TFT.printf(" - Err: %d",err); + } } @@ -396,7 +437,7 @@ void drawDigitalTime() { //SetTime(); // Call only once to set date and time on DS3231 - + int date, month, year, hour, minute, second, dayOfWeek; rtc.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);