Getting data from Bluetooth HC-06 to a Arduino Mega 2560 board

I am trying to setup a joystick on the blynk app and as I move it have it show the position numbers, for now, on the serial monitor window. My code is below. I have the joystick outputting on V0 and V1 virtual pins. I get the red light on the HC-06 bluetooth module to go solid red and the app says it is online (i had paired to it earlier). So I think that side is good. It is just getting the data (position numbers) to show up in the program (com window).
My code is from the BLynk example code page with a few tweaks. I am running Blynk on a new Galaxy S8plus Android phone. Keyestudio HC-06 bluetooth module and Mega 2560 board.
Any thoughts on what I have done wrong?
Thanks
Scott

PS: I noticed in the BLynk app that there is a BLE and a Bluetooth setup. I get bluetooth to work (it connects from what I can see), but what has me perplexed is that the first library that gets added is a SerialBLE.h library. Does it work for both bluetoooth types?
I have also read many posts on the HC-06 and some talked about putting a voltage divider on the Rx pin to drop the voltage from 5v to 3.3v. I tried it both ways.

#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>

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

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

SoftwareSerial SerialBLE(10, 11); // RX, TX
//SoftwareSerial SerialBLE(19, 18); // RX, TX   //tried this as a alternative

BLYNK_WRITE(V1) {
  int x = param[0].asInt();
  int y = param[1].asInt();

  // Do something with x and y
  Serial.print("X = ");
  Serial.print(x);
  Serial.print("; Y = ");
  Serial.println(y);
}

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

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

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

void loop()
{
  Blynk.run();
  Serial.println(V1);
}
1 Like

A few things…

  • The Mega has multiple auxiliary Serial Ports (Serial1, Serial2 & Serial3)… Google that for further info and use one of them instead of SoftwareSerial.

  • V1 is not a variable you can use outside of a Blynk command

  • And you do not want to run a proper Blynk command directly in the void loop()

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/what-is-virtual-pins

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-control-anything-with-blynk-app

PS

Yes

Got it working finally. Had to swap the Rx and Tx from the Bluetooth module to the Arduino mega board. I had Rx to Rx and Tx to Tx. Needs to be Rx to Tx and Tx to Rx. I also found that I needed to close down the serial window and reopen.
Another issue I had was writing a new program to the board. I kept getting “port busy” error showing up. What I found is if you pull the power wire on the Bluetooth module so it is off you can then easily upload your new program. When it is uploaded and ready just plug the power for the blue tooth module back in and you are good to go again.
The joy stick is giving me nice values now to control my 2 tank motors.

2 Likes