Hi all,
this evening I encountered something I couldn’t fix, and now that I’ve fixed it, I don’t understand the problem.
The code below is a very simple integration of a DHT22 sensor on a NodeMCU writing a humidity and a temperature value to Blynk every 5 seconds.
For visual feedback and debugging I temporarily added a physical LED to the NodeMCU. At first I set the interval to 2 seconds, and I noticed that about every 20 seconds there were three spikes in the reading for both temp and humidity: 255 (the temp and hum were read as byte), normal, 255, normal, 255, normal, and then normal for 20 seconds, three spikes, normal for 20 sec, etc.
I then changed the interval to 5 seconds, which led to a single spike in the reading, still about every 20sec.
I changed the type for the readings from byte, to int (reading 2,147,483,647), to float (reading NaN), which changed the values but not the pattern of a misreading every 20 seconds.
Then I commented out all the stuff relating to the LED code, and that solved the problem.
But why?
Edit: after I added some code for a soil moisture sensor the problem returned, and it remained after I commented out the code that I added.
#define BLYNK_PRINT Serial
// Include DHT22 sensor library
#include <DHT.h>
#include <DHT_U.h>
// Include Blynk libraries
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// Get Auth Token in the Blynk App Project Settings
char auth[] = "*removed*";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*removed*";
char pass[] = "*removed*";
//Define variables
DHT dhtA(5, DHT22);
BlynkTimer timer;
//int ledState = LOW;
//int lastLedState = HIGH;
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dhtA.begin();
timer.setInterval(5000L, climateRoutine);
//pinMode(4, OUTPUT);
}
void loop()
{
Blynk.run();
timer.run();
}
void climateRoutine() {
float h1 = dhtA.readHumidity();
float t1 = dhtA.readTemperature();
Blynk.virtualWrite(V0, h1);
Blynk.virtualWrite(V1, t1);
// //Write an LED every time data is sent, remove in final build
// ledState = digitalRead(4);
// if (ledState != lastLedState) {
// if (ledState == HIGH) {
// digitalWrite (4, LOW);
// lastLedState = HIGH;
// } else {
// digitalWrite (4, HIGH);
// lastLedState = LOW;
// }
// }
}