#include <Wire.h>
// Simulated temperature sensor pin
const int tempSensorPin = 34; // Analog pin for the simulated NTC thermistor
// Data logging
const int logInterval = 5000; // Log temperature every 5 seconds (in ms)
const int maxLogSize = 60; // Maximum number of logged values (5 minutes of data)
float temperatureLog[maxLogSize];
int logIndex = 0;
// Constants for temperature simulation
const float maxTemp = 37.0; // Maximum simulated body temperature
const float minTemp = 35.5; // Minimum simulated body temperature
const int adcMaxValue = 4095; // Max ADC value for ESP32
const float refVoltage = 3.3; // Reference voltage for ESP32 ADC
unsigned long lastLogTime = 0;
void setup() {
Serial.begin(115200);
Serial.println("Sleep Stage Temperature Logging");
}
void loop() {
// Simulate temperature reading
int rawValue = analogRead(tempSensorPin);
float currentTemp = map(rawValue, 0, adcMaxValue, minTemp * 100, maxTemp * 100) / 100.0;
// Log the temperature at regular intervals
if (millis() - lastLogTime >= logInterval) {
logTemperature(currentTemp);
lastLogTime = millis();
}
// Display current readings
Serial.print("Current Temperature: ");
Serial.print(currentTemp, 2);
Serial.println(" °C");
delay(500); // Delay for stability
}
void logTemperature(float temperature) {
// Add temperature to the log
temperatureLog[logIndex] = temperature;
logIndex = (logIndex + 1) % maxLogSize; // Circular buffer
// Print log to serial for demonstration
Serial.println("Temperature Log:");
for (int i = 0; i < maxLogSize; i++) {
if (temperatureLog[i] > 0) {
Serial.print(temperatureLog[i], 2);
Serial.print(" °C ");
}
}
Serial.println();
}