Hi, I am trying to get an HC-05 working with newly purchased Nano 33 BLE Sense - I need classic bluetooth, not BLE.
I have spent days reading every post and troubleshooting, to no avail.
The below code works perfectly using software serial on my Uno (you can see the test code at the top) , but I cannot seem to get it working properly using either Serial1 or additional UART on the Nano.
Code is taken from the Serial basics thread.
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); //RX, TX
#include <Wire.h>
UART mySerial (digitalPinToPinName(8), digitalPinToPinName(3), NC, NC); //TX, RX
// Serial comm variables
char readTxt; // string to store incoming message from bluetooth
const byte numChars = 32;
char receivedChars[numChars];
static byte ndx = 0;
boolean newData = false;
void setup(){
mySerial.begin(9600);// Open serial port for HC-05
Serial.begin(9600); // serial communication to check the data on serial monitor
}
void loop(){
SerialReceive();
showNewData();
}
void SerialReceive() {
static boolean receiveInProgress = false;
char startMarker = '<';
char endMarker = '>';
if (mySerial.available()>0) {
while (mySerial.available()>0 && newData == false) {
readTxt = mySerial.read();
if (receiveInProgress == true) { // Receiving command
if (readTxt != endMarker) {
receivedChars[ndx] = readTxt;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else { // Command received
receivedChars[ndx] = '\0'; // terminate the string
receiveInProgress = false;
ndx = 0;
newData = true;
}
}
else if (readTxt == startMarker) { // Beginning of command
receiveInProgress = true;
}
}
}
}
void showNewData() {
if (newData == true) {
Serial.println(receivedChars);
receivedChars[0] = '\0'; // clear the string
newData = false;
}
}
I have looped the serials and can send test messages from mySerial to Serial (USB), so I am certain there is no hardware issue.
Not sure why I need to include <Wire.h>, but it won't compile without it?
I actually do get some data transmitted, but shows nothing but rubbish in the serial window, and data only sometimes makes it through.
After sending the letter c about thirty times, I eventually get this in the serial monitor -
I really hope there is something simple I am missing, can someone please help?
Thank you very much!