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

Problem with multiple serials on Arduino MEGA

Hello. I’m a new Blynk user and I would like to ask you for help. I am working on a monitoring device and I need to use several modules using serial communication such as GPS module, Bluetooth module, and in the future maybe a GSM module. Before I added Blynk commands to the source code, everything worked fine, but now nothing works after loading the code into my Arduino (not even the commands from setup ()). I use Arduino MEGA and I want to control Arduino with Blynk via Bluetooth. The problem is probably in serial communication, because when I remove line “Blynk.begin(Serial2,auth);” everything works fine (of course Blynk not). My source code has more than 1200 lines, so I only select lines where I think is a problem:

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial2 // not sure to define this on MEGA

void setup() {
Wire.begin();
Serial.begin(9600);
Serial1.begin(9600); // for GPS module
Serial2.begin(9600); // for Bluetooth module
Blynk.begin(Serial2,auth); // this is problem !!!
}

I’m not sure if I can use the same baud rate in multiple serials on Arduino MEGA.

One more question, in the loop () I use in addition to Blynk.run (); several user-defined functions that allow the monitoring device to operate. It is possible to use Blynk even though there will be more commands in the loop () (they must be there)?

I will appreciate any help. Thank you for your time.

@Eric18 when you post code it needs to have triple backticks at the beginning and end, so that it displays correctly.
Triple backticks look like this:
```

This line:

tells Blynk to send debug messages to the Serial2 port. This is a strange choice, as you’d normally have the USB port (Serial) connected to your PC and use this to view debug messages. If you want to view debug messages sent to other serial ports then additional hardware (an FTDI TTL to USB adapter) will be needed.

In addition, if you send debug messages to the same serial port that is being used for communication with a Bluetooth adapter then the two sets of messages will get mixed-up and the connection will fail (and your debug messages will be corrupted too).

It’s difficult to comment further without seeing more code, and without understanding more about how your hardware is connected and configured.

Maybe you should start with a simple sketch that gets your Bluetooth Blynk connection working, then go from there?

Pete.

Firstly, thank you Pete for your respond and I apologize for the bad editing of the text as I wrote I’m new here. At the beginning, I had the code as you write, but the result was the same, it didn’t work. I used
#define BLYNK_PRINT Serial
and
Blynk.begin(Serial,auth);
and connection was corrupted too. My hardware consists of Arduino MEGA, RTC, SD card reader, LCD, Bluetooth module HC-06 and GPS module NEO-6m. The GPS module is connected to Serial1 and the Bluetooth module to Serial2 (in the future I plan to use a GSM module on Serial3).
Because I’m from a non-English speaking country and I have everything named in my language, uploaded code would be quite confusing (1200 lines). I believe the error is in this part of the code:
#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

void setup() {
Wire.begin();
Serial.begin(9600);
Serial1.begin(9600); // for GPS module
Serial2.begin(9600); // for Bluetooth module
Blynk.begin(Serial,auth); // this is problem !!!
SD.begin(CSpin);
pinMode(buttonPin, INPUT);
… and some user-defined functions
}
void loop() {
Blynk.run();
… and some user-defined functions
}

Thank you.

Eric

As I said earlier, you can’t use the same serial port for debug messages from Blynk, and the connection to your Bluetooth module. They have to be different.

In which case this:

should have been this:

Blynk.begin(Serial2,auth);

I’d suggest (again) that you simplify things, with just the Mega and the Bluetooth module, and a simple sketch.

Pete.