Integrating 1 Arduino Uno, 2 Bluetooth modules, 2 phones with one Blynk app interface

Hello Blynk Community

I am currently working on a project that uses an arduino uno to control a strip of LED lights. I am using an HC 05 bluetooth module to provide the medium of communication between my hardware and the Blynk app interface that i developed. I understand that Bluetooth is a point to point communication medium i.e. a maximum of 2 devices can ever be paired and communicate with each other. That is to say, for example that two people (with one phone each) cannot control the LED strip via the Blynk app simultaneously. After searching through this forum and the internet i learned 2 things:

  1. The simultaneous control can be achieved with a wifi communication medium.
  2. The arduino uno is able to connect to more than one serial device using software serial.

Since option 1 is not a suitable solution for me at the moment i would like to know if its possible, by exploring option 2, to connect 2 phones (each with the Blynk app interface ) to the single arduino uno controller but via 2 different bluetooth modules i.e. Phone1 connects to the Blynk app via Bluetooth module1 while Phone 2 connects to the same Blynk app via Bluetooth module2. Is such a configuration possible?

Hello again Blynk community

Is there no one on the forum who can offer some advice?

Blynk’s BT option is limited and only for use with one device at a time on the primary communication channel (it basically used the Phone/App as a go between from the device and the server, instead of the device connecting to the server directly).

As you already surmised, If you want to use multiple phones on one project/device then WiFi is the way to go.

Hello GTT

Thanks for the response. I have successfully connected my system consisting of an Arduino Mega 2560 (and using a very similar program on the Arduino Uno) to Blynk via an ESP8266 WiFI module using the code i adapted from the Blynk example sketches:

