Arduino: using Serial and Serial software with bluetooth module

My goal is to use Arduino to establish a connection between a PC and an Android device using the bluetooth HC-05 module.

I am using a USB connection between a PC and Arduino (Serial Monitor) and SoftwareSerial to connect to the HC-05.

My problem is that the connection works well with BT on the PC, but does not work properly in a different way. When sending from PC to BT, all sent characters are received by the BT device only when I close the serial monitor on the PC or when I reset Arduino.

I ruled out the problem with the BT module or Android application, because if in Arduino I implement ECHO code (write on Android and send to Android), everything will be fine.

With the Arduino code below, the expected behavior is: Arduino reset -> Hello word sent, Serial monitor open-> nothing happens, the character written on the serial monitor-> the character received on BT, the character written on BT-> the character received on Serial Monitor, The serial monitor is closed -> nothing happens.

Real behavior: Arduino reset -> Hello word sent, Serial monitor open-> 2 Hello, the word BT and 1 ("goodnight") on the PC, the character written on the serial monitor, β†’ nothing, the character written on BT-> received on Serial Monitor, the serial monitor is closed -> the previous recorded character in the serial monitor received + Hello Word.

How can I fix this problem?

code:

#include <SoftwareSerial.h> SoftwareSerial mySerial(2, 3); // RX, TX int a=0; char c; char d; void setup() { Serial.begin(9600); Serial.println("Goodnight moon!"); mySerial.begin(9600); mySerial.println("Hello, world?"); } void loop() { delay(10); if (Serial.available()) { c=Serial.read(); delay(10); Serial.write(c); } delay(10); if (mySerial.available()) { d=mySerial.read(); delay(10); mySerial.write(d); } } 
+6
source share
6 answers

This code works for me on an Arduino Mini Pro (should be the same as UNO) with the HC-05. I have a HC-05 paired with my laptop. Using HyperTerminal on the COM port associated with the HC-05 and the Arduino serial console, I can send messages bi-directionally. The Serial.println statements appear in the Hyperterminal window as they should.

 #include <SoftwareSerial.h> #define rxPin 8 #define txPin 7 SoftwareSerial mySerial(rxPin, txPin); // RX, TX char myChar ; void setup() { Serial.begin(9600); Serial.println("Goodnight moon!"); mySerial.begin(9600); mySerial.println("Hello, world?"); } void loop(){ while(mySerial.available()){ myChar = mySerial.read(); Serial.print(myChar); } while(Serial.available()){ myChar = Serial.read(); mySerial.print(myChar); } } 
+2
source

I implemented serial communication between Arduino Uno and PC, and that was my code, hope it can help:

 int data; char character; int start_flag = 0; void setup() { Serial.begin(921600); pinMode(2, OUTPUT); } void loop() { if(Serial.available() > 0){ character = (char) Serial.read(); if(character == 's') { start_flag = 1; } if(character == 't') { start_flag = 0; } } if (start_flag == 1) { Serial.print(data); //data that was acquired by internal ADC } } 
+1
source

You can try this . This is about the simplest code that you can use when testing the communication of the Arduino # C # Bluetooth communicator. Note: the code was tested by connecting PIN1 (TX) ↔ MODULE RX, PIN2 (RX) ↔ MODULE TX and dividing PIN1 (TX) 5V by 2.5V before applying it to the module.

Hope this helps everyone who is trying!

0
source

Use this sequential setting. With this code, I can receive and send date on bluetooth from Serial Monitor

 void setup(){ Serial.begin(9600); // Begin the serial monitor at 9600bps bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps bluetooth.print("$"); // Print three times individually bluetooth.print("$"); bluetooth.print("$"); // Enter command mode delay(100); // Short delay, wait for the Mate to send back CMD bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity // 115200 can be too fast at times for NewSoftSerial to relay the data reliably bluetooth.begin(9600); // Start bluetooth serial at 9600 pinMode(led, OUTPUT); pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); } 

For more information, visit http://www.circuitmagic.com/arduino/arduino-and-bluetooth-hc-06-to-control-the-led-with-android-device/

0
source

I recommend using this app for testing:

https://play.google.com/store/apps/details?id=com.vagoscorp.virtualterminal

This allows you to see and send bytes in the form of bytes (a number from 0b00000000 to 0b11111111 (from 0 to 255 in decimal format)) so that you can do a simple echo firmware to check if the data transfer speed works correctly, and with this work start send commands to turn on / off some LEDs.

this is an example of an echo code:

 char dato = 0; void setup() { Serial.begin(9600);//9600 is the default baudrate of the HC-05 (you can change it by AT commands, google it if you want) //pinMode(13, OUTPUT); //enable this pin if you want to use de LED idea //digitalWrite(13, HIGH); } ////////////////////////////////////////////////////////////////////////// void serialEvent() { //if you have received serial data while (Serial.available() > 0) { char dato = (byte)Serial.read();//save the byte Serial.write(dato);//send the just received byte (echo) } } /////////////////////////////////////////////////////////////////////////// void loop() { } 

I hope this helps you

0
source

If you have the same problem, you need to view the BT module as 2 different baud rates on the wired side and on the radio. The radio station is installed through everything that you connect through putty, the wired side is programmed using AT-commands. HC-05 by default is 38400.

0
source

Source: https://habr.com/ru/post/952268/


All Articles