Skip to content

Commit

Permalink
add RPC sample
Browse files Browse the repository at this point in the history
  • Loading branch information
twendtland committed Nov 15, 2024
1 parent 106d8f3 commit 7dd968c
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ jobs:
- name: Build Attributes Sample
run: |
west build --pristine -b nrf9160dk/nrf9160/ns thingsboard/samples/attributes
- name: Build RPC Sample
run: |
west build --pristine -b nrf9160dk/nrf9160/ns thingsboard/samples/rpc
13 changes: 13 additions & 0 deletions samples/rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(rpc-sample)

target_sources(app PRIVATE src/main.c)

target_compile_options(app PRIVATE
-Wall
-Werror
-Wno-unused-parameter
)
32 changes: 32 additions & 0 deletions samples/rpc/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CONFIG_LOG=y
CONFIG_SHELL=y
CONFIG_SHELL_MINIMAL=y
CONFIG_COAP_INIT_ACK_TIMEOUT_MS=4000

# Cellular connectivity
CONFIG_NRF_MODEM_LIB=y
CONFIG_NRF_MODEM_LIB_LOG_FW_VERSION_UUID=y
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_NETWORK_MODE_LTE_M_NBIOT=y
CONFIG_LTE_MODE_PREFERENCE_LTE_M=y
CONFIG_LTE_PSM_REQ=y
CONFIG_LTE_EDRX_REQ=y
CONFIG_NET_SOCKETS_OFFLOAD=y

# Requirements for Thingsboard SDK
CONFIG_COAP=y
CONFIG_NETWORKING=y
CONFIG_NET_IPV4=y
CONFIG_NET_SOCKETS=y
CONFIG_JSON_LIBRARY=y
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y

# Thingsboard SDK
CONFIG_THINGSBOARD=y
CONFIG_THINGSBOARD_USE_PROVISIONING=n
# Set server name and access token
# CONFIG_COAP_SERVER_HOSTNAME=""
# CONFIG_THINGSBOARD_ACCESS_TOKEN=""
71 changes: 71 additions & 0 deletions samples/rpc/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <thingsboard.h>

#include <modem/lte_lc.h>
#include <modem/nrf_modem_lib.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>
#include <zephyr/shell/shell.h>

#include <string.h>

LOG_MODULE_REGISTER(main);

static struct tb_fw_id fw_id = {
.fw_title = "rpc-sample", .fw_version = "v1.0.0", .device_name = "sample-device"};

int main(void)
{
int err = 0;

LOG_INF("Initializing modem library");
err = nrf_modem_lib_init();
if (err) {
LOG_ERR("Failed to initialize the modem library, error (%d): %s", err,
strerror(-err));
return err;
}

err = lte_lc_func_mode_set(LTE_LC_FUNC_MODE_ACTIVATE_LTE);
if (err) {
LOG_ERR("Failed to activate LTE");
return err;
}

LOG_INF("Connecting to LTE network");
err = lte_lc_connect();
if (err) {
LOG_ERR("Could not establish LTE connection, error (%d): %s", err, strerror(-err));
return err;
}
LOG_INF("LTE connection established");

LOG_INF("Connecting to Thingsboards");
err = thingsboard_init(NULL, &fw_id);
if (err) {
LOG_ERR("Could not initialize thingsboard connection, error (%d) :%s", err,
strerror(-err));
return err;
}
}

static void rpc_callback(const uint8_t *data, size_t len)
{
LOG_HEXDUMP_INF(data, len, "RPC repsonse: ");
}

static int cmd_do_rpc(const struct shell *shell, size_t argc, char **argv)
{
int err;

const char *params = "{\"name\":\"gcx\"}";
err = thingsboard_rpc("hello", rpc_callback, params);
if (err) {
LOG_ERR("Could not send RPC, error (%d): %s", err, strerror(-err));
return err;
}

return 0;
}

SHELL_CMD_REGISTER(rpc, NULL, "Send a RPC request", cmd_do_rpc);

0 comments on commit 7dd968c

Please sign in to comment.