// Dano Bosch
// 14/2/2024

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include "DHT.h" //For the themp and humiditi sensor


/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
//#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1   //   QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//dht 11 sensor stuff
#define DHTTYPE DHT11   // DHT 11
#define DHTPIN 42     // Digital pin connected to the DHT sensor
DHT dht(DHTPIN, DHTTYPE);

float humiditi;
float themp;

int debug = 0;


void setup() {
  Serial.begin(115200);
  display.begin(i2c_Address, true); // Address 0x3C default

  display.clearDisplay();
  dht.begin(); //initialising the dht11

  display.setTextSize(2);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  // display.clearDisplay();
  display.println("Welkom You life");
  display.display();
  delay(500);
  display.clearDisplay();

}

void loop() {
  // put your main code here, to run repeatedly:
  thempReading();

  display.setCursor(0, 0);
  display.println((String) "Temp: " + int(themp) + "C" + '\n' + "Humi: " + int(humiditi) + "%");
  display.display();

}

void thempReading(){
  delay(1000); //Sensor can only read every 2 sec but we want it to run asinc
  display.clearDisplay();


  humiditi = dht.readHumidity();
  // Read temperature as Celsius (the default)
  themp = dht.readTemperature();

  if (debug == 1){
  Serial.println((String)humiditi + "% " + themp + "C");
  }

}
