Sauna control with Arduino Doesn't work

I'm still relatively new to C++ and my father would like me to program a control for our sauna but the relays don't switch.
This is my current code:

#include <Arduino.h>
#include <WiFi.h>
#include <DHT.h>
#include <iostream>

float solltemp = 25.0; // Standardwert für Solltemperatur
bool light = false;

// Wi-Fi-Anmeldedaten


// Relais Pins für Ofen und Licht
const int ofenPin1 = 7;  // Relais für Ofen 1
const int ofenPin2 = 6;  // Relais für Ofen 2
const int ofenPin3 = 0;  // Relais für Ofen 3
const int lichtPin = 1;  // Relais für Licht

// DHT-Sensor
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Cloud-Variablen
bool ofen1 = false;
bool ofen2 = false;
bool ofen3 = false;

// Hysterese-Temperaturwerte
float T_ein = solltemp - 5;  // Temperatur, bei der der Ofen eingeschaltet wird
float T_aus = solltemp + 2;  // Temperatur, bei der der Ofen ausgeschaltet wird
float currentTemp = 0.0;  // Aktuelle Temperatur

void setup() {
  // Serielle Kommunikation starten
  Serial.begin(115200);

  // Wi-Fi-Verbindung herstellen
  Serial.println("\nVerbunden mit Wi-Fi!");

  // DHT-Sensor initialisieren
  dht.begin();

  // Relais-Pins konfigurieren
  pinMode(ofenPin1, OUTPUT);
  pinMode(ofenPin2, OUTPUT);
  pinMode(ofenPin3, OUTPUT);
  pinMode(lichtPin, OUTPUT);

  // Relais ausschalten (Relais sind normalerweise LOW aktiv)
  digitalWrite(ofenPin1, LOW);
  digitalWrite(ofenPin2, LOW);
  digitalWrite(ofenPin3, LOW);
  digitalWrite(lichtPin, LOW);

  Serial.println("Setup abgeschlossen!");
}

void loop() {
  // Temperaturmessung
  currentTemp = dht.readTemperature();
  if (isnan(currentTemp)) {
    Serial.println("Fehler beim Lesen des Sensors!");
    delay(2000);
    return;
  }

  // Ofensteuerung basierend auf Temperatur
  if (currentTemp < T_ein) {
    digitalWrite(ofenPin1, HIGH);  // Ofen einschalten
    digitalWrite(ofenPin2, HIGH);
    digitalWrite(ofenPin3, HIGH);
    ofen1 = true;
  } else if (currentTemp > T_aus) {
    digitalWrite(ofenPin1, LOW);  // Ofen ausschalten
    digitalWrite(ofenPin2, LOW);
    digitalWrite(ofenPin3, LOW);
    ofen1 = false;
  }

  // Lichtsteuerung
  if (light) {
    digitalWrite(lichtPin, HIGH);
  } else {
    digitalWrite(lichtPin, LOW);
  }

  // Debug-Ausgabe
  Serial.print("Temperatur: ");
  Serial.print(currentTemp);
  Serial.println(" °C");

  delay(1000);  // Wartezeit für nächste Schleife
}

void onSolltempChange() {
  T_ein = solltemp - 5;
  T_aus = solltemp + 2;
  Serial.println("Solltemperatur geändert!");
}

void onLightChange() {
  Serial.println("Lichtzustand geändert!");
}

void onTemperatureChange() {
  Serial.println("Temperatur geändert!");
}

void onOven1Change() {
  Serial.println("Ofen 1 Zustand geändert!");
  digitalWrite(ofenPin1, ofen1 ? HIGH : LOW);
}

void onOven2Change() {
  Serial.println("Ofen 2 Zustand geändert!");
  digitalWrite(ofenPin2, ofen2 ? HIGH : LOW);
}

void onOven3Change() {
  Serial.println("Ofen 3 Zustand geändert!");
  digitalWrite(ofenPin3, ofen3 ? HIGH : LOW);
}


