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

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.

You don’t need to - you use the non-blocking Blynk.config and Blynk.connect commands instead.

However, if you want to explore your issue further then please create a new topic about it, rather than hijacking this topic.

Pete,

Hello @PeteKnight
I say that the Blynk.run() is required because when testing my program i first tried to establish a connection with the server with a Blynk.connect() as shown in the 3 places labelled in the code segment to follow. I was surprised that the command in ‘Place 1’ did not result in a connection and tried the other places. Unfortunately In all 3 places the result of a Blynk.connected() would be negative (not connected). I have commented out FastLED code segments as I am first trying to establish a stable Blynk connection with my networks. To summarize, the following lines of code is what my firmware consists of:


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

    //Blynk.connect(2000); **//Place 2**
    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));
}

void CheckBlynkConnection()
{ 

  //Blynk.connect(2000);  **//Place 3**
  
  if (!Blynk.connected())
  {

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

    Serial.println("Blynk connection status: Connected");
    
  }
}

#define NOT_USE_BLYNK_BEGIN   true

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

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

  //To test if the code is blocked or not. pin 12 switches on an LED when pin 11 goes to ground
  pinMode(12, OUTPUT);
  pinMode(11,INPUT_PULLUP);
  Serial.println("Establishing WiFi and Blynk connections...");

  #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

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


void loop()
{

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


void MainFunction(){
 
    BlynkCounter++;
    if(BlynkCounter==100){   //100 for 30 seconds
  
      Serial.println("Checking Blynk connection status...");
      CheckBlynkConnection();
      BlynkCounter=0;
      
    }
  
  if(digitalRead(11)==LOW){

    digitalWrite(12,HIGH);
    Serial.println("LED switched ON");
    
  }
  else{

    digitalWrite(12,LOW);
    
  }
  
}

I don’t seem to be having an issue connecting to a WiFi network because the command Blynk.connectWiFi() does achieve a positive result when a WiFi network is available. But i will also look into this and see if somehow solves my problem. Thanks Pete

How do you imagine that the device will connect to the Blynk server when you’ve not yet established a Wi-Fi connection?

Pete.

I am sorry Pete i meant i was surprised about ‘Place 2’.

I was just trying the Blynk.connect() in “Place 1” since it seemed that it was necessary for a connection to occur as shown in the quote above.

I continued testing by implementing the following code and noticed something strange on the serial monitor:

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)){

    Blynk.connect(2000); //Place 2
    Serial.println("Connected to primary Wifi network");    
    BlynkRunCounter=0;

    while(!Blynk.connected()){
      
      Blynk.run();
      Serial.println("BlynkRunCounter: ");
      Serial.println(BlynkRunCounter);
      BlynkRunCounter++;

      if(Blynk.connected()){

        Serial.println("Blynk is connected");
   
      }
      
    }
    
  }
  //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));
}

void loop()
{
   
  if(Blynk.connected()){
    Blynk.run();
    Serial.println("Blynk.run() is running");
  }
  
  timer1.run();
  
}

Serial monitor output:

Establishing WiFi and Blynk connections...
Connected to primary Wifi network
BlynkRunCounter: 
0
BlynkRunCounter: 
1
BlynkRunCounter: 
2
BlynkRunCounter: 
3
BlynkRunCounter: 
4
BlynkRunCounter: 
5
BlynkRunCounter: 
6
BlynkRunCounter: 
7
BlynkRunCounter: 
8
BlynkRunCounter: 
9
BlynkRunCounter: 
10
BlynkRunCounter: 
11
BlynkRunCounter: 
12
BlynkRunCounter: 
13
BlynkRunCounter: 
14
BlynkRunCounter: 
15
BlynkRunCounter: 
16
BlynkRunCounter: 
17
BlynkRunCounter: 
18
BlynkRunCounter: 
19
BlynkRunCounter: 
20
BlynkRunCounter: 
21
BlynkRunCounter: 
22
BlynkRunCounter: 
23
BlynkRunCounter: 
24
BlynkRunCounter: 
25
BlynkRunCounter: 
26
Blynk is connected
Test reconnecting to the wifi when disconnected: 
V3 (Running code adapted from Blynk community member)
//The Blynk.run() in the void loop does not execute//
Checking Blynk connection status...
Blynk connection status: Disconnected

I force a connection with the Blynk server using the while loop which is evident by the fact that the serial monitor prints Blynk is connected which is governed by the ‘If’ statement within the while loop but for some reason the connection then breaks almost immediately becuase the ‘If’ statement governing the Blynk.run in the void loop() never executes. There is no additional code from other libraries or other programming code so I can’t understand why the connection is broken. :confused:

