Thanks for help! I just needed that. For now will be good the easy way, but soon I will rewrite all code to more readable and better functioning.
Now I managed with help mostly everything, but I would like to input time for countdown with something like Time Input in HH:mm, instead of slider.
I have also problems with timer counting down, because time goes too slow, in two seconds it takes off only about one. I realize that it maybe has to do with the delay in loop. How can I fix that?
I used this timer code: [SOLVED] How to countdown?
#include <SimpleTimer.h>
#include <BlynkSimpleShieldEsp8266.h>
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <DHT.h>
int rele1 = 3;
int rele2 = 4;
int rele3 = 5;
int rele4 = 6;
char auth[] = "xxxxxxxxxxxxxxxx";
char ssid[] = "xxxx";
char pass[] = "xxx";
SimpleTimer timer;
int CountdownRemainReset;
int CountdownRemain;
int CountdownTimer;
#define aref_voltage 3.3
#define THERMISTORPIN A0
#define THERMISTORNOMINAL 10000
#define TEMPERATURENOMINAL 25
#define NUMSAMPLES 5
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000
uint16_t samples[NUMSAMPLES];
#define ESP8266_BAUD 115200
ESP8266 wifi(&Serial);
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h = dht.readHumidity();
float t = dht.readTemperature();
void sendSensor()
{
h = dht.readHumidity();
t = dht.readTemperature();
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
byte TempGretja;
BLYNK_WRITE(V0)
// This function will be calLED every time Widget
// in Blynk app writes values to the Virtual Pin V2
{
TempGretja = param.asInt(); // Assigning incoming value from pin V0 to variable
Blynk.virtualWrite(V11, TempGretja);
Serial.println(TempGretja);
}
int TempHlajenja;
BLYNK_WRITE(V1)
{
TempHlajenja = param.asInt(); // Assigning incoming value from pin V0 to variable
Blynk.virtualWrite(V12, TempHlajenja);
Serial.println(TempHlajenja);
}
float Toleranca;
BLYNK_WRITE(V2)
{
Toleranca = param.asFloat(); // Assigning incoming value from pin V0 to variable
Blynk.virtualWrite(V13, Toleranca);
Serial.println(Toleranca);
}
void setup(){
pinMode (rele1, OUTPUT);
digitalWrite (rele1, HIGH);
pinMode (rele2, OUTPUT);
digitalWrite (rele2, HIGH);
pinMode (rele3, OUTPUT);
digitalWrite (rele3, HIGH);
pinMode (rele4, OUTPUT);
digitalWrite (rele4, HIGH);
// Debug console
Serial.begin(9600);
analogReference(EXTERNAL);
// Set ESP8266 baud rate
Serial.begin(ESP8266_BAUD);
Blynk.begin(auth, wifi, ssid, pass);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
while (Blynk.connect() == false) {}
CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
timer.disable(CountdownTimer); // disable it on boot
}
void (* resetFunc) (void) =0;
BLYNK_WRITE(V10) // Reset
{
if (param.asInt()==1) {
resetFunc();
delay(100);
}
}
void loop(){
TempVode();
if (t < TempGretja + Toleranca){
digitalWrite (rele1, LOW);}
else if (t > TempGretja + Toleranca) {
digitalWrite (rele1, HIGH);}
Blynk.run();
timer.run();
Blynk.virtualWrite(V15, TempGretja);
delay(1000);
}
//Functions
//funkcije za timer
void CountdownTimerFunction() {
CountdownRemain--; // remove 1 every second
CountdownShowFormatted(CountdownRemain);
if (!CountdownRemain) { // check if CountdownRemain == 0/FALSE/LOW
timer.disable(CountdownTimer); // if 0 stop timer
Blynk.virtualWrite(V7, LOW); // reset START/STOP button status
Blynk.virtualWrite(17, "TIMER COMPLETE");
Blynk.virtualWrite(18, 255); // LED for timer completed
Blynk.virtualWrite(19, 0); // Timer LED status light off
} else {
Blynk.virtualWrite(18, 0); // LED for timer completed
}
}
// Button Widget (Switch Type): Start/Pause Timer
BLYNK_WRITE(V7) {
if (param.asInt()) {
if (CountdownRemain) { // check if there is a time set or not
timer.enable(CountdownTimer);
Blynk.virtualWrite(19, 255); // Timer LED status light on
} else {
Blynk.virtualWrite(V7, LOW); // if CountdownRemain is set to 0, then dont start hte timer.
Blynk.virtualWrite(17, "COUNTDOWN TIME NOT SET"); // if CountdownRemain is set to 0, then tell the user
}
} else {
timer.disable(CountdownTimer);
Blynk.virtualWrite(19, 0); // Timer LED status light off
}
}
// Button Widget (Momentary): Reset Timer
BLYNK_WRITE(20) {
CountdownRemain = CountdownRemainReset; // reset to original start time
}
// Slider Widget (60-180): Set Timer (mins)
BLYNK_WRITE(21) {
if (timer.isEnabled(CountdownTimer)) { // only update if timer not running
Blynk.virtualWrite(21, param.asInt() ); // if running, refuse to let use change slider
} else {
CountdownRemainReset = param.asInt() * 60 + 1; // + 1 set the timer to 1:00:00 instead of 00:59:59
CountdownRemain = param.asInt() * 60;
CountdownShowFormatted(CountdownRemain);
}
}
void CountdownShowFormatted(int seconds) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = seconds; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
Blynk.virtualWrite(17, days + hours_o + hours + mins_o + mins + secs_o + secs);
}
void TempVode()
{
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
Blynk.virtualWrite(V4, steinhart);
}