Files
2026-10-20 20:09:42 +02:00

103 lines
2.2 KiB
Arduino

#include <OneWire.h>
#include <DallasTemperature.h>
#include <U8g2lib.h>
#include <SD.h>
#define ONE_WIRE_BUS D4
#define PIN_CS D8
#define STATUS_OK 0
#define STATUS_SD_INIT_FAILED 1
#define STATUS_SD_WRITE_FAILED 2
#define ODD 1
#define EVEN 2
#define TEMP_UNDEFINED -99
#define DELAY 600000 // 10 minutes
//#define DELAY 5000 // 5 seconds
OneWire oneWire( ONE_WIRE_BUS );
DallasTemperature DS18B20( &oneWire );
U8G2_SSD1306_128X64_NONAME_F_HW_I2C display(U8G2_R0);
int status = STATUS_OK;
char ident[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int curIdent = 0;
float tempMin = TEMP_UNDEFINED;
float tempMax = TEMP_UNDEFINED;
void setup() {
Serial.begin(115200);
DS18B20.begin();
display.begin();
if (!SD.begin(PIN_CS)) {
status = STATUS_SD_INIT_FAILED;
}
}
void loop() {
char tempStr[8], tempHeadStr[16];
long uptimeSec = millis() / 1000;
DS18B20.requestTemperatures();
float temp = DS18B20.getTempCByIndex(0);
if (temp < tempMin || tempMin == TEMP_UNDEFINED) {
tempMin = temp;
}
if (temp > tempMax || tempMax == TEMP_UNDEFINED) {
tempMax = temp;
}
sprintf(tempStr, "%0.1f", temp);
sprintf(tempHeadStr, "L %0.1f H %0.1f %c", tempMin, tempMax, ident[curIdent]);
Serial.print(uptimeSec);
Serial.print(": ");
Serial.println(tempStr);
display.clearBuffer();
display.setFont(u8g2_font_fub11_tf);
switch(status) {
case STATUS_SD_INIT_FAILED:
display.drawStr(0, 11, "SD init failed");
break;
case STATUS_SD_WRITE_FAILED:
display.drawStr(0, 11, "SD write failed");
break;
case STATUS_OK:
default:
display.drawStr(0, 11, tempHeadStr);
}
display.setFont(u8g2_font_fub42_tn);
display.drawStr(8, 63, tempStr);
display.sendBuffer();
if (status != STATUS_SD_INIT_FAILED) {
File fd = SD.open("temp.csv", FILE_WRITE);
if (fd) {
fd.print(uptimeSec);
fd.print(";");
fd.print(ident[curIdent]);
fd.print(";");
fd.println(tempStr);
fd.close();
status = STATUS_OK;
} else {
status = STATUS_SD_WRITE_FAILED;
}
}
curIdent++;
if (curIdent >= sizeof(ident)) {
curIdent = 0;
}
delay(DELAY);
}