Need some help with Bluetooth communication

Post here first, or if you can't find a relevant section!
Post Reply
tyrooryt
Posts: 8
Joined: Fri Jul 24, 2020 11:36 am

Need some help with Bluetooth communication

Post by tyrooryt »

Hi, I am attempting to run a basic code which will print the text entered in the serial monitor of the Arduino IDE to the mobile phone and vice versa. I am using a bluetooth terminal app on the phone and a HC-05 bluetooth module. I am using an FTDI module to program the STM32. The board I am using is STM32F103C8T6. I was able to run the Blink example. Then I printed the state of the LED in the serial monitor and it all worked. Now I am attempting to run the following code:

void setup()
{
Serial.begin(9600);
while(!Serial){};
Serial.write("plswork");

Serial2.begin(9600);
Serial2.print("Bluetooth_STM32_tester\n");
}

void loop()
{
if(Serial.available())
{
Serial2.write(Serial.read());
}
if(Serial2.available()>0)
{
Serial.println("plss\n");
Serial.write(Serial2.read());
}
}


I have connected the FTDI to pins A9 and A10 of the STM32 and I have connected the Rx and Tx of HC-05 to pins A2 and A3 respectively.When I upload this, the text "Bluetooth_STM32_tester\n" is getting printed on the bluetooth terminal app on the phone, But "plswork" isn't being printed on the serial monitor. Whenever I type something into serial monitor and send, it is being displayed in the phone, but it is not working the other way around.I have used Serial in the blink code to print the state of the LED and it printed. But if I use two Serials, Serial and Serial 2, nothing is being printed on Serial (serial monitor of arduino ide).

Please let me know if you have any suggestions on how to rectify this.

Thanks
User avatar
Bakisha
Posts: 139
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 5
Contact:

Re: Need some help with Bluetooth communication

Post by Bakisha »

Try Serial1 instead of Serial.

Most likely you are sending data to USB port (that acts as Serial).
You can test it by conecting USB cable, but don't connect power pin from FTDI dongle (just TX,RX,GND).
tyrooryt
Posts: 8
Joined: Fri Jul 24, 2020 11:36 am

Re: Need some help with Bluetooth communication

Post by tyrooryt »

I have tried doing what you suggested, but the result is still the same, nothing is being printed in the serial monitor in the IDE. Serial1.write() isn't printing anything to the serial monitor.

