1
\$\begingroup\$

I'm a beginner and I'm working on a project to turn on and off an LED using a phone app. I'm using DOIT ESP32 Devkit V1 which I control via Wifi using Blynk app, jumpers, and an LED.

At first I wrote code to turn on and off the LED when I press a button in the app, and it worked. After that, I added some more code to display the internal temperature of the board (I don't need it to be very accurate) and at last, I've added some more code to turn off the lamp if the temperature is between 67 - 69°C and to display a message "OVERHEATED" and then turn it on again, and display a message "NO MESSAGES" when the temp is above 69°.

The problem is that when the temp shows between 67 - 69°C the LED is turned off and the message is displayed correctly, but when it goes above 70°C, the LED won't turn on although the message is showed correctly. my code:

#define BLYNK_PRINT Serial
#define RELAY_PIN 32
#define TEMP_PIN V5
#define MSG_PIN V6

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "...";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "...";
char pass[] = "...";

#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();

BlynkTimer timer;

void setup() {  
  pinMode(RELAY_PIN, OUTPUT); 
  pinMode(RELAY_PIN, HIGH);
  
  Serial.begin(115200);
  WiFi.begin(ssid, pass);

  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected"); 

  Blynk.begin("...", ssid, pass); 

  timer.setInterval(1000L, myTimerEvent);
}

void myTimerEvent() {
  bool was_overheated = false;
  Blynk.virtualWrite(TEMP_PIN, (temprature_sens_read() - 32) / 1.8);
  if (((temprature_sens_read() - 32) / 1.8) > 69) {
    Blynk.virtualWrite(MSG_PIN, "NO MESSAGES");
    if (was_overheated == true) {
      digitalWrite(RELAY_PIN, HIGH);
      was_overheated = false;
    }
  }

  if (((temprature_sens_read() - 32) / 1.8) > 67 && ((temprature_sens_read() - 32) / 1.8 <= 69) && (was_overheated == false)) {
      was_overheated = true;
      digitalWrite(RELAY_PIN, LOW);
      Blynk.virtualWrite(MSG_PIN, "OVERHEATED!!");       
  }
}

void loop() {
  Blynk.run();
  timer.run();
}

What could be the problem?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You initialise was_overheated to false every time at at the beginning of myTimerEvent(), and only set the RELAY_PIN to HIGH if was_overheated is true, without changing was_overheated inbetween.

Declaring was_overheated as a global variable and initialising it once should help, or you could make it static.

As it is now, it is being set to false every time myTimerEvent() is called.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.