#include <SPI.h> //for IIC -Inter intergrated circuit
#include <Wire.h>
#include <Adafruit_GFX.h> // for graphic
#include <Adafruit_SSD1306.h> //for oled screen
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
//all these 4 heads files are necessary
#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)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// no need to change, always as default
#define LOGO_HEIGHT 32
#define LOGO_WIDTH 32
// 'DINO', 32x32px
//https://javl.github.io/image2cpp/
// const unsigned char bitmap_DINO [] PROGMEM = {
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x3f, 0xf0, 0x00, 0x00, 0xff, 0xf8, 0x00, 0x00, 0xef, 0xf8, 0x00, 0x00, 0xff, 0xf8,
// 0x00, 0x00, 0xff, 0xf8, 0x00, 0x00, 0xff, 0xf8, 0x00, 0x00, 0xff, 0xf8, 0x00, 0x00, 0xfe, 0x00,
// 0x00, 0x00, 0xff, 0xc0, 0x08, 0x01, 0xf8, 0x00, 0x08, 0x01, 0xf8, 0x00, 0x08, 0x07, 0xf8, 0x00,
// 0x0c, 0x1f, 0xff, 0x00, 0x0e, 0x3f, 0xf9, 0x00, 0x0f, 0xff, 0xf8, 0x00, 0x0f, 0xff, 0xf8, 0x00,
// 0x0f, 0xff, 0xf8, 0x00, 0x07, 0xff, 0xf8, 0x00, 0x03, 0xff, 0xf0, 0x00, 0x03, 0xff, 0xf0, 0x00,
// 0x01, 0xff, 0xe0, 0x00, 0x00, 0x7f, 0xc0, 0x00, 0x00, 0x3d, 0xc0, 0x00, 0x00, 0x30, 0xc0, 0x00,
// 0x00, 0x30, 0xc0, 0x00, 0x00, 0x20, 0xc0, 0x00, 0x00, 0x30, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00
// };
int val_t = 0; // value from A0 pin.
int val_h = 0;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3c, safety precheck
Serial.println(F("SSD1306 allocation failed"));
while(true){} // Don't proceed, loop forever
}
// Clear the buffer
//display.clearDisplay();
// Draw a single pixel in white
//display.drawPixel(10, 10, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
//display.display();
dht.begin();
delay(300);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
val_h = digitalRead(2);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // set position
display.print("Temp(C): ");
display.setCursor(0, 10); // set position
display.print("Humid(%): ");
display.setCursor(90, 0); // Start at top-left corner
display.print(t);
display.setCursor(90, 10); // Start at top-left corner
display.print(h);
display.fillRect(10, 20, t / 2.0 + 20.0, 15, SSD1306_INVERSE); //Temp
display.fillRect(10, 40, h / 100.0 * 60.0, 15, SSD1306_INVERSE); //Humid
display.display();
delay(1000);
}
// void testdrawchar(void) {
// display.clearDisplay();
// display.setTextSize(1); // Normal 1:1 pixel scale
// display.setTextColor(SSD1306_WHITE); // Draw white text
// display.setCursor(0, 0); // Start at top-left corner
// display.print("This is a test");
// display.display();
// delay(1000);
// }
// void testdrawline() {
// display.clearDisplay(); // Clear display buffer
// display.drawLine(0, 0, 127, 63, SSD1306_WHITE);
// //0, 0 indicates the position of one end, 127, 63 indicates position of the other end.
// display.display(); // Update screen with each newly-drawn line
// delay(1000);
// }
// void testdrawrect(void) {
// display.clearDisplay();
// display.drawRect(10, 20, 50, 30, SSD1306_WHITE);
// //10, 20 are for the position, 50 is the width of rect, 30 is the height of rect.
// display.display(); // Update screen with each newly-drawn rectangle
// delay(1000);
// }
// void testfillrect(void) {
// display.clearDisplay();
// display.fillRect(10, 20, 50, 30, SSD1306_INVERSE);
// //same as drawrect attributes
// display.display();
// delay(1000);
// }
// void testdrawcircle(void) {
// display.clearDisplay();
// byte radius = 10;//local variable called radius to change the size of circle
// display.drawCircle(40, 50, radius, SSD1306_WHITE);
// //40, 50 indicates the center of the position
// display.display();
// delay(1000);
// }
// void testfillcircle(void) {
// display.clearDisplay();
// byte radius = 10;
// display.fillCircle(40, 50, radius, SSD1306_INVERSE);
// //same as drawCircle
// display.display();
// delay(1000);
// }
// void testdrawroundrect(void) {
// display.clearDisplay();
// byte width = 25; byte height = 10; byte round_val = 2;
// // round_val controls the edge sharpness, it becomes normal rect when val is 0.
// display.drawRoundRect(10, 20, width, height, round_val, SSD1306_WHITE);
// //10, 20 indicates start position
// display.display();
// delay(1000);
// }
// void testfillroundrect(void) {
// display.clearDisplay();
// byte width = 25; byte height = 10; byte round_val = 2;
// display.fillRoundRect(10, 20, width, height, round_val, SSD1306_INVERSE);
// //same as testdrawroundrect
// display.display();
// delay(1000);
// }
// void testdrawtriangle(void) {
// display.clearDisplay();
// display.drawTriangle(
// 10, 10,
// 50, 40,
// 10, 60, SSD1306_WHITE);
// //coordinate of three points
// display.display();
// delay(1000);
// }
// void testfilltriangle(void) {
// display.clearDisplay();
// display.fillTriangle(
// 10, 10,
// 50, 40,
// 10, 60, SSD1306_INVERSE);
// //coordinate of three points
// display.display();
// delay(1000);
// }
// void testdrawbitmap(void) {
// display.clearDisplay();
// display.drawBitmap(18, 20, bitmap_DINO, LOGO_WIDTH, LOGO_HEIGHT, 1);
// //18, 20 indicates the start position of the picture.
// display.display();
// delay(1000);
// }