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

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