/* DS18B20 1-Wire digital temperature sensor with Arduino example code. More info: https://www.makerguides.com */
// ESP32_18B20_2X_COMPARE_003 2x DS18B20 OK
// Include the required Arduino libraries:
#include "OneWire.h"
#include "DallasTemperature.h"
#include <Adafruit_SSD1306.h>
#include <Wire.h>
//#include <WiFi.h>
//#include <HTTPClient.h>
#include <ArduinoJson.h>
//#include "secrets.h" // WiFi Configuration (WiFi name and Password)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
float Compare_4_5 = 0;
float tempC_4_Ven;
float tempC_5_Dilna;
// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS_4 4
#define ONE_WIRE_BUS_5 5
const int Vent_ON = 6;
bool LED_Vent = false;
bool END_Vent = false;
// ......................................................................................
unsigned long previousMillis = 0 ;
unsigned long currentMillis = 0 ;
const unsigned long interval_Zpozdi_ON = 1000; // interval zpoždění ON a zobrazení na OLED
int Time_ON = 10 ;
// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire_4(ONE_WIRE_BUS_4);
OneWire oneWire_5(ONE_WIRE_BUS_5);
// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors_4(&oneWire_4);
DallasTemperature sensors_5(&oneWire_5);
// ------------------------------------------------------------------------------------------------
// ////////// //
// // //
// // ///// //
// ////////// // // ////// // // /////
// // /////// // // // // //
// // // // // // // //
// ////////// ////// ////// ////// ///////
// //
// ------------------------------------------------------- // ------------------------------------
void setup() {
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
pinMode(Vent_ON, OUTPUT);
// Start up the library:
sensors_4.begin();
sensors_5.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Init 0LED");
display.display();
}
// -------------------------------------------------------------------------------
// Porovná teplotu sensor_4 a senzor_5
void Sensor_Compare()
{
Compare_4_5 = tempC_4_Ven - tempC_5_Dilna ;
if (tempC_5_Dilna >= tempC_4_Ven ) { LED_Vent = HIGH; }
else { if ( END_Vent == true && Time_ON == 10 ) { LED_Vent = LOW; }}
// set the LED with the ledState of the variable:
digitalWrite( Vent_ON, LED_Vent);
}
// -------------------------------------------------------------------------------
void Sensor_4_Ven()
{
sensors_4.requestTemperatures();
// Fetch the temperature in degrees Celsius for device index:
tempC_4_Ven = sensors_4.getTempCByIndex(0); // the index 0 refers to the first device
// Print the temperature in Celsius in the Serial Monitor:
Serial.print("Temperature 4: ");
Serial.print(tempC_4_Ven);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.print("C "); Serial.print(" ");
}
// ------------------------------------------------------------------------------
void Sensor_5_Dilna()
{
sensors_5.requestTemperatures();
// Fetch the temperature in degrees Celsius for device index:
tempC_5_Dilna = sensors_5.getTempCByIndex(0); // the index 0 refers to the first device
// Print the temperature in Celsius in the Serial Monitor:
Serial.print("Temperature 5: ");
Serial.print(tempC_5_Dilna);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.print("C "); Serial.print(" ");
}
// ------------------------------------------------------------------------------
void OLED_Teplota()
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.setTextSize(3);
display.print(tempC_5_Dilna,1);
display.setTextSize(1);
display.print(" Dilna");
display.setCursor(0,29);
display.setTextSize(3);
display.print(tempC_4_Ven,1);
display.setTextSize(1);
display.print(" Ven");
if (LED_Vent == LOW)
{
display.setCursor(0,55); display.print("Ventilace "); display.print(Time_ON); display.print(" "); display.print(END_Vent);
}
else {display.setCursor(0,55); display.print("Time_ON: "); display.print(Time_ON); display.print(" "); display.print(END_Vent); }
display.display();
}
// ---------------------------------------------------------------------------------------
// //
// // Hlavní smyčka
// //
// // ////// ////// //////
// // // // // // // //
// // // // // // // //
// ///////// ////// ////// ///////
// //
// ------------------------------------ // ------------------------------------
void loop() {
// Send the command for all devices on the bus to perform a temperature conversion:
Sensor_4_Ven() ;
delay(1000);
Sensor_5_Dilna() ;
// Wait 1 second:
delay(1000);
Sensor_Compare() ;
Serial.println(Compare_4_5);
Serial.println("");
OLED_Teplota();
// načti teplotu pro zobrazení na OLED
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval_Zpozdi_ON)
{
previousMillis = currentMillis;
if (LED_Vent == HIGH && Time_ON > 0 ) {Time_ON = Time_ON - 1 ; }
Serial.print("Time ON: "); Serial.println(Time_ON);
if ( Time_ON == 0 && LED_Vent == HIGH) { END_Vent = true ; Time_ON = 10; }
}
// ----------------------------------------------------------------------------------------
}