BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Functions that must to run frequently with blynk

I’m using Arduino Uno with ESP8266 for my numeric display project. I wanted to use it to display time with connected to it RTC and use Blynk slider to change mode of displayed data to date or other custom variables that I getting from some API. The code issue that I run into is that the Blynk requests take some time to complete and with the current setup in the time mode my code is skipping some seconds. So how can I send the blynk requests and not disturb display function timing?

The second problem that I have encountered is sometimes when I change the slider in Blynk app, Arduino runs into a problem with too big package and stops the program. I don’t know is it normal or not

I only provided the Blynk function and loop


BLYNK_WRITE(V10) 
{
  if(param.asInt()==mode){return;}
switch(param.asInt()){//change DisplayMode
  case 1:
  {
     mode=1;
     Serial.println("hour");
     //here functions to change displayd data to hour
     break;
    }
  case 2:
  {
    mode=2;
    Serial.println("date");
   //here functions to change displayd data to date 
    break;
  }
  case 3:
  {
     mode=3;
    Serial.println("Custom");    
     //here functions to change displayd data to API 
    break;
    }
  }
}
BLYNK_WRITE(V9)// set time to RTC from Blynk server
{
  if(param.asInt()==1){
  setDateToRTC(second(),minute(),hour(),weekday(),day(),month());
  }
}

void loop()
{
  delay(1000);
  if(Blynk.connected()){
      Blynk.run();
      Blynk.syncVirtual(V10);
      Blynk.syncVirtual(V9);  
    }
    
  if(mode==1){
  ClockHourWrite();
  }
  else if(mode==2)
  ClockDateWrite();
  else //mode 3
  ClockCustomWrite();
  

I guess that it’s these two lines that are slowing things down, and they shouldn’t be in there anyway.

I’m guessing that you’ve read about BLYNK_CONNECTED() and how you would normally add Blynk.syncVirtual(vPin) commands in to that special callback function, and confused this with Blynk.connected()

Also, from a Blynk point of view, doing this in your void loop isn’t good practice…

You don’t need this if test to be executed hundreds, if not thousands of time per second, so you’d be better-off putting this code into a function and calling it with a timer.

Pete.

I have changed the code and before that in the meanwhile I have got a new router, I have to change the SSID and password in code too. some new problems come up, like that the code stops at the Blynk.begin function in setup

image

and in the Blynk app the device is reporting connection and instant disconnection from server, every few seconds.

First if all, posting screenshots of your serial monitor output isn’t very helpful, as it’s very difficult to read. Far better to copy the contents of the serial monitor and paste it between triple backticks.

To make any sense of the serial monitor output, we also nee to see your updated code.

Pete.

This all code in my program, after Blynk.begin it stops and dont print debug messages.

#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <WidgetRTC.h>


char auth[] = "";//token is here!

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Lanberg_router_24_06562"; 
char pass[] = "L@@n&3rG";

#define EspSerial Serial1
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(5, 6); // RX, TX
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
WidgetRTC Wrtc;

BlynkTimer timer;

#include <ShiftRegister74HC595.h>
#include <math.h>

#include <Wire.h>
#include "RTClib.h"



RTC_DS1307 rtc;

ShiftRegister74HC595<3> sr (2,3,4);

byte zero=0;


const int  minusButton=8; 
const int  plusButton=9;

static int secPins [2][4]={{0,1,2,3},{4,5,6,7}};// first units then dec
static int minPins [2][4]={{8,9,10,11},{12,13,14,15}};
static int hourPins [2][4]={{16,17,18,19},{20,21,22,23}};

DateTime last=(2020,03,06,00,00,00,00);



byte decToBcd(byte val) {
  return ((val/10*16) + (val%10));
}
byte bcdToDec(byte val) {
  return ((val/16*10) + (val%16));
}

void setDateToRTC(byte seconde, byte minutee, byte houre, byte weekDaye, byte monthDaye, byte monthe){
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(seconde));
  Wire.write(decToBcd(minutee));
  Wire.write(decToBcd(houre));
  Wire.write(decToBcd(weekDaye));
  Wire.write(decToBcd(monthDaye));
  Wire.write(decToBcd(monthe));
  Wire.write(decToBcd(byte(2021)));

  Wire.write(zero); //start

  Wire.endTransmission();

}

