通过sim900短信收发控制stm32设备

  通过sim900短信收发控制stm32设备或者自动发送数据到远程服务器上面

1.连接远程服务器关键步骤:

//      "AT+CIPSTART="TCP","60.186.20.162","5002"    串口调试发送

    GPRS_SendString(AT_CIPHEAD); ////1----GPRS 接收方式

    GPRS_SendString("AT+CIPSTART=\"TCP\",\"60.186.20.162\",\"5002\"");
 
  Delay_10ms(400);//延时要充分

2.发短信命令

     GPRS_SendString("AT+CMGF=1");//set text format
      Delay_10ms(20);
      GPRS_SendString("AT+CSMP=17,167,0,241"); //set SMS format 
      Delay_10ms(20); 
      Clear_GPRS_ReBuffer();
      GPRS_SendString("AT+CMGL=\"ALL\"");// read SMS


main()

GPRS_ON();

Start_GPRS()//sim900 软件开关

while(1)

{

 Clear_GPRS_ReBuffer();

  GPRS_SendString("AT+CMGDA=\"DEL ALL\"");
  Clear_GPRS_ReBuffer();
 while(1)
 {
       while(1)
    { 
     Clear_GPRS_ReBuffer();
      GPRS_SendString("AT+CPIN?");//查询SIM卡状态
      Delay_10ms(100);
      if(strstr2(GPRS_ReBuffer,"OK")!=0) {   break;}
    }
 
      while(1)
      { 
       Clear_GPRS_ReBuffer();
        GPRS_SendString("AT+CSQ");//查询SIM卡状态
        Delay_10ms(100);
        if(strstr2(GPRS_ReBuffer,"OK")!=0) {   break;}
      }
      while(1)
      { 
       Clear_GPRS_ReBuffer();
        GPRS_SendString("AT+IPR=9600");
        Delay_10ms(100);
        if(strstr2(GPRS_ReBuffer,"OK")!=0){break; }
      }
 
      while(1)
      {  Clear_GPRS_ReBuffer();
        GPRS_SendString(AT_CIPHEAD); ////1----GPRS 接收方式
        Delay_10ms(100);
          if(strstr2(GPRS_ReBuffer,"OK")!=0){ break; }   
      }
      while(1)
      { 
        Clear_GPRS_ReBuffer(); 
        GPRS_SendString("AT+CIPSTART=\"TCP\",\"60.186.20.162\",\"5002\"");
        Delay_10ms(400);//
        if(strstr2(GPRS_ReBuffer,CONNECT_OK)!=0)
        {
           RFID_SendString("tsest  sww  !");
          break;
        }     
      }    
    while(1)
     {
       GPRS_SendString(AT_CIPSEND);
       Delay_10ms(100);
       GPRS_SendString("Welcome to hello world  !");
       GPRS_Send_Char(0x1A);           //结束符 
       Delay_10ms(200);
       Clear_GPRS_ReBuffer();
       
       //自动发送短信给目标手机911
      /*GPRS_SendString("AT+CMGS=\"110\"");//
      Delay_10ms(20);
      GPRS_SendString("911");
      GPRS_Send_Char(0x1A);

      Delay_10ms(200);    */ 


      //发送短信控制stm32或者控制它自动发送数据到服务器  
      GPRS_SendString("AT+CMGF=1");//set text format
      Delay_10ms(20);
      GPRS_SendString("AT+CSMP=17,167,0,241"); //set SMS format 
      Delay_10ms(20); 
      Clear_GPRS_ReBuffer();
      GPRS_SendString("AT+CMGL=\"ALL\"");// read SMS
      Delay_10ms(150); 
      if(strstr2(GPRS_ReBuffer,"+86119")!=0)
       {
           GPRS_SendString(AT_CIPSEND);
           Delay_10ms(100);
           GPRS_SendString("Welcome to SMS !");
           GPRS_Send_Char(0x1A);           //结束符 
           Delay_10ms(200);    
           GPRS_SendString("AT+CMGDA=\"DEL ALL\"");//删除短信ALL
       }
     Clear_GPRS_ReBuffer();
    
   }

 }

}

}

/******************************************************************************/
//函数void Start_GPRS()
//实现功能:启动或关断GPRS
///*拉低至少100ms,PA8*/
/******************************************************************************/
void Start_GPRS()//sim900 软件开关
{
 GPIO_ResetBits(GPIOA, GPIO_Pin_1);
  Delay_10ms(100);//1000ms
  GPIO_SetBits(GPIOA, GPIO_Pin_1);   

  Delay_10ms(200);//5000ms
  GPIO_ResetBits(GPIOA, GPIO_Pin_1);
  Delay_10ms(2500);//5000ms

}


void GPRS_ON() //sim900电源总开关
{  
 GPIO_SetBits(GPIOD, GPIO_Pin_4);
  GPIO_ResetBits(GPIOA, GPIO_Pin_1);//sim900_EN 硬开关
  Delay_10ms(200);
}


你可能感兴趣的:(通过sim900短信收发控制stm32设备)