#include"nb.h"
uint8_t check_ack_timeout = 10;
uint8_t ue_exist_flag = 0;
uint8_t ue_need_reboot_flag = 0;
//检查返回的响应是否符合预期
//传入参数为预期返回的字符串
//返回0,未检测到预期值
//其他值,预期字符所在的位置
uint8_t* BC95_check_ack(char *str)
{
char *strx=0;
if(usart1_rece_flag)
{
USART_RX_BUF1[len1]='\0';
strx=strstr((const char*)USART_RX_BUF1,(const char*)str);
}
return (uint8_t*)strx;
}
//发送at指令函数
//cmd:at指令,ack:预期响应,waittime,超时时间
//返回0,发送成功
//返回1,发送超时
uint8_t BC95_send_cmd(char *cmd,char *ack,uint16_t waittime)
{
uint8_t res=0;
usart1_rece_flag=0;
len1 = 0;
memset(USART_RX_BUF1,0,USART_REC_LEN);
UART1_SendString(cmd); //发送出去命令
if(ack&&waittime)
{
while(--waittime)
{
delay_ms(20);//串口在一个字节一个字节的接收,这块每接收一个字节就检测接收到的整个数据是否符合要求
if(usart1_rece_flag) //收到数据,检查数据是否是想要的
{
if(BC95_check_ack(ack))break; //是想要的,发送结束,跳出
usart1_rece_flag=0; //不是想要的,再等
}
}
if(waittime==0){
res=1;
}
return res;
}
}
//上电程序,检测模块是否连接,检查配置是否为自动模式,是否为需要的频段
void BC95_power_on(void)
{
//BC95_send_cmd(SET_UE_REBOOT,"REBOOT",100);
check_ack_timeout = 10;
ue_exist_flag = 1;
UART2_SendString("进入power on \r\n");
//LCD_ShowString(60,70,200,16,16,"进入power on \r\n");
while(BC95_send_cmd("AT\r\n","OK",100)&&check_ack_timeout)
{
UART2_SendString("进入while \r\n");
//LCD_ShowString(60,70,200,16,16,"进入while \r\n");
if(check_ack_timeout)
{
check_ack_timeout--;
ue_exist_flag = 0;
UART2_SendString("等待模块上电! \r\n");
//LCD_ShowString(60,70,200,16,16,"等待模块上电 \r\n");
}
delay_ms(1000);
}
//判断模块是否是自动连接模式,如果不是则将模块设置成自动模式
if(ue_exist_flag&&!BC95_send_cmd(QUERY_UE_CONNECT_MODE,"AUTOCONNECT,FALSE",100))//不是自动连接
{
check_ack_timeout = 3;
while(check_ack_timeout)
{
check_ack_timeout--;
if(!BC95_send_cmd(SET_UE_AUTOCONNECT,"OK",100))
{
UART2_SendString("设置为自动模式成功! \r\n");
if(!BC95_send_cmd(SET_UE_FUNC_1,"OK",100))
UART2_SendString("打开射频成功! \r\n");
//LCD_ShowString(60,90,200,16,16,"设置为自动模式成功! \r\n");
break;
}
delay_ms(1000);
}
ue_need_reboot_flag =1;
}
//判断模块是否是默认设置频段,如果不是则设置成默认频段
if(ue_exist_flag&&BC95_send_cmd(QUERY_UE_BAND,UE_DEFAULT_BAND,100))
{
BC95_send_cmd(SET_UE_DEFAULT_BAND,UE_DEFAULT_BAND,100);
UART2_SendString("设置默认频段! \r\n");
//LCD_ShowString(60,110,200,16,16,"设置默认频段! \r\n");
ue_need_reboot_flag = 1;
}
//重启模块生效配置
if(ue_exist_flag&&ue_need_reboot_flag)
{
ue_need_reboot_flag = 0;
check_ack_timeout = 10;
BC95_send_cmd(SET_UE_REBOOT,"REBOOT",100);
UART2_SendString("重启模块! \r\n");
UART2_SendString((char*)USART_RX_BUF1);
// LCD_ShowString(60,110,200,16,16,"重启模块! \r\n");
while(check_ack_timeout&&!BC95_check_ack("Neul"))
{
if(BC95_check_ack("Neul"))
{
UART2_SendString("重启模块!Neul \r\n");
break;
}else
{
UART2_SendString("重启模块!AAAAAA \r\n");
check_ack_timeout--;
delay_ms(1000);
}
}
}
}
//检查模块的网络状态
//此函数不检查联网状态,仅检查附网注网状态,联网状态可以使用BC95_send_cmd发命令,单独检测
//附网注网失败或者超时返回0,返回1附网注网成功,返回2附网成功
//只是检查,不做过多操作
uint8_t query_net_status(void)
{
uint8_t res = 0;
uint8_t attached_flag = 0;//附网,附网也就是网络是否激活
uint8_t registered_flag = 0;//注网,网络注册
check_ack_timeout = 40;
while(!(attached_flag&®istered_flag)&&check_ack_timeout)//未成功
{
if(!BC95_send_cmd(QUERY_UE_SCCON_STATS,UE_CONNECTED,100))
{
attached_flag = 1;
registered_flag = 1;
res = 1;
UART2_SendString("附网、注网成功! \r\n");
// LCD_ShowString(60,130,200,16,16,"附网、注网成功! \r\n");
break;
}else
{
if(!attached_flag)
{
if(!BC95_send_cmd(QUERY_UE_ATTACH_STATS,UE_ATTACHED_STATS,100))
{
UART2_SendString("附网成功! \r\n");
// LCD_ShowString(60,130,200,16,16,"附网成功! \r\n");
attached_flag = 1;
res =2;
}else
{
UART2_SendString("正在附网...! \r\n");
//LCD_ShowString(60,130,200,16,16,"正在附网...! \r\n");
attached_flag = 0;
}
}
if(attached_flag&&!registered_flag)
{
if(attached_flag&&!BC95_send_cmd(QUERY_UE_EREG_STATS,UE_EREGISTERED_STATS,100))
{
UART2_SendString("注网成功...! \r\n");
//LCD_ShowString(60,150,200,16,16,"注网成功...! \r\n");
registered_flag = 1;
res =1;
}else
{
UART2_SendString("正在注网...! \r\n");
//LCD_ShowString(60,150,200,16,16,"正在注网...! \r\n");
registered_flag = 0;
}
}
}
check_ack_timeout--;
delay_ms(500);
if(!check_ack_timeout&&!attached_flag&&!registered_flag)
{
UART2_SendString("附网、注网失败! \r\n");
//LCD_ShowString(60,150,200,16,16,"附网、注网失败! \r\n");
}
}
return res;
}
//COAP相关初始化
void creat_COAP_server(void)
{
if(BC95_send_cmd(QUERY_COAP,UE_DEFAULT_COAP,100))
{
BC95_send_cmd(SET_COAP_SERVER,"OK",100);
UART2_SendString((char*)USART_RX_BUF1);
UART2_SendString("设置CDP服务器! \r\n");
}
if(!BC95_send_cmd("AT+NSMI=1\r\n","OK",100))//配置后,若消息已发送,串口会自动返回+NSMI:SENT 提示。发送指令
{
UART2_SendString("开启发送消息通知成功! \r\n");
//LCD_ShowString(60,150,200,16,16,"开启发送消息通知成功! \r\n");
}
}
//发送COAP数据
void send_COAP_msg(char *type)
{
char ptr[20]="AT+NMGS=1,";
strcat(ptr,type);
UART2_SendString(ptr);
if(!BC95_send_cmd(ptr,"OK",200))
{
UART2_SendString("发送数据成功! \r\n");
UART2_SendString(ptr);
//LCD_ShowString(60,150,200,16,16,"发送数据成功! \r\n");
//LCD_ShowString(60,170,200,16,16,ptr);
}
}
void NB_init(void)
{
UART2_SendString("进入NB_INIT \r\n");
BC95_power_on();
if(query_net_status())
{
UART2_SendString("初始化成功 \r\n");
//LCD_ShowString(60,150,200,16,16,"初始化成功! \r\n");
creat_COAP_server();
}
}