Chinaunix首页 | 论坛 | 博客
  • 博客访问: 311625
  • 博文数量: 27
  • 博客积分: 758
  • 博客等级: 军士长
  • 技术积分: 369
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 23:10
文章分类

全部博文(27)

文章存档

2014年(1)

2012年(26)

我的朋友

分类: 嵌入式

2012-06-11 12:37:28


  1. /*

  2.   A simple code snippet for setup AT commands
  3.   for a Bluetooth HC-06 slave module.
  4.   
  5.   Procedure:
  6.   
  7.   1. Hardware preparation:
  8.   Connect the right pins on the arduino sensor shield:
  9.      DOUT(bluethooth) --> D2(MCU)
  10.      DIN(bluetooth) --> D3 (MCU)
  11.   
  12.   2. Software preparation:
  13.   Arduino 1.0.1 is needed to compile the code (For older versions,
  14.   you may have to modify the 'SoftwareSerial' to 'NewSoftSerial').
  15.   Make sure you have the module's default baud rate or the rate you set
  16.   last time and fill it to blueToothSerial.begin(YOUR_BAUD_RATE_HERE)
  17.   in function setupBlueToothConnection().
  18.   
  19.   Upload this snippet into the powered up module, it will be done.

  20. */

  21. #include <SoftwareSerial.h> //Software Serial Port library for arduino 1.0.1
  22. #define RxD 2
  23. #define TxD 3

  24. SoftwareSerial blueToothSerial(RxD,TxD);
  25.  
  26. void setup()
  27. {
  28.     Serial.begin(9600);
  29.     pinMode(RxD, INPUT);
  30.     pinMode(TxD, OUTPUT);
  31.     setupBlueToothConnection();
  32.  
  33. }
  34.  
  35. void loop()
  36. {
  37.     // We dont have to do anything but delay here
  38.     delay(50);
  39. }
  40.  
  41. void setupBlueToothConnection()
  42. {
  43.     Serial.println("Setting up Bluetooth slave module");
  44.     
  45.     // The baud rate of AT serial interface should be the same value
  46.     // since your last time configuration, otherwise the fllowing
  47.     // sendBlueToothCommand's won't work. For those new HC-06 modules,
  48.     // the default baud rate may be 9600. If you dont know what the
  49.     // default baud rate is, you can just emumate from 1200~115200 :)
  50.     blueToothSerial.begin(38400);
  51.     
  52.     delay(1000);
  53.     
  54.     // Set communication baud to 38400
  55.     // 1:1200, 2:2400, 3:4800, 4:9600, 5:19200
  56.     // 6:38400, 7:57600, 8:115200
  57.     sendBlueToothCommand("AT+BAUD8");
  58.     
  59.     // Set the bluetooth name
  60.     sendBlueToothCommand("AT+NAMEservo_comm2");
  61.     
  62.     // Set the pair password
  63.     sendBlueToothCommand("AT+PIN1212");
  64.     
  65.     Serial.println("Setup complete!");
  66.  
  67. }
  68.  
  69. void sendBlueToothCommand(char command[])
  70. {
  71.  
  72.     blueToothSerial.print(command);
  73.     Serial.print(command);
  74.     delay(1000);
  75.                        
  76. }

阅读(6588) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~