#include <OneWire.h>
#include <DallasTemperature.h>
// ===== Timer =====
const unsigned long interval = 30UL * 60UL * 1000UL; // 30 min
unsigned long previousMillis = 0;
// ===== DS18B20 =====
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensor(&oneWire);
// ===== Voltage (ADC) =====
#define VOLTAGE_PIN 34 // ADC pin ESP32
const float ADC_MAX = 4095.0; // 12-біт ADC ESP32
const float VOLTAGE_REF = 3.3;
// ===== ID sensor =====
const int sensorID = 1; // for evry esp ESP
float temperature = 0.0;
float voltage = 0.0;
// ===== Sensor functions =====
void readSensors() {
tempSensor.requestTemperatures();
temperature = tempSensor.getTempCByIndex(0);
int raw = analogRead(VOLTAGE_PIN);
voltage = (raw / ADC_MAX) * VOLTAGE_REF;
}
// ===== SETUP =====
void setup() {
Serial.begin(115200);
// DS18B20
tempSensor.begin();
}
// ===== LOOP =====
void loop() {
unsigned long now = millis();
if (now - previousMillis >= interval || previousMillis == 0) {
previousMillis = now;
readSensors();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Voltage: ");
Serial.println(voltage);
}
}