Skip to content
This repository was archived by the owner on Oct 27, 2025. It is now read-only.

Commit 7dd968c

Browse files
committed
add RPC sample
1 parent 106d8f3 commit 7dd968c

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

.github/workflows/build-samples.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ jobs:
3535
- name: Build Attributes Sample
3636
run: |
3737
west build --pristine -b nrf9160dk/nrf9160/ns thingsboard/samples/attributes
38+
39+
- name: Build RPC Sample
40+
run: |
41+
west build --pristine -b nrf9160dk/nrf9160/ns thingsboard/samples/rpc

samples/rpc/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(rpc-sample)
6+
7+
target_sources(app PRIVATE src/main.c)
8+
9+
target_compile_options(app PRIVATE
10+
-Wall
11+
-Werror
12+
-Wno-unused-parameter
13+
)

samples/rpc/prj.conf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
CONFIG_LOG=y
2+
CONFIG_SHELL=y
3+
CONFIG_SHELL_MINIMAL=y
4+
CONFIG_COAP_INIT_ACK_TIMEOUT_MS=4000
5+
6+
# Cellular connectivity
7+
CONFIG_NRF_MODEM_LIB=y
8+
CONFIG_NRF_MODEM_LIB_LOG_FW_VERSION_UUID=y
9+
CONFIG_LTE_LINK_CONTROL=y
10+
CONFIG_LTE_NETWORK_MODE_LTE_M_NBIOT=y
11+
CONFIG_LTE_MODE_PREFERENCE_LTE_M=y
12+
CONFIG_LTE_PSM_REQ=y
13+
CONFIG_LTE_EDRX_REQ=y
14+
CONFIG_NET_SOCKETS_OFFLOAD=y
15+
16+
# Requirements for Thingsboard SDK
17+
CONFIG_COAP=y
18+
CONFIG_NETWORKING=y
19+
CONFIG_NET_IPV4=y
20+
CONFIG_NET_SOCKETS=y
21+
CONFIG_JSON_LIBRARY=y
22+
CONFIG_FLASH=y
23+
CONFIG_FLASH_MAP=y
24+
CONFIG_NVS=y
25+
CONFIG_SETTINGS=y
26+
27+
# Thingsboard SDK
28+
CONFIG_THINGSBOARD=y
29+
CONFIG_THINGSBOARD_USE_PROVISIONING=n
30+
# Set server name and access token
31+
# CONFIG_COAP_SERVER_HOSTNAME=""
32+
# CONFIG_THINGSBOARD_ACCESS_TOKEN=""

samples/rpc/src/main.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <thingsboard.h>
2+
3+
#include <modem/lte_lc.h>
4+
#include <modem/nrf_modem_lib.h>
5+
#include <zephyr/kernel.h>
6+
#include <zephyr/logging/log.h>
7+
#include <zephyr/settings/settings.h>
8+
#include <zephyr/shell/shell.h>
9+
10+
#include <string.h>
11+
12+
LOG_MODULE_REGISTER(main);
13+
14+
static struct tb_fw_id fw_id = {
15+
.fw_title = "rpc-sample", .fw_version = "v1.0.0", .device_name = "sample-device"};
16+
17+
int main(void)
18+
{
19+
int err = 0;
20+
21+
LOG_INF("Initializing modem library");
22+
err = nrf_modem_lib_init();
23+
if (err) {
24+
LOG_ERR("Failed to initialize the modem library, error (%d): %s", err,
25+
strerror(-err));
26+
return err;
27+
}
28+
29+
err = lte_lc_func_mode_set(LTE_LC_FUNC_MODE_ACTIVATE_LTE);
30+
if (err) {
31+
LOG_ERR("Failed to activate LTE");
32+
return err;
33+
}
34+
35+
LOG_INF("Connecting to LTE network");
36+
err = lte_lc_connect();
37+
if (err) {
38+
LOG_ERR("Could not establish LTE connection, error (%d): %s", err, strerror(-err));
39+
return err;
40+
}
41+
LOG_INF("LTE connection established");
42+
43+
LOG_INF("Connecting to Thingsboards");
44+
err = thingsboard_init(NULL, &fw_id);
45+
if (err) {
46+
LOG_ERR("Could not initialize thingsboard connection, error (%d) :%s", err,
47+
strerror(-err));
48+
return err;
49+
}
50+
}
51+
52+
static void rpc_callback(const uint8_t *data, size_t len)
53+
{
54+
LOG_HEXDUMP_INF(data, len, "RPC repsonse: ");
55+
}
56+
57+
static int cmd_do_rpc(const struct shell *shell, size_t argc, char **argv)
58+
{
59+
int err;
60+
61+
const char *params = "{\"name\":\"gcx\"}";
62+
err = thingsboard_rpc("hello", rpc_callback, params);
63+
if (err) {
64+
LOG_ERR("Could not send RPC, error (%d): %s", err, strerror(-err));
65+
return err;
66+
}
67+
68+
return 0;
69+
}
70+
71+
SHELL_CMD_REGISTER(rpc, NULL, "Send a RPC request", cmd_do_rpc);

0 commit comments

Comments
 (0)