Hi, I am building a project based on Blynk. Blynk is a free downloadable app for Android or IOS that links your smartphone to your popular microcontroller (Arduino, RPi, Sparkcore etc). At it's basic level, Blynk sends commands from the phone and does something on the MCU in real time like change a pin state to HIGH if a button is ticked on the smartphone.
I was able to locate a few different projects where people are also adding other more traditional Arduino sketches to work in conjunction with Blynk, so I have some good materials to study to further expand my project, but just to get my feet into the pool, I want my sketch to of course launch Blynk, but also to query a DHT temp/humidity sensor every 10 seconds or so. I have what I believe to be a good draft of the sketch, but I am nearly clueless when it comes to anything based on millis(). The docs section of the Blynk website states that using delay() causes problems. Being relatively new to coding, of course I don't want to make problems for myself. Many, if not most Blynk sketches use the Simple Timers library, but my studies in this forum also indicate that millis() based operations may also work, and possibly use less space as millis() doesn't require the use of a library.
Here is my code thus far
#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);
}
Blynk.run needs to be in the loop() by itself to function properly, and any added code needs to exist in functions outside of the loop(). Can someone augment my code to demonstrate how millis() or Simple Timers are used to call a function every 10 seconds or so? I went through a few basic tutorials, blink w/o delay etc, but am having a tough time reapplying the understanding. I feel if someone can do it in my code, I will be able to comprehend it better and apply the skill into other functions that I will add in once this most basic concept is understood.
but I am nearly clueless when it comes to anything based on millis().
I REALLY can't understand this. It is dirt simple to ask "Is it time to do something?". The answer, of course, depends on what time it is now (millis(), to the rescue) and when the thing needs doing (based on when it was done last, again using millis() to record this).
The blink without delay example shows ALL the steps necessary.
@ HeliBob, thanks for your effort. I tried to include the code you shared, but the sketch wouldn't compile. So I tried to put that block outside of the loop and still nothing. I think I am missing something.
@ PaulS, also, thank you for your thoughts. I tried the blink w/o delay sketch again and when I adjusted the 1000ms interval to 10,000, it rendered the LED on for 10secs and off for 10secs. I don't know how long the DHT needs to get a reading, but it does require the interval time in between readings which is what I think I am trying to accomplish. Also, being new to coding in general, but extremely new to using millis() based events, I find it a bit confusing to better comprehend as nearly all variables involve the word "millis" in their naming. nowMillis, previousMillis, currentMillis, nextWeekMillis, exaggerated for effect. My point is that though it may be easy to comprehend once I get the hang of it, I still find myself thinking that a few of the provided examples are a bit overwhelming for newbs like me. I'll just keep staring and reading through the code till I get my Eureka moment. If you can think of any other simplified ways to help me get this, or have links to other study materials, I would greatly appreciate it, and once again, thank you for your replies.
I don't know how long the DHT needs to get a reading, but it does require the interval time in between readings which is what I think I am trying to accomplish.
The sensor takes at 750 milliseconds. But, it is unlikely that you need to take a reading once a second. If the temperature you are measuring changes often enough that you need to read once a second, you are using the wrong kind of temperature sensor.
If you want to take a reading once every 30 seconds, you (should now) know how to do that.
I find it a bit confusing to better comprehend as nearly all variables involve the word "millis" in their naming.
Yeah, I hate that to. I never use millis in the name. I use names that make sense; you should too.
Suppose that the code used names like now, lastTempReadingTime, and timeBetweenReadings. Could you figure out how to apply the example?
Here are the 2 ways I tried adding your code into my 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);
unsigned long currentTime;
unsigned long lastRunTime;
unsigned long interval = 10000;
char auth[] = "XXXYYYZZZ";
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() // I tried it this way first as your example had Blynk.run(); included
{
currentTime = millis();
if (currentTime - lastRunTime >= interval)
{
lastRunTime = currentTime;
someFunction();
}
Blynk.run();
}
void climateCheck()
{
h = dht.readHumidity();
f = dht.readTemperature(true);
Blynk.virtualWrite(V1, f);
Blynk.virtualWrite(V2, h);
}
and I also tried my best to paste your code outside of void loop()
#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[] = "111112222223333333444444455555555";
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
unsigned long currentTime;
unsigned long lastRunTime;
unsigned long interval = 10000;
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);
}
{
currentTime = millis();
if (currentTime - lastRunTime >= interval)
{
lastRunTime = currentTime;
someFunction();
}
Both times, I posted the 3 unsigned longs above setup as global variables. The 2nd attempt I just generically posted the loop code minus Blynk.run() and still the compiler failed.
Throughout the day, I was also chasing leads to get my issue fixed on Blynk's forums and though I still haven't discovered the difference between Simple Timers and millis();, someone helped me debug my code AND circuit and now it all works. My sketch now pushes data every 10 seconds while keeping an open connection to toggle an LED on another pin.
The only real difference between the working code using the SimpleTimer library and my example using millis() seems to be that my code calls a non existent example function (someFunction()) whilst yours calls the climateCheck() function which exists.
Try my code but call climateCheck() instead of someFunction()