sheng xiaolin
/
Nucleo_GPRS_sim808
learning AT command
Fork of Nucleo_sim808_GPRS by
main.cpp@1:79272efb4415, 2017-06-11 (annotated)
- Committer:
- shawlinrise
- Date:
- Sun Jun 11 10:48:44 2017 +0000
- Revision:
- 1:79272efb4415
- Parent:
- 0:e791ad8b9e26
SIM808_GPRS
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 | 1:79272efb4415 | 7 | //Ticker beat; |
shawlinrise | 0:e791ad8b9e26 | 8 | |
shawlinrise | 0:e791ad8b9e26 | 9 | #define buf_max 200 |
shawlinrise | 1:79272efb4415 | 10 | const char *string = "AT+CIPSTART=\"TCP\",\"116.31.51.113\",9015"; |
shawlinrise | 1:79272efb4415 | 11 | char *content="HELLO ~"; |
shawlinrise | 0:e791ad8b9e26 | 12 | int First_Int = 0; |
shawlinrise | 0:e791ad8b9e26 | 13 | char sim_buf[buf_max]; |
shawlinrise | 0:e791ad8b9e26 | 14 | |
shawlinrise | 0:e791ad8b9e26 | 15 | void SIM808_IRQHandler(void); |
shawlinrise | 0:e791ad8b9e26 | 16 | void CLR_Buf(void); |
shawlinrise | 0:e791ad8b9e26 | 17 | void Wait_CREG(void); |
shawlinrise | 1:79272efb4415 | 18 | int Find(char *a); |
shawlinrise | 1:79272efb4415 | 19 | void send_beat(void); |
shawlinrise | 0:e791ad8b9e26 | 20 | |
shawlinrise | 0:e791ad8b9e26 | 21 | //void getline(); |
shawlinrise | 0:e791ad8b9e26 | 22 | |
shawlinrise | 0:e791ad8b9e26 | 23 | void SIM808_IRQHandler(void) |
shawlinrise | 0:e791ad8b9e26 | 24 | { |
shawlinrise | 0:e791ad8b9e26 | 25 | sim_buf[First_Int] = sim808.getc(); |
shawlinrise | 0:e791ad8b9e26 | 26 | First_Int++; |
shawlinrise | 0:e791ad8b9e26 | 27 | } |
shawlinrise | 0:e791ad8b9e26 | 28 | |
shawlinrise | 0:e791ad8b9e26 | 29 | int main() |
shawlinrise | 0:e791ad8b9e26 | 30 | { |
shawlinrise | 0:e791ad8b9e26 | 31 | sim808.attach(&SIM808_IRQHandler,SerialBase::RxIrq); |
shawlinrise | 0:e791ad8b9e26 | 32 | |
shawlinrise | 1:79272efb4415 | 33 | pc.printf("GPRS Connect Test!"); |
shawlinrise | 0:e791ad8b9e26 | 34 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 35 | |
shawlinrise | 0:e791ad8b9e26 | 36 | sim808.printf("AT"); |
shawlinrise | 0:e791ad8b9e26 | 37 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 38 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 39 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 40 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 41 | |
shawlinrise | 0:e791ad8b9e26 | 42 | Wait_CREG(); |
shawlinrise | 1:79272efb4415 | 43 | pc.printf("Register Succeed!"); |
shawlinrise | 1:79272efb4415 | 44 | |
shawlinrise | 1:79272efb4415 | 45 | sim808.printf("AT+CIPCLOSE=1");//¹Ø±ÕÁ¬½Ó |
shawlinrise | 1:79272efb4415 | 46 | sim808.printf("\r\n"); |
shawlinrise | 1:79272efb4415 | 47 | wait(1); |
shawlinrise | 1:79272efb4415 | 48 | pc.printf("%s",sim_buf); |
shawlinrise | 1:79272efb4415 | 49 | CLR_Buf(); |
shawlinrise | 1:79272efb4415 | 50 | |
shawlinrise | 1:79272efb4415 | 51 | sim808.printf("AT+CIPSHUT");//¹Ø±ÕÒƶ¯³¡¾° |
shawlinrise | 0:e791ad8b9e26 | 52 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 53 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 54 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 55 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 56 | |
shawlinrise | 1:79272efb4415 | 57 | sim808.printf("AT+CGCLASS=\"B\"");//ÉèÖÃGPRSÒƶ¯Ì¨Àà±ðΪB,Ö§³Ö°ü½»»»ºÍÊý¾Ý½»»» |
shawlinrise | 0:e791ad8b9e26 | 58 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 59 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 60 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 61 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 62 | |
shawlinrise | 1:79272efb4415 | 63 | sim808.printf("AT+CGDCONT=1,\"IP\",\"CMNET\"");//ÉèÖÃPDPÉÏÏÂÎÄ,»¥ÁªÍø½ÓÐÒé,½ÓÈëµãµÈÐÅÏ¢ |
shawlinrise | 1:79272efb4415 | 64 | sim808.printf("\r\n"); |
shawlinrise | 1:79272efb4415 | 65 | wait(1); |
shawlinrise | 1:79272efb4415 | 66 | pc.printf("%s",sim_buf); |
shawlinrise | 1:79272efb4415 | 67 | CLR_Buf(); |
shawlinrise | 1:79272efb4415 | 68 | |
shawlinrise | 1:79272efb4415 | 69 | sim808.printf("AT+CGATT=1");//¸½×ÅGPRSÒµÎñ |
shawlinrise | 0:e791ad8b9e26 | 70 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 71 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 72 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 73 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 74 | |
shawlinrise | 1:79272efb4415 | 75 | sim808.printf("AT+CIPCSGP=1,\"CMNET\"");//ÉèÖÃΪGPRSÁ¬½Óģʽ |
shawlinrise | 1:79272efb4415 | 76 | sim808.printf("\r\n"); |
shawlinrise | 1:79272efb4415 | 77 | wait(1); |
shawlinrise | 1:79272efb4415 | 78 | pc.printf("%s",sim_buf); |
shawlinrise | 1:79272efb4415 | 79 | CLR_Buf(); |
shawlinrise | 1:79272efb4415 | 80 | |
shawlinrise | 1:79272efb4415 | 81 | sim808.printf("AT+CIPHEAD=1");//ÉèÖýÓÊÕÊý¾ÝÏÔʾIPÍ·(·½±ãÅжÏÊý¾ÝÀ´Ô´,½öÔÚµ¥Â·Á¬½ÓÓÐЧ) |
shawlinrise | 0:e791ad8b9e26 | 82 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 83 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 84 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 85 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 86 | |
shawlinrise | 1:79272efb4415 | 87 | sim808.printf("%s",(char*)string); |
shawlinrise | 1:79272efb4415 | 88 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 89 | pc.printf("%s",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 90 | CLR_Buf(); |
shawlinrise | 1:79272efb4415 | 91 | |
shawlinrise | 1:79272efb4415 | 92 | sim808.printf("AT+CGDCONT=1"); |
shawlinrise | 1:79272efb4415 | 93 | sim808.printf("\r\n"); |
shawlinrise | 1:79272efb4415 | 94 | wait(1); |
shawlinrise | 1:79272efb4415 | 95 | pc.printf("%s",sim_buf); |
shawlinrise | 1:79272efb4415 | 96 | |
shawlinrise | 1:79272efb4415 | 97 | while(1) |
shawlinrise | 1:79272efb4415 | 98 | { |
shawlinrise | 1:79272efb4415 | 99 | if(Find("CONNECT OK")==1) |
shawlinrise | 1:79272efb4415 | 100 | { |
shawlinrise | 1:79272efb4415 | 101 | sim808.printf("%s",(char*)content); |
shawlinrise | 1:79272efb4415 | 102 | } |
shawlinrise | 1:79272efb4415 | 103 | else |
shawlinrise | 1:79272efb4415 | 104 | { |
shawlinrise | 1:79272efb4415 | 105 | pc.printf("Do Not Connected!"); |
shawlinrise | 1:79272efb4415 | 106 | } |
shawlinrise | 1:79272efb4415 | 107 | } |
shawlinrise | 1:79272efb4415 | 108 | |
shawlinrise | 1:79272efb4415 | 109 | // beat.attach(&send_beat,10); |
shawlinrise | 1:79272efb4415 | 110 | // while(1); |
shawlinrise | 0:e791ad8b9e26 | 111 | } |
shawlinrise | 0:e791ad8b9e26 | 112 | |
shawlinrise | 1:79272efb4415 | 113 | //void send_beat(void) |
shawlinrise | 1:79272efb4415 | 114 | //{ |
shawlinrise | 1:79272efb4415 | 115 | // sim808.printf("AT+CIPSEND"); |
shawlinrise | 1:79272efb4415 | 116 | // sim808.printf("\r\n"); |
shawlinrise | 1:79272efb4415 | 117 | // pc.printf("%s",sim_buf); |
shawlinrise | 1:79272efb4415 | 118 | // if(Find("<")==1) |
shawlinrise | 1:79272efb4415 | 119 | // { |
shawlinrise | 1:79272efb4415 | 120 | // sim808.printf("OK"); |
shawlinrise | 1:79272efb4415 | 121 | // CLR_Buf(); |
shawlinrise | 1:79272efb4415 | 122 | // } |
shawlinrise | 1:79272efb4415 | 123 | // |
shawlinrise | 1:79272efb4415 | 124 | //} |
shawlinrise | 0:e791ad8b9e26 | 125 | |
shawlinrise | 0:e791ad8b9e26 | 126 | //void getline() |
shawlinrise | 0:e791ad8b9e26 | 127 | //{ |
shawlinrise | 0:e791ad8b9e26 | 128 | // pc.putc(sim808.getc()); |
shawlinrise | 0:e791ad8b9e26 | 129 | //} |
shawlinrise | 0:e791ad8b9e26 | 130 | |
shawlinrise | 0:e791ad8b9e26 | 131 | void CLR_Buf(void) |
shawlinrise | 0:e791ad8b9e26 | 132 | { |
shawlinrise | 0:e791ad8b9e26 | 133 | uint16_t k; |
shawlinrise | 0:e791ad8b9e26 | 134 | for(k=0;k<buf_max;k++) //½«»º´æÄÚÈÝÇåÁã |
shawlinrise | 0:e791ad8b9e26 | 135 | { |
shawlinrise | 0:e791ad8b9e26 | 136 | sim_buf[k] = 0x00; |
shawlinrise | 0:e791ad8b9e26 | 137 | } |
shawlinrise | 0:e791ad8b9e26 | 138 | First_Int = 0; //½ÓÊÕ×Ö·û´®µÄÆðʼ´æ´¢Î»Öà |
shawlinrise | 0:e791ad8b9e26 | 139 | } |
shawlinrise | 0:e791ad8b9e26 | 140 | |
shawlinrise | 1:79272efb4415 | 141 | int Find(char *a) |
shawlinrise | 1:79272efb4415 | 142 | { |
shawlinrise | 1:79272efb4415 | 143 | if(strstr(sim_buf,a)!=NULL) |
shawlinrise | 1:79272efb4415 | 144 | return 1; |
shawlinrise | 1:79272efb4415 | 145 | else |
shawlinrise | 1:79272efb4415 | 146 | return 0; |
shawlinrise | 1:79272efb4415 | 147 | } |
shawlinrise | 0:e791ad8b9e26 | 148 | |
shawlinrise | 0:e791ad8b9e26 | 149 | void Wait_CREG(void) |
shawlinrise | 0:e791ad8b9e26 | 150 | { |
shawlinrise | 0:e791ad8b9e26 | 151 | int i; |
shawlinrise | 0:e791ad8b9e26 | 152 | int k; |
shawlinrise | 0:e791ad8b9e26 | 153 | i = 0; |
shawlinrise | 0:e791ad8b9e26 | 154 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 155 | while(i == 0) |
shawlinrise | 0:e791ad8b9e26 | 156 | { |
shawlinrise | 0:e791ad8b9e26 | 157 | CLR_Buf(); |
shawlinrise | 0:e791ad8b9e26 | 158 | sim808.printf("AT+CREG?"); |
shawlinrise | 0:e791ad8b9e26 | 159 | sim808.printf("\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 160 | wait(1); |
shawlinrise | 0:e791ad8b9e26 | 161 | pc.printf("%s\r\n",sim_buf); |
shawlinrise | 0:e791ad8b9e26 | 162 | for(k=0;k<buf_max;k++) |
shawlinrise | 0:e791ad8b9e26 | 163 | { |
shawlinrise | 0:e791ad8b9e26 | 164 | if(sim_buf[k] == ':') |
shawlinrise | 0:e791ad8b9e26 | 165 | { |
shawlinrise | 0:e791ad8b9e26 | 166 | if((sim_buf[k+4] == '1')||(sim_buf[k+4] == '5')) |
shawlinrise | 0:e791ad8b9e26 | 167 | { |
shawlinrise | 0:e791ad8b9e26 | 168 | i = 1; |
shawlinrise | 0:e791ad8b9e26 | 169 | break; |
shawlinrise | 0:e791ad8b9e26 | 170 | } |
shawlinrise | 0:e791ad8b9e26 | 171 | } |
shawlinrise | 0:e791ad8b9e26 | 172 | } |
shawlinrise | 0:e791ad8b9e26 | 173 | pc.printf("CREGING.....\r\n"); |
shawlinrise | 0:e791ad8b9e26 | 174 | } |
shawlinrise | 1:79272efb4415 | 175 | } |