![](/media/cache/profiles/cb9387e10ea6d3c731b177ffa135e92d.jpg.50x50_q85.jpg)
learning AT command
Fork of sim808_GPRS by
main.cpp
- Committer:
- shawlinrise
- Date:
- 2017-06-11
- Revision:
- 1:79272efb4415
- Parent:
- 0:e791ad8b9e26
File content as of revision 1:79272efb4415:
#include "mbed.h" #include "string.h" Serial pc(SERIAL_TX, SERIAL_RX); Serial sim808(D1,D0); DigitalOut myled(LED1); //Ticker beat; #define buf_max 200 const char *string = "AT+CIPSTART=\"TCP\",\"116.31.51.113\",9015"; char *content="HELLO ~"; int First_Int = 0; char sim_buf[buf_max]; void SIM808_IRQHandler(void); void CLR_Buf(void); void Wait_CREG(void); int Find(char *a); void send_beat(void); //void getline(); void SIM808_IRQHandler(void) { sim_buf[First_Int] = sim808.getc(); First_Int++; } int main() { sim808.attach(&SIM808_IRQHandler,SerialBase::RxIrq); pc.printf("GPRS Connect Test!"); wait(1); sim808.printf("AT"); sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); Wait_CREG(); pc.printf("Register Succeed!"); sim808.printf("AT+CIPCLOSE=1");//¹Ø±ÕÁ¬½Ó sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); sim808.printf("AT+CIPSHUT");//¹Ø±ÕÒƶ¯³¡¾° sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); sim808.printf("AT+CGCLASS=\"B\"");//ÉèÖÃGPRSÒƶ¯Ì¨Àà±ðΪB,Ö§³Ö°ü½»»»ºÍÊý¾Ý½»»» sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); sim808.printf("AT+CGDCONT=1,\"IP\",\"CMNET\"");//ÉèÖÃPDPÉÏÏÂÎÄ,»¥ÁªÍø½ÓÐÒé,½ÓÈëµãµÈÐÅÏ¢ sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); sim808.printf("AT+CGATT=1");//¸½×ÅGPRSÒµÎñ sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); sim808.printf("AT+CIPCSGP=1,\"CMNET\"");//ÉèÖÃΪGPRSÁ¬½Óģʽ sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); sim808.printf("AT+CIPHEAD=1");//ÉèÖýÓÊÕÊý¾ÝÏÔʾIPÍ·(·½±ãÅжÏÊý¾ÝÀ´Ô´,½öÔÚµ¥Â·Á¬½ÓÓÐЧ) sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); sim808.printf("%s",(char*)string); wait(1); pc.printf("%s",sim_buf); CLR_Buf(); sim808.printf("AT+CGDCONT=1"); sim808.printf("\r\n"); wait(1); pc.printf("%s",sim_buf); while(1) { if(Find("CONNECT OK")==1) { sim808.printf("%s",(char*)content); } else { pc.printf("Do Not Connected!"); } } // beat.attach(&send_beat,10); // while(1); } //void send_beat(void) //{ // sim808.printf("AT+CIPSEND"); // sim808.printf("\r\n"); // pc.printf("%s",sim_buf); // if(Find("<")==1) // { // sim808.printf("OK"); // CLR_Buf(); // } // //} //void getline() //{ // pc.putc(sim808.getc()); //} void CLR_Buf(void) { uint16_t k; for(k=0;k<buf_max;k++) //½«»º´æÄÚÈÝÇåÁã { sim_buf[k] = 0x00; } First_Int = 0; //½ÓÊÕ×Ö·û´®µÄÆðʼ´æ´¢Î»Öà } int Find(char *a) { if(strstr(sim_buf,a)!=NULL) return 1; else return 0; } void Wait_CREG(void) { int i; int k; i = 0; CLR_Buf(); while(i == 0) { CLR_Buf(); sim808.printf("AT+CREG?"); sim808.printf("\r\n"); wait(1); pc.printf("%s\r\n",sim_buf); for(k=0;k<buf_max;k++) { if(sim_buf[k] == ':') { if((sim_buf[k+4] == '1')||(sim_buf[k+4] == '5')) { i = 1; break; } } } pc.printf("CREGING.....\r\n"); } }