learning AT command

Dependencies:   mbed

Fork of sim808_GPRS by sheng xiaolin

Files at this revision

API Documentation at this revision

Comitter:
shawlinrise
Date:
Sun Jun 11 10:48:44 2017 +0000
Parent:
0:e791ad8b9e26
Commit message:
SIM808_GPRS

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Jun 11 10:28:21 2017 +0000
+++ b/main.cpp	Sun Jun 11 10:48:44 2017 +0000
@@ -4,18 +4,19 @@
 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];
-char *phone="\"17865578811\"";//¶ÌÐŽÓÊÕ·½ÊÖ»úºÅ
-char *content="HELLO ~";//·¢ËͶÌÐÅÄÚÈÝ
-char CtrlZ = 0x1A;
-char temp[50]="AT+CMGS=";
 
 void SIM808_IRQHandler(void);
 void CLR_Buf(void);
 void Wait_CREG(void);
+int Find(char *a);
+void send_beat(void);
 
 //void getline();
 
@@ -25,19 +26,11 @@
             First_Int++; 
 } 
 
-//void callback()
-//{       
-//  getline();
-//}
-
 int main()
 {
-    pc.baud(115200);
-    sim808.baud(115200);
-    
     sim808.attach(&SIM808_IRQHandler,SerialBase::RxIrq);
     
-    pc.printf("Send Message Test!");
+    pc.printf("GPRS Connect Test!");
     wait(1);
     
     sim808.printf("AT");
@@ -47,48 +40,94 @@
     CLR_Buf(); 
 
     Wait_CREG();
-    pc.printf("Register Succeed! ");
-    
-    sim808.printf("AT+CMGF=1");
+    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+CSCS=\"GSM\"");
+    sim808.printf("AT+CGCLASS=\"B\"");//ÉèÖÃGPRSÒƶ¯Ì¨Àà±ðΪB,Ö§³Ö°ü½»»»ºÍÊý¾Ý½»»» 
     sim808.printf("\r\n");
     wait(1);
     pc.printf("%s",sim_buf);
     CLR_Buf();
 
-    sim808.printf("AT+CSMP=17,167,240");
+    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();
 
-    strcat(temp,(char*)phone);
-    sim808.printf("%s",temp);
+    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*)content);
-    sim808.printf("%c",CtrlZ);
+  sim808.printf("%s",(char*)string);
+    wait(1);
     pc.printf("%s",sim_buf);
     CLR_Buf();
-        
-    while(1);
+    
+    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;
@@ -99,13 +138,13 @@
     First_Int = 0;              //½ÓÊÕ×Ö·û´®µÄÆðʼ´æ´¢Î»ÖÃ
 }
 
-//u8 Find(char *a)
-//{ 
-//  if(strstr(sim_buf,a)!=NULL)
-//      return 1;
-//  else
-//          return 0;
-//}
+int Find(char *a)
+{ 
+  if(strstr(sim_buf,a)!=NULL)
+        return 1;
+    else
+            return 0;
+}
 
 void Wait_CREG(void)
 {
@@ -133,5 +172,4 @@
         }
         pc.printf("CREGING.....\r\n");
     }
-}
-
+}
\ No newline at end of file