Hey guys,
I just got my ESP8266. I set up the DHT11 Sensor and got the Code ready. But now i want to have an LED Displaying the Status of the Sensor example Sensor is not working set the Led to Red. But unfortunately it´s not working properly.
Sry for my bad english but it´s not my main language.
Your Help would be appreciated.
My Code:
/**************************************************************
* 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.
*
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
* This example shows how value can be pushed from Arduino to
* the Blynk App.
*
* WARNING :
* For this example you'll need SimpleTimer library:
* https://github.com/jfturcot/SimpleTimer
* and Adafruit DHT sensor library:
* https://github.com/adafruit/DHT-sensor-library
*
* App project setup:
* Value Display widget attached to V5
* Value Display widget attached to V6
*
**************************************************************/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "f82eedd4thatsnotmyauth:)4ccf35e3d";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxx";
char pass[] = "xxxxxxxxxx";
WidgetLED led1(V1);
BlynkTimer timer;
bool ledStatus = false;
#define BLYNK_GREEN "#23C48E"
#define BLYNK_RED "#D3435C"
#define DHTPIN 2 // What digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
DHT dht(DHTPIN, DHTTYPE);
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
void blinkLedWidget()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
led1.setColor(BLYNK_RED);
Serial.println("Led on V1: red");
ledStatus = true;
} else {
led1.setColor(BLYNK_GREEN);
Serial.println("LED on V1: green");
ledStatus = false;
}
}
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth, ssid, pass);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
}
And I can’t find any call to the blinkLedWidget() function.
You must insert another timer to do it:
timer.setInterval(1000L, blinkLedWidget);
But you could just use the same sendSensor function to check the availability of the sensor data and change the led color accordingly
@J4ck I may be mistaken, but I do not see your blinkLedWidget() function being called anywhere. You need to add a timer, timer.setInterval(1000L, blinkLedWidget);
Or, you could eliminate this blinkLedWidget() function and just add the color changing command to the first IF statement, since it is the same conditions, if (isnan(h) || isnan(t)).
void sendSensor(){
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Blynk.setProperty(V1, "color", BLYNK_RED);
Serial.println("Failed to read from DHT sensor!");
Serial.println("Led on V1: red");
ledStatus = true;
} else {
Blynk.setProperty(V1, "color", BLYNK_GREEN);
Serial.println("Received reading from DHT sensor!");
Serial.println("LED on V1: green");
ledStatus = false;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
Yes you will need to include the #define for the colors you wish to use.
As for the timer.setInterval(1000L, blinkLedWidget); it should go right before or after timer.setInterval(1000L, blinkLedWidget); in your setup function.
It´s kinda working now, but it´s switching between Received Reading and Failed to Read
it displays this in serial monitor
Failed to read from DHT sensor!
Led on V1: red
Failed to read from DHT sensor!
Led on V1: red
Received reading from DHT sensor!
LED on V1: green
Received reading from DHT sensor!
Everything is working now but still switching between states some times it stays then it switches to failed again can´t see any patterns
/**************************************************************
* 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.
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
* This example shows how value can be pushed from Arduino to
* the Blynk App.
*
* WARNING :
* For this example you'll need SimpleTimer library:
* https://github.com/jfturcot/SimpleTimer
* and Adafruit DHT sensor library:
* https://github.com/adafruit/DHT-sensor-library
*
* App project setup:
* Value Display widget attached to V5
* Value Display widget attached to V6
*
**************************************************************/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
WidgetLED led1(V1);
BlynkTimer timer;
bool ledStatus = false;
#define BLYNK_GREEN "#23C48E"
#define BLYNK_RED "#D3435C"
#define DHTPIN 2 // What digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
DHT dht(DHTPIN, DHTTYPE);
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor(){
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
led1.on(); //turn on led
if (isnan(h) || isnan(t)) {
Blynk.setProperty(V1, "color", BLYNK_RED);
Serial.println("Failed to read from DHT sensor!");
Serial.println("Led on V1: red");
ledStatus = true;
} else {
Blynk.setProperty(V1, "color", BLYNK_GREEN);
Serial.println("Received reading from DHT sensor!");
Serial.println("LED on V1: green");
ledStatus = false;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth, ssid, pass);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
}