#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define i2c_Address 0x3C // Change this if your OLED has a different I2C address
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define ONE_WIRE_BUS 2 // D2 pin for DS18B20 sensor

Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);

  if (!display.begin(i2c_Address)) {
    Serial.println(F("SH110X allocation failed"));
    for (;;);
  }

  sensors.begin();  // Initialize DS18B20 sensor

  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop() {
  display.clearDisplay();

  // Set text size and color
  display.setTextSize(2);
  display.setTextColor(SH110X_WHITE);

  // Read temperature from DS18B20 sensor
  sensors.requestTemperatures();
  float temperatureC = sensors.getTempCByIndex(0);

  // Display temperature on the screen in Celsius with one decimal place
  display.setCursor(10, 10);
  display.print("Temp:");
  display.setCursor(10, 30);
  display.print(temperatureC, 1); // Display one decimal place

  // Update the display
  display.display();

  // Delay for a while
  delay(2000);
}
