<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                * 導師視頻講解:[**去聽課**](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) (非商務合作**勿擾**,此處**非**技術支持)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看