Simple Timers vs millis()

I am trying to merge my project to function with Blynk, one circuit at a time as I am new to both Arduino, and more so to Blynk. That said, I am wanting to start with just my Arduino Mega w/Ethernet and a single DHT22 sensor for temp and humidity data. I would like the Blynk sketch to push the sensor data to the app once every 10 seconds while still being able to power cycle LEDs when an app widget is ticked. I found THIS thread here in the forum that is a lot like mine, but way more technically involved, so I copied the user’s sketch and modified it as best I can for what I’m trying to accomplish.

My current holdup is that I am relatively new to code for Arduino and my only knowledge of time related things in code are when using a physical RTC. Now that I am into Blynk, and with all the reading I’ve done strongly suggests that I need to get rid of my delays and use Simple Timers. A few users on Arduino Forums convey that it is better to use mllis() based commands as millis() does not require a library and use less space in the sketch.

Another related issue is that I don’t know how to call the function for the desired length of time it needs to take a reading, then take another reading 10 seconds from the previous reading, all while Blynk remains open to see if any widgets in the app have changed states. The Blink Without Delay sketch only shows me how to do something for an equal amount of on and off time. I think the DHT only needs a brief moment to capture a reading, and a few seconds in between to ensure those readings are stable. Plus also, I don’t wish to overburden the server. Can someone help to shed some light for me? Thank you all in advance!

    #include <SPI.h>
    #include <Ethernet.h>
    #include <BlynkSimpleEthernet.h>
    #include <Wire.h>                 //Wire Library for I2C LCD
    #include "DHT.h"                //Humidity Library
    #include <SimpleTimer.h>          //Timer Rotation Library


    //**********DEFINE PINS****************************
    #define DHTPIN A2                                
    #define DHTTYPE DHT22

    DHT dht(DHTPIN, DHTTYPE);

    char auth[] = "XXXXXXXXXXXXXxxxxxxxxxxXXXXXXXXX";

    byte h;  //  Originally, DHT variables were declared as floats to return two decimals,
    byte f;  //  but decimal values are not needed to manage a grow room, and byte takes less space

                
    WidgetTerminal terminal(V1);
    //SimpleTimer sensortimer;


    void setup() {
      Serial.begin(9600);            //Start Serial Port
      Blynk.begin(auth);
      pinMode(DHTPIN, OUTPUT);           //Defines as Output
      dht.begin();
    }


    void loop() {
  
      Blynk.run();
    }

    void climateCheck(){
      h = dht.readHumidity();
      f = dht.readTemperature(true);

    Blynk.virtualWrite(V1, f);
    Blynk.virtualWrite(V2, h);
    
  }

Hello. Did you see our basic Push example? It does what you need. To be more specific :

#include <SimpleTimer.h>
SimpleTimer timer;
void setup() {
   ...
   timer.setInterval(10000L, climateCheck);
   ...
}

Thank you Dmitriy, I just went through the push sketch and got the values on my app every second, but my lack of coding skills left me hanging at that point I guess. Your included examples I believe got me a little further in that the sketch compiled and the app connected, but no values were received in the app with gauge widgets listening to V1 for Fahrenheit and V2 for humidity. Here is my updated sketch.

`
#include “SPI.h”
#include “Ethernet.h”
#include “BlynkSimpleEthernet.h”
#include “Wire.h” //Wire Library for I2C LCD
#include “DHT.h” //Humidity Library
#include “SimpleTimer.h” //Timer Rotation Library

//**********DEFINE PINS****************************
#define DHTPIN A2                                
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

char auth[] = "XYZ";

byte humidity;  //  Originally, DHT variables were declared as floats to return two decimals,
byte fahrenheit;  //  but decimal values are not needed to manage a grow room, and byte takes less space

            
//WidgetTerminal terminal(V1);
SimpleTimer timer;


void setup() {
  Serial.begin(9600);            //Start Serial Port
  Blynk.begin(auth);
  pinMode(DHTPIN, OUTPUT);           //Defines as Output
  dht.begin();
  timer.setInterval(10000L, climateCheck);
}


void loop() 
{
  Blynk.run();
}

void climateCheck()
{
  humidity = dht.readHumidity();
  fahrenheit = dht.readTemperature(true);

Blynk.virtualWrite(V1, fahrenheit);
Blynk.virtualWrite(V2, humidity);

}

`

You don’t run timer. Should be

void loop() 
{
  Blynk.run();
  timer.run();
}

Also I think that type for your variables should be float and not byte.

I added timer.run(); back into the loop and switched my variables back to floats and still no push. I connected an LED to D30 to confirm a valid connection and it works just fine. The graph widgets (V1&V2) are both set to PUSH for their frequency as stated in the DOCs page.:disappointed:

In your sketch and on your hardware, change
#define DHTPIN A2 to a digital pin.
Also, add value display widget and reference V1 or V2. On the dht22 you may be getting NAN , and that may not appear on the graph widget.

Thank you Arduinewb! Your suggestions worked. I could have sworn that the instructions for the sensor said to connect to an analog pin, but after the writeup on adafruit’s site and also the datasheet, I see I was in err. Prior to getting into Blynk, the sketch I used ran the sensor out of A2 and worked just fine. None the less, thanks for getting me on track and also thanks for sharing your Blynkubator sketch, it will teach me all that I want to know. BTW, I’m building an automated indoor greenhouse. I will share my sketch when I get the other elements back in.

Thanks again!

1 Like