NB-IoT

Last Updated on : 2024-11-20 02:17:38download

Tuya Connect Kit For NB-IoT is a lightweight IoT development kit designed for experienced developers. It features small memory footprint, wide applications, and great flexibility. The end devices integrated with this kit can be connected to the Tuya Developer Platform. The Kit can be applied to various use cases, such as the smart home, smart city, outdoor travel, and industrial IoT.

Architecture

NB-IoT

Integration methods

SoC or OpenCPU integrated with SDK

NB-IoT

NB-IoT

Attention (AT) module + MCU integrated with SDK

NB-IoT

AT module integrated with SDK

NB-IoT

SDK directory

├ ── examples (Reference routine)
├── include (SDK header and APIs)
├── src (SDK source code)
├── libraries (External dependencies, including CoAP, LwM2M, OneNet, and Mbed TLS)
├── middleware (LwM2M and OneNet middleware)
├── interface (SDK functional interfaces that must be ported)
├── platform (Interface adaptation for SDK porting. A routine on Linux is provided)
├── utils (General tools)
├── tuyaFilePaths.cmake
├── CMakeLists.txt
├── PortingGuide.md
├── README.md
└── LICENSE

Sequence diagram

NB-IoT

Porting guide

System

void* system_malloc(size_t n); Allocates requested memory and returns a void pointer to the allocated memory.
void* system_calloc(size_t n, size_t size);  Allocates requested memory,  returns a void pointer to the allocated memory, and initializes the allocated memory to zero.
void system_free(void *ptr);  Frees the memory allocated using `system_malloc`, `system_calloc`, or `system_realloc`.
uint32_t system_ticks();  System tick count in milliseconds.
uint32_t system_timestamp();  Gets the timestamp.
void system_sleep(uint32_t time_ms);  System delay.
uint32_t system_random(void);  Generates a system random number.

Network

If the system runs on the NB-IoT module, the hardware platform comes with the LwM2M protocol stack, for example, an NB-IoT module integrated with the OpenCPU SDK. Define the macro #define RUN_IN_NB_SIDE in tuya_iot_config.h. The SDK communicates with the server by using the internal LwM2M protocol. Your hardware platform needs to support the User Datagram Protocol (UDP) protocol stack and implement the following APIs.

INT_T tal_net_socket_create(CONST TUYA_PROTOCOL_TYPE_E type); Creates a UDP socket.
TUYA_ERRNO tal_net_connect(CONST INT_T fd, CONST TUYA_IP_ADDR_T addr, CONST UINT16_T port); Connects to the network.
TUYA_ERRNO tal_net_send(CONST INT_T fd, CONST VOID_T *buf, CONST UINT_T nbytes); Transmits network data.
TUYA_ERRNO tal_net_recv(CONST INT_T fd, VOID_T *buf, CONST UINT_T nbytes); Receives network data.
TUYA_ERRNO tal_net_close(IN CONST INT_T fd); Closes a UDP socket.
OPERATE_RET tal_net_set_block(IN CONST INT_T fd,IN CONST BOOL_T block); Sets the network data reception to blocking or nonblocking, defaulting to nonblocking. This API is not required if nonblocking is the default setting in the system.
OPERATE_RET tal_net_gethostbyname(CONST CHAR_T *domain, TUYA_IP_ADDR_T *addr);  Resolves domain names.

If the system runs on the MCU, the hardware platform does not come with the LwM2M protocol stack, for example, an NB-IoT module interfacing with an MCU. Disable the macro #define RUN_IN_NB_SIDE in tuya_iot_config.h. The SDK transmits and receives data by using the OneNet and LwM2M APIs of the NB-IoT module. The SDK takes care of activation, assembly and disassembly of datagram, and encryption and decryption. Implement the following APIs on the MCU.

CMCC:

You can use AT commands for registration.
OPERATE_RET tuya_nbiot_cmcc_register(TAL_NBIOT_LWM2M_REGISTER_T *reg_params); Registers with CMCC.
OPERATE_RET tuya_nbiot_cmcc_deregister(void); Deregisters with CMCC.
OPERATE_RET tuya_nbiot_cmcc_data_send(UINT8_T* data, UINT_T data_len); Transmits CMCC data.
OPERATE_RET tuya_nbiot_cmcc_rx_poll(void);  Polls for CMCC data.
OPERATE_RET tuya_nbiot_cmcc_lifetime_update(UINT_T lifetime); Updates CMCC lifetime.
To directly connect to the Tuya Cloud, implement the following APIs.
OPERATE_RET tal_net_gethostbyname(CONST CHAR_T *domain, TUYA_IP_ADDR_T *addr);  Resolves domain names.

CTCC:

You can use AT commands for registration.
OPERATE_RET tuya_nbiot_ctcc_lwm2m_register(TAL_NBIOT_LWM2M_REGISTER_T* reg_params); Registers with CTCC.
OPERATE_RET tuya_nbiot_ctcc_lwm2m_deregister(void); Deregisters with CTCC.
OPERATE_RET tuya_nbiot_ctcc_data_send(UINT8_T* data, UINT_T data_len); Transmits CTCC data.
OPERATE_RET tuya_nbiot_ctcc_rx_poll(void); Polls for CTCC data.
OPERATE_RET tuya_nbiot_ctcc_lifetime_update(UINT_T lifetime); Updates CTCC lifetime.

Data persistence

The SDK saves the configuration data in a persistence storage system. Implement the following key-value API for data persistence.

int local_storage_set(const char* key, const uint8_t* buffer, size_t length); Writes data to the key-value database.
int local_storage_get(const char* key, uint8_t* buffer, size_t* length); Reads data from the key-value database.
int local_storage_del(const char* key); Deletes data from the key-value database.