Timer set in segmented switch with DS1307

I´m trying to time set a segmented switch using a DS1307. The trouble is the
when i use Blynk_write i have 4 case to set. the first turn on the relay to work 6 am to 18 am and them turn off, the second turn on the relay to work 5 am to 11pm, the third and fourth is turning on and of the relay. so the third and fourth is not trouble.
can u guys help me?


#include "RTClib.h"
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Wire.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxxxx";

const int hygrometer = 36;  //Sensor de Umidade de solo
int value;

const int photopin = 34;  //Sensor de Luz canal analogico
int valuephoto;

#define DHTTYPE DHT22  //Sensor Umidade do AR e Temperatura
#define DHTPIN 18
DHT dht(DHTPIN, DHT22);
float localHum = 0;
float localTemp = 0;

RTC_DS1307 rtc;

int rele1 = 33; //Luz 
int rele2 = 32; // Exautao

     WidgetLED led1(V5); //register to virtual pin 5
     BLYNK_WRITE(V7) {
      
      switch (param.asInt())
        { 
          case 1: { 
            if(now.hour()== 6 & now.minute() == 00 ){
              digitalWrite(rele1, HIGH);
            }
            else if(now.hour()== 18 & now.minute() == 01 ){
              digitalWrite(rele1, LOW);
            }
            led1.on();
            Blynk.setProperty(V5, "color", "#00FF2E");
            break;
          }
          case 2: { 
            //Luz funciona 18 horas e pausa 6
            if(now.hour()== 5 & now.minute() == 00 ){
              digitalWrite(rele1, HIGH);
            }
            else if(now.hour()== 23 & now.minute() == 01 ){
              digitalWrite(rele1, LOW);
            }
            led1.on();
            Blynk.setProperty(V5, "color", "#FF0000");
          break;
          }
          case 3: { 
            // Funciona ON
            digitalWrite(rele1, HIGH);
            led1.on();
            Blynk.setProperty(V5, "color", "#04C0F8");
            break;
          }
          case 4: {
            // Funciona OFF 
            digitalWrite(rele1, LOW);
            led1.on();
            Blynk.setProperty(V5, "color", "#ED9D00");
            break;
          }
       }
    }

void setup() {  
  Serial.begin(115200);
  
  pinMode(rele1, OUTPUT);                  // definições das portas IN1 e IN2 como portas de saidas 
  pinMode(rele2, OUTPUT);  
  digitalWrite(rele1, LOW);               // desativa porta IN1 
  digitalWrite(rele2, LOW);
  
  dht.begin();
  rtc.begin();
  Wire.begin(21,22);  
  delay(3000);
  
       if (! rtc.isrunning()) {
        rtc.adjust(DateTime(2021, 1, 29, 17, 40, 00));
        }
        
 
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Blynk.begin("xxxxxxxxxxx", ssid, pass);

}

void loop(){
  
    Blynk.run();
    
    getDHT();
    getSolo();
    getLux();
    
    Serial.println("########### ");
    return;
}

void getLux(){
    
    WidgetLED led1(V5); //register to virtual pin 5
  
    delay(2000);    
    return;
}

void getSolo(){
   
      value = analogRead(hygrometer);   //Read analog value 
      value = value;
  
      Serial.print("Soil humidity: ");
      Serial.print(value);
      Serial.println("%");
      Blynk.virtualWrite(V4, value);
      delay(2000);
      return;
}

void getDHT(){
      float tempIni = localTemp;
      float humIni = localHum;
  
      localTemp = dht.readTemperature();                                                                      
      localHum = dht.readHumidity();
  
      Serial.print("TEMPERATURA: ");
      Serial.print(localTemp);
      Serial.println("C");
      Serial.print("UMIDADE: ");
      Serial.print(localHum);
      Serial.println("%");
      delay(2000);
      Blynk.virtualWrite(V1, localTemp);
      Blynk.virtualWrite(V2, localHum);
        if (isnan(localHum) || isnan(localTemp))   // Check if any reads failed and exit early (to try again).
        {
          localTemp = tempIni;
          localHum = humIni;
          return;
        }
}
void printTime() {
    DateTime now = rtc.now();
    char buf1[] = "hh:mm";
    Serial.println(now.toString(buf1));

    char buf2[] = "hh";
    Serial.println(now.toString(buf2));

    char buf3[] = "hh";
    Serial.println(now.toString(buf3));

    char buf4[] = "MM-DD-YYYY";
    Serial.println(now.toString(buf4));
   
    Blynk.virtualWrite(V10, buf1);
      // Send DS13207 date to the App
    Blynk.virtualWrite(V11, buf4);

}

