Hello everyone, I'm doing a project where I use a HM-10 BLE module as an iBeacon detector to find other HM-10 that are set as iBeacons.
When I load a sketch that lets me write the AT commands in the Serial Monitor, the results I get are the ones I want. Every piece of information appears on screen perfectly.
But when I upload a sketch that has the AT command already written in it I get the first part of the information.
This is the code that allows me to write the command "AT+DISI?" in the Serial Monitor.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(11, 10);
void setup(){
Serial.begin(9600);
BTSerial.begin(9600);
while(!Serial);
Serial.println("Write AT commands: ");
}
void loop(){
if(BTSerial.available())
Serial.write(BTSerial.read());
if(Serial.available())
BTSerial.write(Serial.read());
}
Immediately all the information of the iBeacon appears:
OK+DISISOK+DISC:4C000215:74278BDAB64445208F0C720EAF059935:1234FA03C5:B4994C52C407:-032OK+DISCE
This is the code that sends the command by it's own.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial (11,10);
void setup() {
BTSerial.begin(9600);
}
void loop() {
BTSerial.flush();
BTSerial.write("AT+DISI?");
delay(5000);
while(BTSerial.available() >0) {
Serial.write(BTSerial.read());
}
}
And this is the information that appears in the Serial Monitor
OK+DISISOK+DISC:4C000215:74278BDAB64445208F0C720EAF059935:1234F
I do not understand why does this happen, I tried everything I could imagine and still can not see what Im doing wrong.
Thank you in advance for your time!!
Ok, I put an Arduino with the second sketch (the one that gives incomplete info), and another one with the first sketch. Both of them are connected to the same BLE.
I keep having the same response with both, one with half the data and another with full data.
This means that the AT command sent by the second sketch is getting the correct response from the BLE as the arduino with the first sketch is picking up the whole package of data.
So the AT command is sent correctly, the BLE responds correctly, the problem is in picking up the information.
I tried many variations of this sketch, but I keep having the same results.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial (11,10);
void setup() {
BTSerial.begin(9600);
Serial.begin(9600);
}
void loop() {
BTSerial.flush();
if(BTSerial.available()<=0){
BTSerial.write("AT+DISI?");
delay(5000);
}
int num = 0;
while(num <=300){
if(BTSerial.available()){
Serial.write(BTSerial.read());
}
num++;
delay(10);
}
num = 0;
}
I will appreciate any help or suggestion.
Thanks!
while(BTSerial.available() >0) {
Serial.write(BTSerial.read());
}
In your first sketch, this will loop forever. Why not put it in an infinite loop? For example:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial (11,10);
void setup() {
BTSerial.begin(9600);
}
void loop() {
BTSerial.flush();
BTSerial.write("AT+DISI?");
delay(5000);
while(1){
if(BTSerial.available()) {
Serial.write(BTSerial.read());
}
}
}
Hello, thanks for responding!
I want to scan for iBeacons every certain amount of time, with the sketch you provided the scanning would take place just the first time and then it will be stuck in that loop.
Also (I don't know why) it does not work. I loaded it in the arduino that had sketch 2 and both arduinos stopped receiving data frome the BLE.
Thanks again for taking interest in my problem, any other idea is welcome!
Try this:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial (11,10);
void setup() {
BTSerial.begin(9600);
}
void loop() {
BTSerial.flush();
BTSerial.write("AT+DISI?");
delay(5000);
while(1){
while(BTSerial.available() > 0) {
Serial.write(BTSerial.read());
}
}
}
This sketch allows just 1 scan like the other, and it does not work either. I removed the while(1) and data from the BLE was received again, but still incomplete while the other arduino receives the whole package.
This?
#include <SoftwareSerial.h>
SoftwareSerial BTSerial (11,10);
void setup() {
BTSerial.begin(9600);
}
void loop() {
BTSerial.write("AT+DISI?");
while(BTSerial.available() > 0) {
Serial.write(BTSerial.read());
}
delay(5000);
}
That was my first try, is in the second code snippet in the initial question.
After that I thought that maybe the transmission was interrupted in the exact place the data stops, getting out of the while(BTSerial.available() >0).
So I made an extensive loop with while(num <=300) to get one by one every byte of data, trying to emulate what happens in the first sketch. This is the code in my second post.
Thanks anyway Isaac, maybe someone else figures it out.
Brainstorm!
#include <SoftwareSerial.h>
SoftwareSerial BTSerial (11,10);
void setup() {
BTSerial.begin(9600);
Serial.begin(9600);//This has been forgotten!
}
void loop() {
BTSerial.flush();
BTSerial.write("AT+DISI?");
delay(5000);
while(BTSerial.available() >0) {
Serial.write(BTSerial.read());
}
}
Hello Isaac, I missed saying that I had already added the Serial.begin in all my previous sketches. Still no luck

If you read only once in setup(), does it work?
Hello, sorry I being really busy!! But I found the solution to the problem, even though I never found the cause.
What I did was use the library Timer.h that can be found in Arduino Playground - Timer Library
So every 10 seconds I send the AT command and had an "infinite" loop picking up the data after sending it for 5 seconds.
Thanks a lot for your help!!!
Best regards,
Leo
Could you share your final code please?