Hey guys,
I´m using the DS1307 to set a segment stage for 4 options.
Using a blynk segment state to turn light on and off, and 2 set times. the first work 6am to 6pm and the other works 5am to 11pm. the problem is that when i press those two , the relay always go to HIGH, apart the real time.
void getTime() { // Horario do RTC
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();
String currentTime = String(now.hour()) + ":" + now.minute() + ":" + now.second();
Blynk.virtualWrite(V4, currentTime);
}
void getLUX() { // Lê a condiçao da selecao de 4 opcoes no app
if (switchState == 1){ // se opcao 1, irá funcionar de 6 as 18 horas
getTime();
DateTime now = rtc.now();
if(now.hour()== 6 && now.hour() < 18 ){
digitalWrite(rele1, LOW);
}
else{
digitalWrite(rele1, HIGH);
}
Blynk.setProperty(V5, "color", "#FF00FF"); // indica a cor roxa no led virtual
getPhoto(); // checa estado da luz
}
if (switchState == 2){ // se opcao 2, irá funcionar de 5 as 23 horas
getTime();
DateTime now = rtc.now();
if(now.hour()== 5 && now.hour() < 23){
digitalWrite(rele1, LOW);
}
else{
digitalWrite(rele1, HIGH);
}
Blynk.setProperty(V5, "color", "#0000FF"); // indica a cor azul no led virtual
getPhoto(); // checa estado da luz
}
if (switchState == 3){ // se opcao 3, ligada
digitalWrite(rele1, LOW);
Blynk.setProperty(V5, "color", "#008000"); // indica a cor verde no led virtual
getPhoto(); // checa estado da luz
}
if (switchState == 4){ // se opcao 4, desligada
digitalWrite(rele1, HIGH);
Blynk.setProperty(V5, "color", "#FF0000"); // indica a cor vermelha no led virtual
getPhoto(); // checa estado da luz
}
}
the entire code go above
#include <RTClib.h>
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <WidgetRTC.h>
//#include <TimeLib.h>
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
String apiKey = "xxxxx"; // Enter your Write API key from ThingSpeak
const char* server = "api.thingspeak.com";
WiFiClient client;
#include <ThingSpeak.h>
const long CHANNEL = xxxx;
const char *WRITE_API = "xxxx";
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";
char ssid[] = "Oliveiras";
char pass[] = "32734755";
int nivel = 25;
int valueNivel = 0;
const int oneWireBus = 4; //Sensor de temperatura da agua
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
int Photo = 34; //Sensor de Luz canal digital
int valuePhoto = 0;
#define DHTTYPE DHT22 //Sensor Umidade do AR e Temperatura
#define DHTPIN 19
DHT dht(DHTPIN, DHT22);
float localHum = 0;
float localTemp = 0;
RTC_DS1307 rtc;
//WidgetRTC rtc;
int switchState;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
BlynkTimer timer; // Announcing the timer
int rele1 = 32; //Luz
int rele2 = 26; // Exautao
int rele3 = 18; //Bomba
BLYNK_WRITE(V6){
switchState = param.asInt();
getLUX();
}
void setup() {
Serial.begin(115200);
ThingSpeak.begin(client); // Initialize ThingSpeak
pinMode(rele1, OUTPUT); // definições das portas IN1 e IN2 como portas de saidas
pinMode(rele2, OUTPUT);
pinMode(rele3, OUTPUT);
pinMode(Photo, INPUT);
pinMode(nivel, INPUT);
digitalWrite(rele1, LOW); // desativa porta IN1
digitalWrite(rele2, LOW);
digitalWrite(rele3, LOW);
dht.begin();
rtc.begin();
Wire.begin(21,22); //sda Roxo scl Cinza
delay(3000);
timer.setInterval(13000L, getDHT); //timer will run every 1 min
timer.setInterval(10000L, getTempagua);
timer.setInterval(11000L, getTime);
timer.setInterval(12000L, getNivel);
timer.setInterval(14000L, getPhoto);
timer.setInterval(15000L, getLUX);
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(2021, 2, 8, 17, 02, 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 getTempagua(){ // Sensor de temperatura Agua
sensors.requestTemperatures();//Colete informações de temperatura
float tempAgua = sensors.getTempCByIndex(0);//Obtenha a temperatura em graus Celsius
Serial.print("Temp. agua: ");
Serial.print(tempAgua);
Serial.println(" ");
Blynk.virtualWrite(V0, tempAgua);//Atualize variaveis Blynk com os valores de temperatura
ThingSpeak.setField(3,tempAgua);
int x = ThingSpeak.writeFields(CHANNEL, WRITE_API);
if (x == 200) {
Serial.println("Update realizado com sucesso");
}
else {
Serial.println("Problema no canal - erro HTTP " + String(x));
}
Serial.println("Waiting...");
return;
}
void getPhoto(){ // Verifica se luz esta ligada ou desligada
valuePhoto = digitalRead(Photo);
if(valuePhoto == 1){
Blynk.virtualWrite(V1, "Ligada"); //Luz ligada Cor verde
}
else{
Blynk.virtualWrite(V1, "Desligada"); //Luz desligada Cor vermelha
}
Serial.print("Luz: ");
Serial.print(valuePhoto);
Serial.println(" ");
return;
}
void getDHT(){ // Sensor de temperatura e umidade dentro da estufa com condiçao de ligar e desligar a exautão
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("%");
if(localTemp > 26 || localHum > 50){ // se temperatura for maior que 26 graus ou a umidade for maior que 50% liga exautor
digitalWrite(rele3, LOW);
}
else{ // se temperatura for menor que 26 graus e a umidade for menor que 50% desliga exautor
digitalWrite(rele3, HIGH);
}
Blynk.virtualWrite(V2, localTemp); // escreve a temperatura no app
Blynk.virtualWrite(V3, localHum); // escreve a umidade no app
if (isnan(localHum) || isnan(localTemp)) // Check if any reads failed and exit early (to try again).
{
localTemp = tempIni;
localHum = humIni;
return;
}
ThingSpeak.setField(1,localHum);
ThingSpeak.setField(2,localTemp);
int x = ThingSpeak.writeFields(CHANNEL, WRITE_API);
if (x == 200) {
Serial.println("Update realizado com sucesso");
}
else {
Serial.println("Problema no canal - erro HTTP " + String(x));
}
Serial.println("Waiting...");
}
void getTime() { // Horario do RTC
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();
String currentTime = String(now.hour()) + ":" + now.minute() + ":" + now.second();
Blynk.virtualWrite(V4, currentTime);
}
void getLUX() { // Lê a condiçao da selecao de 4 opcoes no app
if (switchState == 1){ // se opcao 1, irá funcionar de 6 as 18 horas
getTime();
DateTime now = rtc.now();
if(now.hour()== 6 && now.hour() < 18 ){
digitalWrite(rele1, LOW);
}
else{
digitalWrite(rele1, HIGH);
}
Blynk.setProperty(V5, "color", "#FF00FF"); // indica a cor roxa no led virtual
getPhoto(); // checa estado da luz
}
if (switchState == 2){ // se opcao 2, irá funcionar de 5 as 23 horas
getTime();
DateTime now = rtc.now();
if(now.hour()== 5 && now.hour() < 23){
digitalWrite(rele1, LOW);
}
else{
digitalWrite(rele1, HIGH);
}
Blynk.setProperty(V5, "color", "#0000FF"); // indica a cor azul no led virtual
getPhoto(); // checa estado da luz
}
if (switchState == 3){ // se opcao 3, ligada
digitalWrite(rele1, LOW);
Blynk.setProperty(V5, "color", "#008000"); // indica a cor verde no led virtual
getPhoto(); // checa estado da luz
}
if (switchState == 4){ // se opcao 4, desligada
digitalWrite(rele1, HIGH);
Blynk.setProperty(V5, "color", "#FF0000"); // indica a cor vermelha no led virtual
getPhoto(); // checa estado da luz
}
}
void getNivel(){ //desliga bomba caso o nivel de agua esteja baixo
valueNivel = digitalRead(nivel);
if(valueNivel == 0){
digitalWrite(rele2, LOW);
Blynk.virtualWrite(V7, "Ligada");
}
else{
digitalWrite(rele2, HIGH);
Blynk.virtualWrite(V7, "Desligada");
}
Serial.print("Agua : ");
Serial.print(valueNivel);
Serial.println();
}
Can u guys help me ?