Hi, Blynkers.
My project must continue work offline if connection with Blynk server is losed. For this case in hardware board I use
RTC ds3231, which must be syncronised RTC widget. I wrote the test program for debuguding this variant. This program worked well, but after updating Blynk-library to v0.4.8 I cannot to syncronise ds3231 with RTC widget.
Here is my program:
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <EEPROM.h>
#include <ESP8266mDNS.h>
#include <WiFiManager.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include "RTClib.h"
#define sda 2
#define scl 14
//#define eeprom_address 0x50 //Адрес устройства
int eeprom_address = 0x50;
WidgetRTC rtc;
WiFiManager wifi;
SimpleTimer timer;
RTC_DS3231 Hrtc;
String ssid;
String pass;
int analogPin = 0;
int rssi;
bool scan_done = false;
bool isFirstConnect = false;
bool write_eeprom_done = false;
bool read_eeprom_done = false;
DateTime now_;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char auth[] = "xxxxxxxxxxxxx";
unsigned int cur_moisture = 0;
//-------------------------------------
void EEPROM_WriteByte(int dev, unsigned int eeaddress, byte data)
{
Wire.beginTransmission(dev);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(5);
}
//-------------------------------------
byte EEPROM_ReadByte(int dev, unsigned int eeaddress)
{
byte rdata = 0xFF;
Wire.beginTransmission(dev);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(dev,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
//-------------------------------------
void store_data()
{
Serial.print("Write to 24C32N");
byte data=0;
for (unsigned int i = 0; i < 64; i++)
{
EEPROM_WriteByte(eeprom_address, i, data);
data++;
}
Serial.println("Done.");
}
//-------------------------------------
void restore_data()
{
int val = 0;
byte data=0;
Serial.println("Read 24C32N: ");
for (unsigned int i = 0; i < 64; i++)
{
data=EEPROM_ReadByte(eeprom_address, i);
Serial.print(data);
Serial.print(" ");
val++;
if (val >= 8) {
val = 0; //Для удобства, в строчку по 8 байт
Serial.println();
}
}
}
//-------------------------------------
void clockDisplay()
{
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + "." + month() + "." + year();
Serial.println(currentTime);
Blynk.virtualWrite(V0, currentTime);
Blynk.virtualWrite(V1, currentDate);
}
//---------------------------------------
unsigned int moist_sen_level()
{
unsigned int val;
val = analogRead(0);
Serial.print("ADC val="); Serial.println(val);
val = (val * 100) / 1023;
Serial.print("moist val(%)="); Serial.println(val);
return (val);
}
//----------------------------------------
void MyWiFi()
{
int mytimeout = millis() / 1000;
ssid = wifi.getSSID();
pass = wifi.getPassword();
Serial.print("WiFi param. are: ");
Serial.print(ssid); Serial.print("__"); Serial.println(pass);
WiFi.begin(ssid.c_str(), pass.c_str());
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
if ((millis() / 1000) > mytimeout + 4)
{ // try for less than 4 seconds to connect to WiFi router
break;
}
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("\nIP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("\nCheck Router ");
}
Blynk.config(auth);
mytimeout = millis() / 1000;
while (Blynk.connect() == false) {
if ((millis() / 1000) > mytimeout + 4)
{ // try for less than 4 seconds
break;
}
}
}
void CheckConnection()
{
if (!Blynk.connected())
{
Serial.println("Not connected to Blynk server");
MyWiFi();
}
/*
else
{
isFirstConnect = 1;
isSyncHrtcProvider = 0;
Serial.println("Still connected to Blynk server");
}
*/
}
//------------------------------------
int i2cscan() {
//scanning i2c
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (error == 4)
{
Serial.print("Unknow error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
}
//------------------------------------
//----------------------------
void syncRTC()
{
if (Blynk.connect())
{
Hrtc.adjust(DateTime(year(), month(), day(), hour(), minute(), second()));
}
}
//----------------------------
void HrtcRead()
{
now_ = Hrtc.now();
Serial.print(now_.hour(), DEC);
Serial.print('-');
Serial.print(now_.minute(), DEC);
Serial.print('-');
Serial.print(now_.second(), DEC);
Serial.println();
}
//------------------------------------
void main_function()
{
if (!write_eeprom_done)
{
long Tbeg_write_eeprom=millis();
store_data();
write_eeprom_done=1;
Serial.print("T write in EEPROM = ");
Serial.println(millis() - Tbeg_write_eeprom);
}
if (!read_eeprom_done)
{
long Tbeg_read_eeprom=millis();
restore_data();
read_eeprom_done=1;
Serial.print("T read EEPROM = ");
Serial.println(millis() - Tbeg_read_eeprom);
}
cur_moisture = moist_sen_level();
Blynk.virtualWrite(V2, cur_moisture);
rssi = WiFi.RSSI();
Serial.print("RSSI="); Serial.println(rssi);
}
//----------------------------------------------------------------------------
void loop()
{
if (!scan_done)
{
i2cscan();
scan_done = true;
}
if (Blynk.connected())
{
Blynk.run();
}
timer.run();
}
//-------------------------------------------
void setup()
{
Serial.begin(115200);
wifi.setTimeout(180);
if (!wifi.autoConnect("AutoConnectAP"))
{
Serial.println("failed to connect and hit timeout");
delay(3000);
}
else
{
Serial.println("connected");
ssid = wifi.getSSID();
pass = wifi.getPassword();
Blynk.config(auth); //Blynk.config(auth,WiFi.localIP());
Serial.print("ssid="); Serial.println(ssid.c_str());
Serial.print("pass="); Serial.println(pass.c_str());
}
if (Blynk.connect() == false)
{
delay(4000);
}
if (Blynk.connect() == true)
{
isFirstConnect = 1;
}
Serial.print("Blynk.connect() = "); Serial.println(Blynk.connect());
Wire.begin(sda, scl);
setSyncInterval(1);
rtc.begin();
timer.setInterval(20000L, syncRTC);
timer.setInterval(10000L, HrtcRead);
timer.setInterval(10000L, clockDisplay);
timer.setInterval(1000L, main_function);
timer.setInterval(20000L, CheckConnection);
Serial.println("done setup");
}
Here what I got:
Done.
T write in EEPROM = 329
Read 24C32N:
255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255
T read EEPROM = 18
ADC val=1
moist val(%)=0
RSSI=-82
ADC val=1
moist val(%)=0
RSSI=-82
ADC val=0
moist val(%)=0
RSSI=-82
ADC val=0
moist val(%)=0
RSSI=-82
ADC val=0
moist val(%)=0
RSSI=-82
ADC val=2
moist val(%)=0
RSSI=-81
ADC val=0
moist val(%)=0
RSSI=-81
ADC val=0
moist val(%)=0
RSSI=-81
ADC val=4
moist val(%)=0
RSSI=-81
0-0-0
15:11:40
ADC val=0
moist val(%)=0
RSSI=-86
ADC val=0
moist val(%)=0
RSSI=-83
ADC val=0
moist val(%)=0
RSSI=-82
ADC val=1
moist val(%)=0
RSSI=-81
ADC val=0
moist val(%)=0
RSSI=-82
ADC val=2
moist val(%)=0
RSSI=-83
ADC val=0
moist val(%)=0
RSSI=-83
ADC val=0
moist val(%)=0
RSSI=-82
ADC val=0
moist val(%)=0
RSSI=-81
ADC val=0
moist val(%)=0
RSSI=-82
0-0-0
15:11:49
In output of serial monitor can be see:
0-0-0 this is the time from ds3231
15:11:40 this is the time of Blynk RTC widget.
What I should to change for avoid such behaviour of program?
Thank You.