Ein freundliches Hallo von Haus zu Haus,
Ich habe in 2017 zusammen mit jemanden ein Sketch geschrieben, um Temperaturdaten eines Sensors auf eine SD-Karte in bestimmten Zeitabständen zu schreiben und zusätzlich auf einer Segment-Anzeige aus zu geben. Das Läuft soweit auch echt super, seit Jahren!
Jetzt benötige ich aber die Teile doch noch mal und würde nun gerne eine Temperatur Anpassung im Code vornehmen, da zwischen Außentemperatur und der Temperatur im Gehäuse inneren ein unterschied von 2.0 Grad Celsius ist.
Könntet ihr mir bitte dabei helfen, wo ich im Code etwas einpflanzen muss, damit die um 2 Grad korrigierte Temperatur angezeigt und Aufgezeichnet wird?
#include <SD.h>
#include <Wire.h>
#include <SPI.h>
#include <OneWire.h>
#include <Arduino.h>
#include <DallasTemperature.h>
#include <TM1637Display.h>
#include "uRTCLib.h"
// some definitions
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// Port definitions
#define redLEDpin 4 // Red LED
#define greenLEDpin 5 // Green LED
#define blueLEDpin 6 // Blue LED
#define CLK 2 // SCL 7-Segment Display Clock signal
#define DIO 3 // SDA 7-Segment Display Data
#define ONE_WIRE_BUS 7 // DS18B20 connected to Pin 7
const int chipSelect = 10; // for the data logging shield, we use digital pin 10 for the SD cs line
// init some general variables
uRTCLib RTC; // init RTC module DS3231
OneWire oneWire(ONE_WIRE_BUS); // init wire for temp sensor
DallasTemperature sensors(&oneWire); // add temp sensor
DeviceAddress insideThermometer = { 0x28, 0xEE, 0xA9, 0x9E, 0x1F, 0x16, 0x02, 0xFF }; //device address can be found out with sample sketch oneWireSearch
TM1637Display display(CLK, DIO); // init the 7-Segment display
// define some user values
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 30000 // mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 30000 // mills between calls to flush() - to write data to the card
#define BLINK_INTERVAL 2000 // mills how long the blue blink led is on or off - blink cycle 50% off, 50% on
long previousMillisLOG = 0; // time of last log run / data capture
long previousMillisSYNC = 0; // time of last sync - flush()
long previousMillisBLINK = 0; // time of last blink
int ledState = LOW; // ledState used to set the blue blink LED
File logFile; // the logging file
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(redLEDpin, HIGH);
while(1);
}
void setup(void)
{
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START
Serial.println(" ");
Serial.println("\t Outdoor Temperatur Logger");
Serial.println("\t by Stefan Förster (SRCD Center)");
Serial.println("\t developed for Berliner AR.Drone");
Serial.println(" ");
Serial.println("\t Code written by Stefan Förster, Dominikus Pfeiffer to 14.04.2017");
Serial.println(" ");
sensors.begin(); DEC; // Start up the library
sensors.setResolution(insideThermometer, 9);
// use debugging LEDs
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
pinMode(blueLEDpin, OUTPUT);
display.setBrightness(0x0f);
// initialize the SD card
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error(" SD card failed, or not present");
}
Serial.println(" SD card initialized.");
Serial.println();
String filename,filenumber;
// create a new file
filename = String();
filenumber = String();
for (uint16_t i = 0; i < 10000; i++) {
filenumber = String("0000") + i;
filename = String("LOG_" + filenumber.substring(filenumber.length() - 4) + ".TXT");
if (! SD.exists(filename))
{
// only open a new file if it doesn't exist
logFile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logFile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
Serial.println(" ");
logFile.println(" stamp \t\t datetime \t\t temp \t\t millis");
#if ECHO_TO_SERIAL
Serial.println(" stamp \t\t datetime \t\t temp \t\t millis");
#endif //ECHO_TO_SERIAL
}
void loop()
{
if(millis() - previousMillisBLINK > BLINK_INTERVAL) {
// save the last time you blinked the LED
previousMillisBLINK = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW){
ledState = HIGH;
}else{
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(blueLEDpin, ledState);
}
// check if we need to do a new reading
if(millis() - previousMillisLOG > LOG_INTERVAL) {
previousMillisLOG = millis();
// new sensor values capture starting
digitalWrite(greenLEDpin, HIGH);
// fetch the time
RTC.refresh();
// log time
logFile.print(" ");
if (RTC.day() < 10) {
logFile.print('0');
}
logFile.print(RTC.day(), DEC);
logFile.print(".");
if (RTC.month() < 10) {
logFile.print('0');
}
logFile.print(RTC.month(), DEC);
logFile.print(".");
logFile.print(RTC.year(), DEC);
logFile.print(" ");
if (RTC.hour() < 10) {
logFile.print('0');
}
logFile.print(RTC.hour(), DEC);
logFile.print(":");
if (RTC.minute() < 10) {
logFile.print('0');
}
logFile.print(RTC.minute(), DEC);
logFile.print(":");
if (RTC.second() < 10) {
logFile.print('0');
}
logFile.print(RTC.second(), DEC);
sensors.requestTemperatures();
float tempinsideThermometer = sensors.getTempC(insideThermometer); //damit wird die Temperatur an tempinsideThermometer gegeben
if (tempinsideThermometer == -127.00) {
Serial.print("Error getting temperature");
display.showNumberDec(8888);
}
else {
display.showNumberDecEx(tempinsideThermometer*100, (0x80 >> 1), false);
}
logFile.print("\t\t ");
logFile.print(tempinsideThermometer);
logFile.print(" ");
logFile.print("C");
// log milliseconds since starting
uint32_t m = millis();
logFile.print("\t ");
logFile.print(m); // milliseconds since start
logFile.println(" ");
#if ECHO_TO_SERIAL
Serial.print(" ");
if (RTC.day() < 10) {
Serial.print('0');
}
Serial.print(RTC.day(), DEC);
Serial.print('.');
if (RTC.month() < 10) {
Serial.print('0');
}
Serial.print(RTC.month(), DEC);
Serial.print('.');
Serial.print(RTC.year(), DEC);
Serial.print(' ');
if (RTC.hour() < 10) {
Serial.print('0');
}
Serial.print(RTC.hour(), DEC);
Serial.print(':');
if (RTC.minute() < 10) {
Serial.print('0');
}
Serial.print(RTC.minute(), DEC);
Serial.print(':');
if (RTC.second() < 10) {
Serial.print('0');
}
Serial.print(RTC.second(), DEC);
Serial.print(" Uhr CEST");
Serial.print("\t\t ");
Serial.print(tempinsideThermometer);
Serial.print(" ");
Serial.print("C\t ");
Serial.print(m); // milliseconds since start
Serial.println(" ");
#endif //ECHO_TO_SERIAL
digitalWrite(greenLEDpin, LOW);
}
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - previousMillisSYNC) > SYNC_INTERVAL) {
previousMillisSYNC = millis();
// blink LED to show we are syncing data to the card & updating FAT!
digitalWrite(redLEDpin, HIGH);
logFile.flush();
digitalWrite(redLEDpin, LOW);
}
}
Mehr zum Temperatur Sensor Projekt findet ihr auch auf meinem Thingiverse Kanal unter https://www.thingiverse.com/thing:2344266
Besten Gruß
Stefan