/*
  Since CurrentTemp is READ_WRITE variable, onCurrentTempChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onCurrentTempChange()  {
  // Add your code here to act upon CurrentTemp change
}

Where's the
#include <sauna.h>

Just kidding, do you got schematics of the hardware, also what hardware is being used?

there seems to be lots of unnecessary code in what you shared.

if you want to test the temperature control then focus only on just that.

Also make sure you drive your relays appropriately (power, protection, ...)

#include <DHT.h>

// Hysterese-Temperaturwerte
const float solltemp = 25.0; // Standardwert für Solltemperatur
const float T_ein = solltemp - 5;  // Temperatur, bei der der Ofen eingeschaltet wird
const float T_aus = solltemp + 2;  // Temperatur, bei der der Ofen ausgeschaltet wird

// Relais Pins für Ofen und Licht
const int ofenPin1 = 7;  // Relais für Ofen 1
const int ofenPin2 = 6;  // Relais für Ofen 2
const int ofenPin3 = 0;  // Relais für Ofen 3

// DHT-Sensor
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
  
  // Relais-Pins konfigurieren
  pinMode(ofenPin1, OUTPUT);
  pinMode(ofenPin2, OUTPUT);
  pinMode(ofenPin3, OUTPUT);
  
  digitalWrite(ofenPin1, LOW);
  digitalWrite(ofenPin2, LOW);
  digitalWrite(ofenPin3, LOW);
   
  Serial.println("Setup abgeschlossen!");
}

void loop() {
  float currentTemp = dht.readTemperature();
  if (isnan(currentTemp)) {
    Serial.println("Fehler beim Lesen des Sensors!");
    delay(2000);
    return;
  }

  // Ofensteuerung basierend auf Temperatur
  if (currentTemp < T_ein) {
    digitalWrite(ofenPin1, HIGH);  // Ofen einschalten
    digitalWrite(ofenPin2, HIGH);
    digitalWrite(ofenPin3, HIGH);
  } else if (currentTemp > T_aus) {
    digitalWrite(ofenPin1, LOW);  // Ofen ausschalten
    digitalWrite(ofenPin2, LOW);
    digitalWrite(ofenPin3, LOW);
  }

  delay(1000);  // Wartezeit für nächste Schleife
}

the code seems meant to be used with Arduino cloud or some other cloud system ➜ which one do you use ?

i use the Arduino Cloud

OK

so what does not work ? the cloud element or the "Hysterese-Temperaturwerte" and relay control ?

how are things wired, powered?

Hi @felixrein2012!

you are using Serial and at the same time pins 0 and 1: could you post which board you are using?

ec2021

good point

@felixrein2012 posted in the GIGA R1 WiFi category, so I assumed this is what he used.

pin #1 is indeed Tx as well and pin 0 is Rx on that board (as for many other Arduinos) but I wonder if Serial is not the USB-C connector. I don't have that board, so I don't know.

EDIT: just checked on the doc, pin 0 and 1 are for Serial1 so as this is not used in the code, those pins can be used likely as normal digital pins.

Serial.begin(9600);  //initialize serial communication over USB
Serial1.begin(9600); //initialize serial communication on RX0/TX0
Serial2.begin(9600); //initialize serial communication on RX1/TX1
Serial3.begin(9600); //initialize serial communication on RX2/TX2
Serial4.begin(9600); //initialize serial communication on RX3/TX3
1 Like

Thanks for your analysis!

In the meantime I tested the code as it is in a Wokwi project. The logic in loop() works (as one would expect from just reading).

However there is a lot of code missing. It's quite likely that the problem with the relays is in that part.

@felixrein2012

Please consider to post the complete code (just replace personal "secrets" like SSID, passwords etc.).

I would generally recommend to rewrite the sketch in a way that it uses the millis() function instead of delay(). That will allow to improve the responsiveness of your code!