Skip to content

Instantly share code, notes, and snippets.

@gregorthebigmac
Last active August 29, 2015 14:22
Show Gist options
  • Save gregorthebigmac/28d889228cfe7d30d0a1 to your computer and use it in GitHub Desktop.
Save gregorthebigmac/28d889228cfe7d30d0a1 to your computer and use it in GitHub Desktop.
Temp/Humidity Sensor with 128x64 LCD
#include "DHT.h"
#include "U8glib.h"
#define DHTPIN 5 // data pin for DHT 22
#define DHTTYPE DHT22 // Identifier from library. Will not work without this!
DHT dht(DHTPIN, DHTTYPE);
U8GLIB_ST7920_128X64_4X u8g(13, 11, 10); // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
void draw()
{
float hum_r[2]; // used for himidity reading. the first in the array is the actual reading, the second is truncated to x.xx
float temp_r[2]; // used for temp reading. same rules apply as for the humidity formatting
int hum; // used for truncation of humidity reading (float hum_r * 100), then convert to an integer for rounding, then truncate
int temp; // same as hum
byte high_temp = 24; // highest allowable temperature in the house in degrees Celsius
byte low_temp = 21; // lowest allowable temperature in the house in degrees Celsius
u8g.setColorIndex(1);
u8g.setFont(u8g_font_6x10);
hum_r[1] = dht.readHumidity();
temp_r[1] = dht.readTemperature();
// this next whole block of code is just for truncating decimals beyond XX.xx When it takes the initial hum/temp reading, it takes
// many more decimal places than this, so I round->truncate it for comparison and display purposes.
hum = hum_r[1] * 100;
temp = temp_r[1] * 100;
hum_r[1] = hum;
temp_r[1] = temp;
hum_r[1] = hum_r[1] / 100;
temp_r[1] = temp_r[1] / 100;
// now everything's rounded and truncated, and put back into a decimal XX.xx
if (isnan(temp_r[1]) or isnan(hum_r[1]))
{
u8g.setPrintPos(0, 7);
u8g.print("Invalid data! Check your wiring.");
delay(5000); // this means it got values from the sensor that weren't real numbers, so it was probably garbage data.
// So I put a delay in here for 5 seconds to give it a chance to get real data. If it doesn't do anything
// for another 5 seconds, we got a problem. Check your wiring.
}
else
{
byte seconds, minutes, hours, days;
seconds = millis() / 1000 % 60;
minutes = millis() / 1000 / 60 % 60;
hours = millis() / 1000 / 60 / 60 % 24;
days = millis() / 1000 / 60 / 60 / 24;
u8g.drawBox(0, 0, 127, 10);
u8g.setColorIndex(0);
u8g.setPrintPos(1, 8);
u8g.print("All temps in DEG C");
u8g.setColorIndex(1);
u8g.setPrintPos(1, 18);
u8g.print("Hi=");
u8g.print(high_temp);
u8g.print(".0");
u8g.print(" Temp=");
u8g.print(temp_r[1]);
u8g.setPrintPos(1, 26);
u8g.print("Lo=");
u8g.print(low_temp);
u8g.print(".0");
u8g.setPrintPos(1, 40);
u8g.print("H=");
u8g.print(hum_r[1]);
u8g.print("%");
u8g.setPrintPos(1, 63);
u8g.print(days);
u8g.print("d ");
u8g.print(hours);
u8g.print("h ");
u8g.print(minutes);
u8g.print("m ");
u8g.print(seconds);
u8g.print("s");
if (temp_r[1] > high_temp)
{
u8g.drawFrame(65, 30, 29, 13);
u8g.setPrintPos(68, 40);
u8g.print("Heat");
u8g.drawBox(100, 30, 25, 13);
u8g.setColorIndex(0);
u8g.setPrintPos(104, 40);
u8g.print("A/C");
}
else if (temp_r[1] < low_temp)
{
u8g.drawFrame(100, 30, 25, 13);
u8g.setPrintPos(104, 40);
u8g.print("A/C");
u8g.drawBox(65, 30, 29, 13);
u8g.setColorIndex(0);
u8g.setPrintPos(68, 40);
u8g.print("Heat");
}
else
{
u8g.drawFrame(65, 30, 29, 13);
u8g.setPrintPos(68, 40);
u8g.print("Heat");
u8g.drawFrame(100, 30, 25, 13);
u8g.setPrintPos(104, 40);
u8g.print("A/C");
}
}
hum_r[0] = hum_r[1];
temp_r[0] = temp_r[1];
}
void setup()
{
dht.begin();
delay(200);
}
void loop()
{
do
{
draw();
} while (u8g.nextPage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment