ESP8266 Centre + NB
宏定义 | 函数
nb_bc35.h 文件参考
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "driver/uart.h"
nb_bc35.h 的引用(Include)关系图:
此图展示该文件直接或间接的被哪些文件引用了:

浏览源代码.

宏定义

#define AT_MODU_NAME   "nb_neul95"
 
#define AT_NB_OPEN_RF   "AT+CFUN=1\r"
 
#define AT_NB_CLOSE_PSM   "AT+CPSMS=0\r"
 
#define AT_NB_CLOSE_EDRX   "AT+CEDRXS=0,5\r"
 
#define AT_NB_CGATT_ATTACH   "AT+CGATT=1\r"
 
#define AT_NB_REPORT_PREFIX   "AT+NMGS="
 
#define AT_NB_LINE_END   "\r\n"
 
#define AT_NB_reboot   "AT+NRB\r"
 
#define AT_NB_hw_detect   "AT+CFUN?\r"
 
#define AT_NB_get_auto_connect   "AT+NCONFIG?\r"
 
#define AT_CMD_PREFIX   "\r\n+NNMI:"
 
#define AT_DATAF_PREFIX   "+NSONMI:"
 
#define CGATT   "AT+CGATT?\r"
 
#define CGATT_DEATTACH   "AT+CGATT=0\r"
 
#define AT_LINE_END   "\r\n"
 
#define AT_CMD_BEGIN   "\r\n"
 
#define AT_USART_PORT   3
 
#define AT_BUARDRATE   9600
 
#define AT_CMD_TIMEOUT   10000
 
#define AT_MAX_LINK_NUM   4
 
#define NB_STAT_LOCALPORT   56
 
#define MAX_SOCK_NUM   5
 
#define UDP_PROTO   17
 
#define MAX_AT_USERDATA_LEN   (1024*5)
 
#define AT_MAX_PAYLOADLEN   512
 
#define IP_LEN   16
 

函数

void NB_ReportData (uint8 mid, uint8 *data_str)
 上报数据到云端 更多...
 
void NB_SendCmd (uint8 *cmd, uint8 cmd_len, uint8 *response_msg)
 向NB模组发送指令 更多...
 
void NB_ResponseTimerCb (void *arg)
 
void NB_Init (void)
 
void NB_RxMsgHandler (uint8 *nb_msg)
 NB消息处理函数 更多...
 
int str_to_hex (const char *bufin, int len, char *bufout)
 将十六进制码流转换为字符串形式 更多...
 
int32_t nb_set_cdpserver (char *host, char *port)
 
int32_t nb_hw_detect (void)
 
int32_t nb_get_netstat (void)
 
int nb_query_ip (void)
 
int32_t nb_send_payload (const char *buf, int len)
 
int32_t nb_check_csq (void)
 
int32_t nb_send_psk (char *pskid, char *psk)
 
int32_t nb_set_no_encrypt (void)
 
int32_t nb_reboot (void)
 
int32_t nb_recv_timeout (int32_t id, uint8_t *buf, uint32_t len, char *ipaddr, int *port, int32_t timeout)
 
int32_t nb_cmd_match (const char *buf, char *featurestr, int len)
 
void nb_step (void)
 
void nb_reattach (void)
 

详细描述


作者
Gump
版本
V0.1
日期
2020-01-29

宏定义说明

#define AT_MODU_NAME   "nb_neul95"
#define AT_NB_OPEN_RF   "AT+CFUN=1\r"
#define AT_NB_CLOSE_PSM   "AT+CPSMS=0\r"
#define AT_NB_CLOSE_EDRX   "AT+CEDRXS=0,5\r"
#define AT_NB_CGATT_ATTACH   "AT+CGATT=1\r"
#define AT_NB_REPORT_PREFIX   "AT+NMGS="
#define AT_NB_LINE_END   "\r\n"
#define AT_NB_reboot   "AT+NRB\r"
#define AT_NB_hw_detect   "AT+CFUN?\r"
#define AT_NB_get_auto_connect   "AT+NCONFIG?\r"
#define AT_CMD_PREFIX   "\r\n+NNMI:"
#define AT_DATAF_PREFIX   "+NSONMI:"
#define CGATT   "AT+CGATT?\r"
#define CGATT_DEATTACH   "AT+CGATT=0\r"
#define AT_LINE_END   "\r\n"
#define AT_CMD_BEGIN   "\r\n"
#define AT_USART_PORT   3
#define AT_BUARDRATE   9600
#define AT_CMD_TIMEOUT   10000
#define AT_MAX_LINK_NUM   4
#define NB_STAT_LOCALPORT   56
#define MAX_SOCK_NUM   5
#define UDP_PROTO   17
#define MAX_AT_USERDATA_LEN   (1024*5)
#define AT_MAX_PAYLOADLEN   512
#define IP_LEN   16

