* 导师视频讲解:[**去听课**](https://www.bilibili.com/video/BV1Cb4y1171H?p=16)
>[success] **技术支持说明**
> 1.**客服**提供简单的技术支持,一般自主学习为主
> 2.可到官方问答社区中提问:[**去提问**](https://bbs.csdn.net/forums/nb-iot)
> 3.工程师**会尽快**解答社区问题,但他们是一线开发,【**难以保证**】解答时效,解答辛苦,感谢理解!
<br/>
>[danger] 本项目为课外学习内容,其内容较为复杂且有一定难度,因此:**未学习前面章节者**,**请止步**!
<br/>
## **数据通信任务说明**
打开task_nbiot.c 文件,可以看到基于NB-IoT的数据通信任务,代码如下:
```
#include "task_nbiot.h"
#include "svc_plugins.h"
#include "svc_log.h"
#include "svc_task.h"
#include "svc_msg.h"
#include <stdio.h>
#include <string.h>
enum {
TASK_NBIOT_AT_TEST = 0,
TASK_NBIOT_AT_TEST_RSP,
TASK_NBIOT_DISABLE_PSM,
TASK_NBIOT_DISABLE_PSM_RSP,
TASK_NBIOT_SET_BAND,
TASK_NBIOT_SET_BAND_RSP,
TASK_NBIOT_DHCP,
TASK_NBIOT_DHCP_RSP,
TASK_NBIOT_MQTT_OPEN,
TASK_NBIOT_MQTT_OPEN_RSP,
TASK_NBIOT_MQTT_CONNECT,
TASK_NBIOT_MQTT_CONNECT_RSP,
TASK_NBIOT_GET_TEMP_HUMI,
TASK_NBIOT_MQTT_PRE_SEND,
TASK_NBIOT_MQTT_SEND,
TASK_NBIOT_MQTT_CLOSE,
TASK_NBIOT_FINISH,
};
static void taskNbiotRunner(uint8_t, void *);
static void taskNbiotDebug(const uint8_t *msg, uint8_t len);
static int taskNbiotDefaultCheck(void);
static int taskNbiotDHCPCheck(void);
static int taskNbiotMqttOpenCheck(void);
static int taskNbiotMqttConnectCheck(void);
void taskNbiotInit()
{
svcLogWriteLcd(!(0), (uint8_t *)"Task: NBIoT", 0, 0, 0);
svcTaskAdd(2000, 1, taskNbiotRunner, 0);
}
void taskNbiotRunner(uint8_t id, void *args)
{
int temp, humi;
char dbg[64], data[64];
const char *topic = "topic/pub";
static int taskNbiotCnt = 0;
static int taskNbiotStep = TASK_NBIOT_AT_TEST;
switch (taskNbiotStep) {
/* AT Test */
case TASK_NBIOT_AT_TEST:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: AT", 0, 0, 0);
svcMsgWriteString("AT\r\n");
taskNbiotStep++;
break;
/* Response: AT Test */
case TASK_NBIOT_AT_TEST_RSP:
svcLogWriteLcd(!(0), (uint8_t *)"RSP: AT", 0, 0, 0);
if (taskNbiotDefaultCheck() == 0) taskNbiotStep++;
else taskNbiotStep--;
break;
/* Disable PSM */
case TASK_NBIOT_DISABLE_PSM:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: QSCLK=0", 0, 0, 0);
svcMsgWriteString("AT+QSCLK=0\r\n");
taskNbiotStep++;
break;
/* Response: Disable PSM */
case TASK_NBIOT_DISABLE_PSM_RSP:
svcLogWriteLcd(!(0), (uint8_t *)"RSP: QSCLK=0", 0, 0, 0);
if (taskNbiotDefaultCheck() == 0) taskNbiotStep++;
else taskNbiotStep--;
break;
/* Set Band */
case TASK_NBIOT_SET_BAND:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: QBAND=8", 0, 0, 0);
svcMsgWriteString("AT+QBAND=1,8\r\n");
taskNbiotStep++;
break;
/* Response: Set Band */
case TASK_NBIOT_SET_BAND_RSP:
svcLogWriteLcd(!(0), (uint8_t *)"RSP: QBAND=8", 0, 0, 0);
if (taskNbiotDefaultCheck() == 0) taskNbiotStep++;
else taskNbiotStep--;
break;
/* DHCP */
case TASK_NBIOT_DHCP:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: DHCP", 0, 0, 0);
svcMsgWriteString("AT+CGPADDR?\r\n");
taskNbiotStep++;
taskNbiotCnt = 0;
break;
/* Response: DHCP */
case TASK_NBIOT_DHCP_RSP:
svcLogWriteLcd(!(0), (uint8_t *)"RSP: DHCP", 0, 0, 0);
if (++taskNbiotCnt > 2) {
if (taskNbiotDHCPCheck() == 0) taskNbiotStep++;
else taskNbiotStep--;
}
break;
/* MQTT Open */
case TASK_NBIOT_MQTT_OPEN:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: QMTOPEN", 0, 0, 0);
svcMsgWriteString("AT+QMTOPEN=0,\"1.15.27.206\",1883\r\n");
taskNbiotStep++;
taskNbiotCnt = 0;
break;
/* Response: MQTT Open */
case TASK_NBIOT_MQTT_OPEN_RSP:
svcLogWriteLcd(!(0), (uint8_t *)"RSP: QMTOPEN", 0, 0, 0);
if (++taskNbiotCnt > 2) {
if (taskNbiotMqttOpenCheck() == 0) taskNbiotStep++;
else taskNbiotStep--;
}
break;
/* MQTT Connect */
case TASK_NBIOT_MQTT_CONNECT:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: QMTCONN", 0, 0, 0);
svcMsgWriteString("AT+QMTCONN=0,\"iotdevice\"\r\n");
taskNbiotStep++;
taskNbiotCnt = 0;
break;
/* Response: MQTT Connect */
case TASK_NBIOT_MQTT_CONNECT_RSP:
svcLogWriteLcd(!(0), (uint8_t *)"RSP: QMTCONN", 0, 0, 0);
if (++taskNbiotCnt > 3) {
if (taskNbiotMqttConnectCheck() == 0) taskNbiotStep++;
else taskNbiotStep = TASK_NBIOT_MQTT_CLOSE;
}
break;
/* Get Temp and Humi */
case TASK_NBIOT_GET_TEMP_HUMI:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: Temp&Humi", 0, 0, 0);
if (svcPluginsGetTempHumi(&temp, &humi) == 0) {
sprintf(dbg, "T: %dC, H: %d%%", temp, humi);
svcLogWriteLcd(!(0), (uint8_t *)"CMD: Temp&Humi", (uint8_t *)dbg, 0, 0);
taskNbiotStep++;
}
break;
/* MQTT Pre-Send */
case TASK_NBIOT_MQTT_PRE_SEND:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: QMTPUB", 0, 0, 0);
sprintf(data, "{\"temp\":%d,\"humi\":%d}", temp, humi);
sprintf(dbg, "AT+QMTPUB=0,0,0,0,\"%s\",%d\r\n", topic, strlen(data));
svcMsgWriteString(dbg);
taskNbiotStep++;
break;
/* MQTT Send */
case TASK_NBIOT_MQTT_SEND:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: SEND", 0, 0, 0);
svcMsgWriteString(data);
taskNbiotStep++;
break;
/* MQTT Close */
case TASK_NBIOT_MQTT_CLOSE:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: CLOSE", 0, 0, 0);
svcMsgWriteString("AT+QMTDISC=0\r\n");
taskNbiotStep++;
break;
/* Finish */
case TASK_NBIOT_FINISH:
svcLogWriteLcd(!(0), (uint8_t *)"CMD: Finish", 0, 0, 0);
taskNbiotStep++;
break;
default: break;
}
}
void taskNbiotDebug(const uint8_t *msg, uint8_t len)
{
uint8_t str[16 + 1];
memset(str, 0, sizeof(str));
for (uint8_t i = 0, j = 0, k = 2; i < len && k < 5; i++) {
str[j++] = msg[i];
if ((i != 0 && i % 15 == 0) || i + 1 == len) {
if (k == 2) svcLogWriteLcd(0, 0, str, 0, 0);
else if (k == 3) svcLogWriteLcd(0, 0, 0, str, 0);
else if (k == 4) svcLogWriteLcd(0, 0, 0, 0, str);
j = 0;
k++;
memset(str, 0, sizeof(str));
}
}
}
int taskNbiotDefaultCheck()
{
uint16_t len;
uint8_t buf[SVC_MSG_BUF_MAX + 1];
memset(buf, 0, sizeof(buf));
len = svcMsgRead(buf, sizeof(buf));
if (len == 0) return -1;
taskNbiotDebug(buf, len);
if (strstr((char *)buf, "OK") != NULL) return 0;
return -1;
}
int taskNbiotDHCPCheck()
{
uint16_t len;
uint8_t buf[SVC_MSG_BUF_MAX + 1];
memset(buf, 0, sizeof(buf));
len = svcMsgRead(buf, sizeof(buf));
if (len == 0) return -1;
taskNbiotDebug(buf, len);
if (strstr((char *)buf, "OK") == NULL) return -1;
char *start = strstr((char *)buf, "+CGPADDR: ");
if (start == NULL) return -1;
char *ip = start + 13;
char *end = strchr(ip, '"');
if (end == NULL) return -1;
*end = 0;
if (strlen(ip) < 7) return -1;
return 0;
}
int taskNbiotMqttOpenCheck()
{
uint16_t len;
uint8_t buf[SVC_MSG_BUF_MAX + 1];
memset(buf, 0, sizeof(buf));
len = svcMsgRead(buf, sizeof(buf));
if (len == 0) return -1;
taskNbiotDebug(buf, len);
if (strstr((char *)buf, "+QMTOPEN: ") != NULL) return 0;
return -1;
}
int taskNbiotMqttConnectCheck()
{
uint16_t len;
uint8_t buf[SVC_MSG_BUF_MAX + 1];
memset(buf, 0, sizeof(buf));
len = svcMsgRead(buf, sizeof(buf));
if (len == 0) return -1;
taskNbiotDebug(buf, len);
if (strstr((char *)buf, "+QMTCONN: ") != NULL) return 0;
return -1;
}
```
<br/>
<br/>
## **商务合作**
如有以下需求,可扫码添加管理员好友,注明“**商务合作**”
* 项目定制开发,技术范围:**NB-IoT**、**CATn(4G)**、**WiFi**、**ZigBee**、**BLE Mesh**以及**STM32**、**嵌入式Linux**等;
* 入驻平台,成为讲师;
* 接项目赚外快;
* 善学坊官网:[www.sxf-iot.com](https://www.sxf-iot.com/)
![](https://img.kancloud.cn/ca/73/ca739f92cab220a3059378642e3bd502_430x430.png =150x)
(非商务合作**勿扰**,此处**非**技术支持)
- 课程介绍
- 配套资源下载
- 配套开发套件简介
- 简介
- 硬件组成 & 技术参数
- 电路原理图 & PCB图
- 拨码开关使用说明
- 第一部分:无线通信 开发指南
- 1.1.1 NB-IoT:技术简介
- 1.1.2 NB:CH34x USB转串口驱动安装
- 1.1.3 NB:AT 指令开发与测试
- 1.1.4 NB:基础指令集简介
- 1.1.5 NB:云端服务器
- 1.1.5.1 PuTTY 简介与安装
- 1.1.5.2 登录云端服务器
- 1.1.6 NB:移远官方工具简介
- 1.1.7 NB:使用UDP协议与云端服务器通信
- 1.1.8 NB:使用TCP协议与云端服务器通信
- 1.1.9 NB:使用MQTT协议与云端服务器通信
- 进阶课程
- 第二部分:STM32 开发指南
- 2.1 搭建开发环境
- 2.1.1 Keil MDK 简介与安装
- 2.1.2 STM32 Pack 简介与安装
- 2.1.3 CH34x 驱动简介与安装
- 2.1.4 其他开发工具
- 2.2 STM32 开发基础
- 2.2.1 新建工程
- 2.2.2 实现第1个程序
- 2.2.3 Hex 文件烧录详解
- 2.3 移植官方标准工程模板
- 2.4 GPIO实验——LED灯
- 2.5 系统延时应用
- 2.6 GPIO实验——按键
- 2.7 GPIO中断实验——按键触发
- 2.8 使用定时器TIM3
- 2.9 串口通信实验
- 2.10 ADC 实验
- 2.11 OLED显示器实验
- 2.12 SDK 设计思想
- 2.13 SDK 架构解析
- 2.14 多任务应用
- 2.15 输入型任务:按键输入
- 2.16 输入型任务:串口接收
- 课外篇:项目实战
- 基于STM32+NB-IoT的温湿度采集
- 系统简介
- 系统搭建
- 系统详解
- 1.代码编译与架构说明
- 2.DHT11温湿度传感器
- 3. 数据通信任务说明
- 版权声明与免责声明