@Costas @Gianca @Dmitriy thanks for your help but I’m finding this rtc not working very well at all wth local blynk server. Is this a know fault / issue. You’ll see when it does connect the time if +1 off but ok in the history graph
here is my code (its a work in progress but you can hopefully follow what I’m trying to do) - it works well but clock hardly ever syncs !
perhaps I’m doing something obviously wrong.
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <OneWire.h> // make sure used ESP OneWire.h file in ESP folder
#include <DallasTemperature.h>
#include <TimeLib.h>
#include <SimpleTimer.h>
char Date[16];
char Time[16];
#define ONE_WIRE_BUS 13 // GPIO13 = onewire to use PIN 7 on WeMos D1 R2 ESP8266 board
#define TEMPERATURE_PRECISION 12
#define RELAY1 13 // D7
#define RELAY2 12 // D6
#define RELAY3 14 // D5
#define RELAY4 4 // D4
DeviceAddress Therm_1 = { 0x28, 0xff, 0x61, 0xd0, 0x64, 0x15, 0x03, 0x5f };
DeviceAddress Therm_2 = { 0x28, 0xFF, 0x2D, 0xD4, 0x64, 0x15, 0x01, 0x25 };
DeviceAddress Therm_3 = { 0x28, 0xFF, 0x18, 0xE7, 0x64, 0x15, 0x01, 0xB3 };
DeviceAddress Therm_4 = { 0x28, 0xFF, 0x70, 0xF4, 0x64, 0x15, 0x02, 0xCE };
DeviceAddress Therm_5 = { 0x28, 0xFF, 0xB0, 0xAB, 0x64, 0x15, 0x01, 0x4C };
int rainSensePin= 0; // analog pin 0 - sensor i/p
long BaudRate = 9600, sysTick = 0;
char GotChar;
SimpleTimer timer;
int clockcounter = 3;
bool clock_sync = 0;
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V8);
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
// Time to sleep (in seconds):
const int sleepTimeS = 10;
bool V1_written = 0;
char auth[] = "xxxxxxxxx"; // ESP token for Wemos D1 Project TEMP on LOCAL SERVER"; // Blynk token for ESP8266 Project on LOCAL SERVER
// Mac address should be different for each device in your LAN
byte arduino_mac[] = { xxx, xxx, xxx, xxx ...}
IPAddress arduino_ip ( 192, 168, 0, 253);
IPAddress dns_ip ( 192, 168, 0, 1);
IPAddress gateway_ip ( 192, 168, 0, 1);
IPAddress subnet_mask(255, 255, 255, 0);
void setup()
{
pinMode (13, INPUT_PULLUP); // set GPIO13 Onewire with Internal PULL_UP resistor on for it to work
Serial.begin(9600);
if(oneWire.reset()==1) { Serial.println ("OneWire has been reset correctly");}
else { Serial.println ("One wire reset was UNSUCCESSFUL!");}
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
sensors.setResolution(Therm_1,12);
sensors.setResolution(Therm_2,12);
sensors.setResolution(Therm_3,12);
sensors.setResolution(Therm_4,12);
sensors.setResolution(Therm_5,12);
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
Serial.print("Resolution set to:");
// sensors.setResolution(TEMPERATURE_PRECISION);
Serial.println(sensors.getResolution());
WiFi.config(arduino_ip, gateway_ip, subnet_mask);
WiFi.begin("xxxx", "xxxx");
Blynk.config(auth,IPAddress(xxx,xxx,xxx,xxx));
// Or like this:
//Blynk.begin(auth, "blynk-cloud.com", 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
while (Blynk.connect() == false) { // wait for Blynk to be connected
}
rtc.begin();
Serial.println ("BLYNK is Connected!");
// Blynk.syncAll(); // Real all virtualpins
// clockcounter = timer.setInterval (300L, gettime); // check if clock has updated every second
//Serial.print ("Clockcounter = ");
// Serial.println (clockcounter);
}
void loop()
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.println("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// printTemperature(Therm_1, 1);
// printTemperature(Therm_2, 2);
// printTemperature(Therm_3, 3);
// printTemperature(Therm_4, 4);
printTemperature(Therm_5, 5);
Blynk.run();
// deepSleep time is defined in microseconds. Multiply
// seconds by 1e6
// Serial.println ("I'm going to sleep");
clockDisplay();
if (clock_sync == 0) { gettime();}
// ESP.deepSleep(sleepTimeS * 1000000); not using this for now
Serial.println ("I'm awake");
delay (1000);
}
BLYNK_WRITE(V1)
{
// BLYNK_LOG("Got a value: %s", param.asStr());
// You can also use:
int i = param.asInt();
int state;
float tempC = sensors.getTempC(Therm_5);
Serial.print ("Temp C = ");
Serial.println(tempC);
Blynk.virtualWrite(V1,tempC);
V1_written = 1; // my little flag
}
bool isFirstConnect = true;
BLYNK_CONNECTED() {
if (isFirstConnect) {
// Blynk.syncAll(); // not doing much for now
isFirstConnect = false;
Serial.println ("Blynk First connected EXECUTED");
}
}
// function to print the temperature for a device to Blynk app
void printTemperature(DeviceAddress deviceAddress, int thermostat_label)
{
// method 1 - slower
//Serial.print("Temp C: ");
//Serial.print(sensors.getTempC(deviceAddress));
//Serial.print(" Temp F: ");
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Thermostat : ");
Serial.println(thermostat_label);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
Serial.println("...temperatere read ok..... pls wait");
Serial.println(" ");
Blynk.virtualWrite(V1,tempC);
}
// Digital clock display of the time in Blynk App
void clockDisplay()
{
// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
sprintf(Date, "%02d/%02d/%04d", day(), month(), year());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
Serial.print("Current time: ");
Serial.print(Time);
Serial.print(" ");
Serial.print(Date);
Serial.println();
// Send time to the App
Blynk.virtualWrite(V2, Time);
// Send date to the App
Blynk.virtualWrite(V3, Date);
}
void gettime(){
Serial.print ("Year in gettime = ");
Serial.println (year());
if(year() != 1970){ // we have got the right time now so kill the SimpleTimer
setSyncInterval(300); // normal 5 minute sync interval but not sure this actually works
// timer.deleteTimer(clockcounter); // don't need this timer anymore
sprintf(Date, "%02d/%02d/%04d", day(), month(), year());
Serial.println(Date);
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
Serial.println(Time);
clock_sync = 1; // set clock_sync flag to TRUE so we don't call gettime function again from void loop ()
}
else {
Serial.println ("still trying to sync :-(");
rtc.begin(); // keep trying to sync the time until year is not 1970
}
}