#include <Arduino.h>
//OLED Screens
#include <U8g2lib.h>
//DHT SENSOR
#include "DHT.h"
#define DHT11Pin 22
#define DHTType DHT11
U8G2_SSD1306_128X64_NONAME_F_HW_I2C display1(U8G2_R0, 5,4,U8X8_PIN_NONE);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C display2(U8G2_R0, 5,4,U8X8_PIN_NONE);
//Globals
//MILLIS
unsigned long currentMillis;
unsigned long displayTimer;
unsigned long displayDelay = 1000;
unsigned long tempTimer;
unsigned long tempDelay = 1000;
DHT HT(DHT11Pin,DHTType);
float humi;
float tempC;
float tempF;
void setup(void) {
Serial.begin(9600);
display1.setI2CAddress(0x78); // sets the address for display 1
display1.begin(); // initializes display 1
//display1.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
display1.clearDisplay(); // clears display 1
display2.setI2CAddress(0x7A); // sets the address for display 2
display2.begin(); // initializes display 2
//display2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
display2.clearDisplay(); // clears display 2
}
void OLEDDisplay1() {
display1.clearDisplay(); // clear display
display1.clearBuffer(); // clear the internal memory
oledDisplayHeader();
oledDisplayHumi();
oledDisplayTempC();
oledDisplayTempF();
display1.sendBuffer(); // transfer internal memory to the display
}
void OLEDDisplay2() {
display2.clearDisplay(); // clear display
display2.clearBuffer(); // clear the internal memory
display2.setFont(u8g2_font_ncenB10_tr);
//display2.drawStr(20,20,"World!"); // write something to the internal memory
display2.setCursor(20,20);
display2.println(humi);
display2.setCursor(20,35);
display2.println(tempC);
display2.setCursor(20,50);
display2.println(tempF);
display2.sendBuffer(); // transfer internal memory to the display
}
void runDisplays() {
if (currentMillis - displayTimer > displayDelay) {
OLEDDisplay1(); // write to display 1
OLEDDisplay2(); // write to display 2
displayTimer = currentMillis; // reset timer
}
}
void oledDisplayHeader()
{
display1.setFont(u8g2_font_ncenB08_tr);
display1.drawStr(0,8, "Humidity");
display1.drawStr(60,8, "Temperature");
}
void oledDisplayHumi() {
char buffHumi[8];
char humiBuff[8];
dtostrf(humi, 2, 0, humiBuff); //converts float to string
sprintf(buffHumi, "%s%s", humiBuff, "%"); //creates string
display1.setFont(u8g2_font_ncenB10_tr); //set the font
display1.drawStrX2(5, 50, buffHumi); //draws humidity string X2 doubles the current font size
}
void oledDisplayTempC() {
char buffTempC[10];
char tempCBuff[10];
dtostrf(tempC, 8, 1, tempCBuff); //converts float to string
sprintf(buffTempC, "%s %s", tempCBuff, "C"); //creates string
display1.setFont(u8g2_font_ncenB10_tr); //set the font
display1.drawStr(70, 30, buffTempC); //draws the tempC string
}
void oledDisplayTempF() {
char buffTempF[10];
char tempFBuff[10];
dtostrf(tempF, 3, 1, tempFBuff); //converts float to string
sprintf(buffTempF, "%s %s", tempFBuff, "F"); //creates string
display1.drawStr(85, 50, buffTempF); //draws the tempF string
}
void checkTemp(){
//delay(1000);
if (currentMillis - tempTimer > tempDelay){
humi = HT.readHumidity();
tempC = HT.readTemperature();
tempF = HT.readTemperature(true);
Serial.print("Humidity:");
Serial.print(humi,1);
Serial.print("%");
Serial.print(" Temperature:");
Serial.print(tempC,1);
Serial.print("C ~ ");
Serial.print(tempF,1);
Serial.println("F");
tempTimer = currentMillis;
}
}
void loop(void){
currentMillis= millis(); // start timer
checkTemp();
runDisplays();
}