How to use BLE Shield based on HM-10 bluetooth module?

22,936

Solution 1

#include <SoftwareSerial.h>

  int led         = 13;
  int bluetoothTx = 2;
  int bluetoothRx = 3;
  SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); 
  int baudrate[8] ={4800,9600,14400,19200,28800,38400,57600,115200};
  int i = 1;

void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);
  while(!Serial){}

  Serial.write("AT sent");
  delay(500);
  bluetooth.write("AT+NAME?");
  delay(500);
  while (bluetooth.available()) {
     Serial.write(bluetooth.read());
   }
  delay(100);
  Serial.println("");

  bluetooth.write("AT+POWE3");
  delay(500);
  while(bluetooth.available()) 
  {
    Serial.write(bluetooth.read());
  }
  delay(100);
  Serial.println("");

  delay(500);
  bluetooth.write("AT+CHAR?");
  delay(500);
  while (bluetooth.available()) {
     Serial.write(bluetooth.read());
   }
  delay(100);
  Serial.println("");

  delay(500);
  bluetooth.write("AT+NAMEFlightline"); //Check Status
  delay(500);
  while (bluetooth.available()) {
      Serial.write((char)bluetooth.read());

    }

  Serial.println("");
  bluetooth.write("AT+CHAR0x2901"); //add charicteristic
  delay(500);
  while (bluetooth.available()) {
      Serial.write(bluetooth.read());

    }
  Serial.println("");
  bluetooth.write("AT+RELI0"); 
  delay(500);
  while (bluetooth.available()) {
      Serial.write(bluetooth.read());
    }
  Serial.println("");
  bluetooth.write("AT+SHOW1");
  delay(100);
  while (bluetooth.available()) {
      Serial.write(bluetooth.read());

    }
  Serial.println("");

  pinMode(led,OUTPUT);
  digitalWrite(led,HIGH);
}

void testAllBaudRates(){
  for(int j=0; j<8; j++)
   {
      bluetooth.begin(baudrate[j]);
      delay(100);
      Serial.println("boud rate " + String(baudrate[j],DEC) +" i-> "+String(j,DEC));
     // Serial.println("");
      bluetooth.write("AT");
      delay(500);
      while (bluetooth.available()) {
        Serial.write(bluetooth.read());
        Serial.println();
       }
       delay(100);
   }
}                                            

// and now a few blinks of the  LED, 
// so that we know the program is running

void loop()
{
  //Read from bluetooth and write to usb serial
  while(bluetooth.available())
  {
    char toSend = (char)bluetooth.read();
    if(toSend == 'x'){
       digitalWrite(led,HIGH);
       Serial.println("set high");
       bluetooth.write("RXOK");
    }else if(toSend == 'y'){
      digitalWrite(led,LOW);
      Serial.println("set low");
      bluetooth.write("RXOK");
    }
    Serial.print(toSend);

  }

  //Read from usb serial to bluetooth
  while(Serial.available())
  {
    char toSend = (char)Serial.read();
    bluetooth.write(toSend);
    Serial.print(toSend);
  }
}

Have a look at my sketch above I have a few things to point out that I wasted time on.

make sure you have the line

while(!Serial){}

Or you may get have a working shield but miss the responses as the serial monitor is no ready.

remember that you wont get a response from the blue-tooth module, with a command from the Serial Monitor if it is connected to a device. It is connected to the device when the light stops flashing.

if you run this sketch you should get this output

AT sent
OK+Set:3
OK+Get:0x2901  <- this may be blank the first time you run it
OK+Set:Flightline
OK+Set:0x2901
OK+Set:0
OK+Set:1

the most comprehensive list of AT commands can be found here

[All the AT commands and a good explanation][1]

You will need to at Characteristics to the device as I have done here

bluetooth.write("AT+CHAR?");

or you will find it to connect to iOS and Android

If you are connecting to Android use the BluetoothLE Classes not Bluetooth ones.

Solution 2

You can use this sketch with baud rate autodetect to control your HM-10. This is a part of Apploader project that allows to upload to Arduino board over BLE.

Share:
22,936
Sebastian
Author by

Sebastian

Enzo's father, passion for technology, Software Engineer, iOS Developer. I work with software development since the days of MSX computers. Over the time, I migrated into the Windows development (Delphi), then went to the java world and now find a way to write software that do cool things for normal people.

Updated on September 03, 2020

Comments

  • Sebastian
    Sebastian almost 4 years

    I'm a new bie on arduino projects. I would like to ask you for some help. I bought a BLE Shield for Arduino from ( http://imall.iteadstudio.com/development-platform/arduino/shields/im130704001.html ). They made this shield using Hm-10 Bluetooth module(http://www.jnhuamao.cn/bluetooth.asp?ID=1). Itead Studio has no sample codes using this shield. I have no idea on how to program it or send AT commands from Arduino.

    I read the “AT commands” at the data sheet (ftp://imall.iteadstudio.com/Shield/IM130704001_ITEAD_BLE_Shield/DS_IM130704001_ITEAD_BLE_Shield.pdf) and I tried to send "AT commands” from arduino to BLE shield using this code ( http://arduino.cc/en/Reference/SoftwareSerial ) but I only received the commands back.

    Did anybody here ever use this HM-10 bluetooth module ?

    I need some arduino sketch for help !

  • Curnelious
    Curnelious over 9 years
    ITS NOT WORKING, why anyway you do : bluetooth.begin(115200); ?? the hm-10 baud is 9600 ,i dont get this line, the default is not 115200 , they never say that . also , if you just connect things to the uno and try to connect nothing is happens
  • Bay
    Bay over 8 years
    You wrote a very super and quick code for humanity. Thanks a lot. I did it well! Loves.
  • Bay
    Bay over 8 years
    How can we make a new Bluetooth service without connect option? on Arduino?
  • Mark Gilchrist
    Mark Gilchrist over 8 years
    I am not sure what you mean sorry
  • Bay
    Bay over 8 years
    When you run your code, you need to click on "connect" button to read/write hex data. I ask, as showing UUID of your BLE, is it possible that sending datas seems like no need to connect like temperature value on Arduino?
  • Mark Gilchrist
    Mark Gilchrist over 8 years
    write another question and I can post that code for you