Last active
April 29, 2017 05:50
-
-
Save nathan-osman/9f386973cf5a5556dbbc5bdc0a099418 to your computer and use it in GitHub Desktop.
Measure temperature and humidity and display the values on an OLED screen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Measure temperature & humidity and display on OLED screen | |
* Copyright 2017 - Nathan Osman | |
*/ | |
#include <DHT.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Fonts/FreeSans12pt7b.h> | |
#define DHTPIN 3 | |
#define DHTTYPE DHT11 | |
DHT dht(DHTPIN, DHTTYPE); | |
#define OLED_RESET 4 | |
Adafruit_SSD1306 display(OLED_RESET); | |
#if (SSD1306_LCDHEIGHT != 64) | |
#error("Height incorrect, please fix Adafruit_SSD1306.h!"); | |
#endif | |
void setup() | |
{ | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); | |
display.setTextColor(WHITE); | |
display.setFont(&FreeSans12pt7b); | |
} | |
void loop() | |
{ | |
// Storage for the message to be shown on the display | |
char szDisp[128]; | |
// Read the data from the sensor | |
float fTemp = dht.readTemperature(); | |
float fHum = dht.readHumidity(); | |
if (isnan(fTemp) || isnan(fHum)) { | |
sprintf(szDisp, "err: sensor"); | |
} else { | |
// Convert the floats into strings | |
char szTemp[8]; | |
char szHum[8]; | |
dtostrf(fTemp, 1, 1, szTemp); | |
dtostrf(fHum, 1, 1, szHum); | |
sprintf(szDisp, "T: %s C\nH: %s%%", szTemp, szHum); | |
} | |
// Show the computed value | |
display.clearDisplay(); | |
display.setCursor(0, 20); | |
display.println(szDisp); | |
display.display(); | |
// Values can only be read from the sensor every two seconds | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment