I've been cracking my head over this for a long while.
Like many before me, I'm trying to send and receive data from a HM-10 bluetooth module, arduino mega adk and the app called BLE Scanner. I've figured out receiving alright. Whatever I write into the serial monitor, shows up on the app. Whatever I write into the software serial, shows up. However, I haven't figured out how to read from the software serial. The available function never gets triggered, and always printing out the software serial read is always -1
The app supports sending data over bluetooth as well. However, using:
#include <SoftwareSerial.h>
SoftwareSerial blueSerial(14, 15); //RX, TX
blueSerial.begin(9600);
Serial.begin(9600);
while (1)
{
blueSerial.listen();
if (Serial.available()) {
Serial.println("Writing");
blueSerial.write(Serial.read());
Serial.println(Serial.read());
Serial.println(blueSerial.available());
}
if (blueSerial.available()) {
Serial.println("Blueserial writing");
Serial.write(blueSerial.read());
}
}
The listen function is used out of pure desperation.
How do I fix this? This should be working right? Since I want to be sending AT commands to the bluetooth modules at some time and read their output, getting this to work is really important.
-
Have you tested the HM10 using a usb to serial adapter?MrFixIt87– MrFixIt8711/13/2018 01:00:12Commented Nov 13, 2018 at 1:00
-
What do you mean? The Arduino is connected to the laptop, and I'm reading of Arduino IDE's Serial Monitor. I don't have any special cables but the one it came with(the one that puts the code in the board)Nephilim– Nephilim11/13/2018 01:30:20Commented Nov 13, 2018 at 1:30
-
1Why on earth are you using software serial????Majenko– Majenko11/13/2018 08:40:26Commented Nov 13, 2018 at 8:40
-
1How do you take this tone and not suggest an alternative? @MajenkoNephilim– Nephilim11/13/2018 14:26:26Commented Nov 13, 2018 at 14:26
-
1If hardware serial doesn't work, then you have a problem with either your hardware or your code. When you have a problem, the right approach is to troubleshoot it: find out what is wrong and fix it. You can ask for help here if needed. If you just give up and switch to software serial, then instead of fixing the initial problem you are just creating another problem.Edgar Bonet– Edgar Bonet11/13/2018 19:36:56Commented Nov 13, 2018 at 19:36
3 Answers 3
Possibly the built-in UART (Hardware Serial) assigned to the same pins is interfering with SoftwareSerial? If you really want to us SoftwareSerial, try it on a pair of pins that has no hardware UART attached.
And/or try SoftwareSerial (again, on a non-UART pair of pins) to connect to your PC (after uploading; you can't upload with it), and use Serial1 to connect to your device. This isn't the recommended or easiest way but it might help debug the problems you're seeing.
On Mega you don't need to use SoftwareSerial. Mega has 4 Serials. Connect the module to RX3/TX3 like on the Fritzing in your question and use Serial3 in sketch.
void setup() {
Serial.begin(9600);
Serial3.begin(8600);
}
void loop() {
while (Serial.available()) {
Serial3.write(Serial.read());
}
while (Serial3.available()) {
Serial.write(Serial3.read());
}
}
-
I have to confirm, that what worked on UNO over SoftwareSerial was not working on Mega ... but switch to Serial2 instead worked.Uli– Uli03/03/2022 11:17:40Commented Mar 3, 2022 at 11:17
-
@Uli, on Mega not all pins support SoftwareSerial03/03/2022 12:20:14Commented Mar 3, 2022 at 12:20
If SoftwareSerial is not working then simply do not use it.
You shouldn't be using it anyway.
You have a Mega with 4 UARTs. You are using a pair of the UART pins, and yet you are using SoftwareSerial.
Don't.
instead use the SerialX
object (where X is the number by the TX and RX for the pins) to communicate.
SoftwareSerial is only for those times when you don't have enough hardware UARTs, and you have plenty.
-
Serial3 didn't work in either direction which is why I switched to software serial in the first placeNephilim– Nephilim11/13/2018 16:54:02Commented Nov 13, 2018 at 16:54
-
@Juraj I rewired the whole thing a bunch of times, it's now on Serial with TX->1 and RX->0 . Same issue throughout.Nephilim– Nephilim11/16/2018 17:04:30Commented Nov 16, 2018 at 17:04
Explore related questions
See similar questions with these tags.