tried the code only the temperature u ask and here the serial monitor…quite many failed
Not very reliable.
Set Serial Monitor baud rate to 115200.
Add your WiFi and token details to the following sketch. I have hacked it so it can cope with bad readings.
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "";
char ssid[] = "";
char pass[] = "";
#define DHTPIN 0 // D3
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
float setpoint = 0;
bool relaystatuschanged = false;
const int relayPin = 5; // D1
float backuptemperature = 99.9; // set artificially high
BLYNK_WRITE(V0)// slider widget
{
setpoint = param.asFloat();
relaystatuschanged = true;
}
void sendSensor()
{
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(t))
{
Serial.println("Failed to read temperature!");
//return;
t = backuptemperature;
}
else
{
backuptemperature = t;
}
Blynk.virtualWrite(V5, t);
if((setpoint >= t) && (relaystatuschanged == true))
{
digitalWrite(relayPin, 0); // assuming relay is active LOW
relaystatuschanged = false;
}
if((setpoint >= t) && (relaystatuschanged == true))
{
digitalWrite(relayPin, 1); // assuming relay is active LOW
relaystatuschanged = false;
}
}
void setup()
{
pinMode(relayPin, OUTPUT);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
timer.setInterval(5000L, sendSensor); // Setup a function to be called every 5 seconds
}
void loop()
{
Blynk.run();
timer.run();
}
and the relay keep going ON
I coded it that way as a sign that there is a problem.
As long as it gets a good temperature reading the first time it reads the sensor it shouldn’t be a problem.
Let’s try a revised sketch without the relay connected.
// norelays.ino
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "";
char ssid[] = "";
char pass[] = "";
#define DHTPIN 0 // D3
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
float backuptemperature = 99.9; // set artificially high
BLYNK_WRITE(V0)// slider widget
{
setpoint = param.asFloat();
relaystatuschanged = true;
}
void sendSensor()
{
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(t))
{
Serial.println("Failed to read temperature!");
//return;
t = backuptemperature;
}
else
{
backuptemperature = t;
}
if(t != 99.9)
{
Blynk.virtualWrite(V5, t);
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
timer.setInterval(5000L, sendSensor); // Setup a function to be called every 5 seconds
}
void loop()
{
Blynk.run();
timer.run();
}
going ON and OFF or just staying ON?
it stays ON
OK this one compiles. Don’t forget to disconnect relay. I just want to see a screen full of good temperature readings.
// norelays.ino
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "";
char ssid[] = "";
char pass[] = "";
#define DHTPIN 0 // D3
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
float backuptemperature = 99.9; // set artificially high
void sendSensor()
{
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(t))
{
Serial.println("Failed to read temperature!");
//return;
t = backuptemperature;
}
else
{
backuptemperature = t;
}
if(t != 99.9)
{
Blynk.virtualWrite(V5, t);
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
timer.setInterval(5000L, sendSensor); // Setup a function to be called every 5 seconds
}
void loop()
{
Blynk.run();
timer.run();
}
Did you copy and paste my last sketch exactly and have you disconnected the relay?
I can’t understand why the DHT sensor is not giving any readings with Blynk.
yes i have copied exactly without any modification and i also had disconnect completely the relay. i also try changing the sensor with spare
One last try:
// norelayswithloop.ino
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "";
char ssid[] = "";
char pass[] = "";
#define DHTPIN 0 // D3
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
long lastreading;
float backuptemperature = 99.9; // set artificially high
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
lastreading = millis();
}
void loop()
{
Blynk.run();
if(lastreading >= millis() + 5000)
{
lastreading = millis();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(t))
{
Serial.println("Failed to read temperature!");
t = backuptemperature;
}
else
{
backuptemperature = t;
}
if(t != 99.9)
{
Blynk.virtualWrite(V5, t);
}
}
}
Any better if you change setup() to:
void setup()
{
Serial.begin(115200);
dht.begin();
lastreading = millis();
Blynk.begin(auth, ssid, pass);
}
still the same as above, nothing happen
Ok this “works”.
// norelayswithloop.ino
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
#define DHTPIN 14 //
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
int lastreading = millis();
float backuptemperature = 99.9; // set artificially high
void setup()
{
dht.begin();
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
if ((millis() - lastreading) >= 5000)
{
lastreading = millis();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(t))
{
Serial.println("Failed to read temperature!");
t = backuptemperature;
}
else
{
backuptemperature = t;
}
if(t < 99)
{
Blynk.virtualWrite(V5, t);
}
}
Blynk.run();
}
Do you still have Serial Monitor set as 115200 baud?
It runs here for me (without a DHT11).
if i change to 115200 baud the serial monitor would go blank without any word