I have two Wemos D1 Minis with attached DHT22 sensors which sends temperature and humidity to my Blynk app.
One of the D1 stops sending data after some time - the code is identical. So I decided to add a “timestamp” for each D1 Mini by sending the actual time to a virtual pin - if this time does not change, the sensor stopped working.
The problem: The RTC time starts with 1970 and nevers syncs. I already tried different codes and even the example code shows “1970”…
Do you have a solution? I am on holiday now and wanted to leave the sensors in the holiday home to control the temperature…
Attached also a screenshot of my Blynk app:
Many thanks in advance!
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <DHT.h>
#define DHTPIN 4 //pin gpio 12 in sensor
#define DHTTYPE DHT22 // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);
char auth[] = "4Llxxxxxxxxxxxxxxx"; // Put your Auth Token here. (see Step 3 above)
SimpleTimer timer;
WidgetRTC rtc;
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
dht.begin();
rtc.begin();
Blynk.begin(auth, "WSHxxxxx", "maxxxxxxxxxxx"); //insert here your SSID and password
Blynk.syncAll();
setSyncInterval(60000);
// Setup a function to be called every second
timer.setInterval(60000L, sendUptime); // Update alle 60 Sekunden
}
void sendUptime()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
String currentTime = String(hour()) + ":" + minute() + ":" + second() + ", ";
String currentDate = String(day()) + "." + month() + "." + year();
Blynk.virtualWrite(16, currentTime, currentDate);
Blynk.virtualWrite(12, t); // virtual pin
Blynk.virtualWrite(13, h); // virtual pin
}
void loop()
{
Blynk.run();
timer.run();
}
The rtc has to get the time from the Blynk Server. Therefore, the function rtc.begin() must be called after the board has connected to Blynk Server, by using BLYNK_CONNECTED() function. Otherwise, the time will start from Jan 1st 1970. The same rule will apply to other functions such as Blynk.syncAll(), etc.
You can change the program to something similar to the following code (which uses the better BlynkTimer, and optional Blynk_WM)
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#define USE_SPIFFS true
//#define USE_SPIFFS false
//#define USE_BLYNK_WM true // https://github.com/khoih-prog/Blynk_WM
#define USE_BLYNK_WM false
#define USE_SSL false
//#define USE_SSL true
#if USE_SSL
#include <WiFiClientSecure.h>
#if USE_BLYNK_WM
#include <BlynkSimpleEsp8266_SSL_WM.h> // https://github.com/khoih-prog/Blynk_WM
#else
#include <BlynkSimpleEsp8266_SSL.h>
#endif
#else
#include <ESP8266WiFi.h>
#if USE_BLYNK_WM
#include <BlynkSimpleEsp8266_WM.h> // https://github.com/khoih-prog/Blynk_WM
#else
#include <BlynkSimpleEsp8266.h>
#endif
#endif
#if !USE_BLYNK_WM
//BLYNK AUTHENTICATION TOKEN
char auth[] = "******";
// MY WIFI CREDENTIALS
char ssid[] = "****";
char pass[] = "****";
#endif
#include <WidgetRTC.h>
#include <DHT.h>
#define DHTPIN 4 //pin gpio 12 in sensor
#define DHTTYPE DHT22 // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
WidgetRTC rtc;
BLYNK_CONNECTED()
{
rtc.begin();
//synchronize the state of widgets with hardware states
Blynk.syncAll();
}
void setup()
{
Serial.begin(115200); // See the connection status in Serial Monitor
dht.begin();
#if USE_BLYNK_WM
Blynk.begin();
#else
Blynk.begin(auth, ssid, pass); //insert here your SSID and password
#endif
// Setup a function to be called every second
timer.setInterval(10000L, sendUptime); // Update alle 10 Sekunden
}
void sendUptime()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
String currentTime = String(hour()) + ":" + String(minute()) + ":" + String(second());
String currentDate = String(day()) + "." + String(month()) + "." + String(year());
Blynk.virtualWrite(V16, currentTime);
Blynk.virtualWrite(V15, currentDate);
Blynk.virtualWrite(V12, t); // virtual pin
Blynk.virtualWrite(V13, h); // virtual pin
}
void loop()
{
Blynk.run();
timer.run();
}
@Hans5546 please edit your post and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
I thought about while year == 1970 in BLYNK_CONNECTED() but don’t want the whole sketch to hang just because of the time, a delay would be no good either. Maybe a millis function would be best?
Many thanks for the replys! Especially to Khoih - your code was working without any changes
One interesting thing: with my old code the time adjusted itself on the next day (maybe after 24h runtime?). But with khoih’s code the time was instantly correct.
And it was absolutely necessary to have the RTC widget in the blynk app! Without it, the time did not sync - maybe this is what 877 needs?
I modified khoih’s code a little bit to restart the esp automatically once a day. I will post the code later.