// include the library code: #include #include #include "DHT.h" // DHT init #define DHTPIN 2 // what pin we're connected to #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); // LCD init // Initialize the library with the numbers of the interface pins LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // you can change the overall brightness by range 0 -> 255 int brightness = 255; // switch DEBUG off by default boolean DEBUG = false; // switch LCD on by default boolean LCD = true; // only send temperature over serial if requested boolean Temp = false; #define INLENGTH 200 #define INTERMINATOR 13 char serInString[INLENGTH+1]; int serIn; int serInIndx = 0; int serOutIndx = 0; unsigned long Start; void readSerialString() { int sb; if(Serial.available()) { while (Serial.available() && serInIndx < INLENGTH ){ sb = Serial.read(); if (sb != INTERMINATOR) { serInString[serInIndx] = sb; serInIndx++; } else break; } } } void clearSerialString() { serOutIndx = 0; serInIndx = 0; } void printStringLCD(String strOutput) { lcd.clear(); lcd.setCursor(0,0); lcd.print(strOutput); delay(2000); lcd.clear(); lcd.setCursor(0,0); } void printSerialString() { int intPosition = 0; if( serInIndx > 0) { lcd.clear(); lcd.setCursor(0,0); if (DEBUG) { lcd.print("Received: "); intPosition = 10; } for(serOutIndx=0; serOutIndx <= serInIndx; serOutIndx++) { if (intPosition >= 16) lcd.scrollDisplayLeft(); if (intPosition == 40) { delay(1000); lcd.clear(); lcd.setCursor(0,0); intPosition = 0; } lcd.print(serInString[serOutIndx] ); //print out the byte at the specified index if (DEBUG) Serial.print( serInString[serOutIndx] ); delay(100); intPosition++; } delay(2000); if (DEBUG) Serial.println(); lcd.clear(); lcd.setCursor(0,0); } } void printSerialStringLCD() { int intPosition = 0; if( serInIndx > 0) { lcd.clear(); for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++) { if (intPosition >= 16) lcd.scrollDisplayLeft(); if (intPosition == 40) { delay(1000); lcd.clear(); lcd.setCursor(0,0); intPosition = 0; } lcd.print(serInString[serOutIndx] ); //print out the byte at the specified index delay(100); intPosition++; } delay(2000); lcd.clear(); lcd.setCursor(0,0); } } void printSerialStringLCD(int start) { int intPosition = 0; if( serInIndx > 0) { lcd.clear(); for(serOutIndx=start; serOutIndx < serInIndx; serOutIndx++) { if (intPosition >= 16) lcd.scrollDisplayLeft(); if (intPosition == 40) { delay(1000); lcd.clear(); lcd.setCursor(0,0); intPosition = 0; } lcd.print(serInString[serOutIndx] ); //print out the byte at the specified index delay(100); intPosition++; } delay(2000); lcd.clear(); lcd.setCursor(0,0); } } void setup() { Serial.begin(9600); Serial.flush(); dht.begin(); // set up the LCD's number of rows and columns: lcd.begin(16, 2); lcd.clear(); lcd.setCursor(0,0); brightness = 100; if (DEBUG) printStringLCD("Setup() completed"); } void loop() { if (DEBUG) digitalWrite (13, HIGH); //turn on debugging LED else digitalWrite (13, LOW); readSerialString(); if( serInIndx > 1) { int command = (serInString[0] - '0') * 10 + (serInString[1] - '0'); switch(command) { case 0: // Need to send 00 for this command! DEBUG = false; printStringLCD("DEBUG is now OFF"); break; case 1: // Need to send 01 for this command! DEBUG = true; printStringLCD("DEBUG is now ON"); break; case 10: LCD = false; lcd.noDisplay(); if (DEBUG) printStringLCD("LCD is now OFF"); break; case 11: LCD = true; lcd.display(); if (DEBUG) printStringLCD("LCD is now ON"); break; case 20: if (DEBUG) printSerialStringLCD(); printSerialStringLCD(3); break; case 31: Temp = true; Start = millis(); if (DEBUG) printStringLCD("Temp is now TRUE"); break; case 30: Temp = false; if (DEBUG) printStringLCD("Temp is now FALSE"); break; default: if (DEBUG) printStringLCD("Unknown command code: "+command); } } // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); float t = dht.readTemperature(); // check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(t) || isnan(h)) { if (DEBUG == 1) printStringLCD("Failed to read from DHT"); } else { if (Temp) { Serial.print("{"); Serial.print(h); Serial.print(","); Serial.print(t); Serial.println("}"); // if we have been sending for 3 seconds since last 31 command - auto stop sending if (millis() - Start > 3000) { Temp = false; if (DEBUG) printStringLCD("Temp is now FALSE"); } } if (LCD) { lcd.setCursor(0,0); lcd.print("Temp: "); lcd.print(t); lcd.print("C "); lcd.setCursor(0,1); lcd.print("Hum: "); lcd.print(h); lcd.print("% "); } } clearSerialString(); }