如何使用基于HM-10蓝牙模块的BLE Shield? [英] How to use BLE Shield based on HM-10 bluetooth module?

查看:35
本文介绍了如何使用基于HM-10蓝牙模块的BLE Shield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 arduino 项目的新手.我想请你帮忙.我从 ( http://imall.iteadstudio.com/development-platform/arduino/shields/im130704001.html).他们使用Hm-10蓝牙模块(http://www.jnhuamao.cn/bluetooth.asp?ID=1).Itead Studio 没有使用此屏蔽的示例代码.我不知道如何编程或从 Arduino 发送 AT 命令.

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.

我阅读了数据表中的AT 命令"(ftp://imall.iteadstudio.com/Shield/IM130704001_ITEAD_BLE_Shield/DS_IM130704001_ITEAD_BLE_Shield.pdf),我尝试使用此代码(http://arduino.cc/en/Reference/SoftwareSerial )但我只收到了命令.

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.

这里有人用过这个 HM-10 蓝牙模块吗?

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

我需要一些 arduino 草图来寻求帮助!

I need some arduino sketch for help !

推荐答案

#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.

确保你有线路

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

可以在这里找到最全面的 AT 命令列表

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?");

或者你会发现它连接到iOS和Android

or you will find it to connect to iOS and Android

如果您要连接到 Android,请使用 BluetoothLE 类而不是蓝牙类.

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

这篇关于如何使用基于HM-10蓝牙模块的BLE Shield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