Trying to make STM32F103 and HC-05 work together

Post here first, or if you can't find a relevant section!
User avatar
tutankhamen
Posts: 12
Joined: Tue Dec 05, 2017 9:27 am
Location: San Francisco

Trying to make STM32F103 and HC-05 work together

Post by tutankhamen » Tue Dec 05, 2017 9:43 am

I have a project where on one side I'm using an Android phone to control STM32F103 mc via BT on other side. I'm using Arduino_STM32 library and Arduino IDE. I have some weird behavior with incoming data. I made a simple test which is sending 10 bytes string "0123456789" from android to stm32, but I'm receiving only random 1-5 bytes. Code on stm32 side:

Code: Select all

long received = 0;
const unsigned long buffer_size = 128;
uint8_t buffer[buffer_size];

void setup() {
    Serial1.begin(9600);
}

[...]
void loop() {
    [...]
    while(Serial1.available()){
        buffer[received++ % buffer_size] = Serial1.read();
    }
}
So, if will send "0123456789" from android, let say, 3 times every 1 second, on stm32 i will receive 3 portions like this:

"0", "0567", "047"

or something like this, i.e. I always receive the first byte and it's always correct, but it looks like the rest bytes could be randomly lost. If I'm sending data from stm32 to Android - everything works just fine. Also, I tried to use the same code with Arduino nano - everything works just perfect, i.e. I could transfer the data in both directions without any problem. Also tried to use Serial2 and Serial3, tried to change baud rate.

Android code looks like this:

Code: Select all

BluetoothSocket socket;
OutputStream outStream;
[...]
void init(...) {
    socket = ConnectToSTM32(...);
    [...]
    outStream = socket.getOutputStream();
}
void send(byte[] bytes) {
    outStream.write(bytes);
}
[...]
String strToSend = "0123456789";
send(strToSend.getBytes());
Wiring is like this:

Code: Select all

[HC-05] [STM32]
GND --- GND
+5V --- VCC
RXD --- PA9
TXD --- PA10
Am I doing something wrong?

stevestrong
Posts: 1976
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Trying to make STM32F103 and HC-05 work together

Post by stevestrong » Tue Dec 05, 2017 9:55 am

It seems an overkill to use long variables, I think "uint16_t" would cover your needs.

Have you tried two blue pills transferring data to each other?
Or between PC and BP via an USB to serial adapter?

Have you checked with a scope the incoming data? Maybe the HC module is not outputting the data as you expect.

User avatar
tutankhamen
Posts: 12
Joined: Tue Dec 05, 2017 9:27 am
Location: San Francisco

Re: Trying to make STM32F103 and HC-05 work together

Post by tutankhamen » Wed Dec 06, 2017 5:49 am

I've connected my stm32f103 to arduino nano directly via uart and the result is the same, i.e. I'm sending 10 bytes every 5 seconds from arduino, but stm32 receives randomly only 1-5 bytes. Transferring the data in opposite direction works just perfect. So, now there are three possibilities -

1. Problem with this particular device (i doubt it because of behavior is the same on all three uart ports)
2. Problem with my code (I don't see what exactly could be wrong, moreover, i've tried lot of different combinations)
3. Problem with HardwareSerial implementation in Arduino_STM32 library (it also doesn't look real because I don't see anybody else experience the same bug)

User avatar
RogerClark
Posts: 7553
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Trying to make STM32F103 and HC-05 work together

Post by RogerClark » Wed Dec 06, 2017 8:46 am

Voltage levels ??

stevestrong
Posts: 1976
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Trying to make STM32F103 and HC-05 work together

Post by stevestrong » Wed Dec 06, 2017 9:47 am

Beside what Roger pointed out (voltage levels), how do you interpret the received bytes? I cannot see it in your code.
The variable "received" should sometimes be reset, where do you do it?

Please post a simple sketch which allows us to reproduce your problem.

User avatar
Pito
Posts: 1707
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Trying to make STM32F103 and HC-05 work together

Post by Pito » Wed Dec 06, 2017 1:01 pm

What about

Code: Select all

void loop() {
    [...]
    if(Serial1.available()){
        buffer[received++ % buffer_size] = Serial1.read();
    }
}
PS: Afaik the HC-05 goes into sleep when not active for 5 secs.. You have to send a char to the module in order to wake the HC-05 up.
Pukao Hats Cleaning Services Ltd.

User avatar
mrburnette
Posts: 2063
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Trying to make STM32F103 and HC-05 work together

Post by mrburnette » Wed Dec 06, 2017 1:24 pm

You can always use your PC if equipped with BT (for testing.)

Also, serial device to device transfer can confound anyone. Years ago, I built a QBF Arduino unit to both send and receive 4800/9600 serial streams. Maybe I should update the design to use BT.

https://www.hackster.io/rayburne/the-qb ... tor-ae7015

Anyway, if you have an old mini/micro AVR board around, you might build yourself a QBF.


Ray

MarkB
Posts: 10
Joined: Sun Oct 02, 2016 11:40 pm

Re: Trying to make STM32F103 and HC-05 work together

Post by MarkB » Wed Dec 06, 2017 1:47 pm

What's going on in the rest of your code?

One possibility that comes to mind is that interrupts are being disabled (or a long (relative to the serial character rate) period of time spent in an interrupt service routine) in some other section of the code for a long enough period to cause Serial to misbehave. A test would be to cut out everything except for the minimum necessary to receive the character string.

This brings up the question of how you are looking at the received data to see that is in error. That is, is it echoed back to Serial1/Bluetooth, a different port, or shown on a local display?

User avatar
tutankhamen
Posts: 12
Joined: Tue Dec 05, 2017 9:27 am
Location: San Francisco

Re: Trying to make STM32F103 and HC-05 work together

Post by tutankhamen » Wed Dec 06, 2017 4:58 pm

So many answers! Thank you guys!
RogerClark wrote:
Wed Dec 06, 2017 8:46 am
Voltage levels ??
HC-05 module is feeding from STM32F103's +5V and RX/TX are connected to the 5V tollerant PA9/PA10 pins. This is what you meant? Sorry, I'm not a HW guy, i.e. these are my first steps :)
stevestrong wrote:
Wed Dec 06, 2017 9:47 am
Beside what Roger pointed out (voltage levels), how do you interpret the received bytes? I cannot see it in your code.
The variable "received" should sometimes be reset, where do you do it?

Please post a simple sketch which allows us to reproduce your problem.
I'm writing all received bytes into a buffer and then output them to ssd1306 display:

Code: Select all

display.print("Received: ");
display.print(received);
display.println(" byte(s)");
for (int xx=0; xx<received; ++xx) {
    display.print(buffer[xx]);
    display.print(", ");
}
I will post an example as soon as I will get home.

User avatar
tutankhamen
Posts: 12
Joined: Tue Dec 05, 2017 9:27 am
Location: San Francisco

Re: Trying to make STM32F103 and HC-05 work together

Post by tutankhamen » Wed Dec 06, 2017 6:01 pm

Pito wrote:
Wed Dec 06, 2017 1:01 pm
\PS: Afaik the HC-05 goes into sleep when not active for 5 secs.. You have to send a char to the module in order to wake the HC-05 up.
Thank you for the tip - I didn't know that. But I don't think it relates to the issue, but I will definitely modify my code to avoid it.

Post Reply