#include <rtthread.h>
#include <rtdevice.h>
#include <string.h>
#include "board.h"
#include "drv_gpio.h"
#include "at.h"
at_response_t resp = RT_NULL;
void at_init(void)
{
at_client_init("uart2", 512);
//创建响应结构体,最大长度为512字节,数据行数无限制,超时时间为5s
resp = at_create_resp(512,0,rt_tick_from_millisecond(5000));
if(!resp)
{
rt_kprintf("No memory for response structure!\r\n");
}
}
INIT_APP_EXPORT(at_init);
void at_test(int argc,char *argv[])
{
char *str;
// char table[40]="AT+CIPSEND=";
// char *cmd_str=table;
if(strcmp(argv[1],"AT")==0)
{
at_exec_cmd(resp,"AT");//测试命令
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
rt_kprintf("success\r\n");
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"RST")==0)
{
at_exec_cmd(resp,"AT+RST");//重启模块
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
rt_kprintf("RST success\r\n");
}
else
{
rt_kprintf("RST fail\r\n");
}
}
else if(strcmp(argv[1],"CWMODE")==0)//设置模式,1-station模式,2-AP模式,3-AP+station模式
{
if(strcmp(argv[2],"1")==0)
{
at_exec_cmd(resp,"AT+CWMODE=1");//station模式
}
else if(strcmp(argv[2],"2")==0)
{
at_exec_cmd(resp,"AT+CWMODE=2");//AP模式
}
else if(strcmp(argv[2],"3")==0)
{
at_exec_cmd(resp,"AT+CWMODE=3");//AP+station模式
}
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
rt_kprintf("CWMODE success\r\n");
}
else
{
rt_kprintf("CWMODE fail\r\n");
}
}
else if(strcmp(argv[1],"CWSAP")==0)//配置AP参数
{
at_exec_cmd(resp,"AT+CWSAP=\"test426\",\"12345678\",1,3");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
rt_kprintf("success\r\n");
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CIFSR")==0)//查看本机ip
{
at_exec_cmd(resp,"AT+CIFSR");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
//rt_kprintf("success\r\n");
at_resp_parse_line_args_by_kw(resp, ".","%s" ,str);
rt_kprintf("ip=%s\r\n",str);
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CWMODE?")==0)//查看应用模式
{
at_exec_cmd(resp,"AT+CWMODE?");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
//rt_kprintf("success\r\n");
at_resp_parse_line_args_by_kw(resp, ":","%s" ,str);
rt_kprintf("%s\r\n1-STATION,2-AP,3-STATION+AP\r\n",str);
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CIPMUX?")==0)//查看是否建立多连接
{
at_exec_cmd(resp,"AT+CIPMUX?");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
//rt_kprintf("success\r\n");
at_resp_parse_line_args_by_kw(resp, ":","%s" ,str);
rt_kprintf("%s\r\n0-single,1-mux\r\n",str);
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CIPMODE?")==0)//查看是透传还是非透传
{
at_exec_cmd(resp,"AT+CIPMODE?");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
//rt_kprintf("success\r\n");
at_resp_parse_line_args_by_kw(resp, ":","%s" ,str);
rt_kprintf("%s\r\n0-non-Penetrate,1-Penetrate\r\n",str);
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CIPMUX")==0)//开启多连接模式
{
at_exec_cmd(resp,"AT+CIPMUX=1");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
rt_kprintf("success\r\n");
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CIPSERVER")==0)//创建服务器
{
at_exec_cmd(resp,"AT+CIPSERVER=1,8080");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
rt_kprintf("success\r\n");
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CIPSERVER_OFF")==0)//关闭服务器
{
at_exec_cmd(resp,"AT+CIPSERVER=0");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
rt_kprintf("success\r\n");
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CIPSTATUS")==0)//查看当前连接
{
at_exec_cmd(resp,"AT+CIPSTATUS");
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
//rt_kprintf("success\r\n");
at_resp_parse_line_args_by_kw(resp, ":","%s" ,str);
rt_kprintf("%s\r\n",str);
at_resp_parse_line_args_by_kw(resp, "+CIPSTATUS:","%s" ,str);
rt_kprintf("%s\r\n",str);
}
else
{
rt_kprintf("fail\r\n");
}
}
else if(strcmp(argv[1],"CIPSEND")==0)//发送数据
{
char cmd_table[40]="AT+CIPSEND=";
at_exec_cmd(resp,strcat(cmd_table,argv[2]));
if(at_resp_get_line_by_kw(resp, "OK")!=RT_NULL)
{
rt_kprintf("success\r\n");
at_exec_cmd(resp,"1234");
// at_resp_parse_line_args_by_kw(resp, ":","%s" ,str);
// rt_kprintf("%s\r\n",str);
// at_resp_parse_line_args_by_kw(resp, "+CIPSTATUS:","%s" ,str);
// rt_kprintf("%s\r\n",str);
}
else
{
rt_kprintf("fail\r\n");
}
}
}
MSH_CMD_EXPORT(at_test, AT Client send commands to AT Server and get response data);