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