RTC time or Timer

Hi there !

I am building a small temperature management system.

(Ce n’est pour l’instant qu’un test, donc le codes ets un peu en bazard)

The principle is simple, I try to manage the temperature according to cycles that I call ON and OFF. In ON I need X temperature and in OFF I need Y temperature.

I have a cycle management button (cycle 1: 18 hours and cycle 2: 12 hours) and a variable for the start time.

I wrote my code so that if I choose 11 a.m. the cycle stops at 11 p.m. There everything is fine but if I choose the start of the cycle at 3 p.m. it will stop at 3 a.m. Since I use the RTC clock in 24H my function cannot work. (I put a copy of code to better understand)

What would be the best solution? Set up a timer?
I need your opinions and your solutions.
(if you need more details don’t hesitate to ask me!

Thanks

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;

///////////////////////////////////////////VPIN RECEVEUR///////////////////////////////////////////////

WidgetBridge bridge1(V10); //Temp Som

////////////////////////////////////////////////////////WIDGET///////////////////////////////////////////////////////////////////////////////////

WidgetTerminal terminal(V100);
WidgetLED ledGrow(V3);
WidgetLED ledFlow(V4);
WidgetLED etatCyle(V5);
WidgetRTC rtc;

////////////////////////////////////////////////////////CONSTANTE///////////////////////////////////////////////////////////////////////////////////

int timeOn;
int timeOff;
int cycle;
int Hour = hour(); 

int tempSom;

////////////////////////////////////////////////////////BLYNK///////////////////////////////////////////////////////////////////////////////////

BLYNK_CONNECTED() {
  Blynk.syncAll();
}
BLYNK_WRITE(V1) {
  timeOn = param.asInt();
}
BLYNK_WRITE(V2) {
  cycle = param.asInt();
}
BLYNK_WRITE(V10) {
  tempSom = param.asInt();
}
char auth[] = "xx";
char ssid[] = "xx";
char pass[] = "xx";


////////////////////////////////////////////////////////SETUP///////////////////////////////////////////////////////////////////////////////////

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, IPAddress(10, 3, 141, 1), 8080);

  rtc.begin();
  setSyncInterval(500);

  timer.setInterval(10000L, setupCycle);
  timer.setInterval(10000L, printTerminal);
   timer.setInterval(2000L, relais);
  

  terminal.clear();
  terminal.println("Controleur Indoor V1.2");
  terminal.println("");
  terminal.println("---------------------------");
  terminal.flush();
}

void loop() {

  Blynk.run();
  timer.run();
}

////////////////////////////////////////////////////////SELECTION CYCLE///////////////////////////////////////////////////////////////////////////////////

void setupCycle() { 
  
  int Hour = hour(); 

  if (cycle == HIGH) {
    timeOff = (timeOn + 18);
    Blynk.virtualWrite(V3, 255);
    Blynk.virtualWrite(V4, 0);

  }
  if (cycle == LOW) {
    timeOff = (timeOn + 12);
    Blynk.virtualWrite(V3, 0);
    Blynk.virtualWrite(V4, 255);
  }
}

////////////////////////////////////////////////////////DEBUG///////////////////////////////////////////////////////////////////////////////////


void printTerminal() { 

  int Hour = hour();
  Serial.print("Time On :  ");
  Serial.println(timeOn);
  Serial.print("Time Off:  ");
  Serial.println(timeOff);
  Serial.print("Cycle :  ");
  Serial.println(cycle);
  //Serial.print("Heure actuelle : ");
  //Serial.println(Hour);
  Serial.println("");
  Serial.println("---------------------------");

  terminal.print("Time On :  ");
  terminal.println(timeOn);
  terminal.print("Time Off:  ");
  terminal.println(timeOff);
  terminal.print("Cycle :  ");
  terminal.println(cycle);
  //terminal.print("Heure actuelle : ");
  //terminal.println(Hour);
  terminal.println("");
  terminal.println("---------------------------");
  terminal.println("");


  terminal.flush();
}

////////////////////////////////////////////////////////TIME ON///////////////////////////////////////////////////////////////////////////////////

