I’m a beginner in electronics. I’m working on a smart garden project that turn on/off led strips using the timer widget on blynk and measure temperature and humidity with the dht11 sensor, measure lux with the ldr sensor.The board that I’m using is a Nodemcu 1.0. Here is the code I wrote for the project
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
DHT dht(D3,DHT11);
BlynkTimer timer;
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
#include "FastLED.h"
#define PIN 6
#define NUM_LEDS 60
CRGBArray<NUM_LEDS>leds;
int n =0;
int sign =1;
unsigned int x=0;
unsigned int y=0;
float rV,ldrV,ldrR;
int lux;
WidgetTerminal terminal(V4);
BLYNK_WRITE(V3){
x = param.asInt();
}
BLYNK_WRITE(V2){
y = param.asInt();
}
void rgb(){
if(x==1){
if(y==1){
for(int i=0; i<60; i++){
if(i%2 == 0){
if(sign==1){
leds[i] = CRGB (75,0,130);
}
else{
leds[i] = CRGB(138,43,226);
}
}
else{
if(sign==1){
leds[i] = CRGB (0,0,139);
}
else{
leds[i] = CRGB(0,191,255);
}
}
sign = -sign;
FastLED.show();
delay(1);
}
}
if(y==2){
for(int i=0; i<60; i++){
leds[i] = CRGB (255,255,255);
}
FastLED.show();
delay(1);
}
if(y==3){
for(int i=0; i<60; i++){
if(i%2 == 0){
leds[i] = CRGB (255,0,0);
}
else{
leds[i] = CRGB (0,0,255);
}
FastLED.show();
delay(1);
}
}
}
else if(x==0){
for(int i= 0;i<60;i++){
leds[i] = CRGB(0,0,0);
}
FastLED.show();
delay(1);
}
}
void ldrm(){
int l = analogRead(A0);
rV = (float)l/1023 * 3.14;
ldrV = 3.14 - rV;
ldrR = ldrV/rV * 4660;
lux = 12518931*pow(ldrR,-1.405);
Blynk.virtualWrite(V5,lux);
}
void sendSensor(){
float h = dht.readHumidity();
float t = dht.readTemperature();
if(isnan(h) || isnan(t)){
return;
}
Blynk.virtualWrite(V0,t);
Blynk.virtualWrite(V1,h);
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
FastLED.addLeds<NEOPIXEL, PIN> (leds, NUM_LEDS);
pinMode(A0,INPUT);
terminal.clear();
while(Blynk.connect() == false){
}
terminal.println("Smart garden v0.1.1: ");
terminal.println("Connected");
terminal.flush();
dht.begin();
timer.setInterval(2000L,sendSensor);
timer.setInterval(3000L,ldrm);
timer.setInterval(60L,rgb);
}
void loop()
{
Blynk.run();
timer.run();
}
The problem that I’m having with is after a few seconds, the temperature, humidity and lux values on blynk weirdly jump to some random number, ie humidity jump to 95% and stay at that value. I’ve tried using the timer.setTimeout method, calling function 2 after function 1 is finished (with setInterval(time,function1) in the set up), but the same results happened. It would be great to get some help!