I have this project that has been running for a long time now and suddenly stoped working. When I was checking if there was any hardward malfunction I notice that the problem is the value I’m getting from RTC.
I tried to make it sync many times but no luck. It keep returning me the amout of time since I turned it on and not the current time…
I’m using a NodeMCU ESP8266 wth a DS18B20. I’ll be really happy if anybody can give me a hand here
#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WidgetRTC.h>
#include <TimeLib.h>
SimpleTimer timerRT;
SimpleTimer timerRL;
WidgetLCD lcd(V1);
WidgetRTC rtc;
WidgetLED led(V9);
#define Pin D3// pin DATA ds18b20
#define relay D4
char auth[] = "auth";
OneWire ourWire(Pin);
DallasTemperature sensors(&ourWire);
int userSet;
int maxTemp;
int timerCheck;
int tempMin;
char currentTime;
BLYNK_ATTACH_WIDGET(rtc, V5);
void setup()
{
Serial.begin(9600);
setSyncInterval(1);
Blynk.begin(auth, "network", "password");
while (Blynk.connect() == false) {
// Wait until connected
}
sensors.begin();
sensors.setResolution(10);
pinMode(relay, OUTPUT);
setSyncInterval(30);
rtc.begin();
Blynk.syncAll();
timerRT.setInterval(2000L, ReadTemp);
timerRL.setInterval(2500L, Relay);
}
bool isFirstConnect = true;
BLYNK_CONNECTED()
{
rtc.begin();
}
void ReadTemp() //Read temperature from sensor and activate relay
{
sensors.requestTemperatures();
lcd.print(0, 0, "Temp:");
lcd.print(5, 0, String(sensors.getTempCByIndex(0),1));
lcd.print(10, 0,"MAX:");
lcd.print(14, 0, maxTemp);
lcd.print(10, 1, "MIN:");
lcd.print(14, 1, tempMin);
String currentTime = String(hour()) + ":" + minute() + ":" + second();
lcd.print(0, 1, currentTime);
if(digitalRead(relay)==LOW)
{
//lcd.print(0, 1, "DESLIGADO");
led.off();
}
if(digitalRead(relay)==HIGH)
{
//lcd.print(0, 1, "LIGADO ");
led.on();
}
}
void Relay()
{
if((userSet==1&&sensors.getTempCByIndex(0)<tempMin)||((sensors.getTempCByIndex(0)<tempMin)&&(18>hour()>15)))
{
digitalWrite(relay, HIGH);
//lcd.print(0, 1, "LIGADO ");
led.on();
}
if(sensors.getTempCByIndex(0)>=maxTemp||((15>hour()||hour()>18)&&userSet==0))
{
digitalWrite(relay, LOW);
//lcd.print(0, 1, "DESLIGADO");
led.off();
}
}
BLYNK_WRITE(V7) //Check user selection (On/Off)
{
userSet = param.asInt();
}
BLYNK_WRITE(V8) //Check user max temperature setting
{
maxTemp = param.asInt();
tempMin = maxTemp - 2;
}
void loop()
{
Blynk.run();
timerRT.run();
timerRL.run();
}