void relais(){

  int Hour = hour();

  if (Hour >= timeOn && Hour <= timeOff)
  {
    Serial.println("Cycle On");
    Serial.println("");
  Serial.println("---------------------------");
    terminal.println("Cycle On");
    terminal.println("");
  terminal.println("---------------------------");
    Blynk.virtualWrite(V5, 255);
  }
  else
  {
    Serial.println("Cycle Off");
    terminal.println("Cycle Off");
    Blynk.virtualWrite(V5, 0);
  }

}

use automations run your code to read your data at select times rather then coding to only read at specific times. Will make your code wayyyy easier to adjust accordingly. Just go to automations and enable a switch to start turn the sensor reading on and then pick the time for when to run it you can set 5 weekly automation with free blynk

I am using Blynk 1.0 legacy.

Unfortunately I need my code to run non-stop, I have to graft other application on this cycle principle.

In addition, if I use a timer instead of the RTC clock in the event of a power cut, everything starts from zero…

I think I found a solution using modulo:

void setupCycle() {
  
   int Hour = hour();

   if (cycle == HIGH) {
     timeOff=(timeOn+18)%24;
     Blynk. virtualWrite(V3, 255);
     Blynk. virtualWrite(V4, 0);

   }
   if (cycle == LOW) {
     timeOff=(timeOn+12)%24;
     Blynk. virtualWrite(V3, 0);
     Blynk. virtualWrite(V4, 255);
   }
}

@PeteKnight Could you give me your opinion and your experience?

Not without significantly more information.

Pete.

My code is not clean at all, it’s only for now a test

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;

///////////////////////////////////////////VPIN RECEVEUR///////////////////////////////////////////////

WidgetBridge bridge1(V10); //Temp Som

////////////////////////////////////////////////////////WIDGET///////////////////////////////////////////////////////////////////////////////////

WidgetTerminal terminal(V100);
WidgetLED ledGrow(V20);
WidgetLED ledFlow(V21);
WidgetLED etatCyle(V22);
WidgetRTC rtc;

////////////////////////////////////////////////////////CONSTANTE///////////////////////////////////////////////////////////////////////////////////

int timeOn;
int timeOff;
int cycle;
int Hour = hour();

int cycleOn;
int cycleOff;

int tempSom;
int ThermoCycleOn;
int ThermoCycleOff;

////////////////////////////////////////////////////////BLYNK///////////////////////////////////////////////////////////////////////////////////

BLYNK_CONNECTED() {
  Blynk.syncAll();
}
BLYNK_WRITE(V1) {
  timeOn = param.asInt();
}
BLYNK_WRITE(V2) {
  cycle = param.asInt();
}
BLYNK_WRITE(V6) {
  ThermoCycleOn = param.asInt();
}
BLYNK_WRITE(V7) {
  ThermoCycleOff = param.asInt();
}
BLYNK_WRITE(V10) {
  tempSom = param.asInt();
}
char auth[] = "9xxSe";
char ssid[] = "xxx";
char pass[] = "xx";


////////////////////////////////////////////////////////SETUP///////////////////////////////////////////////////////////////////////////////////

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, IPAddress(10, 3, 141, 1), 8080);

  rtc.begin();
  setSyncInterval(500);

  timer.setInterval(5000L, setupCycle);
  timer.setInterval(5000L, printTerminal);
  timer.setInterval(5000L, etatCycle);


  terminal.clear();
  terminal.println("Controleur Indoor V1.2");
  terminal.println("");
  terminal.println("---------------------------");
  terminal.flush();
}

void loop() {

  Blynk.run();
  timer.run();
}

////////////////////////////////////////////////////////SELECTION CYCLE///////////////////////////////////////////////////////////////////////////////////

void setupCycle() {

  int Hour = hour();

  if (cycle == HIGH) {
    timeOff = (timeOn + 18) % 24;
    Blynk.virtualWrite(V20, 255);
    Blynk.virtualWrite(V21, 0);

  }
  if (cycle == LOW) {
    timeOff = (timeOn + 12) % 24;
    Blynk.virtualWrite(V20, 0);
    Blynk.virtualWrite(V21, 255);
  }
}

////////////////////////////////////////////////////////DEBUG///////////////////////////////////////////////////////////////////////////////////


