[Solved] Temperature too low > LED on

Hi,
I’m setting up a temperature control with an ESP8266 and a DHT22. I want to receive a notification and an LED should be switched on when temperature is too low.
I could manage to get a notification by adding the notification and eventor widget (“When V10 is lower than 20, send notification: “temp too low”.”). Unfortunately when adding the event “When V10 is lower than 20,turn ON gpio5.” the LED connected to GPIO5 won’t be switched on.
When adding a button widget to turn on GPIO5 the LED will be switched on when pressing the button. So everything is wired correctly.

What am I doing wrong?

My code looks like this:

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * 
 *
 **************************************************************
 * 
 * 
 *
 * WARNING :
 * For this example you'll need SimpleTimer library:
 *   https://github.com/jfturcot/SimpleTimer
 * Visit this page for more information:
 *   http://playground.arduino.cc/Code/SimpleTimer
 *
 * 
 *   DHT22 ----gpio12
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 12 //pin gpio 12 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxx";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "xxxxxxxxx", "xxxxxxxxx"); //insert here your SSID and password
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendUptime);
}

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  Blynk.virtualWrite(10, t); // virtual pin 
  Blynk.virtualWrite(11, h); // virtual pin 
}

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

In the app, simply create a LED widget and assign a virtual pin to it. Called LEDPIN below.

Then add this code to your sendUptime() function.

  float t = dht.readTemperature();
  if(t < 20){
    Blynk.virtualWrite( LEDPIN ,255); // turn LED widget on full brightness (255)
  } else {
    Blynk.virtualWrite( LEDPIN ,0); // turn LED widget off
  }

You could also then make the 20 a variable and attach it to a slider widget so you can set the notification temp easily.

3 Likes

Sorry for a maybe noob question. If I understand you right with this code a virtual LED will be switched on. But I intend to switch on an LED which is physically connected to gpio5.
How can I do that? It doesn’t have to be managed by the app. Also I would be happy if this behaviour can be reached by changing my code.

in sendUptime() after reading the temperature:

if(t > x){
  digitalWrite(y, 1);
}
else{
   digitalWrite(y, 0);
}

Technically with your 10s timer you should check if t is in the same range as it was when you last checked to prevent needlessly sending an ON (or OFF) signal when the LED is already ON (or OFF) but it’s not essential on 10s intervals.

Thanks for the answers.
Now I wrote following:

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  Blynk.virtualWrite(10, t); // virtual pin 
  Blynk.virtualWrite(11, h); // virtual pin 
  if (isnan(t)) {
    return;
  }
  if (t < 20) {
    Blynk.notify(String("Frost im Gartenhaus : ") + t + String("°C"));
  }
  if(t < 20){
    digitalWrite(5, 1);
  }
  else{
    digitalWrite(5, 0);
  }
}

Now I get a message every 10 seconds not only once the event is reached.
Furthermore the LED isn’t switched on. When creating a button widget and switching the LED on and off by the button widget the LED will switch on when the temperature is too low. Might it be that gpio5 hast to be initialised in any way?

I’m an idiot. I forgot
pinMode(5, OUTPUT);

But I’m still getting a notification every 10 seconds.

Is your LED working now?

I’ll dig out an extra bit of code so you only get notifications when the temperatures moves in or out of the range rather than every 10s.

The LED is working indeed.
Thanks for the notification thing.

Good. Proper notifications:

Declare a global variable:

`bool temperatureMoved = true; // means it moved below 20C

mod your function to something like this:

  if ((t < 20) && (temperatureMoved == true)) {
    temperatureMoved = false;
    Blynk.notify(String("Frost im Gartenhaus : ") + t + String("°C"));
    digitalWrite(5, 1);
  }
  else{
    temperatureMoved = true;
    digitalWrite(5, 0);
  }

Unfortunately that doesn’t work either. I get a notification every 10 seconds and the LED switches on and off every 10 seconds. The LED should be switched on as long as the temperature is below 20.
Her my whole code, mabe there is a mistake
/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social networks: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
*
*
**************************************************************
*
*
*
* WARNING :
* For this example you’ll need SimpleTimer library:
* https://github.com/jfturcot/SimpleTimer
* Visit this page for more information:
* http://playground.arduino.cc/Code/SimpleTimer
*
*
* DHT22 ----gpio12
*
**************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 12 //pin gpio 12 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxx";  // Put your Auth Token here. (see Step 3 above)
bool temperatureMoved = true; // means it moved below 20C

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "xxxxxx", "xxxxxx"); //insert here your SSID and password
   pinMode(5, OUTPUT);
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendUptime);
}

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  Blynk.virtualWrite(10, t); // virtual pin 
  Blynk.virtualWrite(11, h); // virtual pin 

  if ((t < 20) && (temperatureMoved == true)) {
    temperatureMoved = false;
    Blynk.notify(String("Frost im Gartenhaus : ") + t + String("°C"));
    digitalWrite(5, 1);
  }
  else{
    temperatureMoved = true;
    digitalWrite(5, 0);
  }
}

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

When adding
if (isnan(t)) {
return;
}
I loose my values every 10 seconds.

you still need to include this but it should be before the 2 virtualWrite commands.

I see what the problem is. It is the else statement, just give me a minute.

if you change the else statement to the following you should be good to go:

   if ((t >= 20) && (temperatureMoved == false)) {
    temperatureMoved = true;
    digitalWrite(5, 0);
  }

temperatureMoved is perhaps not a very good variable name under the circumstances but it should do the job.

1 Like

Finally it works.
Thank you so much for your help and your effort.
I’m going to try some other things in the near future.