#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>

// OLED display configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// DS18B20 sensor configuration
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// DHT22 sensor configuration
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Function to convert Celsius to Fahrenheit
float celsiusToFahrenheit(float celsius) {
  return celsius * 1.8 + 32;
}

void setup() {
  // Initialize the serial communication
  Serial.begin(9600);

  // Initialize the OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  // Initialize the DS18B20 sensor
  sensors.begin();

  // Initialize the DHT22 sensor
  dht.begin();
}

void loop() {
  // Request temperature from DS18B20 sensor
  sensors.requestTemperatures();
  float tempC_DS18B20 = sensors.getTempCByIndex(0);
  float tempF_DS18B20 = celsiusToFahrenheit(tempC_DS18B20);
  
  // Get temperature and humidity from DHT22 sensor
  float tempC_DHT22 = dht.readTemperature();
  float tempF_DHT22 = celsiusToFahrenheit(tempC_DHT22);
  float humidity = dht.readHumidity();

  // Get DS18B20 sensor address (ID)
  DeviceAddress sensorAddress;
  if (sensors.getAddress(sensorAddress, 0)) {
    display.clearDisplay();
    display.setCursor(0, 0);
    
    // Display DS18B20 ID
    display.print("DS18B20");
    display.println();
    display.print("ID: ");
    for (uint8_t i = 0; i < 8; i++) {
      if (sensorAddress[i] < 16) display.print("0");
      display.print(sensorAddress[i], HEX);
    }
    display.println();

    // Display DS18B20 temperature in C and F
    display.print("Temp: ");
    display.print(tempC_DS18B20);
    display.print("C ");
    display.print(tempF_DS18B20);
    display.println("F");

    // Display DHT22 temperature and humidity
    display.print("DHT22");
    display.println();
    display.print("Temp: ");
    display.print(tempC_DHT22);
    display.print("C ");
    display.print(tempF_DHT22);
    display.println("F");

    display.print("Humidity: ");
    display.print(humidity);
    display.println(" %");

    display.display();  // Update the display
  } else {
    Serial.println("Error reading DS18B20 sensor address");
  }

  delay(2000);  // Wait for 2 seconds before updating the data
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
temp1:GND
temp1:DQ
temp1:VCC
r1:1
r1:2
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND