![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
learning the AT command
Fork of Nucleo_sim808_sms by
main.cpp@0:e791ad8b9e26, 2017-06-11 (annotated)
- Committer:
- shawlinrise
- Date:
- Sun Jun 11 10:28:21 2017 +0000
- Revision:
- 0:e791ad8b9e26
SIM808_SMS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shawlinrise | 0:e791ad8b9e26 | 1 | #include "mbed.h" |
shawlinrise | 0:e791ad8b9e26 | 2 | #include "string.h" |
shawlinrise | 0:e791ad8b9e26 | 3 | |
shawlinrise | 0:e791ad8b9e26 | 4 | Serial pc(SERIAL_TX, SERIAL_RX); |
shawlinrise | 0:e791ad8b9e26 | 5 | Serial sim808(D1,D0); |
shawlinrise | 0:e791ad8b9e26 | 6 | DigitalOut myled(LED1); |
shawlinrise | 0:e791ad8b9e26 | 7 | |
shawlinrise | 0:e791ad8b9e26 | 8 | #define buf_max 200 |
shawlinrise | 0:e791ad8b9e26 | 9 | int First_Int = 0; |
shawlinrise | 0:e791ad8b9e26 | 10 | char sim_buf[buf_max]; |
shawlinrise | 0:e791ad8b9e26 | 11 | char *phone="\"17865578811\"";//¶ÌÐŽÓÊÕ·½ÊÖ»úºÅ |
shawlinrise | 0:e791ad8b9e26 | 12 | char *content="HELLO ~";//·¢ËͶÌÐÅÄÚÈÝ |
shawlinrise | 0:e791ad8b9e26 | 13 | char CtrlZ = 0x1A; |
shawlinrise | 0:e791ad8b9e26 | 14 | char temp[50]="AT+CMGS="; |
shawlinrise | 0:e791ad8b9e26 | 15 | |
shawlinrise | 0:e791ad8b9e26 | 16 | void SIM808_IRQHandler(void); |
shawlinrise | 0:e791ad8b9e26 | 17 | void CLR_Buf(void); |
shawlinrise | 0:e791ad8b9e26 | 18 | void Wait_CREG(void); |
shawlinrise | 0:e791ad8b9e26 | 19 | |
shawlinrise | 0:e791ad8b9e26 | 20 | //void getline(); |
shawlinrise | 0:e791ad8b9e26 | 21 | |
shawlinrise | 0:e791ad8b9e26 | 22 | void SIM808_IRQHandler(void) |
shawlinrise | 0:e791ad8b9e26 | 23 | { |
shawlinrise | 0:e791ad8b9e26 | 24 | sim_buf[First_Int] = sim808.getc(); |
shawlinrise | 0:e791ad8b9e26 | 25 | First_Int++; |
shawlinrise | 0:e791ad8b9e26 | 26 | } |
shawlinrise | 0:e791ad8b9e26 | 27 | |
shawlinrise | 0:e791ad8b9e26 | 28 | //void callback() |
shawlinrise | 0:e791ad8b9e26 | 29 | //{ |
shawlinrise | 0:e791ad8b9e26 | 30 | // getline(); |
shawlinrise | 0:e791ad8b9e26 | 31 | //} |
shawlinrise | 0:e791ad8b9e26 | 32 | |
shawlinrise | 0:e791ad8b9e26 | 33 | int main() |
shawlinrise | 0:e791ad8b9e26 | 34 | { |
shawlinrise | 0:e791ad8b9e26 | 35 | pc.baud(115200); |
shawlinrise | 0:e791ad8b9e26 | 36 | sim808.baud(115200); |
shawlinrise | 0:e791ad8b9e26 | 37 | |
shawlinrise | 0:e791ad8b9e26 | 38 | sim808.attach(&SIM808_IRQHandler,SerialBase::RxIrq); |
shawlinrise | 0:e791ad8b9e26 | 39 | |
shawlinrise | 0:e791ad8b9e26 | 40 | pc.printf("Send Message Test!"); |
shawlinrise | 0:e791ad8b9e26 | 41 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 42 | |
shawlinrise | 0:e791ad8b9e26 | 43 | sim808.printf("AT"); |
shawlinrise | 0:e791ad8b9e26 | 44 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 45 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 46 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 47 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 48 | |
shawlinrise | 0:e791ad8b9e26 | 49 | Wait_CREG(); |
shawlinrise | 0:e791ad8b9e26 | 50 | pc.printf("Register Succeed! "); |
shawlinrise | 0:e791ad8b9e26 | 51 | |
shawlinrise | 0:e791ad8b9e26 | 52 | sim808.printf("AT+CMGF=1"); |
shawlinrise | 0:e791ad8b9e26 | 53 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 54 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 55 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 56 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 57 | |
shawlinrise | 0:e791ad8b9e26 | 58 | sim808.printf("AT+CSCS=\"GSM\""); |
shawlinrise | 0:e791ad8b9e26 | 59 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 60 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 61 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 62 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 63 | |
shawlinrise | 0:e791ad8b9e26 | 64 | sim808.printf("AT+CSMP=17,167,240"); |
shawlinrise | 0:e791ad8b9e26 | 65 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 66 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 67 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 68 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 69 | |
shawlinrise | 0:e791ad8b9e26 | 70 | strcat(temp,(char*)phone); |
shawlinrise | 0:e791ad8b9e26 | 71 | sim808.printf("%s",temp); |
shawlinrise | 0:e791ad8b9e26 | 72 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 73 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 74 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 75 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 76 | |
shawlinrise | 0:e791ad8b9e26 | 77 | sim808.printf("%s",(char*)content); |
shawlinrise | 0:e791ad8b9e26 | 78 | sim808.printf("%c",CtrlZ); |
shawlinrise | 0:e791ad8b9e26 | 79 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 80 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 81 | |
shawlinrise | 0:e791ad8b9e26 | 82 | while(1); |
shawlinrise | 0:e791ad8b9e26 | 83 | } |
shawlinrise | 0:e791ad8b9e26 | 84 | |
shawlinrise | 0:e791ad8b9e26 | 85 | |
shawlinrise | 0:e791ad8b9e26 | 86 | //void getline() |
shawlinrise | 0:e791ad8b9e26 | 87 | //{ |
shawlinrise | 0:e791ad8b9e26 | 88 | // pc.putc(sim808.getc()); |
shawlinrise | 0:e791ad8b9e26 | 89 | //} |
shawlinrise | 0:e791ad8b9e26 | 90 | |
shawlinrise | 0:e791ad8b9e26 | 91 | |
shawlinrise | 0:e791ad8b9e26 | 92 | void CLR_Buf(void) |
shawlinrise | 0:e791ad8b9e26 | 93 | { |
shawlinrise | 0:e791ad8b9e26 | 94 | uint16_t k; |
shawlinrise | 0:e791ad8b9e26 | 95 | for(k=0;k<buf_max;k++) //½«»º´æÄÚÈÝÇåÁã |
shawlinrise | 0:e791ad8b9e26 | 96 | { |
shawlinrise | 0:e791ad8b9e26 | 97 | sim_buf[k] = 0x00; |
shawlinrise | 0:e791ad8b9e26 | 98 | } |
shawlinrise | 0:e791ad8b9e26 | 99 | First_Int = 0; //½ÓÊÕ×Ö·û´®µÄÆðʼ´æ´¢Î»Öà |
shawlinrise | 0:e791ad8b9e26 | 100 | } |
shawlinrise | 0:e791ad8b9e26 | 101 | |
shawlinrise | 0:e791ad8b9e26 | 102 | //u8 Find(char *a) |
shawlinrise | 0:e791ad8b9e26 | 103 | //{ |
shawlinrise | 0:e791ad8b9e26 | 104 | // if(strstr(sim_buf,a)!=NULL) |
shawlinrise | 0:e791ad8b9e26 | 105 | // return 1; |
shawlinrise | 0:e791ad8b9e26 | 106 | // else |
shawlinrise | 0:e791ad8b9e26 | 107 | // return 0; |
shawlinrise | 0:e791ad8b9e26 | 108 | //} |
shawlinrise | 0:e791ad8b9e26 | 109 | |
shawlinrise | 0:e791ad8b9e26 | 110 | void Wait_CREG(void) |
shawlinrise | 0:e791ad8b9e26 | 111 | { |
shawlinrise | 0:e791ad8b9e26 | 112 | int i; |
shawlinrise | 0:e791ad8b9e26 | 113 | int k; |
shawlinrise | 0:e791ad8b9e26 | 114 | i = 0; |
shawlinrise | 0:e791ad8b9e26 | 115 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 116 | while(i == 0) |
shawlinrise | 0:e791ad8b9e26 | 117 | { |
shawlinrise | 0:e791ad8b9e26 | 118 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 119 | sim808.printf("AT+CREG?"); |
shawlinrise | 0:e791ad8b9e26 | 120 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 121 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 122 | pc.printf("%s\r\n",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 123 | for(k=0;k<buf_max;k++) |
shawlinrise | 0:e791ad8b9e26 | 124 | { |
shawlinrise | 0:e791ad8b9e26 | 125 | if(sim_buf[k] == ':') |
shawlinrise | 0:e791ad8b9e26 | 126 | { |
shawlinrise | 0:e791ad8b9e26 | 127 | if((sim_buf[k+4] == '1')||(sim_buf[k+4] == '5')) |
shawlinrise | 0:e791ad8b9e26 | 128 | { |
shawlinrise | 0:e791ad8b9e26 | 129 | i = 1; |
shawlinrise | 0:e791ad8b9e26 | 130 | break; |
shawlinrise | 0:e791ad8b9e26 | 131 | } |
shawlinrise | 0:e791ad8b9e26 | 132 | } |
shawlinrise | 0:e791ad8b9e26 | 133 | } |
shawlinrise | 0:e791ad8b9e26 | 134 | pc.printf("CREGING.....\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 135 | } |
shawlinrise | 0:e791ad8b9e26 | 136 | } |
shawlinrise | 0:e791ad8b9e26 | 137 |