void ClockHourWrite(){// get time and prints on the clocks digits
  
  DateTime now = rtc.now();
  Serial.println(String(now.second())+":"+now.minute()+":"+now.hour());
  Serial.println(now.second());
  nixieWrite(secPins[0],now.second()%10);//sec unit
  if(floor(last.second()/10)!=floor(now.second()/10)){nixieWrite(secPins[1],floor(now.second()/10));}//sec dec
  
  if(last.minute()%10!=now.minute()%10){nixieWrite(minPins[0],now.minute()%10);}
  if(floor(last.minute()/10)!=floor(now.minute()/10)){nixieWrite(minPins[1],floor(now.minute()/10));}
  
  if(last.hour()%10!=now.hour()%10){nixieWrite(hourPins[0],now.hour()%10);}
  if(floor(last.hour()/10)!=floor(now.hour()/10)){nixieWrite(hourPins[1],floor(now.hour()/10));}
  
  last=now;
  
  }

void ClockDateWrite(){// get time and prints on the clocks digits
  
  DateTime now = rtc.now();
  Serial.println(String(now.day())+":"+now.month()+":"+now.year()%2000);
  
  if(last.day()!=now.day()){
  nixieWrite(secPins[0],(now.year()%2000)%10);
  nixieWrite(secPins[1],floor((now.year()%2000)/10));
  
  nixieWrite(minPins[0],now.month()%10);
  nixieWrite(minPins[1],floor(now.month()/10));
  
  nixieWrite(hourPins[0],now.day()%10);
  nixieWrite(hourPins[1],floor(now.day()/10));
  
  last=now;
  }
  }
  
  void ClockCustomWrite(){
    //This will be custom number to display
    
    }
  
  void nixieWrite(int pins[],uint8_t value){// gets set of pins for nixie digit and number, and sends it to 
     sr.set(pins[3],(value & 0x08) >> 3);
     sr.set(pins[2],(value & 0x04) >> 2);
     sr.set(pins[1],(value & 0x02) >> 1);
     sr.set(pins[0],value & 0x01);
  }

/*BLYNK_CONNECTED() {
  // Synchronize time on connection
  Wrtc.begin();
}
*/

int mode=1;
void clockLoop(){
  Serial.println("clockLoop");
  Blynk.syncVirtual(V10);
  Blynk.syncVirtual(V9);
      
  if(mode==1){
  ClockHourWrite();
  }
  else if(mode==2)
  ClockDateWrite();
  else //mode 3
  ClockCustomWrite();
  }



//char buf[30];

void setup()
{
  // Debug console
  Serial.begin(9600);
  

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(100);

  Blynk.begin(auth, wifi, ssid, pass);
  delay(100);
  Serial.println("connected up!");

  timer.setInterval(1000L, clockLoop);//every second call clockloop function

    //rtc thing:
  if (! rtc.begin()) {
  Serial.println("rtcNotRunning");
}
//if (! rtc.isrunning()) {
  
  //setDateToRTC(second(),minute(),hour(),weekday(),day(),month());
  DateTime now = rtc.now();
  Serial.println(String(now.second())+":"+now.minute()+":"+now.hour()+"   "+now.year());
  nixieWrite(hourPins[1],floor(now.hour()/10));
  nixieWrite(hourPins[0],now.hour()%10);
  nixieWrite(minPins[1],floor(now.minute()/10));
  nixieWrite(minPins[0],now.minute()%10);
  nixieWrite(secPins[1],floor(now.second()/10));
  nixieWrite(secPins[0],now.second()%10);
  
}

 
BLYNK_WRITE(V10) // V10 is the number of Virtual Pin  
{
  if(param.asInt()==mode){return;}
switch(param.asInt()){//change DisplayMode
  case 1:
  {
     mode=1;
     Serial.println("hour");
     DateTime now = rtc.now();
     nixieWrite(hourPins[1],floor(now.hour()/10));
     nixieWrite(hourPins[0],now.hour()%10);
     nixieWrite(minPins[1],floor(now.minute()/10));
     nixieWrite(minPins[0],now.minute()%10);
     nixieWrite(secPins[1],floor(now.second()/10));
     nixieWrite(secPins[0],now.second()%10);
     break;
    }
  case 2:
  {
    mode=2;
    Serial.println("date");
    DateTime now = rtc.now();
    nixieWrite(secPins[0],(now.year()%2000)%10);
    nixieWrite(secPins[1],floor((now.year()%2000)/10));
    nixieWrite(minPins[0],now.month()%10);
    nixieWrite(minPins[1],floor(now.month()/10));
    nixieWrite(hourPins[0],now.day()%10);
    nixieWrite(hourPins[1],floor(now.day()/10));
    break;
  }
  case 3:
  {
     mode=3;
    Serial.println("Custom");    
    break;
    }
  }
}
BLYNK_WRITE(V9)// set time to RTC from Blynk server
{
  if(param.asInt()==1){
  setDateToRTC(second(),minute(),hour(),weekday(),day(),month());
  }
}

