/***************** DHT ******************/
#define DHTPIN 8
#include "dht.h" // DHT library test sketch for DHT22/DHT11 && Arduino "AUTHOR: Rob Tillaart"
dht DHT;
int HUM = 0;
int TEMP = 0;
/***************************************/
/**************** OLED *****************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
/***************************************/
void setup()
{
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
};
display.display();
delay(1000);
}
void loop()
{
/****** For DHT22 *******/
//int chk = DHT.read22(DHTPIN);
/************************/
/****** For DHT11 *******/
int chk = DHT.read22(DHTPIN);
/************************/
HUM = DHT.humidity; // Read the humidity
TEMP = DHT.temperature; // Read the temperature
/**** Display Data *****/
Serial.print("Humidity");Serial.println(HUM);
Serial.print("Temperature");Serial.println(TEMP);
display.clearDisplay();
oledDisplayHeader();
oledDisplay(1,1,16,HUM,"%"); // Display humidity
oledDisplay(1,60,16,TEMP,"C");// Display temperature
display.display();
/************************/
delay(2000);
}
/*************** Generate OLED Header ***************/
void oledDisplayHeader()
{
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(1, 1);
display.print("HUMEDAD");
display.setCursor(60, 1);
display.print("TEMPERATURA");
}
/*************** Generate OLED Display ***************/
void oledDisplay(int size, int x,int y, float value, String unit)
{
int charLen = 7;
int xo = x + charLen*3.2;
int xunit = x + charLen*3.6;
int xval = x;
// ***********************
int16_t x, y;
uint16_t textWidth, textHeight;
const char strHello[] = String(value);
// Setup text rendering parameters
_display.setTextSize(1);
_display.setTextColor(WHITE, BLACK);
// Measure the text with those parameters
_display.getTextBounds(strHello, 0, 0, &x, &y, &textWidth, &textHeight);
// Center the text on the display
_display.setCursor(_display.width() / 2 - textWidth / 2, _display.height() / 2 - textHeight / 2);
// Print out the string
_display.print(strHello);
// Render the graphics buffer to screen
_display.display();
// **********************
display.setTextSize(size);
display.setTextColor(WHITE);
if (unit=="%"){
display.setCursor(x, y);
display.print(value,0);
display.print(unit);
} else {
if (value>99){ xval = x; }
else { xval = x + charLen; };
display.setCursor(xval, y);
display.print(value,0);
display.drawCircle(xo, y + 2, 2, WHITE); // Print degree symbols
display.setCursor(xunit, y);
display.print(unit);
};
}