I have been struggling for a few days reading and processing the data from my bluetooth module (HC05). It is now working okay and I am sending data that is sent one character at a time through Bluetooth using Putty on my PC and the line of data sends a Carriage Return after I hit the enter key.
I found some code from the forum that I have been working with and it works well. The code below keeps processes the serial data a character at a time until it finds the Carriage Return '\r'. It then it prints it out, as one, to the putty terminal via Bluetooth again.
PROBLEM: I wish compare the data string and then act accordingly. Although what I am comparing looks the same in the Putty terminal, I never get a match. I have tried adding the \0 null terminator in the form of "Hello\0" but that does not work either.
I have tried to look at the length of the string or do string to DEC / HEX conversions on it to look at its contents in a better way, but I keep getting syntax errors. The string is actually defined as char inputString[40]; and from research, I wonder if that is a 'c' type string not Arduino type. At this point I am unsure if I have hidden control codes in the string or the string is of a different type format.
Any help would be greatly appreciated.
// Demo to read and compare Serial input data over bluetooth
// fails to compare although it prints what I expect in the String.
// Please see the '<<<< DOES NOT EQUATE!!!!' comment to where the problem is.
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(15, 14); // RX, TX
#define Soft_TXD 14 //PA1 17 TXD Soft Serial Port for Bluetooth
#define Soft_RXD 15 //PA2 18 RXD Soft Serial Port for Bluetooth
//Bluetooth serial
const byte MAX_STRING_LEN = 40;
char inputString[MAX_STRING_LEN]; // a string to hold incoming data
byte strLen = 0; // current length of rec'd string
char inChar;
void setup() {
SoftSerial.begin(9600); // Soft Serial for Bluetooth
}
// the loop function runs over and over again forever
void loop() {
// Check the serial port for any pending data
if (processSerial()) {
// Received a complete string. For now, just echo it back
SoftSerial.println(String("Data Received: ") + inputString);
if (inputString == "Hello") { // <<<< DOES NOT EQUATE!!!!
showHello();
}
//Re-initialise the input string to 0
inputString[0] = '\0'; // Make sure array is empty
strLen = 0; // reset length to zero
}
}
// FUNCTIONS
// ---------
boolean processSerial() { // Check the Serial port
// and gather any pending data
while (SoftSerial.available()) {
char inChar = (char)SoftSerial.read();
if (inChar == '\r') { // We are done receiving the string,
// if we received carriage return.
SoftSerial.print("Found esc r :");
SoftSerial.println(inChar, DEC); // Print out the value of CR \r
return true;
}
// add it to the inputString if we have room
if (strLen < (MAX_STRING_LEN - 1)) {
inputString[strLen++] = inChar;
inputString[strLen] = '\0';
}
}
return false;
}
void showHello() {
SoftSerial.print("Matched: Hello");
}
// Demo to read and compare Serial input data over bluetooth
// fails to compare although it prints what I expect in the String.
// Please see the '<<<< DOES NOT EQUATE!!!!' comment to where the problem is.
#if 0
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(15, 14); // RX, TX
#else
#define SoftSerial Serial
#endif
#define Soft_TXD 14 //PA1 17 TXD Soft Serial Port for Bluetooth
#define Soft_RXD 15 //PA2 18 RXD Soft Serial Port for Bluetooth
//Bluetooth serial
const byte MAX_STRING_LEN = 40;
char inputString[MAX_STRING_LEN]; // a string to hold incoming data
byte strLen = 0; // current length of rec'd string
char inChar;
// -----------------------------------------------------------------------------
void setup() {
SoftSerial.begin(9600); // Soft Serial for Bluetooth
Serial.println ("reading Hello");
}
// -----------------------------------------------------------------------------
// the loop function runs over and over again forever
void loop() {
// Check the serial port for any pending data
if (processSerial()) {
// Received a complete string. For now, just echo it back
SoftSerial.println(String("Data Received: ") + inputString);
#if 0
if (inputString == "Hello") { // <<<< DOES NOT EQUATE!!!!
#else
if (! strcmp (inputString, "Hello")) {
#endif
showHello();
}
//Re-initialise the input string to 0
inputString[0] = '\0'; // Make sure array is empty
strLen = 0; // reset length to zero
}
}
// -----------------------------------------------------------------------------
// FUNCTIONS
// ---------
boolean processSerial() { // Check the Serial port
// and gather any pending data
while (SoftSerial.available()) {
char inChar = (char)SoftSerial.read();
if (inChar == '\r') { // We are done receiving the string,
// if we received carriage return.
SoftSerial.print("Found esc r :");
SoftSerial.println(inChar, DEC); // Print out the value of CR \r
return true;
}
// add it to the inputString if we have room
if (strLen < (MAX_STRING_LEN - 1)) {
inputString[strLen++] = inChar;
inputString[strLen] = '\0';
}
}
return false;
}
void showHello() {
SoftSerial.print("Matched: Hello");
}
Thank you for your responses aarg & gcjr. It works perfectly! At least I was on the right track about c style strings
I did modify it slightly as my code comment 'DOES NOT EQUATE' was an instruction to the forum as a 'marker' what was wrong and not what I wanted as a logical condition. So, in fact, I wish to test to see if it DID equate.
A small fly in the ointment
The only small issue I have now is that my IDE is throwing up strcmp as undefined.
I do not use the standard Arduino IDE. Instead, I use Visual Studio with the Visual Micro plugin.
I did look to see if I need to include any C library for the strcmp function but the answer was no, so I tested it 'as was' and it works even with the 'undefined' IDE identifier. Apparently this is built into the Arduino compiler (if that is the correct term). I am sure I came across an #include statement, in the last few days, that was mentioned when using c style strings but I can not find the info now.
This is obviously not a show stopper but it would be nice if someone knew a resolution.
Castingflame:
This is obviously not a show stopper but it would be nice if someone knew a resolution.
When you compile an Arduino sketch the Arduino pre-processor does a lot of "stuff" in an attempt to make writing sketches "easier".
One of those things is that it adds a lot of #include directives for common libraries I thinks you might use at the top of the sketch. Unfortunately the VS IDE doesn't know that when the intellisense is syntax checking your sketch.
The fix, as gcjr points out, is to add the #include manually.