void loop()
{
  Blynk.run();
  timer.run();

}
}

It’s part of the code, where is the rest?

Pete.

I have updated the last comment

Looks like you disregarded what I said previously…

This line is for use with a Mega, not an Uno.

Pete.

Still prints that it cant connect to wifi (with connecting and disconnecting in app) and it is stopps at Blynk.begin

image

But when i comment all the timer stuff it goes through loop normally and connects to wifi with no problems

image



#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <WidgetRTC.h>


char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Lanberg_router_24_06562"; //RouterTOTO 2.4GHz
char pass[] = "L@@n&3rG";//malina123

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(5, 6); // RX, TX
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
WidgetRTC Wrtc;

BlynkTimer timer;

#include <ShiftRegister74HC595.h>
#include <math.h>

#include <Wire.h>
#include "RTClib.h"



RTC_DS1307 rtc;

ShiftRegister74HC595<3> sr (2,3,4);

byte zero=0;


const int  minusButton=8; 
const int  plusButton=9;

static int secPins [2][4]={{0,1,2,3},{4,5,6,7}};// first units then dec
static int minPins [2][4]={{8,9,10,11},{12,13,14,15}};
static int hourPins [2][4]={{16,17,18,19},{20,21,22,23}};

DateTime last=(2020,03,06,00,00,00,00);



byte decToBcd(byte val) {
  return ((val/10*16) + (val%10));
}
byte bcdToDec(byte val) {
  return ((val/16*10) + (val%16));
}

void setDateToRTC(byte seconde, byte minutee, byte houre, byte weekDaye, byte monthDaye, byte monthe){
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(seconde));
  Wire.write(decToBcd(minutee));
  Wire.write(decToBcd(houre));
  Wire.write(decToBcd(weekDaye));
  Wire.write(decToBcd(monthDaye));
  Wire.write(decToBcd(monthe));
  Wire.write(decToBcd(byte(2021)));

  Wire.write(zero); //start

  Wire.endTransmission();

}

void ClockHourWrite(){// get time and prints on the clocks digits
  
  DateTime now = rtc.now();
  Serial.println(String(now.second())+":"+now.minute()+":"+now.hour());
  Serial.println(now.second());
  nixieWrite(secPins[0],now.second()%10);//sec unit
  if(floor(last.second()/10)!=floor(now.second()/10)){nixieWrite(secPins[1],floor(now.second()/10));}//sec dec
  
  if(last.minute()%10!=now.minute()%10){nixieWrite(minPins[0],now.minute()%10);}
  if(floor(last.minute()/10)!=floor(now.minute()/10)){nixieWrite(minPins[1],floor(now.minute()/10));}
  
  if(last.hour()%10!=now.hour()%10){nixieWrite(hourPins[0],now.hour()%10);}
  if(floor(last.hour()/10)!=floor(now.hour()/10)){nixieWrite(hourPins[1],floor(now.hour()/10));}
  
  last=now;
  
  }

void ClockDateWrite(){// get time and prints on the clocks digits
  
  DateTime now = rtc.now();
  Serial.println(String(now.day())+":"+now.month()+":"+now.year()%2000);
  
  if(last.day()!=now.day()){
  nixieWrite(secPins[0],(now.year()%2000)%10);
  nixieWrite(secPins[1],floor((now.year()%2000)/10));
  
  nixieWrite(minPins[0],now.month()%10);
  nixieWrite(minPins[1],floor(now.month()/10));
  
  nixieWrite(hourPins[0],now.day()%10);
  nixieWrite(hourPins[1],floor(now.day()/10));
  
  last=now;
  }
  }
  
  void ClockCustomWrite(){
    //This will be custom number to display
    
    }
  
  void nixieWrite(int pins[],uint8_t value){// gets set of pins for nixie digit and number, and sends it to 
     sr.set(pins[3],(value & 0x08) >> 3);
     sr.set(pins[2],(value & 0x04) >> 2);
     sr.set(pins[1],(value & 0x02) >> 1);
     sr.set(pins[0],value & 0x01);
  }
