【问题标题】:How to use BLE Shield based on HM-10 bluetooth module?如何使用基于HM-10蓝牙模块的BLE Shield?
【发布时间】:2013-11-07 02:13:28
【问题描述】:

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

我阅读了数据表 (ftp://imall.iteadstudio.com/Shield/IM130704001_ITEAD_BLE_Shield/DS_IM130704001_ITEAD_BLE_Shield.pdf) 上的“AT 命令”,并尝试使用此代码 (http://arduino.cc/en/Reference/SoftwareSerial) 从 arduino 向 BLE shield 发送“AT 命令”,但我只收到了返回的命令。

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

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

【问题讨论】:

    标签: bluetooth arduino bluetooth-lowenergy hm-10


    【解决方案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);
      }
    }
    

    看看我上面的草图,我有几件事要指出我浪费了时间。

    确保你有这条线

    while(!Serial){}
    

    或者您可能有一个工作屏蔽,但由于串行监视器没有准备好而错过响应。

    请记住,如果蓝牙模块连接到设备,您将不会收到来自串行监视器的命令的响应。当灯停止闪烁时,它已连接到设备。

    如果你运行这个草图,你应该得到这个输出

    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 命令列表

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

    您需要像我在这里所做的那样设置设备的特性

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

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

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

    【讨论】:

    • 你为人类写了一个非常超级快速的代码。非常感谢。我做得很好!爱。
    • 如何在没有连接选项的情况下创建新的蓝牙服务?在 Arduino 上?
    • 我不明白你的意思很抱歉
    • 当你运行你的代码时,你需要点击“连接”按钮来读/写十六进制数据。我问,在显示你的 BLE 的 UUID 时,发送数据是否可能不需要像 Arduino 上的温度值那样连接?
    • 写另一个问题,我可以为您发布该代码
    【解决方案2】:

    您可以使用 this sketch 和波特率自动检测来控制您的 HM-10。这是Apploader project 的一部分,允许通过 BLE 上传到 Arduino 板。

    【讨论】:

      【解决方案3】:

      这也有点晚了,但是试试下面的代码,如果你发送它“AT”它应该会给你一个“OK”:

      #include <SoftwareSerial.h>  
      
      int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
      int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3
      
      SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
      
      void setup()
      {
        Serial.begin(9600);  // Begin the serial monitor at 9600bps
      
        bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
        delay(100);  // Short delay, wait for the Mate to send back CMD
        bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
        // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
        bluetooth.begin(9600);  // Start bluetooth serial at 9600
      }
      
      void loop()
      {
        if(bluetooth.available())  // If the bluetooth sent any characters
        {
          // Send any characters the bluetooth prints to the serial monitor
          Serial.print((char)bluetooth.read());  
        }
        if(Serial.available())  // If stuff was typed in the serial monitor
        {
          // Send any characters the Serial monitor prints to the bluetooth
          bluetooth.print((char)Serial.read());
        }
        // and loop forever and ever!
      }
      

      【讨论】:

      • 它不工作,为什么你这样做:bluetooth.begin(115200); ?? hm-10 波特是 9600 ,我没有得到这条线,默认不是 115200 ,他们从不这么说。另外,如果您只是将事物连接到 uno 并尝试连接,则不会发生任何事情
      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 2014-10-18
      相关资源
      最近更新 更多