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);
}