/*
BLYNK_CONNECTED() {
  // Synchronize time on connection
  Wrtc.begin();
}
*/

int mode=1;
void clockLoop(){
  Serial.println("clockLoop");
  //Blynk.syncVirtual(V10);
 //Blynk.syncVirtual(V9);
      
  if(mode==1){
  ClockHourWrite();
  }
  else if(mode==2)
  ClockDateWrite();
  else //mode 3
  ClockCustomWrite();
  }



//char buf[30];

void setup()
{
  // Debug console
  Serial.begin(9600);
  

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(100);

  Blynk.begin(auth, wifi, ssid, pass);
  
  Serial.println("connected up!");

  timer.setInterval(1000L, clockLoop);//every second call clockloop function

    //rtc thing:
  if (! rtc.begin()) {
  Serial.println("rtcNotRunning");
}
//if (! rtc.isrunning()) {
  
  //setDateToRTC(second(),minute(),hour(),weekday(),day(),month());
  DateTime now = rtc.now();
  Serial.println(String(now.second())+":"+now.minute()+":"+now.hour()+"   "+now.year());
  nixieWrite(hourPins[1],floor(now.hour()/10));
  nixieWrite(hourPins[0],now.hour()%10);
  nixieWrite(minPins[1],floor(now.minute()/10));
  nixieWrite(minPins[0],now.minute()%10);
  nixieWrite(secPins[1],floor(now.second()/10));
  nixieWrite(secPins[0],now.second()%10);
  
}

 
BLYNK_WRITE(V10) // V10 is the number of Virtual Pin  
{
  if(param.asInt()==mode){return;}
switch(param.asInt()){//change DisplayMode
  case 1:
  {
     mode=1;
     Serial.println("hour");
     DateTime now = rtc.now();
     nixieWrite(hourPins[1],floor(now.hour()/10));
     nixieWrite(hourPins[0],now.hour()%10);
     nixieWrite(minPins[1],floor(now.minute()/10));
     nixieWrite(minPins[0],now.minute()%10);
     nixieWrite(secPins[1],floor(now.second()/10));
     nixieWrite(secPins[0],now.second()%10);
     break;
    }
  case 2:
  {
    mode=2;
    Serial.println("date");
    DateTime now = rtc.now();
    nixieWrite(secPins[0],(now.year()%2000)%10);
    nixieWrite(secPins[1],floor((now.year()%2000)/10));
    nixieWrite(minPins[0],now.month()%10);
    nixieWrite(minPins[1],floor(now.month()/10));
    nixieWrite(hourPins[0],now.day()%10);
    nixieWrite(hourPins[1],floor(now.day()/10));
    break;
  }
  case 3:
  {
     mode=3;
    Serial.println("Custom");    
    break;
    }
  }
}
BLYNK_WRITE(V9)// set time to RTC from Blynk server
{
  if(param.asInt()==1){
  setDateToRTC(second(),minute(),hour(),weekday(),day(),month());
  }
}

void loop()
{
  Blynk.run();
  timer.run();

}

I’d suggest that you take-out some of the nested routines that are called from the timed routine, and see which ones are causing the problems.

Pete.

Even if the timed routine is empty, It still stops at Blynk.begin and don’t process any further. I think the problem is the timer itself somehow breaking the Blynk.begin

I don’t understand what you mean by this.

Pete.

Even if the timed routine is empty, It still stops at Blynk.begin and don’t process any further. I think the problem is the timer itself somehow breaking the Blynk.begin

I don’t understand what exactly you’ve changed when you’ve done this.
Replying with very short and somewhat cryptic answers isn’t going to get your problem solved quickly, and you risk community members such as myself becoming bored with keep having to ask questions and getting drip-fed the results.

Maybe you should run one of the simpler sketch-builder sketches to confirm that your library installation is working with timers.

Pete.

It works with this simple sketch

define BLYNK_PRINT Serial


//#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "LRJWdn_W0OOln4_5KmGpZRwnsEB_i9Ev";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Lanberg_router_24_06562"; //RouterTOTO 2.4GHz
char pass[] = "L@@n&3rG";//malina123



// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(5,6); // RX, TX
BlynkTimer timer;

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void clockLoop(){
  Serial.println("123");
  }

void setup()
{
  // Debug console
  Serial.begin(9600);
  

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  Serial.println("connected up!");
  timer.setInterval(1000L, clockLoop);//every second call clockloop function
  

}

void loop()
{
  Blynk.run();
  timer.run();
}