Programm disconnects dozens time a day

hi there
i took a piece of code someone wrote to display date from server on OLED display
i added blynk project and i’m getting a messeges all the time that my project went offline
need your help

my code::

// simplestesp8266clock.ino
// Libraries needed:
//  Time.h & TimeLib.h:
//  Timezone.h:
//  SSD1306.h & SSD1306Wire.h:
//  NTPClient.h:
//  ESP8266WiFi.h & WifiUDP.h:
// 128x64 OLED pinout:
// GND goes to ground
// Vin goes to 3.3V
// Data to I2C SDA (GPIO 4)
// Clk to I2C SCL (GPIO 5)
// OLED I2C ADDRESS   0x3d

#include <ESP8266WiFi.h>
#include <WifiUDP.h>
#include <String.h>
#include <Wire.h>
#include <SSD1306.h>
#include <SSD1306Wire.h>
#include <NTPClient.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
#include <BlynkSimpleEsp8266.h>
// Define NTP properties
#define NTP_OFFSET   60 * 60      // In seconds
#define NTP_INTERVAL 60 * 1000    // In miliseconds
#define NTP_ADDRESS  ""  // change this to whatever pool is closest (see

// Set up the NTP UDP client

// Create a display object
SSD1306  display(0x3d, 4, 5); //0x3d for the Adafruit 1.3" OLED, 0x3C being the usual address of the OLED

char auth[] = "XXXXXXX"; 
const char ssid[] = "Rooms";   // insert your own ssid 
const char password[] = "XXXXXX";
const char pass[] = "XXXXXX";// and password
String date;
String t;
const char * days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} ;
const char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"} ;
const char * ampm[] = {"AM", "PM"} ;
//const int ledPin = 0;// the number of the LED pin

  // You'll get HIGH/1 at startTime and LOW/0 at stopTime.
  // this method will be triggered every day
  // until you remove widget or stop project or
  // clean stop/start fields of widget
  //Serial.print("Got a value: ");

void setup () 
  Serial.begin(9600); // most ESP-01's use 115200 but this could vary
  //pinMode(ledPin, OUTPUT);
  //digitalWrite(ledPin, LOW);
  timeClient.begin();   // Start the NTP UDP client
  Wire.pins(4, 5);  // Start the OLED with GPIO 0 and 2 on ESP-01
  Wire.begin(4, 5); // 4=sda, 2=scl

  // Connect to wifi
  Serial.print("Connecting to ");
  display.drawString(0, 14, "Connecting to Wifi...");
  WiFi.begin(ssid, password);
  Blynk.begin(auth, ssid, pass);
  while (WiFi.status() != WL_CONNECTED) 
  Serial.print("Connected to WiFi at ");
  display.drawString(0, 28, "Connected.");


void loop() 
  if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
    date = "";  // clear the variables
    t = "";
    // update the NTP client and get the UNIX UTC timestamp 
    unsigned long epochTime =  timeClient.getEpochTime();

    // convert received time stamp to time_t object
    time_t local, utc;
    utc = epochTime;

    // Then convert the UTC UNIX timestamp to local time
    TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, +120};  //UTC - 5 hours - change this as needed
    TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, +180};   //UTC - 6 hours - change this as needed
    Timezone usEastern(usEDT, usEST);
    local = usEastern.toLocal(utc);

    // now format the Time variables into strings with proper names for month, day etc
    date += days[weekday(local)-1];
    date += ", ";
    date += months[month(local)-1];
    date += " ";
    date += day(local);
    date += ", ";
    date += year(local);

    // format the time to 12-hour format with AM/PM and no seconds
    t += hourFormat12(local);
    t += ":";
    if(minute(local) < 10)  // add a zero if minute is under 10
      t += "0";
    t += minute(local);
    t += " ";
    t += ampm[isPM(local)];

    // Display the date and time
    Serial.print("Local date: ");
    Serial.print("Local time: ");

    // print the date and time on the OLED
    display.drawStringMaxWidth(64, 12, 128, t);
    display.drawStringMaxWidth(64, 38, 128, date);
  else // attempt to connect to wifi again if disconnected
    display.drawString(0, 10, "Connecting to Wifi...");
    WiFi.begin(ssid, password);
    display.drawString(0, 24, "Connected.");
  //delay(10);    //Send a request to update every 10 sec (= 10,000 ms)

Your loop is kinda hefty! You should probably move the time sync and display stuff to its own functions and call them with timers instead. Putting much more than and in the main loop isn’t recommended.

1 Like

@elibt Welcome to the Blynk Forum.

First, I fixed your posted code formatting… please use this method, as required in the pinned topic that you should have read at the beginning: Welcome to Blynk Community!

Blynk - FTFC

Secondly there is entire sections (links at top of this page) one of which is called Help Center that has answers to many common questions and issues like you are running into

Here is a brief excerpt from one:

Avoiding the void

:point_up: :fire: :warning: VERY IMPORTANT: You can’t send sensor data in your void loop()

– Why?
– Because Blynk sends data over the Internet, and when you put something into void loop(), your microcontroller will execute it :scream_cat: gazillion number of times. Which means that Blynk Cloud will be flooded with gazillion messages from your hardware.

And when Blynk Cloud notices that, it automatically :scissors:︎ cuts your connection. Sorry…

– OK, what should I do then?
– Send sensor data in intervals!

Using timers to send sensor data in intervals

There are lots of ways to send data in intervals, but here is a simple one. We recommend using a BlynkTimer for that. It’s included in Blynk Library Package, so if you installed Library correctly, you are all set.

1 Like

i appreciate your help :slight_smile:
but unfortunatelly i couldnt understand how to use it in my code ?

I am not sure I understand… are you saying that words of direction and the links I provided (in GREEN) don’t work for you?

if you can’t make use of supplied guidance or read the Documents, then I don’t see how we can be much more help… we generally do not edit or fix your code for you, but we will help guide you in learning Blynk so you can create your own great IoT projects.

i had issues in my computer so link didnt work
will try it