ESP8266_1/ESP8266_7 wifi系统板串口配置(AP+Station模式)

ESP8266_wifi配置

 

1.打开串口

int openSerialWifi(char *cSerialName)
{
    int iFd;

    struct termios opt; 	
    //打开串口
    iFd = open(cSerialName,  O_RDWR | O_NOCTTY | O_NDELAY);                        
    if(iFd < 0) {
        perror(cSerialName);
        return -1;
    }
    //获取终端参数
    tcgetattr(iFd, &opt);      
    //设置波特率
    cfsetispeed(&opt, B115200);  
    cfsetospeed(&opt, B115200);
    
    printf("uart4 init seccessful \n");

   #if 1
    /*
     * raw mode
     */
    opt.c_lflag   &=   ~(ECHO   |   ICANON   |   IEXTEN   |   ISIG);
    opt.c_iflag   &=   ~(IXON | IXOFF | IXANY);
    opt.c_oflag   &=   ~(OPOST);   //不执行输出*******************************************
    opt.c_cflag   &=   ~PARENB;    //无奇偶校验
    opt.c_cflag   &=   ~CSTOPB;    //1位停止位
    opt.c_cflag   &=   ~CSIZE;     //先把数据位清零
    opt.c_cflag   |=   CS8;        //把数据位设置为8位

    /*
     * 'DATA_LEN' bytes can be read by serial
     */
    opt.c_cc[VMIN]   =   DATA_LEN;                                      
    opt.c_cc[VTIME]  =   150;
	#endif

	
    if (tcsetattr(iFd,   TCSANOW,   &opt)<0) {
        return   -1;
    }

    return iFd;
}

2.设置AT指令

void Set_Wifi(void)
{
    //设置为AP+Station模式
    char cmd1[20]="AT+CWMODE=3\r\n";  
    //重置 
    char cmd2[20]="AT+RST\r\n"; 
    //连接ap       
    char cmd3[40]="AT+CWSAP=\"ouya\",\"123456789\"\r\n";
    //设置为透传 
    char cmd4[20]="AT+CIPMODE=1\r\n"; 
    //开启单连接
    char cmd5[20]="AT+CIPMUX=0\r\n";
    //TCP通信,设置为客户端模式  
    char cmd6[50]="AT+CIPSTART=\"TCP\",\"192.168.4.2\",8080\r\n"; 
    //开始发送数据
    char cmd7[15]="AT+CIPSEND\r\n";     

    unsigned char ucHeartCounter = 0;
    boolean espFlag = true;
    //设置为AP+Station模式
    sendCmdWifi(cmd1);  
    sleep(1);
    //重置
    sendCmdWifi(cmd2);  
    sleep(2);
    //连接ap
    sendCmdWifi(cmd3);  
    sleep(5);
    //设置为透传
    sendCmdWifi(cmd4);   
    sleep(1);
    sendCmdWifi(cmd5);   
    sleep(1);
    //TCP通信,设置为客户端模式
    sendCmdWifi(cmd6);   
    sleep(1);
    //开始发送数据
    sendCmdWifi(cmd7);

}

3.发送数据

void sendCmdWifi(char *str)
{
    int isNum,len=0;
    int txslen = strlen(str);
    //向文件描述符写入数据
    isNum = write(fd_wifi,str, txslen);
    if (isNum < 0) 
        {
            printf("write data to serial failed! \n");
        }
     else
        {
            printf("Tx %d \r\n",isNum);
        }
        //清空数组
        memset(tmp_wifi,0,1024);
}

4.主函数

#include

#define DATA_LEN                0xFF 
extern int fd_wifi;
char tmp_wifi[1024];
int wififlag=0;


void main()
{
       sendCmdWifi("hello,esp8266");
       return 0;
}

 

你可能感兴趣的:(wifi模块,esp8266,AP+Station)