Hello Blynk community
I am trying a different approach. I would like to scan available WiFi networks and if one network matches one of the hardcoded networks then a Wifi connection followed by a Blynk connection must be executed. I looked through the web and through this forum but could not find something that worked the way I needed. My internet searches only indicated code when an esp8266 is used as a stand alone controller or is interfaced with an arduino uno, either case the code would not run on the IDE with the controller selected as the arduino uno. I did come across something on the blynk forum here which was posted by tbdltee:

  String APlist = wifi.getAPList();
  Serial.print ("AP List:");
  Serial.println (APlist);

But this concatenates and prints the combined list of wifi networks in the vicinity. I am looking for a way to test each individual scanned wifi ssid against the my hardcoded ones. Can someone please advise what is the best way to do this.

Are you saying that you are now using an Uno with an ESP-01 as a WiFi modem?

Is there any reason why you don’t just switch to a NOdeMCU or an ESP32?
There are so many great tools for these boards to scan for and switch between available wifi networks (such as WiFiMulti) that insisting on using an Uno + ESP-01 is severely restricting what you can do.

Pete.

Hello Pete
Yes this is the setup I have been using ever since I transferred from Bluetooth. But just to avoid confusion let me just add that after a few google searches I noticed that the ESP8266 Wifi module is also referred to as the ESP01. I am referring to the little WiFi module shown here

I was looking into the Node MCU after your suggestion to consider a controller with more SRAM and memory (which is a second challenge I sought help with on another Blynk topic ). I require one with more then 12 GPIO and so am I looking into the variant shown here. The reason I am hesitant to switch is that I have already written a large chunk of the code required to complete my project’s firmware and I am unsure if the code will be completely compatible with the ESP8266 node MCU. I know that I can program the NodeMCU using the arduino IDE, and aside from changes to GPIO allocation, If I will have to make any major changes to my code. My firmware is currently relying on the following libraries:


#include <Blynk.h>
#include <FastLED.h>
#include "LedControl.h"
 

The ESP-01 is used as a Wi-Fi module for Arduino etc, but that wasn’t what it was designed for. It’s actually more powerful that nour Arduino, oit just lacks the GPIO ports.

You won’t find any ESP8266 based processor with 12 or more GPIOs. You would need to use an ESP32 instead (although I’m not sure why you would need 12+ GPIO’s for an LED controller).

Switching to an ESP8266 or ESP32 based board from an Arduino Uno + ESP-01 system is very simple. Support for the board needs to be added into the Arduino IDE, but this is a 5 minute job.
A few libraries need to be added, and the code needs to be simplified to remove all of the SoftwareSerial rubbish, but once again that is very simple.

It depends on how you’ve written your code as to how simple it will be to migrate the GPIOs from one board to another. The worst case is that you would need to do a find and replace in your code. The ‘correct’ way to reference GPIOs is to use variable names and have a translation table at the beginning of your code like this:

#define relay_1_pin  4
#define relay_2_pin  5
#define relay_3_pin  13
#define relay_4_pin  14

// later in the code...

digitalWrite (relay_1_pin, LOW);

That way, you can easily re-map the GPIO pins to different uses, or migrate from one board type to another.
If you doi use a NodeMCU at any point then you will find that the board has “D” numbers printed next to the pins rather than GPIO’s. You can use these numbers in the code rather than the corresponding GPIOs (D0 = GPIO16, D1 = GPIO5, D2 = GPIO4 etc), but this makes your code less portable between board types, so best to stick to GPIO numbers and add a note next to the pin definitions in the code so it makes wiring-up the board easier.

Making the move to a proper IoT board that has built-in WiFi is something you won’t regret.

I’m not sure if you’ve read this before…

It doesn’t cover ESP32’s, and if you really do need 12+ GPIO’s the that’s what you’ll need, but hopefully it convinces you that the Arduino + ESP-01 are not the way to go.

Pete.

I did find one which claims to be an ESP8266 (NodeMCU V3 Lua ESP8266 ESP-12E WIFI Development Board), the one I linked in my previous message. It has more then 12 GPIO and I am considering it strongly. I require more GPIOs since I also have a few manual push buttons as well as 2 LED bar graphs that I am driving through a MAX7219 IC.

Okay thanks for putting my concerns to rest. I think the controller/dev board I linked in the previous post will be adequate for my purposes. Ideally I would have liked to use a Particle photon/argon which would have made the WiFi application a breeze, but to minimise cost I think the Nodemcu will be a better option. Infact, it more cost effective then the arduino uno+ESP01 combination.

I did find a number of users who complained about connectivity and stability issues with the NodeMCU and this was another concern. But I think the best way to put that to rest is to play around with one and see how it goes.

Yes I did read through this. It was because of that post of yours that I begun considering the NodeMCU and Wemos D1 mini. Thanks, that was a very thorough and helpful post.

Thanks for your help Pete. I will procure and start playing around with NodeMCU and hopefully have better luck with it.

There are no NodeMCU/ESP8266 devices with 12+ useable GPIOs.
You need to read this:

Pete.

1 Like