What is wrong with the code below.
It gives right readings only when fan is running.
In the first place I thought it was because of the missing delay, which I added first to 250, now 500. Still not working.
When fan stops, reading is 54612 in Blynk and same in serial monitor.
If I use simple example code from BH1750FVI, example, all works fine.
Do not understand.
#include <BlynkSimpleEsp8266.h>
#include "DHTesp.h"
#include "BH1750FVI.h"
char ssid[] = "X";
char pass[] = "Y";
char auth[] = "Z";
DHTesp dht;
BH1750FVI LightSensor(BH1750FVI::k_DevModeContLowRes);
float h;
float t;
uint16_t lux;
boolean fanOn = false;
void setup() {
Serial.begin(9600);
LightSensor.begin();
Blynk.begin(auth, ssid, pass);
dht.setup(D4, DHTesp::DHT22);
pinMode(D1, OUTPUT);
digitalWrite(D1, HIGH);
Blynk.virtualWrite(V8, "Off");
}
void loop() {
Blynk.run();
h = dht.getHumidity();
t = dht.getTemperature();
lux = LightSensor.GetLightIntensity();
Serial.print("{\"lux\": ");
Serial.print(lux);
Serial.print(", \"temp\": ");
Serial.print(t);
Serial.print("}\n");
Blynk.virtualWrite(V5, t);
Blynk.virtualWrite(V6, h);
Blynk.virtualWrite(V1, lux);
delay(500);
if (h > 50)
{
fanOn = true;
Blynk.virtualWrite(V8, "On");
}
if (fanOn)
{
tuuleta();
}
}
void tuuleta()
{
digitalWrite(D1, LOW); // start fan. Low trigger relay = LOW starts fan
if (h < 40)
{
fanOn = false;
Blynk.virtualWrite(V8, "Off");
digitalWrite(D1, HIGH);
}
}
int ambient()
{
// Not in use yet
}