---
title: BG77 & TB
tags: ThingsBoard
image: https://i.imgur.com/mBV4rvj.jpg
description:
disqus: hackmd
---
# BG77 & ThingsBoard

[toc]
## BG77 EVB
### Installation
1. Install ==BG77== module (GNSS ANT, MAIN ANT)
2. Install CHT SIM card
3. DIP switch (開發板右上角) to ==UART, PCM== postion (default is BT)
4. (optional) Install USB 2.0 to RS232 cable and [driver](http://www.z-tek.com.cn/data/upload/file/202005/180365fe6add53f1a49d3e46b785a9fb.rar)
5. ==J801== : Install Micro USB cable and driver ( [Windows](https://drive.google.com/file/d/1Hr-stc5smT_ilWCBYMYtwWj1_nJ3OQAG/view?usp=sharing) | [Linux](https://drive.google.com/file/d/16fViEriTPJkpOw9mssfW38dfJnwbhAB4/view?usp=sharing) )
6. ==S201== : Power switch ON
7. (optional) ==S302== : Press PWRKEY to turn on BG77 module
:::info
USB interface is used for AT command communication, data transmission, firmware upgrade and GNSS NEMA output
:::

### QCOM_V1.6 (開發板上電後,Led藍燈不規則慢閃!)
:::info
請選擇裝置管理員(Device Manager) > Modems > ==Quectel USB Modem (COM# and Port Speed)==
:::

[下載點](https://downloads.rakwireless.com/Cellular/RAK2013-NB-IoT-Pi-HAT/Tools/QCOM_V1.6.zip)

### Chunghwa Telecom – Taiwan (NB-IoT)
* [Chinese Page Overview](https://www.cht.com.tw/home/campaign/M-IoT/index.html)
|Cmd |Delay after the cmd|Num of retries|Wait time for cmd response|
|-------------------------------------|-----|---|------|
|AT+QCFG=”band”,0,4,80,1 |200ms|0 |2000ms|
|AT+QCFG=”nwscanseq”,03,1 |200 |0 |2000 |
|AT+QCFG=”iotopmode”,1 |200 |0 |2000 |
|AT+CGDCONT=1,”IPV4V6”,”internet.iot” |200 |5 |300 |
|AT+CREG? |300 |255|300 |
```
AT+QCFG=?
+QCFG: "servicedomain",(1,2),(0,1)
+QCFG: "nwscanseq",(0203),(0,1)
+QCFG: "band",(0),(0-0x100002000000000F0E189F),(0-0x10004200000000090E189F),(0,1)
+QCFG: "iotopmode",(0-2),(0,1)
+QCFG: "celevel",(0-2)
+QCFG: "urc/ri/ring",("off","pulse","always","auto","wave"),(1-2000),(1-10000),(1-10000),("off","on"),(1-5)
+QCFG: "urc/ri/smsincoming",("off","pulse","always"),(1-2000),(1-5)
+QCFG: "urc/ri/other",("off","pulse"),(1-2000),(1-5)
+QCFG: "risignaltype",("respective","physical")
+QCFG: "urc/delay",(0,1)
+QCFG: "ledmode",(0,1,3)
+QCFG: "gpio",<mode>,<pin>[,[<dir>,<pull>,<drv>]/[<val>][,<save>]]
+QCFG: "airplanecontrol",(0,1)
+QCFG: "cmux/urcport",(0-4)
+QCFG: "apready",(0,1),(0,1),(100-3000)
+QCFG: "nccconf",(0-1FF)
+QCFG: "psm/enter",(0,1)
+QCFG: "psm/urc",(0,1)
+QCFG: "simeffect",(0,1)
+QCFG: "lapiconf",(0-2),(0,1)
+QCFG: "nasconfig",(0-7FFF)
+QCFG: "apn/display",(0,1)
+QCFG: "irat/timer",(5-300),(5-20)
+QCFG: "nb1/bandprior",<band_priority_seq>
+QCFG: "bip/auth",(0-3)
+QCFG: "timer",(3402)
+QCFG: "timeupdate",(0,1)
+QCFG: "cmux/flowctrl",(0,1)
+QCFG: "fast/poweroff",<pin>,(0,1)
```
### AT Commands to setup Network Configuration
```
RDY
APP RDY
/*確認模組安裝無誤有回應*/
>at
OK
/*查看模組韌體版本*/
>at+qgmr
BG77LAR02A04_01.005.01.005
OK
>ati
Quectel
BG77
Revision: BG77LAR02A04
OK
/*確認SIM Card是否就緒*/
>at+cpin?
+CPIN: READY
OK
/* Switch off the radio module */
>AT+CFUN=0
/*請參照手冊,Queries and configures the searching sequence of RATs.*/
/*00 Automatic (eMTC > NB-IoT > GSM), 01 GSM, 02 eMTC, 03 NB-IoT03 means NB-IoT only*/
>AT+QCFG="nwscanseq",03,1
OK
/*請參照手冊,中華電信NB-IoT 是走Band 8,頻段為900MHz,Cat M1 是走Band 3,頻段為1800MHz。*/
>AT+QCFG="band",0,4,80,1
OK
/*請參照手冊,Queries and configures the network category to be searched under LTE RAT.*/
/*0 eMTC, 1 NB-IoT, 2 eMTC and NB-IoT*/
>AT+QCFG="iotopmode",1
OK
/*設定中華電信APN*/
>AT+CGDCONT=1,"IPV4V6","internet.iot"
OK
/* 儲存設定值 */
>AT&W
/* Switch on the radio module (or reboot the device) */
>AT+CFUN=1
OK
+CPIN: READY
+QUSIM: 1
+QIND: SMS DONE
```
### Useful (Query) AT Commands
```
//查詢當下使用的APN資訊
>at+cgdcont?
+CGDCONT: 1,"IPV4V6","internet.iot","0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0",0,0,0
OK
//查詢註冊狀態
>AT+CEREG?
+CEREG: 0,1
OK
>AT+CREG?
+CREG: 0,3
OK
//查詢訊號品質,回傳值第一個參數不為99,及Quectel訊號強度指令
>at+csq
+csq: 16,99
OK
>at+qcsq
+QCSQ: "NBIoT",-80,-90,132,-8
OK
//查詢網路資訊
>AT+QNWINFO
+QNWINFO: "NBIoT","46692","LTE BAND 8",3640
OK
//檢查是否有連到網路,若有連到網路,回傳值最後一參數要為9
>at+cops?
+cops: 0,0,"Chunghwa Telecom",9
OK
//Show PDP Address
>at+cgpaddr
+CGPADDR: 1,132.26.162.10,26.63.22.250.113.209.101.16.194.152.57.226.0.180.1.32
OK
//Ping Google DNS
>AT+QPING=1,"8.8.8.8"
OK
+QPING: 0,"8.8.8.8",32,620,255
+QPING: 0,"8.8.8.8",32,83,255
+QPING: 0,"8.8.8.8",32,118,255
+QPING: 0,"8.8.8.8",32,117,255
+QPING: 0,4,4,0,83,620,234
```
### GNSS
* [BG77](https://drive.google.com/file/d/1LgK9lhjUZlzhr4y9JS3IRNcnB7xu78GX/view?usp=sharing)
### BG77 MQTT + TB MQTT Gateway API
:::info
The maximum length of the data is 4096 bytes and the data that beyond 4096 bytes will be omitted.
After inputting data, tap ==Ctrl+Z== to send.
:::
* [BG77](https://drive.google.com/file/d/1UQkrzlZ0pdHWkDbjxrLvkPOuULPoaoHV/view?usp=sharing)
* [TB](https://thingsboard.io/docs/reference/gateway-mqtt-api/)
* [實測](/0a1UNafnQ-OvM2eLHsR43w)
#### QMTPUB (QMTOPEN > QMTCONN > QMTPUB > QMTCLOSE)
> QMTOPEN
```
>AT+QMTOPEN=?
+QMTOPEN: (0-5),<host_name>,(0-65535)
OK
>AT+QMTOPEN=1,"demo.thingsboard.io",1883
OK
+QMTOPEN: 1,0
```
> QMTCONN
```
>AT+QMTCONN=?
+QMTCONN: (0-5),<clientID>,<username>,<password>
OK
>AT+QMTCONN=1,"demo.thingsboard.io","101",""
OK
+QMTCONN: 1,0,0
```
> QMTPUB
```
>AT+QMTPUB=?
+QMTPUB: (0-5),(0-65535),(0-2),(0,1),<topic>,(1-4096)
OK
>AT+QMTPUB=1,0,0,0,"v1/gateway/telemetry"
> {
"UGS 000001":[
{
"ts":1629183289000,
"values":{
"battery":42,
"level":80
}
}
]
}
OK
+QMTPUB: 1,0,0
```

> QMTCLOSE
```
>AT+QMTCLOSE=?
+QMTCLOSE: (0-5)
OK
>AT+QMTCLOSE=1
OK
```
#### QMTSUB
> QMTSUB
```
//<client_idx> Integer type. MQTT client identifier. Range: 0–5.
//<msgID> Integer type. Message identifier of packet. Range: 1–65535.
//<topic> String type. The topic that the client wants to subscribe to or unsubscribe from.
//<qos> Integer type. The QoS level at which the client wants to publish the messages.
//0 At most once
//1 At least once
//2 Exactly once
>AT+QMTSUB=?
+QMTSUB: (0-5),(1-65535),<topic>,(0-2)
OK
>AT+QMTOPEN=1,"demo.thingsboard.io",1883
OK
+QMTOPEN: 1,0
>AT+QMTCONN=1,"demo.thingsboard.io","101",""
OK
+QMTCONN: 1,0,0
>AT+QMTSUB=1,1,"v1/gateway/attributes/response",1
OK
+QMTSUB: 1,1,0,1
>AT+QMTPUB=1,0,0,0,"v1/gateway/attributes/request"
> {"id": 1, "device": "UGS 000001", "client": true, "key": "fw-ver"}
OK
+QMTPUB: 1,0,0
+QMTRECV: 1,1,"v1/gateway/attributes/response","{"id":1,"device":"UGS 000001","value":"v0.0.4"}"
```

### BG77 MQTT + TB MQTT Device API
* [BG77](https://drive.google.com/file/d/1UQkrzlZ0pdHWkDbjxrLvkPOuULPoaoHV/view?usp=sharing)
* [TB](https://thingsboard.io/docs/reference/mqtt-api/)

```
/*Configure Optional Parameters of MQTT*/
AT+QMTCFG=?
+QMTCFG: "version",(0-5),(3,4)
+QMTCFG: "pdpcid",(0-5),(1-16)
+QMTCFG: "ssl",(0-5),(0,1),(0-5)
+QMTCFG: "keepalive",(0-5),(0-3600)
+QMTCFG: "session",(0-5),(0,1)
+QMTCFG: "timeout",(0-5),(1-60),(1-10),(0,1)
+QMTCFG: "will",(0-5),(0,1),(0-2),(0,1),"willtopic","willmessage"
+QMTCFG: "aliauth",(0-5),"productkey","devicename","devicesecret"
OK
>AT+QMTOPEN=1,"104.196.24.70",1883
>AT+QMTOPEN=1,"demo.thingsboard.io",1883
OK
+QMTOPEN: 1,0
>AT+QMTCONN=1,"104.196.24.70","101",""
>AT+QMTCONN=1,"demo.thingsboard.io","101",""
OK
+QMTCONN: 1,0,0
>AT+QMTPUB=1,0,0,0,"v1/devices/me/telemetry"
>{"level":"86%"}
OK
+QMTPUB: 1,0,0
```
:::info
AT+QMTDISC vs. AT+QMTCLOSE?
這2個指令是相似的功能, 客戶可以使用AT+QMTCLOSE即可, 會直接關閉socket;
AT+QMTDISC則是會發送MQTT disconnect封包給伺服器, 然後再關閉socket。
:::
>AT+QMTDISC=1
```
This command is used when a client requests a disconnection from MQTT server. A DISCONNECT
message is sent from the client to the server to indicate that it is about to close its TCP/IP connection.
```
>AT+QMTCLOSE=1
```
This command closes a network for MQTT client.
```
### MQTT + TLS
* 待更新 ...
### CoAP
* [BG77](https://drive.google.com/file/d/1PE1_GbG8ZpduXFk8e-hRCjqo8KKYAdI7/view?usp=sharing)
### CoAP + DTLS
* * 待更新 ...
### LwM2M
* [BG77](https://drive.google.com/file/d/18uPO_Fgh_RuC9z468kUgPr4LU8tKY0lB/view?usp=sharing)
* [TB](https://thingsboard.io/docs/reference/lwm2m-api/#telemetry-upload-api)
### LwM2M + DTLS
* 待更新 ...
### QFlash_V5.3 (更新BG77模組韌體)
==Quectel UMTS<E modules upgrade firmware via **USB port** by default==
[QFlash下載點](https://drive.google.com/file/d/12PnI_Ch_2jrnxcRy0zvzFwFqbATGAocv/view?usp=sharing)

* ==Quectel USB DM Port==
* Load FW Files (./update/firehose)



## ThingsBoard
:::info
* ==Attribute==
例如韌體版本、偵測器ID、所在位置等等,屬於不會變更或是變更頻率很低,且不需要保存變更前的數字。
* ==Telemetry==
記錄感測值的數據,例如溫、濕度,資料本身不需要把時間加入 (部分場景或應用可能會要求感測當下擷取時間),存在資料庫的時間是以後台的時間為主。
:::
### Device ==Authentication==
* https://thingsboard.io/docs/user-guide/device-credentials/
* Access Tokens
* Basic MQTT Credentials
* X.509 Certificates
### Message ==Encryption==
* MQTT over TLS
* LwM2M over DTLS
## Reference(s)
- https://docs.iotcreators.com/docs/quectel-bg95-m3-nusim
- https://en.na4.teamsupport.com/knowledgeBase/18027787
- https://www.twilio.com/docs/iot/supersim/cellular-modem-knowledge-base/quectel-supersim
- https://forums.quectel.com/t/bg77-nb-iot-psm-issues/7369/3
- https://github.com/johncobb/catm1_bg96
- https://forum.rakwireless.com/t/rak5860-wisblock-nb-iot-bg77-series-gnss/3522/13
- Book: LTE Cellular Narrowband Internet of Things (NB-IoT): Practical Projects for ...
By Hossam Fattah