ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
* 导师视频讲解:[**去听课**](https://www.bilibili.com/video/BV1Cb4y1171H?p=16) >[success] **技术支持说明:** >**1**.一般以自主学习为主 > **2**.可到官方问答社区中提问:[**去提问**](https://bbs.csdn.net/forums/zigbee) > **3**.工程师**会尽快**解答社区问题,但他们是一线开发,【**难以保证**】解答时效,解答辛苦,感谢理解! <br/> ## **必读说明** 本课程**属于课外篇**,其内容较为复杂且有一定难度,因此: 1. 需要先学完以下课程,否则请停止学习! 《NB-IoT开发指南》 《STM32开发指南》 2. 我司原则上不为本项目提供技术支持,恳请理解! <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) (非商务合作**勿扰**,此处**非**技术支持)