Hi all, so I haven't been able to find a way to get this working after a lot of searching, I'm using an HM-18 bluetooth module and some example code to read data from an hm-10.
Essentially, to get this to do what I want, I need to compare strings that have a special character in them, because the module broadcasts OK+CONN when it connects, and OK+LOST when it disconnects. I need to be able to ignore these two messages but also would like to do something with these two updates.
So, basically
If the module output is OK+CONN do x
if the module output is OK+LOST do y
if the module output is neither do z
It seems like the + character is what's causing all my issues.
#include "Keyboard.h"
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial HM10(2, 3); // RX = 2, TX = 3
char appData;
String inData = "";
String conok = "OK+CONN";
int rawdata;
void setup()
{
Serial.begin(9600);
Serial.println("HM10 serial started at 9600");
HM10.begin(9600); // set HM10 serial at 9600 baud rate
Keyboard.begin();
}
void loop(){
HM10.listen(); // listen the HM10 port
while (HM10.available() > 0) { // if HM10 sends something then read
appData = HM10.read();
inData = String(appData); // save the data in string format
}
}
Do you have any code where you try anything at all using String comparison and if statements?
What you ask is straightforward. There is nothing special about '+' that would keep you from receiving it, making it part of a group of characters to be handled together (String or array of chars, small s C strings) and
And comparing it to other such entities that might or might not have '+'.
There really are very few special characters at all. The plus sign isn't one of them.
I'd help with the String thing, but I don't use them.
Everything I tried to do a comparison failed to work, I've tried so many variances using strcmp and other methods that it's all a bit of a mess.
The example that was in the original example for the bluetooth module I found used this.
if ( inData == "F") {
Serial.println("LED OFF");
digitalWrite(13, LOW); // switch OFF LED
delay(500);
}
I've tried changing it to the following, however I get nothing from it. My code doesn't do much because I'm adding bluetooth into something else, so I just want to get this up and running, even just with serial outputs before merging it.
When I tried just the demo changing an LED on and off, it did work.
void loop(){
HM10.listen(); // listen the HM10 port
while (HM10.available() > 0) { // if HM10 sends something then read
appData = HM10.read();
inData = String(appData); // save the data in string format
Serial.print(inData);
if (inData == "OK+CONN") {
Serial.println("Connected");
}
}
}
void loop(){
HM10.listen(); // listen the HM10 port
while (HM10.available() > 0) { // if HM10 sends something then read
appData = HM10.read();
inData = String(appData); // save the data in string format
Serial.print(inData);
if (inData.equals("OK+CONN")){
Serial.println("Connected");
}
}
}
No extra spaces or carriage returns from what I can tell annoyingly, I also tried getting the printing "HM10.read" directly, however that returns a string of numbers that also does not work.
OK+CONN reads out as 7975436778781310
and OK+LOST reads out as 7975437679831310
However, if I use Serial.printlm(HM10.read());
It outputs the following for OK+CONN
79 43 79 78 10
each with a carriage return between them, and OK+LOST is similar.
The numbers are ASCII character codes. 13 is carriage return (\r) and 10 is line feed (\n). "OK+CONN" is not equal to "OK+COMM\r\n"
The serial input basics tutorial may be of interest. It receives the serial data into a string (null terminated character array). You cannot use == with strings, you need to use the strcmp() function.
If you use Strings (String class objects) use the trim() function to remove any white space characters from the String.
Your mistake was reading a single character, turning it into a single-character String, and comparing it to a multi-character string. There is no way a single character will equal a string of multiple characters.
Using .readString() works because it returns a String of multiple characters, unlike .read() which only returns one character.