k k / Mbed OS dhtcode

Dependencies:   DHT TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DHT.h"
00003 #include "TextLCD.h"    // LCD1602
00004  
00005 DHT sensor(PB_3, DHT11);
00006 Serial pc(SERIAL_TX, SERIAL_RX);
00007  
00008  // LCD pins
00009 TextLCD lcd(D2, D3, D9, D10, D11, D12);
00010 
00011 // diplay text on LCD
00012 void textLCD(char *text, int line) {
00013     char tmpBuf[16];
00014     for (int i = 0; i < 16; i++) tmpBuf[i] = 0x20;
00015     for (int i = 0; i < strlen(text); i++) {
00016         if (i < 16) tmpBuf[i] = text[i];
00017     }
00018     
00019     lcd.locate(0, line);
00020     lcd.printf(tmpBuf);
00021 }
00022 
00023 
00024 int main()
00025 {
00026     int error = 0;
00027     float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
00028     pc.baud(9600);
00029     lcd.cls();
00030     char tmpString[16];
00031     char tmpString2[16];
00032     int iKey = 0;
00033     int oldiKey = 0;
00034     int temp_format = 0;
00035     int humid_format = 0;
00036     
00037     while(1) {  
00038         wait(2.0f);
00039         error = sensor.readData();
00040         if (0 == error) {
00041             c   = sensor.ReadTemperature(CELCIUS);
00042             f   = sensor.ReadTemperature(FARENHEIT);
00043             k   = sensor.ReadTemperature(KELVIN);
00044             h   = sensor.ReadHumidity();
00045             dp  = sensor.CalcdewPoint(c, h);
00046             dpf = sensor.CalcdewPointFast(c, h);
00047             pc.printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\r\n", k, c, f);
00048             pc.printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\r\n", h, dp, dpf);
00049             pc.printf("Tempformat: %d HumidFormat: %d\r\n", temp_format, humid_format);
00050             switch(temp_format) {
00051                 case 0 : sprintf(tmpString, " Temp: %4.2fC", c); break;
00052 //                case 1 : sprintf(tmpString, " Temp: %4.2fF", f); break;
00053 //                case 2 : sprintf(tmpString, " Temp: %4.2fK", k); break;
00054             }     
00055             textLCD(tmpString, 1);
00056             switch(humid_format) {
00057                 case 0 : sprintf(tmpString2, "Humid: %4.2f", h); break;
00058 //                case 1 : sprintf(tmpString2, "Dewpoint: %4.2f", dp); break;
00059 //                case 2 : sprintf(tmpString2, "DewpFast: %4.2f", dpf); break;
00060             }                   
00061             textLCD(tmpString2, 0);            
00062         } else {
00063             pc.printf("Error: %d\n", error);
00064             textLCD("Error", 0);
00065         }
00066     }
00067 }