![](/media/cache/profiles/cb9387e10ea6d3c731b177ffa135e92d.jpg.50x50_q85.jpg)
BT AT Command Learing
Fork of Nucleo_sim808_BT by
main.cpp@1:9e30224e0773, 2017-07-03 (annotated)
- Committer:
- shawlinrise
- Date:
- Mon Jul 03 00:26:26 2017 +0000
- Revision:
- 1:9e30224e0773
- Parent:
- 0:a3b9a955988e
BT Connect
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shawlinrise | 0:a3b9a955988e | 1 | #include "mbed.h" |
shawlinrise | 0:a3b9a955988e | 2 | #include "string.h" |
shawlinrise | 0:a3b9a955988e | 3 | |
shawlinrise | 0:a3b9a955988e | 4 | Serial pc(SERIAL_TX, SERIAL_RX); |
shawlinrise | 0:a3b9a955988e | 5 | Serial sim808(D1,D0); |
shawlinrise | 0:a3b9a955988e | 6 | Timer send_t; |
shawlinrise | 0:a3b9a955988e | 7 | |
shawlinrise | 0:a3b9a955988e | 8 | #define buf_max 200 |
shawlinrise | 0:a3b9a955988e | 9 | volatile int First_Int = 0; |
shawlinrise | 0:a3b9a955988e | 10 | volatile int Rcv_End = 0; |
shawlinrise | 0:a3b9a955988e | 11 | char sim_buf[buf_max]; |
shawlinrise | 0:a3b9a955988e | 12 | const char *senddata="Bluetooth test\r\n"; |
shawlinrise | 1:9e30224e0773 | 13 | char CtrlZ = 0x1A; |
shawlinrise | 0:a3b9a955988e | 14 | |
shawlinrise | 0:a3b9a955988e | 15 | void CLR_Buf(void); |
shawlinrise | 0:a3b9a955988e | 16 | int Find(char *a); |
shawlinrise | 1:9e30224e0773 | 17 | void Wait_Connect(void); |
shawlinrise | 0:a3b9a955988e | 18 | |
shawlinrise | 0:a3b9a955988e | 19 | void SIM808_IRQHandler(void) |
shawlinrise | 0:a3b9a955988e | 20 | { |
shawlinrise | 0:a3b9a955988e | 21 | sim_buf[First_Int++] = sim808.getc(); |
shawlinrise | 0:a3b9a955988e | 22 | if(sim_buf[First_Int] == 0X0A) |
shawlinrise | 0:a3b9a955988e | 23 | { |
shawlinrise | 0:a3b9a955988e | 24 | Rcv_End = 1; |
shawlinrise | 0:a3b9a955988e | 25 | } |
shawlinrise | 0:a3b9a955988e | 26 | } |
shawlinrise | 0:a3b9a955988e | 27 | |
shawlinrise | 0:a3b9a955988e | 28 | int main() |
shawlinrise | 0:a3b9a955988e | 29 | { |
shawlinrise | 0:a3b9a955988e | 30 | sim808.attach(&SIM808_IRQHandler); |
shawlinrise | 0:a3b9a955988e | 31 | pc.printf("SIM808 BlueTooth Test!\r\n"); |
shawlinrise | 0:a3b9a955988e | 32 | pc.printf("Çë´ò¿ªÊÖ»úÀ¶ÑÀ´®¿ÚÖúÊÖ!\r\n"); |
shawlinrise | 0:a3b9a955988e | 33 | |
shawlinrise | 0:a3b9a955988e | 34 | sim808.printf("AT+BTPOWER=1\r\n");//¿ªÆôÀ¶ÑÀµçÔ´ |
shawlinrise | 0:a3b9a955988e | 35 | wait(0.1); |
shawlinrise | 0:a3b9a955988e | 36 | sim808.printf("AT+BTUNPAIR=0\r\n");//ɾ³ýÅä¶ÔµÄÀ¶ÑÀÉèÖà |
shawlinrise | 0:a3b9a955988e | 37 | pc.printf("SIM808Ä£¿é¿ªÊ¼ÊÕË÷À¶ÑÀÉ豸,ÇëÈ·ÈÏÊÖ»úÀ¶ÑÀ´¦Óڿɱ»·¢ÏÖ\r\n"); |
shawlinrise | 0:a3b9a955988e | 38 | |
shawlinrise | 0:a3b9a955988e | 39 | CLR_Buf(); |
shawlinrise | 0:a3b9a955988e | 40 | Rcv_End = 0; |
shawlinrise | 0:a3b9a955988e | 41 | |
shawlinrise | 0:a3b9a955988e | 42 | do |
shawlinrise | 0:a3b9a955988e | 43 | { |
shawlinrise | 0:a3b9a955988e | 44 | pc.printf("ÊÕË÷É豸ÖÐ............\r\n"); |
shawlinrise | 1:9e30224e0773 | 45 | sim808.printf("AT+BTSCAN=1,10\r\n"); //ÊÕË÷¸½½üµÄÀ¶ÑÀÉ豸£¬ÊÕË÷ʱ¼ä10S |
shawlinrise | 1:9e30224e0773 | 46 | wait(10); |
shawlinrise | 1:9e30224e0773 | 47 | }while(Rcv_End == 1 && strstr((const char*)sim_buf,"+BTSCAN: 0")==NULL); //µÈ´ýÊÕË÷µ½É豸²ÅÍ˳ö |
shawlinrise | 0:a3b9a955988e | 48 | |
shawlinrise | 0:a3b9a955988e | 49 | CLR_Buf(); |
shawlinrise | 0:a3b9a955988e | 50 | Rcv_End = 0; |
shawlinrise | 0:a3b9a955988e | 51 | |
shawlinrise | 0:a3b9a955988e | 52 | do |
shawlinrise | 0:a3b9a955988e | 53 | { |
shawlinrise | 1:9e30224e0773 | 54 | pc.printf("Åä¶ÔÖÐ............\r\n"); |
shawlinrise | 1:9e30224e0773 | 55 | sim808.printf("AT+BTPAIR=0,1\r\n"); |
shawlinrise | 0:a3b9a955988e | 56 | wait(0.2); |
shawlinrise | 1:9e30224e0773 | 57 | sim808.printf("AT+BTPAIR=1,1\r\n"); |
shawlinrise | 1:9e30224e0773 | 58 | wait(0.1); |
shawlinrise | 1:9e30224e0773 | 59 | }while(Rcv_End == 1 && strstr((const char*)sim_buf,"+BTPAIR: 1")==NULL);//Åä¶Ô³É¹¦ |
shawlinrise | 0:a3b9a955988e | 60 | |
shawlinrise | 0:a3b9a955988e | 61 | CLR_Buf(); |
shawlinrise | 0:a3b9a955988e | 62 | Rcv_End = 0; |
shawlinrise | 0:a3b9a955988e | 63 | |
shawlinrise | 1:9e30224e0773 | 64 | pc.printf("SIM808Ä£¿éÀ¶ÑÀÅä¶Ô³É¹¦!\r\n"); |
shawlinrise | 1:9e30224e0773 | 65 | wait(1); |
shawlinrise | 1:9e30224e0773 | 66 | pc.printf("Çë´ò¿ªÀ¶ÑÀÖúÊÖÁ¬½ÓSIM808!\r\n"); |
shawlinrise | 1:9e30224e0773 | 67 | wait(1); |
shawlinrise | 0:a3b9a955988e | 68 | |
shawlinrise | 1:9e30224e0773 | 69 | Wait_Connect(); |
shawlinrise | 1:9e30224e0773 | 70 | sim808.printf("AT+ACPT=1\r\n"); |
shawlinrise | 1:9e30224e0773 | 71 | wait(1); |
shawlinrise | 1:9e30224e0773 | 72 | if(Find("OK")|Rcv_End == 1) |
shawlinrise | 0:a3b9a955988e | 73 | { |
shawlinrise | 1:9e30224e0773 | 74 | pc.printf("É豸Á¬½Ó³É¹¦!\r\n"); |
shawlinrise | 1:9e30224e0773 | 75 | |
shawlinrise | 1:9e30224e0773 | 76 | CLR_Buf(); |
shawlinrise | 1:9e30224e0773 | 77 | Rcv_End = 0; |
shawlinrise | 1:9e30224e0773 | 78 | |
shawlinrise | 1:9e30224e0773 | 79 | send_t.start(); |
shawlinrise | 1:9e30224e0773 | 80 | while(1); |
shawlinrise | 0:a3b9a955988e | 81 | { |
shawlinrise | 1:9e30224e0773 | 82 | if(send_t.read()>5) |
shawlinrise | 1:9e30224e0773 | 83 | { |
shawlinrise | 1:9e30224e0773 | 84 | sim808.printf("AT+BTSPPSEND\r\n"); |
shawlinrise | 1:9e30224e0773 | 85 | wait(0.1); |
shawlinrise | 1:9e30224e0773 | 86 | sim808.printf((char *)senddata); |
shawlinrise | 1:9e30224e0773 | 87 | sim808.printf("%c",CtrlZ); |
shawlinrise | 1:9e30224e0773 | 88 | send_t.start(); |
shawlinrise | 1:9e30224e0773 | 89 | CLR_Buf(); |
shawlinrise | 1:9e30224e0773 | 90 | } |
shawlinrise | 0:a3b9a955988e | 91 | } |
shawlinrise | 1:9e30224e0773 | 92 | } |
shawlinrise | 1:9e30224e0773 | 93 | else |
shawlinrise | 1:9e30224e0773 | 94 | { |
shawlinrise | 1:9e30224e0773 | 95 | pc.printf("É豸Á¬½Óʧ°Ü!\r\n"); |
shawlinrise | 0:a3b9a955988e | 96 | } |
shawlinrise | 0:a3b9a955988e | 97 | } |
shawlinrise | 0:a3b9a955988e | 98 | |
shawlinrise | 0:a3b9a955988e | 99 | |
shawlinrise | 0:a3b9a955988e | 100 | |
shawlinrise | 0:a3b9a955988e | 101 | |
shawlinrise | 0:a3b9a955988e | 102 | void CLR_Buf(void) |
shawlinrise | 0:a3b9a955988e | 103 | { |
shawlinrise | 0:a3b9a955988e | 104 | uint16_t k; |
shawlinrise | 0:a3b9a955988e | 105 | for(k=0;k<buf_max;k++) //½«»º´æÄÚÈÝÇåÁã |
shawlinrise | 0:a3b9a955988e | 106 | { |
shawlinrise | 0:a3b9a955988e | 107 | sim_buf[k] = 0x00; |
shawlinrise | 0:a3b9a955988e | 108 | } |
shawlinrise | 0:a3b9a955988e | 109 | First_Int = 0; //½ÓÊÕ×Ö·û´®µÄÆðʼ´æ´¢Î»Öà |
shawlinrise | 0:a3b9a955988e | 110 | } |
shawlinrise | 0:a3b9a955988e | 111 | |
shawlinrise | 0:a3b9a955988e | 112 | int Find(char *a) |
shawlinrise | 0:a3b9a955988e | 113 | { |
shawlinrise | 0:a3b9a955988e | 114 | if(strstr(sim_buf,a)!=NULL) |
shawlinrise | 0:a3b9a955988e | 115 | return 1; |
shawlinrise | 0:a3b9a955988e | 116 | else |
shawlinrise | 0:a3b9a955988e | 117 | return 0; |
shawlinrise | 0:a3b9a955988e | 118 | } |
shawlinrise | 0:a3b9a955988e | 119 | |
shawlinrise | 1:9e30224e0773 | 120 | void Wait_Connect(void) |
shawlinrise | 1:9e30224e0773 | 121 | { |
shawlinrise | 1:9e30224e0773 | 122 | int i; |
shawlinrise | 1:9e30224e0773 | 123 | i = 0; |
shawlinrise | 1:9e30224e0773 | 124 | CLR_Buf(); |
shawlinrise | 1:9e30224e0773 | 125 | while(i == 0) |
shawlinrise | 1:9e30224e0773 | 126 | { |
shawlinrise | 1:9e30224e0773 | 127 | CLR_Buf(); |
shawlinrise | 1:9e30224e0773 | 128 | wait(1); |
shawlinrise | 1:9e30224e0773 | 129 | if(Find("+BTCONNECTING:")) |
shawlinrise | 1:9e30224e0773 | 130 | { |
shawlinrise | 1:9e30224e0773 | 131 | i = 1; |
shawlinrise | 1:9e30224e0773 | 132 | break; |
shawlinrise | 1:9e30224e0773 | 133 | } |
shawlinrise | 1:9e30224e0773 | 134 | |
shawlinrise | 1:9e30224e0773 | 135 | pc.printf("ÇëÔÚÀ¶ÑÀÖúÊÖÖÐÁ¬½ÓÉ豸!\r\n"); |
shawlinrise | 1:9e30224e0773 | 136 | } |
shawlinrise | 1:9e30224e0773 | 137 | } |