#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.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)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHTPIN 8 // Digital pin connected to DHT11
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
// Wire.begin(21, 22); // Wemos D1 R32
// Wire.begin(20, 21); // PICO
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
// for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
dht.begin();
// Clear the buffer
display.clearDisplay();
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
delay(2000);
// display.display() is NOT necessary after every single drawing command,
// unless that's what you want...rather, you can batch up a bunch of
// drawing operations and then update the screen all at once by calling
// display.display(). These examples demonstrate both approaches...
testdrawline(); // Draw many lines
testfillrect(); // Draw rectangles (filled)
testfillcircle(); // Draw circles (filled)
testfillroundrect(); // Draw rounded rectangles (filled)
testdrawstyles(); // Draw 'stylized' characters
testscrolltext(); // Draw scrolling text
// Invert and restore display, pausing in-between
display.invertDisplay(true);
delay(1000);
display.invertDisplay(false);
delay(1000);
dhtReslts();
}
void loop() {
// display.clearDisplay();
// float h = dht.readHumidity();
// // Read temperature as Celsius (the default)
// float t = dht.readTemperature();
// // Read temperature as Fahrenheit (isFahrenheit = true)
// float f = dht.readTemperature(true);
// // Check if any reads failed and exit early (to try again).
// if (isnan(h) || isnan(t) || isnan(f)) {
// Serial.println(F("Failed to read from DHT sensor!"));
// display.setTextSize(1); // Draw 2X-scale text
// display.setTextColor(SSD1306_WHITE);
// display.println(F("Failed to read,"));
// display.println(F(" from DHT sensor!"));
// delay(2000);
// display.clearDisplay();
// return;
// }
// // Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// // Compute heat index in Celsius (isFahreheit = false)
// float hic = dht.computeHeatIndex(t, h, false);
// Serial.print(F("Humidity: "));
// Serial.print(h);
// Serial.print(F("% Temperature: "));
// Serial.print(t);
// Serial.print(F("°C "));
// Serial.println(f);
// display.setTextSize(1); // Draw 1X-scale text
// display.setTextColor(SSD1306_WHITE);
// display.print(F("Humidity: "));
// display.println(h);
// display.print(F("% Temperature: "));
// display.print(t);
// display.print(F("°C "));
// display.print(f);
// display.println(F("°F "));
// display.display();
// delay(2000);
}
void testdrawline() {
int16_t i;
display.clearDisplay(); // Clear display buffer
for(i=0; i<display.height(); i+=4) {
display.drawLine(display.width()-1, 0, 0, i, SSD1306_WHITE);
display.display();
delay(1);
}
for(i=0; i<display.width(); i+=4) {
display.drawLine(display.width()-1, 0, i, display.height()-1, SSD1306_WHITE);
display.display();
delay(1);
}
delay(1000); // Pause for 2 seconds
display.clearDisplay();
}
void testfillrect(void) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2; i+=3) {
// The INVERSE color is used so rectangles alternate white/black
display.fillRect(i, i, display.width()-i*2, display.height()-i*2, SSD1306_INVERSE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
}
delay(1000);
}
void testfillcircle(void) {
display.clearDisplay();
for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) {
// The INVERSE color is used so circles alternate white/black
display.fillCircle(display.width() / 2, display.height() / 2, i, SSD1306_INVERSE);
display.display(); // Update screen with each newly-drawn circle
delay(1);
}
delay(1000);
}
void testfillroundrect(void) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2-2; i+=2) {
// The INVERSE color is used so round-rects alternate white/black
display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i,
display.height()/4, SSD1306_INVERSE);
display.display();
delay(1);
}
delay(1000);
}
void testdrawstyles(void) {
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(10,10); // Start at top-left corner
display.println(F(" Hello,"));
display.println(" world!");
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1); // Draw 1X-scale text
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(10,10); // Start at top-left corner
display.println("Ali Almurtadha");
display.println(" Ibrahim`s,");
display.println("");
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(20,30);
display.println(" DHT Project. ");
display.display();
// Invert and restore display, pausing in-between
display.invertDisplay(true);
delay(3000);
display.invertDisplay(false);
delay(3000);
display.invertDisplay(true);
delay(3000);
display.clearDisplay();
}
void testscrolltext(void) {
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 10);
display.println(F("Mahdi's"));
display.println(F(" School"));
display.println(F(" Gr-6"));
display.display(); // Show initial text
delay(100);
// Scroll in various directions, pausing in-between:
display.startscrollright(0x00, 0x0F);
delay(2000);
display.stopscroll();
delay(1000);
display.startscrollleft(0x00, 0x0F);
delay(2000);
display.stopscroll();
delay(1000);
display.startscrolldiagright(0x00, 0x07);
delay(2000);
display.startscrolldiagleft(0x00, 0x07);
delay(2000);
display.stopscroll();
delay(1000);
}
void dhtReslts() {
for(;;) {
display.clearDisplay();
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
// Serial.println(F("Failed to read from DHT sensor!"));
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.println(F("Failed to read,"));
display.println(F(" from DHT sensor!"));
delay(2000);
display.clearDisplay();
}
display.setCursor(10, 25);
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.println(F("Read from"));
display.println(F(" DHT sensor!..."));
display.invertDisplay(false);
display.display();
delay(1000);
display.clearDisplay();
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.println(F("°F "));
display.setCursor(10, 10);
display.setTextSize(1); // Draw 1X-scale text
display.setTextColor(SSD1306_WHITE);
display.print(F("Humidity: "));
display.println(h);
display.println("");
display.println(F(" Temperature: "));
display.println("");
display.print(" ");
display.print(t);
display.print(F(" C "));
display.print(f);
display.println(F(" F "));
display.invertDisplay(true);
display.display();
delay(5000);
}
}