void printTerminal() {

  int Hour = hour();
  Serial.println("---------------------------");
  Serial.println("");
  Serial.print("Time On :  ");
  Serial.println(timeOn);
  Serial.print("Time Off:  ");
  Serial.println(timeOff);
  //Serial.print("Heure actuelle : ");
  //Serial.println(Hour);
  Serial.println("");
  Serial.println("---------------------------");

  terminal.print("Time On :  ");
  terminal.println(timeOn);
  terminal.print("Time Off:  ");
  terminal.println(timeOff);
  //terminal.print("Heure actuelle : ");
  //terminal.println(Hour);
  terminal.println("");
  terminal.println("---------------------------");
  terminal.println("");



  terminal.flush();
}

////////////////////////////////////////////////////////ETAT CYCLE///////////////////////////////////////////////////////////////////////////////////

void etatCycle() {

  int Hour = hour();


  if (Hour >= timeOn && Hour <= timeOff)
  {
     cycleOn = 1; 
     cycleOff = 0; 
    Serial.println("Cycle On");
    terminal.println("Cycle On");
    Blynk.virtualWrite(V22, 255);
  }
  else
  {
    cycleOff = 1;
    cycleOn = 0;
    Serial.println("Cycle Off");
    terminal.println("Cycle Off");
    Blynk.virtualWrite(V22, 0);
  }

}

Let me give you an insight into what happens in my mind when I start reading your original post…

What button?
Is it a physical button? If so is it attached to the master or slave device, and if so to which pin. Is it normally HIGH OR LOW.
Or, is it a button widget? If so is it attached to a digital or virtual pin, and which pin is this?

What is this variable for the start time? Is it a variable within the sketch, and if so which one?
Or is it a variable that’s associated with a widget?

So, from this one sentence I have multiple questions, that aren’t answer by having a quick glance at the code because that contains no in-code documentation.
At this point I ask myself why you aren’t using the time input widget, or maybe two time input widgets and a switch to change between one set of times and another, but decide that I don’t really want to get into that discussion, and move on.

So when I say:

It doesn’t mean that I want to to repeat what you’ve already said, it means that you need to provide significantly more new, additional and not previously provided information, which I don’t really feel like trying to extract from you one drop at a time.

Pete.

Here no bridge for the moment

I am using RTC clock

I have a virtual button attached to V2 for 18 hour or 12 hour duration selection.

A slider (variable) in V1 which allows me to set the departure time (between 00 hours and 24 hours)

I think you’re missing the point. This was just one example of the gaps in the information that you’ve provided. You have to look at the data you are providing from the point of view of someone who has no information about what you are trying to achieve, and provide all of that data in a structured format, with screenshots and code as appropriate, if you want people to get on board with assisting you.
You don’t even explain that you are using Blynk Legacy with a local server, or the type of hardware that you’re using, which would help to avoid people wasting their time with suggestions that aren’t appropriate to your situation.

You have to help others to help you, and for that you need to provide significantly more information.

Pete.

Using blynk Legacy, local server / esp8266 /

Project :

know if a cycle is “ON” or “OFF” defined by:

  • timeOn (adjustable by a slider) → SETUP CYCLE
  • timeOff (which is equal to timeOn + 18 hours or 12 hours with a selection by a switch) → SETUP CYCLE
  • cycle status ( cycleOn and cycleOff ) → CYCLE STATUS

And send its information via LED

Works with RTC

The code works, but isn’t perfect. I would like to check the hours from timeOn to timeOff regardless of what time the cycle starts
Let cycleOn start at timeOn and 12 or 18 hours RTC later go to cycleOff.

My problem is that when the cycle is OFF I have a return cycleOn and cycleOff each time the loop etatCycle is read

Here is my simplified code


#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;

WidgetLED ledGrow(V20);     //show setup cycle state
WidgetLED ledFlow(V21);    //show setup cycle state
WidgetLED etatCyle(V22);    //show cycle on/off

WidgetRTC rtc;

int timeOn;                // attached to slider ( 00 -> 23) on V1
int timeOff;
int cycle;
int Hour = hour(); // hours from RTC

int cycleOn;
int cycleOff;

