Stuck on Blynk.run when phone is not connceted to Blynk

Hello Blynkers,
I am new to the use of this app and would like my code to run whether or not my phone is connected to blynk. Now i read about if Blynk.connect() statements, but my ESP01 is not loosing connection to blynk. Rather, when my phone exits the app the code seems to get stuck on Blynk.run and does not continue down with the code unless i log back in. Is there anyway of detecting when the phone is connected to the server? This way i can decide when a user is interested in using blynk and loop Blynk.run. Essentially, what i want to do is automate a process, and have the blynk app as a monitoring device. I donā€™t want monitoring to happen 24/7, just when the user feels the need to get involved. Please help if you can shed some light on the issue.
Thank you for reading!

Hi there, Your program should continue to run regardless if you have your phone connected or not. As long as the esp is still able to connect to the Blynk server.

Are you using the cloud server?
Does your esp still have Internet connection when you are having your problem?

Hi Nick,
Thanks for responding! I believe i may have worded my question poorly. But I am connected to the cloud server , and my ESP always has internet connection. My problem is that when i disconnect my phone from the server the code stops running, as though it were stuck on Blynk.run. Then i reconnect my phone and the code runs fine. Below is my loop:

void loop()
{

Blynk.run();

val = analogRead(sensor) ;
if (val<100)
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

}

So when i disconnect my phone from the Blynk cloud, the led stops blinking when my potentiometer is below 100.

Ok.

You need to eliminate any delays from your code. when you use delays in your code, the processor does nothing else. Blynk does not like that.

Investigate using the simple timer library and functions in place of the delay.

Thanks Nick, I will give that a shot.

1 Like

In response to Blynk not liking delays, I basically just took the code from " blink without delay" as a trigger for my potentiometer. I basically have the same problem. Everything works fine as long as my phone is connected to the server. If i turn the phone off the arduino with esp01 set up just wait for me to log back on to the app and then check the potentiometer. While the phone is logged off, the led is not triggered by changing potentiometer values. Wish there was a way to check if the phone is connected to the server!

void loop()
{

Blynk.run();
 
check= val;

val = analogRead(sensor) ;
if (val<100)
{
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

}

if (currentMillis - previousMillis >=2000) {
      digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level)
       
      }

}

}

Ok. Well the advice of not using delays is still very valid. Use simple timer instead.

As for your continuing issue, are you using ESP as a shield or as a standalone? Im guessing from your posts that you are using ESP as a shield for an Arduino board. I believe that this hardware setup can be troublesome but maybe others can comment as I have never tried that particular combination.

With regards to you wanting a way to detect a phone connection to the Blynk server within the Arduino code, I totally agree that this would be an awesome feature.

Thanks for mentioning this library. I think its useful regardless.

Thanks for your input!

I tried your code using a homebrew-duino, an ESP-01 connected as a shield, and a pot on a breadboard (photo below).

With only your loop() function as a reference I filled in what I had to - everything above loop() plus the lines inside of loop() commented with ā€˜jrobertā€™ are mine; the rest is yours. This program blinks the LED when the analog value < 100 and not otherwise. My Blynk app was connected to Blynk.

To replicate your test with the Blynk app, I tapped the Blynk panelā€™s Stop key, back-arrowed out of Blynk to the Android desktop, and put my tablet to sleep. Result: Blinking continued to respond to the analog value and did not depend on my tabletā€™s connectedness to Blynk.
As a second test, I woke the tablet and put it into airplane mode to be sure it could not be connected to Blynk. Result: As before, the Arduinoā€™s blinking responded to the analog value.

Something in my code must be different from yours in a critical way. Here is mine for you test/compare:

#include <Arduino.h>

#include <Blynk.h>
#include <BlynkESP8266_Lib.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <localWiFi.h>  // my WiFi and Blynk credentials
#include <SoftwareSerial.h>

const uint8_t led = 13;                // on-board LED
const uint8_t sensor = A0;             // analog in - pot wiper
uint16_t check, val;
const uint32_t interval = 1500L;
static unsigned long previousMillis;

SoftwareSerial EspSerial(2,3);                // s/w rx, tx
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

void setup() {
   pinMode(led, OUTPUT);
   Serial.begin(115200);
   
   // Set ESP8266 baud rate
   EspSerial.begin(ESP8266_BAUD);
   delay(10);

   Blynk.begin(BLYNKJRS_Test1, wifi, WIFIID_Procy2, WIFIPW_Procy2);
}


void loop()
{
  Blynk.run();

  check = val;

  val = analogRead(sensor) ;
  Serial.println(val);            // jrobert added: show the analog value
  if (val < 100)
  {
    unsigned long currentMillis = millis();
    
    if (currentMillis - previousMillis >= interval) {
      digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
    }

    if (currentMillis - previousMillis >= 2000) {
      digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level)
      previousMillis = currentMillis;       // jrobert added
    }
  }
}

Hardware configuration:
Red & Black = +5 & Gnd (everywhere)
Violet = A0 in from potā€™s center tap
Yellow = Esp01 Tx -> '328 SoftwareSerial Rx
Green = Esp01 Rx -> '328 SoftwareSerial Tx

Thanks JRobert. I will try your code when i get home later today. Yes i connect a little differently.

#define BLYNK_PRINT Serial
#include < ESP8266_Lib.h>
#include < BlynkSimpleShieldEsp8266.h>
#include < SimpleTimer.h>

char auth = ā€œ";
char ssid[] = "
";
char pass[] = "
**********************ā€;

// or Software Serial on Uno, Nanoā€¦
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{
pinMode(12, OUTPUT);
pinMode(11, INPUT);
// Set console baud rate
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
// Blynk.connectWiFi(ssid, pass);
// Blynk.config(auth);
Blynk.begin(auth, wifi, ssid, pass);
}

Then the loop.
Thanks for sharing! ill reply once i see how your code does on my side.

Use the #include... set that works for you. Your IDE will fail to find a couple of files Iā€™ve #included. They are empty files Iā€™ve had to add to my libraries to make them compatible with both the Arduino IDE and eclipse Arduino to be able to use them interchangeably.

Hey JRobert,
My free time has been cut short a little bit, but i tried out your code and it works just fine without phone engaging with Blynk server. I couldnā€™t figure out quickly just what was wrong with mine. I will look into it soon. At any rate, thank you very much!