函数说明

void NB_ReportData ( uint8  mid,
uint8 data_str 
)

上报数据到云端

AT Command
AT+NMGS=数据长度,内容

例如:当编解码插件中的messageId为0x11时,

  • 发送0、1两个数据:AT+NMGS=03,110001
  • 发送9、10、11、16、17、32、33七个数据:AT+NMGS=08,11090A0B10112021
注解
上报数据的格式需要与IoT平台的“编解码插件”的解码顺序一致,不然上报的数据无法正确解析
参数
[in]data_str需要上报的十六进制数据字符串
42  {
43 
44  uint8 report_buf[128];
45 
46  /* 因为数据字符串是十六进制的,两个字符表示一个十六进制数,故数据长度为os_strlen(data_str)/2 */
47  os_sprintf(report_buf, AT_NB_REPORT_PREFIX "%02d,%02X%s" , os_strlen(data_str)/2 + 1, mid, data_str );
48 
49  NB_SendCmd(report_buf, os_strlen(report_buf), NULL);
50 
51 }
#define os_sprintf
Definition: osapi.h:54
void NB_SendCmd(uint8 *cmd, uint8 cmd_len, uint8 *res_msg)
向NB模组发送指令
Definition: nb_bc35.c:70
#define os_strlen
Definition: osapi.h:43
unsigned char uint8
Definition: c_types.h:43
#define NULL
Definition: c_types.h:73
#define AT_NB_REPORT_PREFIX
Definition: nb_bc35.h:34

函数调用图:

这是这个函数的调用关系图:

void NB_SendCmd ( uint8 cmd,
uint8  cmd_len,
uint8 res_msg 
)

向NB模组发送指令

  1. 将指令通过串口发送给NB模组

将响应消息复制到响应消息数组 response_msg[]

  1. 置位响应消息接收标志 response_flag。此标志位用于给串口消息处理函数 NB_RxMsgHandler 里的分支作判断,从而决定是否将接受的消息与响应消息作匹配。因为串口接收到的消息可能是云端 下发的指令,所以这里面加了这个标志位。
参数
[in]cmd要发送的指令字符串
[in]cmd_len指令字符串长度
[in]res_msg期望接收到的响应消息,参见NB模组的AT指令参考手册
待办事项:
增加对查询指令返回的查询结果信息的处理。将采用匹配消息头和存储消息体的方法。
70  {
71 
72  uint8 at_cmd[128];
73 
74  os_printf("MCU >>>>>> NB : %s\n", cmd);
75 
76  /* 这里在命令前面加上 AT_CMD_BEGIN 是为了清除掉NB模块中可能存在的消息缓存 */
77  /* 若是在调用该函数前NB模块中缓存了一些“意料之外”的消息,这时发送的指令会连同前面的消息
78  一同被NB模块解析,结果必然出错。所以在每次发送消息前加上了 AT_CMD_BEGIN */
79  os_sprintf(at_cmd, AT_CMD_BEGIN "%s" AT_LINE_END, cmd);
80  uart1_tx_buffer(at_cmd, cmd_len+2);
81 
82  /* 拷贝响应消息到响应消息数组并置位响应标志位,等待串口串接收中断调用 */
83 
84  if (res_msg != NULL) {
85  os_memcpy(response_msg, res_msg, sizeof(res_msg));
86  os_printf("response_msg is %s\n", response_msg);
87  response_flag = 1;
88  }
89 
90 }
#define os_sprintf
Definition: osapi.h:54
void uart1_tx_buffer(uint8 *buf, uint16 len)
Definition: uart.c:214
uint8 response_msg[128]
Definition: nb_bc35.c:23
uint8 response_flag
Definition: nb_bc35.c:22
#define os_printf
Definition: osapi.h:62
#define os_memcpy
Definition: osapi.h:36
unsigned char uint8
Definition: c_types.h:43
#define AT_CMD_BEGIN
Definition: nb_bc35.h:47
#define NULL
Definition: c_types.h:73
#define AT_LINE_END
Definition: nb_bc35.h:46

函数调用图:

这是这个函数的调用关系图:

void NB_ResponseTimerCb ( void *  arg)
void NB_Init ( void  )
140  {
141 
142  static uint8 cmd_order = 1;
143 
144  ESP_DEBUG("cmd_order = %d", cmd_order);
145 
146  switch (cmd_order) {
147 
148  case 0:
149  NB_SendCmd("", 1, "OK");
150  cmd_order = 1;
151  break;
152  case 1:
153  NB_SendCmd("AT", os_strlen("AT"), "OK");
154  cmd_order = 2;
155  break;
156  case 2:
158  cmd_order = 3;
159  break;
160  case 3:
162  cmd_order = 4;
163  break;
164  case 4:
166  cmd_order = 5;
167  break;
168  case 5:
170  cmd_order = 10;
171  break;
172  case 6:
174  NB_SendCmd("AT", os_strlen("AT"), "OK");
175  cmd_order = 6;
176  break;
177  default:
178  break;
179  }
180 
181 }
void NB_SendCmd(uint8 *cmd, uint8 cmd_len, uint8 *res_msg)
向NB模组发送指令
Definition: nb_bc35.c:70
#define ESP_DEBUG(fmt,...)
Definition: driver/uart.h:35
#define os_strlen
Definition: osapi.h:43
#define AT_NB_CLOSE_EDRX
Definition: nb_bc35.h:32
unsigned char uint8
Definition: c_types.h:43
#define AT_NB_OPEN_RF
Definition: nb_bc35.h:30
#define AT_NB_CGATT_ATTACH
Definition: nb_bc35.h:33
#define AT_NB_CLOSE_PSM
Definition: nb_bc35.h:31

函数调用图:

这是这个函数的调用关系图:

void NB_RxMsgHandler ( uint8 nb_msg)

NB消息处理函数

NB消息有以下几种类型:

  • 动作指令的执行情况。如“OK”、“ERROR”
  • 查询指令的查询结果

云端下发的控制命令。以“+NNMI:”为前缀

当NB发送消息来的时候,我们先判断在接收到这条消息之前有没有发送过指令,即判断响应标志位 response_flag是否被置位,再决定是否去匹配响应消息。

参数
[in]nb_msg串口接收到的NB消息
106  {
107 
108  if (response_flag != 0 ) { /* 等待响应匹配 */
109 
110  ESP_DEBUG("response_flag = 1");
111 
112  if (0 != os_strstr(nb_msg, response_msg)) {
113 
114  ESP_DEBUG("OK! response message parse!");
115 
116  os_memset(response_msg, 0, sizeof(response_msg));
117  response_flag = 0;
118 
119  NB_Init();
120 
121  return;
122  } else {
123 
124  ESP_DEBUG("ERROR! response message parse failed");
125 
126  return;
127  }
128  } else {
129 
130  ESP_DEBUG("response_flag = 0");
131 
132  /* 匹配下发消息前缀 */
133 
134  /* 命令下发 */
135 
136  }
137 
138 }
uint8 response_msg[128]
Definition: nb_bc35.c:23
uint8 response_flag
Definition: nb_bc35.c:22
#define ESP_DEBUG(fmt,...)
Definition: driver/uart.h:35
#define os_memset
Definition: osapi.h:38
#define os_strstr
Definition: osapi.h:46
void NB_Init(void)
Definition: nb_bc35.c:140
int str_to_hex ( const char *  bufin,
int  len,
char *  bufout 
)

将十六进制码流转换为字符串形式

参数
[in]bufin输入的十六进制码流
[in]len十六进制码流的长度
[out]bufout输出的十六进制字符串
返回
int
示例
bufin[0] = 0x01, bufin[1] = 0x02, bufin[2] = 0x03, len = 3, 则bufout = "010203" bufin[0] = 0x01, bufin[1] = 0x05, bufin[2] = 0x06, bufin[3] = 0x07, len = 4, 则bufout = "01050607"
197 {
198  int i = 0;
199  if (NULL == bufin || len <= 0 || NULL == bufout)
200  {
201  return -1;
202  }
203  for(i = 0; i < len; i++)
204  {
205  os_sprintf(bufout+i*2, "%02X", bufin[i]);
206  }
207  return 0;
208 }
#define os_sprintf
Definition: osapi.h:54
#define NULL
Definition: c_types.h:73
int32_t nb_set_cdpserver ( char *  host,
char *  port 
)
int32_t nb_hw_detect ( void  )
int32_t nb_get_netstat ( void  )
int nb_query_ip ( void  )
int32_t nb_send_payload ( const char *  buf,
int  len 
)
int32_t nb_check_csq ( void  )
int32_t nb_send_psk ( char *  pskid,
char *  psk 
)
int32_t nb_set_no_encrypt ( void  )
int32_t nb_reboot ( void  )
int32_t nb_recv_timeout ( int32_t  id,
uint8_t buf,
uint32_t  len,
char *  ipaddr,
int *  port,
int32_t  timeout 
)
int32_t nb_cmd_match ( const char *  buf,
char *  featurestr,
int  len 
)
void nb_step ( void  )
void nb_reattach ( void  )