1.先弄清楚四种AT指令的格式
AT+<cmd>= ? 测试命令,用于向模块询问支持的设置项目。
AT+<cmd>? 读取命令,用于让模块上报某个命令代表的设置项当前的值。
AT+<cmd>=p1 设置命令,用于向模块设置某个项目的值
AT+<cmd> 执行命令,用于向模块执行某个操作
2.可以用AT+CLAC
查看所有支持的指令
[09:18:38.230]发→◇AT+CLAC
□
[09:18:38.242]收←◆AT+CLAC
AT+COPS
AT+CGATT
AT+NEARFCN
AT+NCSEARFCN
……………//太长了,就不复制了
//*************************************************//
具体指令:
1.ATI 返回模块厂商和型号等基本信息
2.ATE<0/1> 0:指令码不回显 1:指令码回显
3.AT+CGMI 返回制造商名字
4.AT+CGMM/AT+CGMM=? 返回制造商模块的型号编码
5.AT+CGMR/AT+CGMR=? 返回制造商模块的版本号
6.AT+CGSN=1 ***返回制造商模块的IMEI***
7.AT+CEREG 用于设置模块 网络 注册状态(协议方面)
8.AT+CSCON ***模块与基站 网络 的射频网络是否连接(检测是否进入PSM状态)***
9.AT+CLAC 列出所有支持的指令
10.AT+CSQ ***用来测试信号强度(信号强度取决于:基站、位置、NB模块天线设计匹配)***
11.AT+CGPADDR ***取核心网和基站分配给NB模块的本次通信的临时IP地址***
12.AT+COPS 用来设置运营商
13.AT+CGATT=<0关/1开> ***用来设置或检测模块是否连接上核心网***
14.AT+CGACT 用来使能或禁止PDP上下文
15.AT+CIMI 用来获取IMSI(国际用户识别码)
16.AT+CGDCONT Define a PDP Context
17.AT+CFUN ***用来设置NB模块内部的射频单元,与自动联网/手动联网有关***
18.AT+CMEE ***用来打印NB模块错误信息***
19.AT+CCLK 返回当前时间
20.AT+CPSMS 设置PSM模式相关参数
21.AT+CEDRXS 设置eDRX模式相关参数
22.AT+CEER 用来打印NB设备端出错的扩展信息
23.AT+CEDRXRDP eDRX模式相关的动态参数设置
24.AT+CTZR 用来设置或获取时区信息
25.AT+CIPCA PDP上下文初始化
26.AT+CGAPNRC APN速率控制
//*************************************************//
1.AT+CSMS 短信服务相关
2.AT+CNMA 与新接收消息相关
3.AT+CSCA Service Centre Address
4.AT+CMGS NB设备主动向云平台发送消息
5.AT+CMGC Send SMS Command
6.AT+CSODCP Send Originating Data via the Control Plane
7.AT+CRTDCP Report Terminating Data via Control Plane
//************************************************//
1.AT+NRB ***软件复位模块***
2.AT+NUESTATS ***返回UE(user equipment,用户设备,NB模块)的状态信息***
3.AT+NEARFCN 设置搜索频率850MHZ(电信的)
4.AT+NSOCR 相当于socket函数 ,让模块内部创建一个socket接口
5.AT+NSOST SendTo函数 (UDP Only)
6.AT+NSOSTF 相当于带flag的sendto函数,在UDP中进行消息发送
7.AT+NSORF 相当于recv函数,在UDP中进行消息接收
8.AT+NSOCL 相当于close函数,用于关闭socket
9.AT+NSONMI 指示socket收到消息
10.AT+NPING 相当于ping命令,用来测试当前模块和远端网络地址是否接通
11.AT+NBAND 用于设置当前模块的Band,BC95-B 5
12.AT+NLOGLEVEL 设置debug log信息的输出level
13.AT+NCONFIG ***用于对UE进行配置***
14.AT+NATSPEED ***配置波特率9600***
15.AT+NCCID 用于获取NB卡的唯一编码(ICCID)
16.AT+NFWUPD 用于通过UART来升级模块内部固件
17.AT+NPOWERCLASS Set the Mapping for Band and Power Class
18.AT+NPSMR Power Saving Mode Status Report
19.AT+NPTWEDRXS Paging Time Window Value and eDRX Setting
20.AT+NPIN PIN Operation
21.AT+NCSEARFCN Clear Stored EARFCN
//***********************************************//
1.AT+NCDP ***用于设置CDP(电信云的IP地址)***
2.AT+QSECSWT 设置加密模式(开发中)
3.AT+QSETPSK 设置调制(开发中)
4.AT+NMGS ***用于向电信云服务器发送消息***
5.AT+NMGR ***接收消息Get Messages***
6.AT+NNMI ***模块收到电信云下行的数据后会自动接收提示***
7.AT+NSMI ***模块向电信云上行数据后提示***
8.AT+NQMGR 向模块查询有无收到电信云下行的数据
9.AT+NQMGS 向模块查询消息有无发送成功
10.AT+NMSTATUS 查询模块在消息发送阶段的状态
11.AT+QLWULDATAEX 发送连接和不连接Send CON/NON Messages
12.AT+QLWULDATASTATUS Query CON Messages Sending Status
//**********************************//
1.验证AT指令可用
[17:06:27.301]发→◇AT
□
[17:06:27.310]收←◆
OK
2.读信号质量
[17:14:53.397]发→◇AT+CSQ
□
[17:14:53.409]收←◆
+CSQ:28,99
OK
3.获取IMEI号
[17:37:41.343]发→◇AT+CGSN=1
□
[17:37:41.358]收←◆AT+CGSN=1
+CGSN:865823049053753
OK
4.模块注册到NB网络
[17:44:05.006]发→◇AT+CEREG=1
□
[17:44:05.023]收←◆AT+CEREG=1
OK
[17:46:31.083]发→◇AT+CEREG?
□
[17:46:31.098]收←◆AT+CEREG?
+CEREG:1,1
OK
5.查看模块物理地址
[09:28:14.281]发→◇AT+CGPADDR
□
[09:28:14.296]收←◆AT+CGPADDR
+CGPADDR:0,10.24.31.22
OK
6.设置运营商
[09:36:26.509]发→◇AT+COPS=0
□
[09:36:26.524]收←◆AT+COPS=0
OK
[09:36:03.288]发→◇AT+COPS?
□
[09:36:03.300]收←◆AT+COPS?
+COPS:0,2,"46000"
OK
7.设置或检测模块是否连接上核心网
[09:42:46.151]发→◇AT+CGATT=1
□
[09:42:46.170]收←◆AT+CGATT=1
OK
[09:42:40.348]发→◇AT+CGATT=?
□
[09:42:40.363]收←◆AT+CGATT=?
+CGATT:(0,1)
OK
8.返回当前时间
[09:48:12.326]发→◇AT+CCLK?
□
[09:48:12.339]收←◆AT+CCLK?
+CCLK:20/09/11,01:48:15+32
OK
和我电脑差了八个小时,没找到设置时区的,在程序里加上吧……
9.软复位模块,时间会更新,误差归零,可以定时来复位,去除累积误差
[10:07:37.746]发→◇AT+NRB
□
[10:07:37.758]收←◆REBOOTING
[10:07:38.619]收←◆Boot: Unsigned
Security B.. Verified
Protocol A..
[10:07:41.753]收←◆Verified
Apps A......
[10:07:42.685]收←◆Verified
[10:07:43.001]收←◆
REBOOT_CAUSE_APPLICATION_AT
Neul
OK
[10:07:52.599]收←◆
+QLWEVTIND:0
+QLWEVTIND:3
10.功能选项AT+CFUN<0/1>
参数0代表基本功能,1代表基本功能+附加功能,附加功能每个厂商定义不一样。
[11:39:10.580]发→◇AT+CFUN=1
□
[11:39:10.595]收←◆AT+CFUN=1
OK
[11:39:18.994]发→◇AT+CFUN?
□
[11:39:19.008]收←◆AT+CFUN?
+CFUN:1
OK
1.Stm32准备工作
(1)初始化一个串口,并打开接收中断
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
Usart2RecIT();
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART2) {
//BC35-G NB-iot通信模块
HAL_UART_Receive_IT(&huart2, &uart1Data, 1);
uart2WriteByte(uart1Data);
uart2DataFlg = 1;
//HAL_UART_Transmit(&huart2,&uart2Data,1,0xFFFF);
}
}
(2)将当前发送的指令保存在栈中
typedef struct
{
uint8_t* ATSendbuf;
uint8_t* ATRecvbuf;
int16_t ATSendlen;
int16_t ATRecvlen;
int8_t* ATack;
int8_t* ATNack;
}NBIOT_ATCmdTypeDef;
static void NBIOT_ATCmd_SetCmdStack(NBIOT_ATCmdTypeDef* ATCmdStack, int8_t* Sendbuf, uint16_t Sendlen, int8_t* ATack, int8_t* ATNack)
{
ATCmdStack->ATSendbuf = (uint8_t *)Sendbuf;
ATCmdStack->ATSendlen = Sendlen;
ATCmdStack->ATack = ATack;
ATCmdStack->ATNack = ATNack;
}
typedef struct
{
uint8_t sdata[MaxUartBC95];
uint16_t slen;
}sensordataType_Def;
sensordataType_Def nbdata;
sensordataType_Def rlynbdata;
(3)发送AT指令
void NB_SEND(uint8_t *buf,uint16_t len)
{
HAL_UART_Transmit_IT (&huart2,buf,len);
}
int8_t NBIOT_ATCMD_SEND(NBIOT_ATCmdTypeDef* ATCmdStack, int8_t* Sendbuf, uint16_t Sendlen, int8_t* ATack, int8_t* ATNack,int32_t tm)
{
NBIOT_ATCmdTypeDef ATCmd;
NBIOT_ATCmd_SetCmdStack(&ATCmd,Sendbuf,Sendlen,ATack,ATNack);
Uart2BufClear();
NB_SEND(ATCMD->ATSendbuf,ATCMD->ATSendlen);//发送
ReceiveNBIOT(tm);//延时用于串口接收模组返回数据
return (analysisNBData(ATCMD->ATack,ATCMD->ATNack));//返回设置是否成功
}
(4)接收模组返回数据函数
void ReceiveNBIOT(uint16_t waittime)//发送AT后的等待时间
{
HAL_Delay(waittime);
nbdata.slen = uartReadByte(&huart2,&nbdata.sdata[0],MaxUartBC95);
if (nbdata.slen > MaxUartBC95)
{
nbdata.slen = MaxUartBC95;
}
memset (&rlynbdata.sdata[0],0,MaxUartBC95);
memcpy (&rlynbdata.sdata[0],&nbdata.sdata[0],nbdata.slen);
rlynbdata.slen = nbdata.slen;
}
(5)分析指令是否设置成功
int8_t analysisNBData(int8_t* RLY,int8_t* NRLY)
{
int8_t flg = NACK;
if (rlynbdata.slen)
{
if(strstr_my((int8_t *)&rlynbdata.sdata[0],RLY) == NULL)
{
if(strstr_my((int8_t *)&rlynbdata.sdata[0],NRLY) == NULL)
{
flg = NACK;
}
else
{
flg = ACKERROR;
}
}
else
{
flg = ACKOK;
}
}
return flg;
}
2.在Stm32中使用AT指令的组合
现在我们用NBIOT_ATCMD_SEND
函数反复操作AT指令就可以啦
(1)重启
从第二部分看,似乎重启只需要一条AT指令,但是官方推荐在关机之前,使用AT+CFUN保存频点、去网络附着。开机后我们再打开附着。
void NBIOT_reCON(void)
{
int8_t OK[] = "OK";
int8_t ERROR[] = "ERROR";
int8_t ATOK[] = "AT\r\n";
int8_t ATCFUN0[] = "AT+CFUN=0\r\n";
int8_t ATNRB[] = "AT+NRB\r\n";
int8_t ATCFUN1[] = "AT+CFUN=1\r\n";
int8_t ATCGATT[] = "AT+CGATT=1\r\n";
NBIOT_ATCmdTypeDef ATCmd;
NBIOT_ATCMD_SEND(&ATCmd,ATOK,strlen((char *)ATOK),OK,ERROR,1000);
NBIOT_ATCMD_SEND(&ATCmd,ATNRB,strlen((char *)ATNRB),OK,ERROR,5000);
NBIOT_ATCMD_SEND(&ATCmd,ATCFUN0,strlen((char *)ATCFUN0),OK,ERROR,3000);
NBIOT_ATCMD_SEND(&ATCmd,ATOK,strlen((char *)ATOK),OK,ERROR,1000);
NBIOT_ATCMD_SEND(&ATCmd,ATCFUN1,strlen((char *)ATCFUN1),OK,ERROR,5000);
NBIOT_ATCMD_SEND(&ATCmd,ATCGATT,strlen((char *)ATCGATT),OK,ERROR,3000);
}
2.读取IMEI并解析
int8_t Q_NBIOT_IMEI(int8_t *imei)
{
int8_t ATCGSN[] = "AT+CGSN=1\r\n"; //Request the IMEI number +CGSN:490154203237511
int8_t OK[] = "OK";
int8_t ERROR[] = "ERROR";
NBIOT_ATCmdTypeDef ATCmd;
NBIOT_ATCMD_SEND(&ATCmd,ATCGSN,strlen((char *)ATCGSN),OK,ERROR,1000);
memset((void *)imei, 0x0, sizeof(imei));
if (sscanf((const char*)&rlynbdata.sdata[0], "%*[^+CGSN]%*[^:]:%[^\r]", imei) <= 0)
{
return -1;
}
return 0;
}
sscanf()函数使用方法
3.读取网络时间并解析
int8_t Q_NBIOT_CCLK(int8_t *localtime, size_t localtime_len)
{
int8_t ATCCLK[] = "AT+CCLK?\r\n"; //+CCLK:19/12/01,02:05:38+32
int8_t OK[] = "OK";
int8_t ERROR[] = "ERROR";
int rly,num = 0;
NBIOT_ATCmdTypeDef ATCmd;
while (num++ < 3)
{
NBIOT_ATCMD_SEND(&ATCmd,ATCCLK,strlen((char *)ATCCLK),OK,ERROR,2000);
rly = net_gettime((char *)&rlynbdata.sdata[0],(char *)localtime,strlen((char *)&rlynbdata.sdata[0]));//
if (0 == rly)
break;
}
return 0;
}
4.读取温度和电量
//+QCHIPINFO:TEMP,30
//+QCHIPINFO:VBAT,3316
int8_t Q_NBIOT_CHIPINFO(int8_t **var)
{
int8_t ATCCHIPINFO[] = "AT+QCHIPINFO=ALL\r\n";
int8_t OK[] = "OK";
int8_t ERROR[] = "ERROR";
int rly,num = 0;
NBIOT_ATCmdTypeDef ATCmd;
int8_t temp[10];
int8_t vbat[10];
memset(temp,0,sizeof(temp));
memset(vbat,0,sizeof(vbat));
while (num++ < 3)
{
NBIOT_ATCMD_SEND(&ATCmd,ATCCHIPINFO,strlen((char *)ATCCHIPINFO),OK,ERROR,2000);
rly = getinfo(&rlynbdata.sdata[0],strlen((char *)&rlynbdata.sdata[0]),(int8_t*)"TEMP",4,temp,sizeof(temp));
if (-1 == rly)
continue;
rly = getinfo(&rlynbdata.sdata[0],strlen((char *)&rlynbdata.sdata[0]),(int8_t*)"VBAT",4,vbat,sizeof(vbat));
if (0 == rly)
break;
}
if (0 == rly)
{
memcpy(*var,temp,10);
memcpy(*(var+1),vbat,10);
return 0;
}
return -1;
}
5.联网上传数据到电信云平台
(1)先看看官方给出的步骤
(2)联网
这里模组和平台通信的协议采用的是Coap协议,CoAP的默认端口号为5683。
int8_t CenterConnect(void)
{
int8_t rly = -1;
rly = Q_Link();
if (ACKOK == rly)
{
return 0;
}
NBIOT_reCON();
rly = Q_Link();
if (ACKOK == rly)
{
return 0;
}
rly = NBIOT_CONNECT();
return rly;
}
int8_t NBIOT_CONNECT(void)
{
int8_t C1[] = "1";
int8_t C0[] = "0";
int8_t OK[] = "OK";
int8_t ERROR[] = "ERROR";
int8_t ATOK[] = "AT\r\n";
int8_t ATNRB[] = "AT+NRB\r\n";
int8_t ATCFUN0[] = "AT+CFUN=0\r\n";
int8_t ATCPSMS[] = "AT+CPSMS=1\r\n";
int8_t ATNCSEARFCN[] = "AT+NCSEARFCN\r\n";
int8_t ATCFUN1[] = "AT+CFUN=1\r\n";
int8_t ATCGATT[] = "AT+CGATT=1\r\n";
int8_t ATCGATTq[] = "AT+CGATT?\r\n";
int8_t ATNCONFIG[] = "AT+NCONFIG=CELL_RESELECTION,TRUE\r\n";
int8_t ATNCDP[] = "AT+NCDP=180.101.147.115,5683\r\n";
int8_t ATNCONFIGTRUE[] = "AT+NCONFIG=AUTOCONNECT,TRUE\r\n";
int8_t ATCEDRXS[] = "AT+CEDRXS=0,5\r\n";
NBIOT_ATCmdTypeDef ATCmd;
NBIOTReset();
NBIOT_ATCMD_SEND(&ATCmd,ATOK,strlen((char *)ATOK),OK,ERROR,1000);
NBIOT_ATCMD_SEND(&ATCmd,ATNCDP,strlen((char *)ATNCDP),OK,ERROR,1000);
NBIOT_ATCMD_SEND(&ATCmd,ATNRB,strlen((char *)ATNRB),OK,ERROR,5000);
NBIOT_ATCMD_SEND(&ATCmd,ATCFUN0,strlen((char *)ATCFUN0),OK,ERROR,3000);
NBIOT_ATCMD_SEND(&ATCmd,ATNCSEARFCN,strlen((char *)ATNCSEARFCN),OK,ERROR,1000);
NBIOT_ATCMD_SEND(&ATCmd,ATCPSMS,strlen((char *)ATCPSMS),OK,ERROR,1000);
NBIOT_ATCMD_SEND(&ATCmd,ATNCONFIG,strlen((char *)ATNCONFIG),OK,ERROR,1000);
NBIOT_ATCMD_SEND(&ATCmd,ATCFUN1,strlen((char *)ATCFUN1),OK,ERROR,5000);
NBIOT_ATCMD_SEND(&ATCmd,ATNCONFIGTRUE,strlen((char *)ATNCONFIGTRUE),OK,ERROR,2000);
NBIOT_ATCMD_SEND(&ATCmd,ATCGATT,strlen((char *)ATCGATT),OK,ERROR,3000);
NBIOT_ATCMD_SEND(&ATCmd,ATCEDRXS,strlen((char *)ATCEDRXS),OK,ERROR,3000);
return (NBIOT_ATCMD_SEND(&ATCmd,ATCGATTq,strlen((char *)ATCGATTq),C1,C0,2000));
}
(3)上传数据AT+QLWULDATAEX
/AT+NMGR
(发布)
int8_t NBIOT_QLWULDATAEX(int8_t* Sendbuf, uint16_t Sendlen)
{
int8_t OK[] = "OK";
int8_t ERROR[] = "ERROR";
int8_t CMEE[] = "AT+CMEE=1\r\n";
//int8_t ATCMEE[] = "AT+NMSTATUS?\r\n";
NBIOT_ATCmdTypeDef ATCmd;
uint16_t buflen;
buflen = sizeof(data_buf)/2;
if (Sendlen > buflen)
{
Sendlen = buflen;
}
NBIOT_ATCMD_SEND(&ATCmd,CMEE,strlen((char *)CMEE),OK,ERROR,1000);
memset (NMGScmd_buf,0,sizeof(NMGScmd_buf));
NBIOT_trans (Sendbuf,data_buf,Sendlen);
sprintf((char *)NMGScmd_buf,"AT+QLWULDATAEX=%d,%s,0x0001\r\n",Sendlen,data_buf);
//NBIOT_ATCMD_SEND(&ATCmd,ATCMEE,strlen((char *)ATCMEE),OK,ERROR,1000);
return ( NBIOT_ATCMD_SEND(&ATCmd,NMGScmd_buf,strlen((char *)NMGScmd_buf),OK,ERROR,2000));
}
uint8_t SData2NET(int8_t *sensordata,uint16_t slen)
{
uint8_t rly;
//uint8_t r;
rly = NBIOT_QLWULDATAEX(&sensordata[0],slen);
//r = NBIOT_QLWULDATASTATUS(&sensordata[0],slen);
if (ACKOK != rly)
{
rly = NBIOT_QLWULDATAEX(&sensordata[0],slen);
printf("Try to send data again... \n\n");
}
if(ACKOK == rly)
{
printf("Send data OK!\n\n");
}
return rly;
}
也可以用AT+NMGS
来发布
int8_t NBIOT_NMGS(int8_t* Sendbuf, unsigned int Sendlen)
{
int8_t OK[] = "OK";
int8_t ERROR[] = "ERROR";
NBIOT_ATCmdTypeDef ATCmd;
unsigned int buflen;
buflen = sizeof(data_buf)/2;
if (Sendlen > buflen)
{
Sendlen = buflen;
}
memset (NMGScmd_buf,0,sizeof(NMGScmd_buf));
NBIOT_trans (Sendbuf,data_buf,Sendlen);
sprintf((char *)NMGScmd_buf,"AT+NMGS=%d,%s\r\n",Sendlen,data_buf);
return (NBIOT_ATCMD_SEND(&ATCmd,NMGScmd_buf,strlen((char *)NMGScmd_buf),OK,ERROR,3000));
}
6.接收平台下发数据AT+NMGR
(订阅)
int8_t NBIOT_NMGR(void)
{
int8_t OK[] = "OK";
int8_t ERROR[] = "ERROR";
NBIOT_ATCmdTypeDef ATCmd;
int rly;
memset (NMGRcmd_buf,0,sizeof(NMGRcmd_buf));
sprintf((char *)NMGRcmd_buf,"AT+NMGR\r\n");
rly = NBIOT_ATCMD_SEND(&ATCmd,NMGRcmd_buf,strlen((char *)NMGRcmd_buf),OK,ERROR,3000);
if(-1 == rly)
{
return rly;
}
return 1;
}
int8_t analyistNetData()
{
int8_t rly = NBIOT_NMGR();
if(rly == -1)
{
return -1;
}
//读rlynbdata中数据,然后按照约定解析
}