Your BLYNK_WRITE(V7) function will only be called when the value of your segmented switch is changed by you, so doing the time comparison within your case statement won’t work.

You need to have process which is called by a timer (say every 1 minute) which does what your existing `case’ statement does.

Also, you need to move this…

out of your void loop and get rid of your delays within these functions. Read this:

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

Pete.

like this,Pete?


BLYNK_WRITE(V7) {
      
      switch (param.asInt())
        { 
          case 1: { 
            void getTime(){
              DateTime now = rtc.now();
              Blynk.virtualWrite(V10, buf1);// Send DS13207 date to the App
              Blynk.virtualWrite(V11, buf4);
              if(now.hour()== 6 & now.minute() == 00 ){
                digitalWrite(rele1, HIGH);
              }
              else if(now.hour()== 18 & now.minute() == 01 ){
                digitalWrite(rele1, LOW);
              }
              led1.on();
              Blynk.setProperty(V5, "color", "#00FF2E");
              break;
            }
          }
          case 2: { 
             void getTime() {
             DateTime now = rtc.now();
             Blynk.virtualWrite(V10, buf1);// Send DS13207 date to the App
             Blynk.virtualWrite(V11, buf4);
             if(now.hour()== 5 & now.minute() == 00 ){
              digitalWrite(rele1, HIGH);
             }
             else if(now.hour()== 23 & now.minute() == 01 ){
              digitalWrite(rele1, LOW);
             }
             led1.on();
             Blynk.setProperty(V5, "color", "#FF0000");
             break;
            }
          }
          case 3: { 
            // Funciona ON
            digitalWrite(rele1, HIGH);
            led1.on();
            Blynk.setProperty(V5, "color", "#04C0F8");
            break;
          }
          case 4: {
            // Funciona OFF 
            digitalWrite(rele1, LOW);
            led1.on();
            Blynk.setProperty(V5, "color", "#ED9D00");
            break;
          }
       }
    }

i work on my void loop


void setup() {  
  Serial.begin(115200);
  
  pinMode(rele1, OUTPUT);                  // definições das portas IN1 e IN2 como portas de saidas 
  pinMode(rele2, OUTPUT);  
  digitalWrite(rele1, LOW);               // desativa porta IN1 
  digitalWrite(rele2, LOW);
  
  dht.begin();
  rtc.begin();
  Wire.begin(21,22);  
  delay(3000);
  
  timer.setInterval(1000L, getDHT); //timer will run every sec 
  timer.setInterval(1000L, getSolo);
  timer.setInterval(1000L, getTime);
  
       if (! rtc.isrunning()) {
        rtc.adjust(DateTime(2021, 1, 29, 17, 40, 00));
        }
        
 
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Blynk.begin("xxxxxxxxxxxxx", ssid, pass);

}

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

No, you’ve not done this…

Pete

can u give me a example?

maybe i got this


case 1: { 
            
              DateTime now = rtc.now();
              
              if(now.hour()== 6 & now.minute() == 00 ){
                digitalWrite(rele1, HIGH);
              }
              else if(now.hour()== 18 & now.minute() == 01 ){
                digitalWrite(rele1, LOW);
              }
              led1.on();
              Blynk.setProperty(V5, "color", "#00FF2E");
              break;
            
          }

Declare a global variable to store your V7 widget value and update this within your BLYNK_WRITE(V7) function.

Then, put the rest of your BLYNK_WRITE(V7) into a new function, and do the ‘switch/case’ on the new global variable.

Call this function with another timer.

Also, don’t have all your timers calling their timed functions at exactly the same time, and don’t try to read your DHT sensor so frequently.

It also helps if you post your full code, rather than snippets.

Pete.

Pete, Like that?

#include "RTClib.h"
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Wire.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxx";
char pass[] = "";

const int hygrometer = 36;  //Sensor de Umidade de solo
int value;

const int photopin = 34;  //Sensor de Luz canal analogico
int valuephoto;

#define DHTTYPE DHT22  //Sensor Umidade do AR e Temperatura
#define DHTPIN 18
DHT dht(DHTPIN, DHT22);
float localHum = 0;
float localTemp = 0;

RTC_DS1307 rtc;
int switchState;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
BlynkTimer timer; // Announcing the timer

int rele1 = 33; //Luz 
int rele2 = 32; // Exautao

WidgetLED led1(V5); //register to virtual pin 5
BLYNK_WRITE(V7) {
  switchState = param.asInt();
  switch (param.asInt()){
    case 1:{ 
      getTime1();
    }
    case 2:{ 
      getTime1();
    }
    case 3:{  // Funciona ON
     getTime1();
      
    }
    case 4:{  // Funciona OFF
     getTime1();
    }
  }
}

void setup() {  
  Serial.begin(115200);
  
  pinMode(rele1, OUTPUT);                  // definições das portas IN1 e IN2 como portas de saidas 
  pinMode(rele2, OUTPUT);  
  digitalWrite(rele1, LOW);               // desativa porta IN1 
  digitalWrite(rele2, LOW);
  
  dht.begin();
  rtc.begin();
  Wire.begin(21,22);  
  delay(3000);
  
  timer.setInterval(1000L, getDHT); //timer will run every sec 
  timer.setInterval(2000L, getSolo);
  timer.setInterval(3000L, getTime);
  timer.setInterval(4000L, getTime1);

  
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    rtc.adjust(DateTime(2021, 1, 29, 17, 40, 00));
  }
  
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Blynk.begin("xxxxxx", ssid, pass);

}

void loop(){
  Blynk.run();
  timer.run();  
}
    
void getSolo(){
  value = analogRead(hygrometer);   //Read analog value 
  value = value;
  
  Serial.print("Soil humidity: ");
  Serial.print(value);
  Serial.println("%");
  Blynk.virtualWrite(V4, value);
  return;
}

void getDHT(){
  float tempIni = localTemp;
  float humIni = localHum;
  
  localTemp = dht.readTemperature();                                                                      
  localHum = dht.readHumidity();
  
  Serial.print("TEMPERATURA: ");
  Serial.print(localTemp);
  Serial.println("C");
  Serial.print("UMIDADE: ");
  Serial.print(localHum);
  Serial.println("%");
  
  Blynk.virtualWrite(V1, localTemp);
  Blynk.virtualWrite(V2, localHum);
  if (isnan(localHum) || isnan(localTemp))   // Check if any reads failed and exit early (to try again).
  {
    localTemp = tempIni;
    localHum = humIni;
    return;
  }
}
void getTime() {
    
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
   
    
}

void getTime1() {
  

  if (switchState == 1){
     DateTime now = rtc.now();
    if(now.hour()== 6 & now.hour() < 18 ){
      digitalWrite(rele1, HIGH);
    }
    else{
      digitalWrite(rele1, LOW);
    }
    led1.on();
    Blynk.setProperty(V5, "color", "#00FF2E");
  }
       
  if (switchState == 2){
      DateTime now = rtc.now();
  if(now.hour()== 5 & now.hour() < 23){
    digitalWrite(rele1, HIGH);
  }
  else{
    digitalWrite(rele1, LOW);
  }
  led1.on();
  Blynk.setProperty(V5, "color", "#FF0000");
  }
      
  if (switchState == 3){
       digitalWrite(rele1, HIGH);
      led1.on();
      Blynk.setProperty(V5, "color", "#04C0F8");
  }
      
  if (switchState == 4){
     digitalWrite(rele1, LOW);
      led1.on();
      Blynk.setProperty(V5, "color", "#ED9D00");
  }
}

I guess it achieves the same overall result, but in a very messy way!

Why have you done this…

BLYNK_WRITE(V7) {
  switchState = param.asInt();
  switch (param.asInt()){
    case 1:{ 
      getTime1();
    }
    case 2:{ 
      getTime1();
    }
    case 3:{  // Funciona ON
     getTime1();
      
    }
    case 4:{  // Funciona OFF
     getTime1();
    }
  }
}

rather than this…

BLYNK_WRITE(V7)
{
  switchState = param.asInt();
  getTime1();
}

and why have you replaced your switch/case statement that should be in getTime1() with a series of if statements?

You’ve also ignored this piece of advice…

Pete.

1 Like

it is a piece of art, this way . thanks very much

And i did´t ignore this piece, i just so focus to fix the Blynk_write.

That´s my code after the change. I have more code to do still. And i Thank you very much to help me .

#include "RTClib.h"
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Wire.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXX";
char ssid[] = "Oliveiras";
char pass[] = "XXXX";

const int hygrometer = 36;  //Sensor de Umidade de solo
int value;

const int photopin = 34;  //Sensor de Luz canal analogico
int valuephoto;

#define DHTTYPE DHT22  //Sensor Umidade do AR e Temperatura
#define DHTPIN 18
DHT dht(DHTPIN, DHT22);
float localHum = 0;
float localTemp = 0;

RTC_DS1307 rtc;
int switchState;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
BlynkTimer timer; // Announcing the timer

int rele1 = 33; //Luz 
int rele2 = 32; // Exautao

WidgetLED led1(V5); //register to virtual pin 5
BLYNK_WRITE(V7) {
  switchState = param.asInt();
  getTime1();
  }

void setup() {  
  Serial.begin(115200);
  
  pinMode(rele1, OUTPUT);                  // definições das portas IN1 e IN2 como portas de saidas 
  pinMode(rele2, OUTPUT);  
  digitalWrite(rele1, LOW);               // desativa porta IN1 
  digitalWrite(rele2, LOW);
  
  dht.begin();
  rtc.begin();
  Wire.begin(21,22);  
  delay(3000);
  
  timer.setInterval(100000L, getDHT); //timer will run every 1 min 
  timer.setInterval(2000L, getSolo);
  timer.setInterval(3000L, getTime);
  timer.setInterval(4000L, getTime1);

  
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    rtc.adjust(DateTime(2021, 1, 29, 17, 40, 00));
  }
  
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Blynk.begin("XXXX", ssid, pass);

}

void loop(){
  Blynk.run();
  timer.run();  
}
    
void getSolo(){
  value = analogRead(hygrometer);   //Read analog value 
  value = value;
  
  Serial.print("Soil humidity: ");
  Serial.print(value);
  Serial.println("%");
  Blynk.virtualWrite(V4, value);
  return;
}

void getDHT(){
  float tempIni = localTemp;
  float humIni = localHum;
  
  localTemp = dht.readTemperature();                                                                      
  localHum = dht.readHumidity();
  
  Serial.print("TEMPERATURA: ");
  Serial.print(localTemp);
  Serial.println("C");
  Serial.print("UMIDADE: ");
  Serial.print(localHum);
  Serial.println("%");
  
  Blynk.virtualWrite(V1, localTemp);
  Blynk.virtualWrite(V2, localHum);
  if (isnan(localHum) || isnan(localTemp))   // Check if any reads failed and exit early (to try again).
  {
    localTemp = tempIni;
    localHum = humIni;
    return;
  }
}
void getTime() {
    
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
   
    
}

void getTime1() {
  

  if (switchState == 1){
     DateTime now = rtc.now();
    if(now.hour()== 6 & now.hour() < 18 ){
      digitalWrite(rele1, HIGH);
    }
    else{
      digitalWrite(rele1, LOW);
    }
    led1.on();
    Blynk.setProperty(V5, "color", "#00FF2E");
  }
       
  if (switchState == 2){
      DateTime now = rtc.now();
  if(now.hour()== 5 & now.hour() < 23){
    digitalWrite(rele1, HIGH);
  }
  else{
    digitalWrite(rele1, LOW);
  }
  led1.on();
  Blynk.setProperty(V5, "color", "#FF0000");
  }
      
  if (switchState == 3){
       digitalWrite(rele1, HIGH);
      led1.on();
      Blynk.setProperty(V5, "color", "#04C0F8");
  }
      
  if (switchState == 4){
     digitalWrite(rele1, LOW);
      led1.on();
      Blynk.setProperty(V5, "color", "#ED9D00");
  }
}