51单片机——wifi模块
1.AT指令
# include "reg52.h"
# include "intrins.h"
# include <string.h> # define SIZE 12
sfr AUXR = 0x8E ;
sbit D5 = P3^ 7 ;
char cmd[ SIZE] ; code char LJWL[ ] = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n" ;
code char LJFWQ[ ] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n" ; char TCMS[ ] = "AT+CIPMODE=1\r\n" ;
char SJCS[ ] = "AT+CIPSEND\r\n" ; void UartInit ( void )
{ AUXR = 0x01 ; SCON = 0x50 ; TMOD &= 0x0F ; TMOD |= 0x20 ; TH1 = 0xFD ; TL1 = 0xFD ; TR1 = 1 ; EA = 1 ; ES = 1 ;
} void Delay1000ms ( )
{ unsigned char i, j, k; _nop_ ( ) ; i = 8 ; j = 1 ; k = 243 ; do { do { while ( -- k) ; } while ( -- j) ; } while ( -- i) ;
} void sendByte ( char data_msg)
{ SBUF = data_msg; while ( ! TI) ; TI = 0 ;
} void sendString ( char * str)
{ while ( * str != '\0' ) { sendByte ( * str) ; str++ ; }
} void main ( )
{ D5 = 1 ; UartInit ( ) ; while ( 1 ) { sendString ( LJWL) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; sendString ( LJFWQ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; sendString ( TCMS) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; sendString ( SJCS) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; }
} void Uart_Handler ( ) interrupt 4
{ static int i = 0 ; if ( RI) { RI = 0 ; cmd[ i] = SBUF; i++ ; if ( i == SIZE) { i = 0 ; } if ( strstr ( cmd, "en" ) ) { D5 = 0 ; i = 0 ; memset ( cmd, '\0' , SIZE) ; } if ( strstr ( cmd, "se" ) ) { D5 = 1 ; i = 0 ; memset ( cmd, '\0' , SIZE) ; } } if ( TI) ;
}
2.TCP通信点灯
# include "reg52.h"
# include "intrins.h"
# include <string.h> # define SIZE 12
sfr AUXR = 0x8E ;
sbit D5 = P3^ 7 ;
char cmd[ SIZE] ; code char LJWL[ ] = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n" ;
code char LJFWQ[ ] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n" ; char TCMS[ ] = "AT+CIPMODE=1\r\n" ;
char SJCS[ ] = "AT+CIPSEND\r\n" ; void UartInit ( void )
{ AUXR = 0x01 ; SCON = 0x50 ; TMOD &= 0x0F ; TMOD |= 0x20 ; TH1 = 0xFD ; TL1 = 0xFD ; TR1 = 1 ; EA = 1 ; ES = 1 ;
} void Delay1000ms ( )
{ unsigned char i, j, k; _nop_ ( ) ; i = 8 ; j = 1 ; k = 243 ; do { do { while ( -- k) ; } while ( -- j) ; } while ( -- i) ;
} void sendByte ( char data_msg)
{ SBUF = data_msg; while ( ! TI) ; TI = 0 ;
} void sendString ( char * str)
{ while ( * str != '\0' ) { sendByte ( * str) ; str++ ; }
} void main ( )
{ int mark = 0 ; D5 = 1 ; UartInit ( ) ; while ( 1 ) { if ( mark == 0 ) { sendString ( LJWL) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; sendString ( LJFWQ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; sendString ( TCMS) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; sendString ( SJCS) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; mark = 1 ; } else { sendString ( "chenlichen shuai\r\n" ) ; Delay1000ms ( ) ; } }
} void Uart_Handler ( ) interrupt 4
{ static int i = 0 ; if ( RI) { RI = 0 ; cmd[ 0 ] = SBUF; if ( cmd[ 0 ] == '1' ) { D5 = 0 ; } if ( cmd[ 0 ] == '0' ) { D5 = 1 ; } } if ( TI) ;
}
3.优化连接过程
# include "reg52.h"
# include "intrins.h"
# include <string.h> # define SIZE 12
sfr AUXR = 0x8E ;
sbit D5 = P3^ 7 ;
sbit D6 = P3^ 6 ; char buffer[ SIZE] ;
code char LJWL[ ] = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n" ;
code char LJFWQ[ ] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n" ;
char TCMS[ ] = "AT+CIPMODE=1\r\n" ;
char SJCS[ ] = "AT+CIPSEND\r\n" ;
char RESET[ ] = "AT+RST\r\n" ;
char AT_OK_Flag = 0 ;
char AT_Connect_Net_Flag = 0 ; void UartInit ( void )
{ AUXR = 0x01 ; SCON = 0x50 ; TMOD &= 0x0F ; TMOD |= 0x20 ; TH1 = 0xFD ; TL1 = 0xFD ; TR1 = 1 ; EA = 1 ; ES = 1 ;
} void Delay1000ms ( )
{ unsigned char i, j, k; _nop_ ( ) ; i = 8 ; j = 1 ; k = 243 ; do { do { while ( -- k) ; } while ( -- j) ; } while ( -- i) ;
} void sendByte ( char data_msg)
{ SBUF = data_msg; while ( ! TI) ; TI = 0 ;
} void sendString ( char * str)
{ while ( * str != '\0' ) { sendByte ( * str) ; str++ ; }
} void main ( )
{ int mark = 0 ; D5 = D6 = 1 ; UartInit ( ) ; Delay1000ms ( ) ; sendString ( LJWL) ; while ( ! AT_OK_Flag) ; AT_OK_Flag = 0 ; sendString ( LJFWQ) ; while ( ! AT_OK_Flag) ; AT_OK_Flag = 0 ; sendString ( TCMS) ; while ( ! AT_OK_Flag) ; AT_OK_Flag = 0 ; sendString ( SJCS) ; while ( ! AT_OK_Flag) ; if ( AT_Connect_Net_Flag) { D5 = 0 ; } if ( AT_OK_Flag) { D6 = 0 ; } while ( 1 ) { Delay1000ms ( ) ; sendString ( "chenlichen shuai\r\n" ) ; }
} void Uart_Handler ( ) interrupt 4
{ static int i = 0 ; char tmp; if ( RI) { RI = 0 ; tmp = SBUF; if ( tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == 'F' ) { i = 0 ; } buffer[ i++ ] = tmp; if ( buffer[ 0 ] == 'W' && buffer[ 5 ] == 'G' ) { AT_Connect_Net_Flag = 1 ; memset ( buffer, '\0' , SIZE) ; } if ( buffer[ 0 ] == 'O' && buffer[ 1 ] == 'K' ) { AT_OK_Flag = 1 ; memset ( buffer, '\0' , SIZE) ; } if ( buffer[ 0 ] == 'F' && buffer[ 1 ] == 'A' ) { for ( i= 0 ; i< 5 ; i++ ) { D5 = 0 ; Delay1000ms ( ) ; D5 = 1 ; Delay1000ms ( ) ; } sendString ( RESET) ; memset ( buffer, '\0' , SIZE) ; } if ( buffer[ 0 ] == 'L' && buffer[ 2 ] == '1' ) { D5 = 0 ; memset ( buffer, '\0' , SIZE) ; } if ( buffer[ 0 ] == 'L' && buffer[ 2 ] == '0' ) { D5 = 1 ; memset ( buffer, '\0' , SIZE) ; } if ( i == 12 ) i = 0 ; } }
4.esp工作在路由器模式
# include "reg52.h"
# include "intrins.h"
# include <string.h> # define SIZE 12
sfr AUXR = 0x8E ;
sbit D5 = P3^ 7 ;
sbit D6 = P3^ 6 ; char buffer[ SIZE] ;
char LYMO[ ] = "AT+CWMODE=2\r\n" ;
char DLJ[ ] = "AT+CIPMUX=1\r\n" ;
char JLFW[ ] = "AT+CIPSERVER=1\r\n" ;
char FSSJ[ ] = "AT+CIPSEND=0,5\r\n" ; char AT_OK_Flag = 0 ;
char AT_Connect_Net_Flag = 0 ;
char Client_Connect_Flag = 0 ; void UartInit ( void )
{ AUXR = 0x01 ; SCON = 0x50 ; TMOD &= 0x0F ; TMOD |= 0x20 ; TH1 = 0xFD ; TL1 = 0xFD ; TR1 = 1 ; EA = 1 ; ES = 1 ;
} void Delay1000ms ( )
{ unsigned char i, j, k; _nop_ ( ) ; i = 8 ; j = 1 ; k = 243 ; do { do { while ( -- k) ; } while ( -- j) ; } while ( -- i) ;
} void sendByte ( char data_msg)
{ SBUF = data_msg; while ( ! TI) ; TI = 0 ;
} void sendString ( char * str)
{ while ( * str != '\0' ) { sendByte ( * str) ; str++ ; }
} void main ( )
{ int mark = 0 ; D5 = D6 = 1 ; UartInit ( ) ; Delay1000ms ( ) ; sendString ( LYMO) ; while ( ! AT_OK_Flag) ; AT_OK_Flag = 0 ; sendString ( DLJ) ; while ( ! AT_OK_Flag) ; AT_OK_Flag = 0 ; sendString ( JLFW) ; while ( ! Client_Connect_Flag) ; AT_OK_Flag = 0 ; if ( Client_Connect_Flag) { D5 = 0 ; D6 = 0 ; } while ( 1 ) { sendString ( FSSJ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; sendString ( "Hello" ) ; Delay1000ms ( ) ; Delay1000ms ( ) ; }
} void Uart_Handler ( ) interrupt 4
{ static int i = 0 ; char tmp; if ( RI) { RI = 0 ; tmp = SBUF; if ( tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == '0' || tmp == ':' ) { i = 0 ; } buffer[ i++ ] = tmp; if ( buffer[ 0 ] == 'W' && buffer[ 5 ] == 'G' ) { AT_Connect_Net_Flag = 1 ; memset ( buffer, '\0' , SIZE) ; } if ( buffer[ 0 ] == 'O' && buffer[ 1 ] == 'K' ) { AT_OK_Flag = 1 ; memset ( buffer, '\0' , SIZE) ; } if ( buffer[ 0 ] == '0' && buffer[ 2 ] == 'C' ) { Client_Connect_Flag = 1 ; memset ( buffer, '\0' , SIZE) ; } if ( buffer[ 0 ] == ':' && buffer[ 1 ] == 'o' && buffer[ 2 ] == 'p' ) { D5 = 0 ; memset ( buffer, '\0' , SIZE) ; } if ( buffer[ 0 ] == ':' && buffer[ 1 ] == 'c' && buffer[ 2 ] == 'l' ) { D5 = 1 ; memset ( buffer, '\0' , SIZE) ; } if ( i == 12 ) i = 0 ; } }