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

Arduino Mega not connecting to BLE, 'SerialBLE' was not declared in this scope

Hi I am trying to build an app to control a project for my engineering controls class but I am having problems connecting my phone to my arduino mega, I am using the supplied code given by the blynk website but i keep getting a compile error telling me ‘SerialBLE’ was not declared in this scope. and I am not sure why its giving me this code or what I have to change in the example code to make it connect. I know my wiring of the Hm10 BLE is right becasue I can control my board from other apps from my phone but I would really like to use the Blynk app. my phone and app are all up to date and I have Tx connected to Rx->0, and Rx connected to Tx ->1


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


#include <BlynkSimpleSerialBLE.h>

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


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

  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);

  Serial.println("Waiting for connections...");
}

void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

There does appear to be a bug in the Sketch Builder code for the Mega & HM10 or HC08 - I’ll raise this as an issue on GitHub.

The fix is simple, but before I explain this I’ll explain why you’ve wired the HM10 up wrongly…

The Mega has four serial ports, referenced as Serial, Serial1, Serial2 and Serial3.

The USB connector is attached to Serial via a TTL to USB chip. Pins 0 (Rx) & 1 (Tx) are also connected to Serial, but they bypass the TTL to USB chip.

None of the other serial ports have this TTL to USB converter.
As you will want to connect your Mega to your PC via the USB connector to upload coed and see debug messages, Serial is the port that should be used for this, and nothing else should be connected to pins 0 1nd 1.

Your MH10 can be connected to any of the other three serial ports. The pins are:
Serial1 19 (RX) and 18 (TX)
Serial2 17 (RX) and 16 (TX)
Serial3 15 (RX) and 14 (TX)

We’ll go for using Serial2 simply because I have a hard time spotting coding errors when trying to see the difference between Serial and Serial1 in multiple lines of code, and the difference between Serial and Serial2 is a bit more obvious.
This means that Tx on your HM10 will connect to Serial2 Rx (pin 17) and Rx on your HM10 will connect to Serial2 Tx (pin 16).

The code you should use then will be this:

/*************************************************************
  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: Bluetooth support is in beta!

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino Mega 2560 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 <BlynkSimpleSerialBLE.h>

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


void setup()
{
 
  Serial2.begin(9600);  // Use to connect to HM10 via pins 16 & 17
 
 
  Serial.begin(9600); // Debug console
  
  Blynk.begin(Serial2, auth);

  Serial.println("Waiting for connections...");
}

void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Edited to add…
I discovered that this also affects the Arduino Uno sketch, but in a different way.
Both issues raised as a single GitHub issue here:

Pete.