BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

timer.setInterval

esp8266

#1

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!


#2

Comment out this timer and test… if you still have an issue, then continue to troubleshoot from there, if no further issue then you simply are running too much too fast, and you can focus your troubleshooting in that direction.


#3

BTW both these functions will try to run at exactly the same time every 6 seconds… try spacing out the timing into non-consecutive ranges.


#4

timer.setInterval(2000L,sendSensor);

That is way to often! Don’t really understand why people insists on (near) real time data when they check the (in this case temperature) on their phones like once every hour :wink: At least, set it to 5000, or even better 600000 :stuck_out_tongue:

timer.setInterval(60L,rgb);

What ever it does, 60 ms is a very short time! I would amp up that too.


#5

This has nothing to do with your issue, however, may I suggest you use meaningful variable names rather than x, y and n … especially global variables. Trivial variable names make the code hard to read.

A trivial local variable name is generally acceptable. We can infer ‘i’ in this case is some form of counter,

for(int i=0; i<60; i++)

although, we can’t easily determine what you’re counting.

You don’t have a single comment in your entire sketch. Comments are extremely helpful, especially when you’re asking someone else to review your code,

/*
 * (block comment)
 */
// (single line comment)

Meaningful variable names are a form of comments.

For what it’s worth …

Joe