/*************************************************************
  Download latest Blynk library here:
  https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  WARNING!
    It's very tricky to get it working. Please read this article:
    http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino Mega ADK board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial


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

#include <FastLED.h>
#define LED_PIN     10
#define NUM_LEDS    30
CRGB leds[NUM_LEDS];

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

// Your WiFi credentials.
// Set password to "" for open networks.
//WIFI 1 Credentials
char ssid0[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
char pass0[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
/WIFI 2 Credentials
char ssid1[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
char pass1[] = "xxxxxxxxxxxxxxxxxxxxxxxx";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// 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()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  //String APlist = wifi.getAPList ();
  //Serial.print ("AP List:");
  //Serial.println (APlist);
  Serial.println("Establishing WIFI connection with Blynk...");

  //Blynk.begin(auth, wifi, ssid0, pass0);                        //Wifi credentials 1
  Blynk.begin(auth, wifi, ssid1, pass1);                          //Wifi credentials 2
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

  
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  
  
  Serial.println("LED strip control with preset light settings using Blynk ");
  timer1.setInterval(100L, LightSettings);
  
}

With a manual selection of credentials 1 or 2 everything works great. However i thought to improve the capability of my project by allowing the firmware to connect to whichever of the networks was available (i.e. if Wifi credentials 1 is not available on startup then check if Wifi credentials 2 is available and connect to it). Since i learned that Blynk.begin() is a blocking function (which ever credentials i used with the first Blynk.begin("credentials1") it will hung there if that network was not available did not try the next Blynk.begin(credentials2) ).
The attempted to resolve this by first trying to connect to the network using the Wifi.begin() and once i have a connection then use a Blynk.config() to establish a connection with blynk as shown in the sketch below:

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial


#include <Blynk.h>
#include <WiFi.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include <FastLED.h>
#define LED_PIN     10
#define NUM_LEDS    30
CRGB leds[NUM_LEDS];

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

// Your WiFi credentials.
// Set password to "" for open networks.
//Wifi credentials 1
char ssid0[] = "xxxxxxxxxxxxxxxxxxx";
char pass0[] = "xxxxxxxxxxxxxxxxxx";
//Wifi credentials 2
char ssid1[] = "xxxxxxxxxxxxxx";
char pass1[] = "xxxxxxxxxxxxxx";

char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 8080;

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// 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()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
   //String APlist = wifi.getAPList ();
  //Serial.print ("AP List:");
  //Serial.println (APlist);
  Serial.println("Establishing WIFI connection with Blynk...");
  
  WiFi.begin(ssid1, pass1);  // Non-blocking if no WiFi available
  delay(7000);
  Blynk.config(auth, server, port);
  Serial.println(WiFi.status());                                        
  while(WiFi.status()==WL_IDLE_STATUS){

    Serial.print(".");
    delay(1000);
    
  }
    
  if(WiFi.status()==WL_CONNECTED){

    Serial.println("Esp8266 is connected to wifi");
    Blynk.begin(auth, wifi, ssid1, pass1);
    
    if (Blynk.connected()){
      
      Serial.println("ESP8266 is connected to Blynk cellphone wifi");
          
    }
  }  
  else if (WiFi.status()==WL_CONNECT_FAILED){

    Serial.println("Try the next available wifi");
    
  }
  
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  
  
  Serial.println("LED strip control with preset light settings using Blynk ");
  timer1.setInterval(100L, LightSettings);
  
}

The first challenge however is when i try to compile the sketch in the Arduino IDE, i get an error message:

“no matching function for call to ‘BlynkWifi::config(char [33], char [16], int&)’”

I can’t seem to figure out why i am getting this message as I have included the blynk library and it stands to reason that this function would be a part of it.

The second challenge that I am trying to resolve is that when i use the Wifi.status() function it actually does not work as expected which i assume is because i am using the esp8266 wifi module instead of an arduino shield (the result of this function is 255 which is a “WL_NO_SHIELD”). I came across a few suggestions but after a few attempts I realized these would not work because it seems they work only for an ESP8266 controller (not an arduino paired to an Esp8266 wifi module), which was evident by the fact that the ESP8266WiFi.h library would only be recognized in the arduino IDE when the ESP8266 board was selected.
If someone can suggest a better way in which to achieve the goal-Enable the Arduino to check which my two networks are available and connect to the one detected to be available-i will be extremely grateful.

Hello Blynk Community.
Please, can someone offer some advice.

Hello Blynk community.

An update on my progress with regard to this issue. After a few more hours of research i came across a post on the Blynk forum which worked like a charm to sort out the issue of the compile error. Thank you at @BTT . For anyone else experiencing the same problem, here is the link:

I played around with these lines of code, added a small segment of code I read in another post, to achieve automatic WiFi network selection and the resulting code segment that I have developed thus far looks like this:

void SetupBlynkConnection(){

  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
  Blynk.config(wifi, auth, server, port);
  Blynk.connectWiFi(ssid0, pass0);
  //Check if the first network is avaiable and connect
  if (Blynk.connectWiFi(ssid0, pass0)) {                                                    

    Serial.println("Attempting to connect to Wifi network 1");
    Blynk.connect(5000);
    Blynk.run();
    
  }
  //Check if second network is available and connect
  else if(!Blynk.connectWiFi(ssid0, pass0)){
    
    Serial.println("Wifi network 1 is not available so establishing connection to Wifi network 2");
    wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
    Blynk.config(wifi, auth, server, port);
    Blynk.connectWiFi(ssid1, pass1);
    Blynk.connect(5000);
    //Blynk.run();
    
    if(Blynk.connectWiFi(ssid1, pass1)){
      Serial.println("Connected to Blynk via Wifi network 2");
    }
    //if no networks are available then continue with rest of program
    else{

      Serial.println("No Wifi networks are avaialable for connection");
      
    }
  }
}

void CheckBlynkConnection(){
  
  Connected2Blynk = Blynk.connected();
  
  if(!Connected2Blynk){

     Serial.println("Blynk connection status: Disconnected\nAttempting to re-establish connection to Blynk...");
     SetupBlynkConnection();
     Blynk.connect(5000);
     Blynk.run();
     
  }
  else{
    
    Serial.println("Blynk connection status: Connected");     
 
  }
}

This segment of code is able to successfully detect and select the network to connect to. All this works great when the second network is available only this is not the case when the first network is available: the new challenge that i am facing is that when the first network is available the SetupBlynkConnection() subroutine executes as expected but when the CheckBlynkConnection() subroutines executes it achieves a disconnected status continuously. I am playing around with the lines of code still but can’t seem to definitively determine why this is happening. Any advice from the community would be greatly appreciated.

The code calles many functions unnecessarily and repetitively and creates those unwanted disconnections as you’re experiencing.

Try the following running code

bool SetupBlynkConnection()
{
  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
  Blynk.config(wifi, auth, cloudBlynkServer.c_str(), BLYNK_SERVER_HARDWARE_PORT);

  //Check if the first network is avaiable and connect
  if (Blynk.connectWiFi(ssid, pass)) 
  {                                                    
    Serial.println("Connected to primary Wifi network");    
  }
  //Check if second network is available and connect
  else
  {   
    Serial.println("Primary Wifi network not available, connecting to backup Wifi network");
    //wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
    //Blynk.config(wifi, auth, server, port);
    
    if (Blynk.connectWiFi(ssid1, pass1))
    {
      Serial.println("Connected to Blynk via backup Wifi network");
    }
    //if no networks are available then continue with rest of program
    else
    {
      Serial.println("No Wifi networks are avaialable for connection");     
      return false;
    }
  }
  return (Blynk.connect(5000));
}

void CheckBlynkConnection()
{ 
  if (!Blynk.connected())
  {

     Serial.println("Blynk connection status: Disconnected\nAttempting to re-establish connection to Blynk...");
     if ( SetupBlynkConnection() )
      Serial.println("Blynk connection status: OK...");
  }
}

#define NOT_USE_BLYNK_BEGIN   true

void setup()
{
  ...
  
  // Debug console
  Serial.begin(115200);
  delay(1000);
  Serial.println("\nStart Teensy4");

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  
  Serial.println("Start Blynk");

  #if USE_BLYNK_WM
    Blynk.begin(wifi);
  #else
    #if NOT_USE_BLYNK_BEGIN
      SetupBlynkConnection();
    #else
      //Blynk.begin(auth, wifi, ssid, pass);
      Blynk.begin(auth, wifi, ssid, pass, cloudBlynkServer.c_str(), BLYNK_SERVER_HARDWARE_PORT);
    #endif
  #endif
  
  timer.setInterval(30000L, CheckBlynkConnection);    // check every 30s if still connected to server
  ...
}

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

and this is the debug terminal output (runningTeensy4 and ESP8266 shield)

Start Teensy4
Start Blynk
[1332] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Teensy

[1332] Connecting to HueNet1
[7948] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[15641] IP = 192.168.2.107
+CIFSR:STAMAC,5c:cf:7f:66:05:d2
[15641] Connected to WiFi
Connected to primary Wifi network
[25790] Ready (ping: 22ms).
[26229] Time sync: OK
Blynk connection status: Disconnected                  <= remove router HueNet1
Attempting to re-establish connection to Blynk...      <=  Blynk loss
[62817] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Teensy

[62817] Connecting to HueNet1
[69433] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[79996] Failed to connect WiFi
Primary Wifi network not available, connecting to backup Wifi network
[79996] Connecting to HueNet                              <= backup WiFi
[86614] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[92303] IP = 192.168.2.107
+CIFSR:STAMAC,5c:cf:7f:66:05:d2
[92303] Connected to WiFi
Connected to Blynk via backup Wifi network          <= Backup WiFi reconnected
[102514] Ready (ping: 21ms).                        <= and Blynk reconnected OK

and the debug output when Primary AP is not available at the beginning

Start Teensy4
Start Blynk
[1332] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Teensy

[1332] Connecting to HueNet1
[7948] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[18512] Failed to connect WiFi              <= failed to connect to primary AP
Primary Wifi network not available, connecting to backup Wifi network
[18512] Connecting to HueNet                <= connecting to backup AP
[25130] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[30821] IP = 192.168.2.107
+CIFSR:STAMAC,5c:cf:7f:66:05:d2
[30821] Connected to WiFi                    <= connected to backup AP
Connected to Blynk via backup Wifi network   <= and Blynk
[41040] Ready (ping: 22ms).

Hello @khoih
Thank you very much for your advice and assistance. Your code was helpful, I found that this code runs much quicker to connect to one of my 2 WiFi networks, however I am having a few problems. I added the code segment you suggested as shown here:


bool SetupBlynkConnection()
{
  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
  Blynk.config(wifi, auth, server, 8080);

  //Check if the first network is available and connect
  if (Blynk.connectWiFi(ssid1, pass1)) 
  {                                                    
    Serial.println("Connected to primary Wifi network");    
  }
  //Check if second network is available and connect
  else
  {   
    Serial.println("Primary Wifi network not available, connecting to backup Wifi network");
    //wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
    //Blynk.config(wifi, auth, server, port);
    
    if (Blynk.connectWiFi(ssid0, pass0))
    {
      Serial.println("Connected to Blynk via backup Wifi network");
    }
    //if no networks are available then continue with rest of program
    else
    {
      Serial.println("No Wifi networks are available for connection");     
      return false;
    }
  }
  return (Blynk.connect(5000));
}

void CheckBlynkConnection()
{ 
  if (!Blynk.connected())
  {

     Serial.println("Blynk connection status: Disconnected\nAttempting to re-establish connection to Blynk...");
     if ( SetupBlynkConnection() )                                                                                    //
      Serial.println("Blynk connection status: OK...");
  }
}

#define NOT_USE_BLYNK_BEGIN   true

void setup()
{
  
  // Debug console
  Serial.begin(9600);
  delay(1000);
  Serial.println("\nStart Teensy4");

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  
  Serial.println("Start Blynk");

  #if USE_BLYNK_WM
    Blynk.begin(wifi);
  #else
    #if NOT_USE_BLYNK_BEGIN
      SetupBlynkConnection();
    #else
      //Blynk.begin(auth, wifi, ssid, pass);
      Blynk.begin(auth, wifi, ssid0, pass0, server, 8080);
    #endif
  #endif
  
  //timer.setInterval(30000L, CheckBlynkConnection);    // check every 30s if still connected to server
  

    FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  
  
  //Blynk.begin(auth, wifi, ssid0, pass0);
  //SetupBlynkConnection();

  Serial.println("WS2812 RGB LED strip test reconnecting to the wifi when disconnected: V3 (Running code provided by Blynk community member)");
  timer1.setInterval(300, MainFunction);
  
}

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


void MainFunction(){

  if(PresetLightSelection==1){

      PresetLightSettings();
        
  }
  
  BlynkCounter++;
  if(BlynkCounter==50){   //Check if Blynk is connected every 15 seconds approximately 

    Serial.println("Checking Blynk connection status...");
    CheckBlynkConnection();
    BlynkCounter=0;
    
  }
  
}

Here is the serial monitor output when the blynk print command is included in the sketch:

Start Teensy4
Start Blynk
[1045] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[19118] Connecting to NETWORK1
[20129] ESP is not responding
Primary Wifi network not available, connecting to backup Wifi network
[20667] Connecting to NETWORK2
[23844] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
compile time:May 20 2016 15:08:19
OK
[29232] +CIFSR:STAIP,"192.168.43.102"
+CIFSR:STAMAC,"3c:71:bf:25:ef:e0"
[29243] Connected to WiFi
Connected to Blynk via backup Wifi network
WS2812 RGB LED strip test reconnecting to the wifi when disconnected: V3 (Running code provided by Blynk community member)
[40665] Ready (ping: 651ms).
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...

As you can see, the system never realizes that the connection between Blynk and my Esp Hardware has been established when one of the 2 WiFi networks are available. So even when the system does connect to Blynk, it never runs the through the code segment

 if ( SetupBlynkConnection() )
     Serial.println("Blynk connection status: OK..."); 

Situation 1:
If both networks are unavailable on startup then the program stalls after it completes the void setup subroutine (it prints the WS2812 RGB LED strip test reconnecting to the wifi when disconnected: V3 (Running code provided by Blynk community member) and stops running). If either of the 2 networks becomes available the program then unfreezes and connects to the available network.
Situation 2:
If both the network fails/unavailable after a connection was established then when either of the networks returns/available then the system still remains frozen. Here is the serial output for this situation:

Start Teensy4
Start Blynk
Primary Wifi network not available, connecting to backup Wifi network
No Wifi networks are available for connection
WS2812 RGB LED strip test reconnecting to the wifi when disconnected: V3 (Runing code provided by Blynk community member)
Checking Blynk connection status...
Blynk connection status: Disconnected    //Here is where it stalls under situation 1
Attempting to re-establish connection to Blynk...
Connected to primary Wifi network
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Blynk connection status: Disconnected
Attempting to re-establish connection to Blynk...
Primary Wifi network not available, connecting to backup Wifi network
No Wifi networks are available for connection   //Here is where it stalls under situation 2

I apologize for not mentioning this earlier but I need my program to run even when there is no Wifi network available and reconnect when there is 1 available. I am playing around with the commands now to see if sprinkling some Blynk.connect() and Blynk.run() here and there might resolve the issues.

The code is just doing what you ask it to do

  1. Every 15s, it’ll print Checking Blynk connection status... no matter what.
    It’s only expected to run the SetupBlynkConnection() only when Blynk connection is lost.
...
if (!Blynk.connected())
  {

     Serial.println("Blynk connection status: Disconnected\nAttempting to re-establish connection to Blynk...");
     if ( SetupBlynkConnection() )                                                                                    //
      Serial.println("Blynk connection status: OK...");
  }
...
if(BlynkCounter==50){   //Check if Blynk is connected every 15 seconds approximately 

    Serial.println("Checking Blynk connection status...");
    CheckBlynkConnection();
    BlynkCounter=0;
    
  }
...
  1. This blocking Blynk.run() has been discussed many times here in the forum.

You can avoid that by

void loop()
{
 if (Blynk.connected())
 {
    Blynk.run();
 }

  timer1.run();
}

Well than I don’t think that i am asking it correctly to do what I want because the program is stalling both with and without the Blynk.run() inside of the ‘if statement’. It is not running the SetupBlynkConnection() code segment. It is actually stalling the program for a short while before running this sub routine once and then it stalls completely. There is other programming code that I have implement which is commands sent from Blynk to switch on an LED strip. One of them alternates the LED’s colours and when the program stalls, the LEDs also freeze. This is not what we programmed it to do or at least not what i intended for it to do.

I have already tried placing the Blynk.run() inside of the ‘if statement’ the way you are suggesting but found that sometimes the hardware does not connect to Blynk at all this way.

You have to look at the blocking problem in your remaining hidden code, for example

if(PresetLightSelection==1){

      PresetLightSettings();
        
  }

Only you who hold the key and know where to find the bug. I’m afraid many people as well as I won’t have any more idea to share. That’s it.

You can also have a look at (for ESP8266 and ESP32)

Okay thanks for your advice and assistance @khoih, you gave a push in the right direction. I am currently using a method of elimination to detect where the culprit is that is causing the stalling.

As you know, WS2812 lib. is notoriously blocking and bad, yet not easily to be fixed. These links will help you start your research

https://forum.pjrc.com/threads/58838-Non-Blocking-WS2812-library-still-has-significant-delay
https://forum.pjrc.com/threads/58442-Non-Blocking-WS2812-LED-Library-and-Teensy-4-0
https://forum.pjrc.com/threads/58621-WS2812Serial-question-conflict-between-FastLED-and-Adafruit-NeoPixel-libraries
https://forum.pjrc.com/threads/57875-Teensy-4-0-and-FastLED

Good luck

I have isolated the WS2812 fastLED code segments and i still have the same problem: The code Blynk.connected() does not return a ‘connected’ indication when the Blynk.run() is placed within the ‘if statement’ code segment:

if(Blynk.connected()){
    Blynk.run();
}

So i concluded that the solution to the problem lies in altering the code responsible for the Blynk app to hardware connection. So just to be more explicit when my project is connected to a WiFi network that has access to the internet Blynk.connected() never achieves a positive result. It appears that a Blynk.run() is required to connect the app and hardware in the first place in order for the Blynk.connected() to result in a connected status. I am stuck in a vicious cycle and i have tried playing around with the code to break that cycle but i have been unsuccessful: I tried adding in a while loop and a counter to run the Blynk.run() command when the project connects to any available WiFi network:

bool SetupBlynkConnection()
{
  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
  Blynk.config(wifi, auth, server, port);

  //Check if the first network is available and connect
  if (Blynk.connectWiFi(ssid0, pass0)) 
  {                                                    
    Serial.println("Connected to primary Wifi network");    
    BlynkRunCounter=0;

    while(BlynkRunCounter<50){

      Blynk.run();
      Serial.println("BlynkRunCounter: ");
      Serial.println(BlynkRunCounter);
      BlynkRunCounter++;
    }
    
  }
  //Check if second network is available and connect
  else
  {   
    Serial.println("Primary Wifi network not available, connecting to backup Wifi network");

    Blynk.connect(2000);                                //Necessary in order to make the system connect to the second Wifi network
    
    if (Blynk.connectWiFi(ssid1, pass1))
    {
      Serial.println("Connected to Blynk via backup Wifi network");
      BlynkRunCounter=0;
      while(BlynkRunCounter<50){

        Blynk.run();
        Serial.println("BlynkRunCounter: ");
        Serial.println(BlynkRunCounter);
        BlynkRunCounter++;
      }
    
    }
    //if no networks are available then continue with rest of program
    else
    {
      Serial.println("No Wifi networks are available for connection");     
      return false;
    }
  }
  return (Blynk.connect(2000));
}

I am running out of ideas on how to break the viscous cycle. Did anyone experience a similar problem? Please, if anyone has any different approaches to try it would be really helpful.

I’m very new to Blynk but I’m using this code to scan for networks and connect to whichever one is available. It only checks at start up right now, but this could be checked anytime if needed. I can’t remember where I found this approach, but it’s working great. Maybe this approach is totally wrong, but I’m using Wemos D1 Mini Pro which uses the ESP8266 which I think is what you’re using. I’m not showing it here, but I also was able to find a serial number for each of five different Wemos D1 Mini Pro devices. Because I could identify which board my code was on, I could have one sketch downloaded to each device and the code could run differently based on which device was connected. Kind of fun. If the code shown below works for you, I can show you the additional program, but I removed it for clarity.


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiClient.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

char ssid1[] = "network1";  //home
char pass1[] = "xxxxxxxxxx";
char ssid2[] = "network2";  //work
char pass2[] = "xxxxxx";
char ssid3[] = "network3";  //cabin
char pass3[] = "xxxxxxxxxxxxxx";

BlynkTimer timer;

void mainLoop()
{
  //most of your code goes here
}

void setup() {
  Serial.begin(9600);
  int n = WiFi.scanNetworks(); // WiFi.scanNetworks will return the number of networks found
  Serial.println("scan done");
  if (n == 0)
    Serial.println("no networks found");
  else
  {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i)
    {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(")");
      Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
      Serial.println("");
      //This is where I'll connect to which ever WiFi system is available
      if (WiFi.SSID(i).equals(ssid1)) {
        Blynk.begin(auth, ssid1, pass1);
      } else if (WiFi.SSID(i).equals(ssid2)) {
        Blynk.begin(auth, ssid2, pass2);
      } else if (WiFi.SSID(i).equals(ssid3)) {
        Blynk.begin(auth, ssid3, pass3);
      }
      delay(10);
    }
  }
  timer.setInterval(1000L, mainLoop);  //run every second
}

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

Maybe reading the documentation would help…
https://docs.blynk.cc/#blynk-firmware-connection-management

NOTE: After Blynk.config(...) is called, your hardware is not yet connected to the server. It will try to connect while until it hits first instance of Blynk.run() or Blynk.connect() routine.
To skip connecting to the server or to disconnect manually, call Blynk.disconnect() after configuration.

The poor grammar in this statement is somewhat confusing, but I think it should say “It will not try to connect until it hits the first instance of Blynk.run() or until the Blynk.connect() routine is called.”

Pete.

Hello @PeteKnight.
Thanks but I have already read this extract of the documentation, I did not find particularly helpful in this situation. Maybe i over looked something? Please do tell if there is something specific that occurred to you when you read these lies that you thought would be helpful in my situation.

I tried a Blynk.connect() inside of the SetupBlynkConnection() previously but this not help. As long as the Blynk.run() remains inside of the if statement, a connected status indication is not being reached :confused:

Hello @docaberle. Thank you very much for sharing. I came across something like this as well. TI tried something similar but i am trying to avoid using blocking functions such as the Blynk.begin(). Its been documented, and tested, to prevent any code from executing until the project hardware connects to the Blynk server. My issue is that i would like my program to execute when no internet connection is available and reconnect when an internet connection becomes available.
Did you experience a situation when your project was running and none of your networks were available? what happens to your program when none of your networks are available?

In that case, you’re structuring your code incorrectly.
But, without knowing where these snippets of code fit into your larger overall picture its difficult to know how to advise.
You seemed to be saying that you’d discovered that “that a Blynk.run() is required to connect the app and hardware in the first place in order for the Blynk.connected() to result in a connected status”. This is partially true, but in my experience a Blynk.connect() immediately after the Blynk.config() is a better way to do it.

Also, I don’t use Blynk.connectWiFi but prefer to manage my own WiFi connection via WiFi.config and WiFi.begin. This is mostly because I assign static IP addresses to all if my devices and don’t always use Blynk, so it makes my code more universal if I call the underlying WiFi libraries rather than using the Blynk wrapper library for the WiFi connection.

Pete.

Wow, you’re absolutely right. If it cannot connect to a network nothing else happens after Blynk.begini. With the code below you’ll see only this in the monitor:

before
[71] Connecting to MyNetwork


void setup() {
  Serial.begin(9600);
  Serial.println("before");
  Blynk.begin(auth, ssid, pass);
  Serial.println("after");
  delay(10);
  timer.setInterval(1000L, mainLoop);
}

Too bad there wasn’t a timeout parameter from Blynk.begin that would bail out of the function if it couldn’t connect. The library could be modified, but that’s never fun and easy to overwrite.