Help with code

Why will this while statement not work in a ESP8266 or Arduino?
While temp is less then 100, or timer is not more then 1 second, readtemp()

while(temp < 100) || (millis() < time_now + 1000) {
readtemp();
}

This is not really HA related, you’d get better luck on StackOverflow but here is my quick analysis anyway :slight_smile:

First, your while is malformed in my opinion, only the temp part is in the while, it should not compile.

millis() returns the number of milliseconds passed since the ESP8266 or Arduino board began running the current program.
Souldn’t your second part of the condition be (millis() < 1000) then? Or you want to run for 1 second every time?

I assume that this is not the full code, I hope that you’re doing something to initialise temp before the if.
If not, do it, as undefined/null variable compared to 100 is unpredictable. Or replace it with a do...while

Finally, if you want your loop to stop based on the temp value, it will be necessary to change it.

time_now = millis();
do {
  temp = readtemp();
} while(temp < 100 || millis()-time_now <= 1000);

EDIT:

error: expected primary-expression before '||' token
   while(temp < 100) || (millis() < time_now + 1000) {
                     ^
exit status 1
expected primary-expression before '||' token

Thanks for replying I’m not a member of Stackoverflow and its some what related as its a sensor feeding HA info.

All variables are initialized, I just didn’t include that as it would be a long post.

Yes it is. Im not using this variant of C++ it as often as I like or any lanaguage and know if it was a “if” statement it would work, as in
if (temp < 100) || temp > 200)

This is what i’m trying to do.

I’m trying to heat some water remotely. Once it reaches a certain temp it can shut off, but if it takes x amount of time then that means the water evaporated so low the sensor is not submerged. Maybe what I need to look into is a “if” statement which I may be able to do but thought this is what “while” was for.

What I would do, still missing a lot in the code but I leave that up to you to complete it:

int maxWaitingTime = 5; // Time to wait for water to heat, in seconds
int waitingTime = 1; // Time to wait before retrying the process, in seconds
float tempThreshold = 80.0; // Target temperature
unsigned long time_now = 0; // Internal use
float currentTemperature = 0; // Internal use
 
void setup() {
    Serial.begin(115200);
}

float getCurrentTemperature() {
  // Do something to get the temperature and return it
  // return ...
}
 
void loop() {
  Serial.println("Trying to heat water");
   
  time_now = millis(); // This will take the current time and use it to limit the heating period

  // Use a do...while as current temperature might be already above threshold
  do {
    currentTemperature = getCurrentTemperature();

    //wait for water to heat a bit
    if (currentTemperature < tempThreshold) delay(waitingTime *1000);

  } while(currentTemperature < tempThreshold || millis() < time_now + (maxWaitingTime * 1000));

  if (currentTemperature >= tempThreshold) {
    // Water is successfully heated
   
  } else {
    // Sensor is probably no longer submerged

  }
}

Caveat:
Be aware that there is nothing here to on/off the heating device.
Also, if the sensor is no longer submerged, you might want to send an alert or something and off the heating device.
All this is not in this code.

Additional info:
You can look after the MQTT client to send info to HA, you’ll use it by adding a library to your sketch

#include <PubSubClient.h>

EDIT: Water can’t reach 100°C/212°F except at see level or below. Do not put 100°C as target except if it is under pressure.

while(currentTemperature < tempThreshold || millis() < time_now + (maxWaitingTime * 1000));

Current temp and set threshold is being ignored, time portion is taking it out of the loop. If I remove time then temps are followed, looks like two conditions not allowed which is strange, every where I have looked says it can be done.

void runit2(){


  if(heatrequest == 1){
    
      time_now = millis(); // This will take the current time and use it to limit the heating period

  // Use a do...while as current temperature might be already above threshold
  do {
    sendtemp();
//client.publish("/botwarm/settemp/tempThreshold", String(tempThreshold).c_str(), false);
//client.publish("/botwarm/settemp/temp", String(temp).c_str(), false);
    //wait for water to heat a bit
    if (temp < tempThreshold) delay(waitingTime *1000);
        digitalWrite(relay, HIGH);
         client.publish("/botwarm/heat/status", "1", false);
         client.loop();
               if(heatrequest == 0){
                        digitalWrite(relay, LOW);
                        client.publish("/botwarm/heat", "0", false);
                          delay(50);
                        client.publish("/botwarm/heat/status", "0", false);
                          delay(50);
         return;
        }
 
 // } while(temp < tempThreshold || millis() < time_now + (maxWaitingTime * 1000));
    } while(temp < tempThreshold);         
    

    

  if (temp >= tempThreshold) {
    // Water is successfully heated

        heatrequest = 0;
        digitalWrite(relay, LOW);
        client.publish("/botwarm/heat", "0", false);
        client.publish("/botwarm/heat/status", "0", false);
        return;
     
  }
  }
  else {
        heatrequest = 0;
        digitalWrite(relay, LOW);
        client.publish("/botwarm/heat", "0", false);
        client.publish("/botwarm/heat/status", "0", false);
      
 } 
  }