In short, I've hooked up an HM-10 to digital pins 10 and 11 on my Uno (required because TX/RX (0/1) can't be used whilst a serial connection is present). The Bluetooth module is a Sunfounder HM-10 which comes with a data sheet. I'm successfully communicating with the HM-10 and I've written a modified version of serial comms tutorial code provided so it stores the reads up in a buffer before outputting them which works fine, I can set all properties on the HM-10 using this method.
The delays were necessary as it seems the clock cycle can't 'keep up' - bytes go missing when reading/writing, so if I attempt to write by just using Serial.read()
in loop()
alone then not all of it is read/written.
Here's the code.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Goodnight moon!");
delay(500);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.print("Wake up wake up wake up wake up wake up! Wake up wake up wake up wake up wake up!");
}
void loop() // run over and over
{
if (mySerial.available()) {
String s = "";
char c;
while((c = mySerial.read()) != -1) {
s += c;
delay(10);
}
Serial.println("Received: " + s);
}
if (Serial.available()) {
String s = "";
char c;
while((c = Serial.read()) != -1) {
s += c;
delay(10);
}
delay(10);
Serial.println("Sent: " + s);
mySerial.print(s);
}
}
This allows me to show input and output as follows:
Sent: AT+ADDR?
Received: OK+ADDR:F4B85EB42D64
Sent: AT+ADVI?
Received: OK+Get:0
Sent: AT+ALLO?
Received: OK+Get:0
Sent: AT+BATT?
Received: OK+Get:077
So good so far. Here's what I can't figure out from the data sheet. How do I make the bluetooth module discoverable on, say, my Mac or from my iPhone? I've tested setting AT+ROLE0
or AT+ROLE1
followed by AT+RESET
and also set AT+NAME
so I know what to expect, yet I can't discover the module.
Also to note, the status LED is flashing as per the specification as an unconnected status (500ms high, 500ms low) which indicates AT+PIO10
is setup. AT+ADTY?
is set to 0 which allows it to be advertising, responds to scanning and connectable.
I'm likely missing something quite obvious. Any ideas?