BLYNK_CONNECTED() {
 Blynk.syncAll();
}
BLYNK_WRITE(V1) {
 timeOn = param.asInt();
}
BLYNK_WRITE(V2) {
 cycle = param.asInt();
}

char auth[] = "xx";
char ssid[] = "xx";
char pass[] = "xx";

void setup()
{
 Serial.begin(9600);
 Blynk.begin(auth, ssid, pass, IPAddress(10, 3, 141, 1), 8080);

 rtc.begin();
 setSyncInterval(500);

 timer.setInterval(1000L, setupCycle);
 timer.setInterval(1000L, etatCycle);
}

void loop() {

 Blynk.run();
 timer.run();
}

////////////////////////////////////////////////////////SETUP CYCLE
//selection between 12 hours or 14 hours with V2 button and blink led

void setupCycle() {

 int Hour = hour();

 if (cycle == HIGH) {              //if button = 1 -> set growcycle 18 hours on then off
   timeOff = (timeOn + 18) % 24;
   Blynk.virtualWrite(V20, 255);
   Blynk.virtualWrite(V21, 0);

 }
 if (cycle == LOW) {                 ////if button = 0 -> set flowcycle 12 hours on then off
   timeOff = (timeOn + 12) % 24;
   Blynk.virtualWrite(V20, 0);
   Blynk.virtualWrite(V21, 255);
 }
}



////////////////////////////////////////////////////////ETAT CYCLE
//verification of cycle state if hour is between timeOn and timeOff

void etatCycle() {
 int Hour = hour();
 if (Hour > timeOn && cycleOn == 0) // testing cycleOn
 {
   cycleOn = 1;
   cycleOff = 0;
   Serial.println("Cycle On");
   Blynk.virtualWrite(V22, 255);
 }
 if (Hour > timeOff && cycleOn == 1) // testing cycleOff
   
 {
   cycleOff = 1;
   cycleOn = 0;
   Serial.println("Cycle Off");
   Blynk.virtualWrite(V22, 0);
 }
}

You code would display in full if you put the triple backticks on a separate line.

Pete.

This is ok ?

Can you help me ?

That’s because you are changing the value of the cycleOn Variable in your first if statement, which makes the second if statement evaluate as true as well.

You need a return command at the end of your first if statement so that the second if statement isn’t executed if the first one evaluates as true.

Pete.

////////////////////////////////////////////////////////ETAT CYCLE
//verification of cycle state if hour is between timeOn and timeOff

void etatCycle() {
  int Hour = hour();
  if (Hour > timeOn && cycleOn == 0) // testing cycleOn
  {
    cycleOn = 1;
    cycleOff = 0;
    Serial.println("Cycle On");
    Blynk.virtualWrite(V22, 255);
    return;
  }
  if (Hour > timeOff && cycleOn == 1) // testing cycleOff
    
  {
    cycleOff = 1;
    cycleOn = 0;
    Serial.println("Cycle Off");
    Blynk.virtualWrite(V22, 0);
  }
}

Same problem with this modification

Anyway I don’t think this is the right approach, I also have problems when the cycle goes past midnight…

I don’t know how to do it or what other solution exists.

As I’ve said before, if you describe the big picture overview of what you are trying to achieve and why, instead of getting bogged-down in the details of why your current approach isn’t working, then you may get further.
I suspect that it would be far better to use the time input widget or timer widget to achieve your goals, but you haven’t yet explained what your goals are.

Pete.

I can’t really talk about this project on the forum but I can explain it to you in DM. I am not sure that people appreciate this kind of project but it is necessary for my health.

No thanks.

Pete.

Ok, I will explain here.

I need to make an indoor garden. For that I must be able to have a light management (lamp).

The cycles correspond to the time of sunshine necessary for growth (18 hours per day) and flowering (12 hours per day).
So I need to control the cycle ( cycleOn or cycleOff ) of lighting. And the lighting time (timeOn and timeOff ).

I also need the slider to choose when to start the cycle

That’s the best explanation I can give

Why have you fixed on using a slide4 for this?
Why not the time input or timer widget, or a segmented switch?

Pete.

Because I don’t know what is the best solution. and that I don’t know the others

It seemed to me to be the best solution, and because of my reduced capacity I try to make an analogy (with for example the thermostat slider which sets a temperature, so why not a departure time)