1

I'm trying to communicate with the STM32 microcontroller via serial communication. Before I was using the USB cable and the communication worked flawlessly. However when I switched the same serial mode from USB to BLE (HM-10), the microcontroller fails to respond correctly for the first try, but it is okay after that. What could be a problem?

I tried searching if it was something to do with buffers or start/stop bits. But it didn't help me much because I had no clue about how to implement them through code. I'm using the MbedOS to program my dev board.

CODE:

#include "mbed.h"

DigitalOut led1(LED1);

 #define MAX_INPUT_LENGTH 2

int code[MAX_INPUT_LENGTH];

Serial bt(PD_5, PD_6);

//Serial pc(USBTX, USBRX);

int main()

{

    while(1)
    {
        while(bt.readable()==1)
        {
            volatile char str[2];

            bt.scanf("%2s",str);  

                int index = 0;
                while(index<=MAX_INPUT_LENGTH)
                {                                                                
                    code[index] = str[index] - '0';                            // convert to an int.
                    index++;                                                // increase the index.
                }

            if(code[0]==0 && code[1]==1)
            {
                bt.printf("\n01 RECIEVED.");
                for(int x=0;x<15;x++)
                {
                    led1 = 1;
                    wait(0.25);
                    led1 = 0;
                    wait(0.25);
                }
            }
            else
                bt.printf("\nINCORRECT VALUE RECIEVED.");
        }
    }    
}

I expected the output from ble serial to work the same way as the usb serial, but it failed for the first run.

Matthijs
  • 2,483
  • 5
  • 22
  • 33
  • I propose you investigate what the microcontroller receives. If you have spare pins, you could use two serial connections: the first one for Bluetooth, and the second one via USB to get debug messages. – Codo Apr 03 '19 at 09:38
  • BTW: Does the microcontroller write debug messages at boot time to the same serial output that you use for the Bluetooth module? If so, it could confuse the module. I had a similar problem with another microcontroller. In the end, I used a MOSFET to power on the Bluetooth module with some delay. – Codo Apr 03 '19 at 10:59
  • @Codo I tried using the two serial connection method you mentioned. I used the USB serial hooked to my pc as the output to check the printed data and the Bluetooth that connects to an Android device to send data from a serial monitor. As soon as I power up my board, the mc did write "INCORRECT VALUE RECEIVED" on the pc serial monitor. At the time, I hadn't even sent anything from the Bluetooth. – Anshul Sood Apr 03 '19 at 12:00
  • Have you check if the Bluetooth module writes something to the serial port at startup? That could explain it. But if extra output from the BLE module is the problem, you can add some code that is run at startup and reads (and discards) all data from the serial connection. – Codo Apr 03 '19 at 12:39

0 Answers0