Initially with the blink code, I have used Serial to print the state of the LED, and it worked. So I thought that (Rx1,Tx1) are Serial, (Rx2,Tx2) are Serial2, since I am able to send text from the serial monitor to the phone (HC-05 is connected to Rx2 and Tx2 (pins A2 and A3).
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Need some help with Bluetooth communication

Post by fredbox »

If using the STM32 official core, set both USART and USB to enabled with no generic Serial.
Then define the serial port names so they won't move around on you.

Code: Select all

#define Serial SerialUSB
//                      RX    TX
HardwareSerial Serial1(PA10, PA9);
HardwareSerial Serial2(PA3, PA2);
HardwareSerial Serial3(PB11, PB10);
You can also use non-standard names if you want. I have a GPS module connected to PA9 and PA10.

Code: Select all

#define mySerial SerialUSB
HardwareSerial gpsSerial(PA10,PA9);
void setup(){
  mySerial.begin(9600);
  gpsSerial.begin(9600);
}
void loop() {
  if (gpsSerial.available()) {
    char ch = gpsSerial.read();
    mySerial.print(ch);
  }
}
The output from this is a series of NMEA messages.
tyrooryt
Posts: 8
Joined: Fri Jul 24, 2020 11:36 am

Re: Need some help with Bluetooth communication

Post by tyrooryt »

Hi,

I am encountering this error in the definition of HardwareSerial:

Code: Select all

Bluetooth_Basic:28:32: error: no matching function for call to 'HardwareSerial::HardwareSerial(<anonymous enum>, <anonymous enum>)'
 HardwareSerial Serial1(PA10,PA9);
                                ^
/home/user/Documents/stm32_stuff/codes/Bluetooth_Basic/Bluetooth_Basic.ino:28:32: note: candidates are:
In file included from /home/user/.arduino15/packages/stm32duino/hardware/STM32F1/cores/maple/wirish.h:69:0,
                 from /home/user/.arduino15/packages/stm32duino/hardware/STM32F1/cores/maple/Arduino.h:30,
                 from sketch/Bluetooth_Basic.ino.cpp:1:
/home/user/.arduino15/packages/stm32duino/hardware/STM32F1/cores/maple/HardwareSerial.h:128:5: note: HardwareSerial::HardwareSerial(usart_dev*, uint8, uint8)
     HardwareSerial(struct usart_dev *usart_device,
     ^
/home/user/.arduino15/packages/stm32duino/hardware/STM32F1/cores/maple/HardwareSerial.h:128:5: note:   candidate expects 3 arguments, 2 provided
/home/user/.arduino15/packages/stm32duino/hardware/STM32F1/cores/maple/HardwareSerial.h:125:7: note: constexpr HardwareSerial::HardwareSerial(const HardwareSerial&)
 class HardwareSerial : public Stream {
       ^
/home/user/.arduino15/packages/stm32duino/hardware/STM32F1/cores/maple/HardwareSerial.h:125:7: note:   candidate expects 1 argument, 2 provided
/home/user/.arduino15/packages/stm32duino/hardware/STM32F1/cores/maple/HardwareSerial.h:125:7: note: constexpr HardwareSerial::HardwareSerial(HardwareSerial&&)
/home/user/.arduino15/packages/stm32duino/hardware/STM32F1/cores/maple/HardwareSerial.h:125:7: note:   candidate expects 1 argument, 2 provided
exit status 1
no matching function for call to 'HardwareSerial::HardwareSerial(<anonymous enum>, <anonymous enum>)'
I have downloaded the file from this link:https://github.com/rogerclarkmelbourne/Arduino_STM32
I extracted the file and placed it in /home/user/Arduino/libraries/
I am running the arduino IDE on Ubuntu 18.04.4.

I don't understand why it is going to the hidden directory and using the HardwareSerial code from there. Could you please let me know if I made a mistake in placing the downloaded file?
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Need some help with Bluetooth communication

Post by ag123 »

please mention core used along with the question, it saves a lot of guessing as the different cores have different features and implementation.
this is the libmaple (roger's) core.
in libmaple core Serial is your usb-serial, Serial1 is uart1 (pa9, pa10), Serial2 is uart2 (pa2, pa3), Serial3 is uart3 (pb10, pb11)
these are pre-defined
https://github.com/rogerclarkmelbourne/ ... ial.h#L188
https://github.com/rogerclarkmelbourne/ ... rial.h#L95

e.g.

Code: Select all

void setup()  {
   //usb serial
   Serial.begin();
   
   //uart1
   Serial1.begin(115200);
   
   //uart2
   Serial2.begin(115200);
}

...

you can probably find this in STM32F1/libraries/A_STM32_Examples/examples/Communication/USB-uart-w-signals
locally in your core folders
https://github.com/rogerclarkmelbourne/ ... -w-signals
tyrooryt
Posts: 8
Joined: Fri Jul 24, 2020 11:36 am

Re: Need some help with Bluetooth communication

Post by tyrooryt »

Sorry for not mentioning the core. I realized some mistakes I made and reinstalled stuff again.

1. Initially, I followed a tutorial on youtube without paying attention to what "core" is. I used this link to install the boards: http://dan.drown.org/stm32duino/package ... index.json

2. I think this created a package in the hidden folder of arduino (i'm using ubuntu 18) in the home directory, then I came across Roger Clarke's github page and downloaded that file and placed it in the ~/Arduino/hardware folder (I had to create this hardware folder).

3. I tried to resolve that HardwareSerial error by copying the HardwareSerial.h and HardwareSerial.cpp file from the hidden arduino folder to the hardware folder with the RogerClarke Files. This led to a mishmash of many files and I could get nowhere, so deleted every arduino directory, uninstalled and reinstalled arduino.

4. I have followed these links and installed stm32duino:

Code: Select all

https://github.com/stm32duino/wiki/wiki/Getting-Started
https://github.com/stm32duino/wiki/wiki/Upload-methods#serial-1
5. Here is the code I used now:

Code: Select all

#include<HardwareSerial.h>

HardwareSerial Serial2(PA3, PA2);

void setup()
{
  Serial.begin(9600);
  while(!Serial){};
  Serial.write("Testing");
  
  Serial2.begin(9600);
  Serial2.print("Bluetooth_STM32_tester\n");
  
}

void loop()
{

  if(Serial.available())
  {
    Serial2.write(Serial.read());
  }
  if(Serial2.available()>0)
  {
    Serial.write(Serial2.read());
  }
}
6. This is working fine and I am able to send text both ways. But the Serial.Write("Testing") still isn't being printed.

7. The code is working even if I replace all instances of Serial with Serial1 (I am using the FTDI module to upload and the microusb of the stm32 is unconnected)

8. However, If I use this statement: HardwareSerial Serial1(PA10, PA9);
I am getting this error: (mulitple definitions of Serial1):

Code: Select all

Arduino: 1.8.13 (Linux), Board: "Generic STM32F1 series, BluePill F103C8, STM32CubeProgrammer (Serial), Enabled (generic 'Serial'), None, Low/Full Speed, Smallest (-Os default), Newlib Nano (default)"

/home/user/.arduino15/packages/STM32/tools/xpack-arm-none-eabi-gcc/9.2.1-1.1/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /tmp/arduino_cache_332391/core/core_f156caa939a61c73cea7dfa536e0569d.a(HardwareSerial.cpp.o):(.bss.Serial1+0x0): multiple definition of `Serial1'; sketch/Bluetooth_Basic.ino.cpp.o:(.bss.Serial1+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Generic STM32F1 series.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
9. Can someone please correct me if i'm wrong here: the links mentioned in 4. are for the arduino core and Roger's is for the Maple core.
User avatar
Bakisha
Posts: 139
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 5
Contact:

Re: Need some help with Bluetooth communication

Post by Bakisha »

Try

Code: Select all

Serial1.print("Testing");
or use

Code: Select all

Serial1.write("Testing\n");
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Need some help with Bluetooth communication

Post by fredbox »

6. This is working fine and I am able to send text both ways. But the Serial.Write("Testing") still isn't being printed.
Outputting anything to the USB or other serial ports during setup is usually a futile exercise.

Roger's libmaple based core has been around for a few years and for a long time was the only relatively easy option to program the maple mini and pill boards.

Now we have the official STM32Duino core that is in active development and recommended by many (including me) as the best way for individuals new to the STM32 platform. See here to get started. I converted all of my projects to this core when v1.6.0 was released and am only using this core going forward.

Lots of people claim that the binary size of the programs is smaller with the older Roger/Leaflabs core, but I've found that it depends on your code. Sometimes the official core produces a smaller program. Unless your program is so big that it doesn't fit in memory, you shouldn't be too concerned with the size. STM32Duino code will never be anywhere close to the code sizes for the ATMega processors.
Post Reply

Return to “General discussion”