Skip to content

Commit

Permalink
v0.5.0 API update (#6)
Browse files Browse the repository at this point in the history
* migrate to NCS 2.1 and Golioth v0.5.0 API

* revise LED behavior on connection and reading

* unused code cleanup

* add lightdb state functionality
  • Loading branch information
ChrisGammell authored Dec 19, 2022
1 parent 9638c84 commit 091ca1e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 48 deletions.
5 changes: 4 additions & 1 deletion prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
CONFIG_GOLIOTH=y
CONFIG_GOLIOTH_SYSTEM_CLIENT=y


CONFIG_LOG_BACKEND_GOLIOTH=y
CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=2048

Expand Down Expand Up @@ -90,6 +91,8 @@ CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y

CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192

CONFIG_GOLIOTH_SYSTEM_CLIENT_LOG_LEVEL_DBG=n

CONFIG_OPENTHREAD_DEBUG=n
104 changes: 59 additions & 45 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#include <dk_buttons_and_leds.h>
#include <logging/log.h>
#include <zephyr/logging/log.h>

#include <drivers/uart.h>
#include <zephyr/drivers/uart.h>
#include <usb/usb_device.h>

#include <net/golioth/system_client.h>

#include <net/openthread.h>
#include <openthread/thread.h>

#include <drivers/sensor.h>
#include <zephyr/drivers/sensor.h>
#include <device.h>

#include <init.h>



LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
LOG_MODULE_REGISTER(red_demo_main, LOG_LEVEL_DBG);

#define CONSOLE_LABEL DT_LABEL(DT_CHOSEN(zephyr_console))
#define OT_CONNECTION_LED DK_LED2
#define OT_CONNECTION_LED DK_LED1

int sensor_interval = 60;
int counter = 0;
Expand All @@ -33,19 +33,18 @@ static struct k_work on_disconnect_work;

static struct golioth_client *client = GOLIOTH_SYSTEM_CLIENT_GET();

static void golioth_on_message(struct golioth_client *client,
struct coap_packet *rx)
{
uint16_t payload_len;
const uint8_t *payload;
uint8_t type;
static K_SEM_DEFINE(connected, 0, 1);

type = coap_header_get_type(rx);
payload = coap_packet_get_payload(rx, &payload_len);

if (!IS_ENABLED(CONFIG_LOG_BACKEND_GOLIOTH) && payload) {
LOG_HEXDUMP_DBG(payload, payload_len, "Payload");
static int sensor_push_handler(struct golioth_req_rsp *rsp)
{
if (rsp->err) {
LOG_WRN("Failed to push sensor: %d", rsp->err);
return rsp->err;
}
dk_set_led_off(DK_LED2);
LOG_DBG("Sensor successfully pushed");
return 0;
}

// This work function will submit a LightDB Stream output
Expand Down Expand Up @@ -91,47 +90,49 @@ void my_sensorstream_work_handler(struct k_work *work)
sensor_value_to_double(&temp)
);


// printk("string being sent to Golioth is %s\n", sbuf);

err = golioth_lightdb_set(client,
GOLIOTH_LIGHTDB_STREAM_PATH("redSensor"),
COAP_CONTENT_FORMAT_TEXT_PLAIN,
sbuf,
strlen(sbuf));
// Async send data to the cloud, get confirmation and call the push handler
err = golioth_stream_push_cb(client, "redSensor",
GOLIOTH_CONTENT_FORMAT_APP_JSON,
sbuf, strlen(sbuf),
sensor_push_handler, NULL);
if (err) {
LOG_WRN("Failed to send sensor: %d", err);
printk("Failed to send sensor: %d\n", err);
}

LOG_DBG("LED off");
dk_set_led_on(DK_LED1);
dk_set_led_on(DK_LED2);


}

K_WORK_DEFINE(my_sensorstream_work, my_sensorstream_work_handler);


// This work function will take a sensor reading
// This work function initiates a sensor reading
// And kick off a LightDB stream event
// It should be called every time the timer fires

void my_sensor_work_handler(struct k_work *work)
{

LOG_DBG("LED on, taking sensor readings");

dk_set_led_off(DK_LED2); // Inverted logic

dk_set_led_on(DK_LED2);
k_work_submit(&my_sensorstream_work);


}

K_WORK_DEFINE(my_sensor_work, my_sensor_work_handler);

static int counter_set_handler(struct golioth_req_rsp *rsp)
{
if (rsp->err) {
LOG_WRN("Failed to set counter: %d", rsp->err);
return rsp->err;
}

LOG_DBG("Counter successfully set");

return 0;
}


void my_timer_handler(struct k_timer *dummy) {

char sbuf[sizeof("4294967295")];
Expand All @@ -141,14 +142,15 @@ void my_timer_handler(struct k_timer *dummy) {

LOG_INF("Interval of %d seconds is up, taking a reading", sensor_interval);

err = golioth_lightdb_set(client,
GOLIOTH_LIGHTDB_PATH("number_of_timed_updates"),
COAP_CONTENT_FORMAT_TEXT_PLAIN,
sbuf, strlen(sbuf));
err = golioth_lightdb_set_cb(client, "counter",
GOLIOTH_CONTENT_FORMAT_APP_JSON,
sbuf, strlen(sbuf),
counter_set_handler, NULL);
if (err) {
LOG_WRN("Failed to update counter: %d", err);
LOG_WRN("Failed to set counter: %d", err);
return;
}

counter++;


Expand All @@ -159,21 +161,26 @@ void my_timer_handler(struct k_timer *dummy) {
K_TIMER_DEFINE(my_timer, my_timer_handler, NULL);


static void golioth_on_connect(struct golioth_client *client)
{
k_sem_give(&connected);

LOG_INF("Connected to Golioth!");
}

static void on_ot_connect(struct k_work *item)
{
ARG_UNUSED(item);

// dk_set_led_off(OT_CONNECTION_LED); // Turn LED off when connected

client->on_message = golioth_on_message;
golioth_system_client_start();
dk_set_led_off(OT_CONNECTION_LED); // Turn LED off when connected

}

static void on_ot_disconnect(struct k_work *item)
{
ARG_UNUSED(item);

// dk_set_led_on(OT_CONNECTION_LED); // Turn LED on when NOT connected
dk_set_led_on(OT_CONNECTION_LED); // Turn LED on when NOT connected
}


Expand Down Expand Up @@ -284,13 +291,20 @@ void main(void)
return;
}

//Turn on LED 1 while connecting
dk_set_led_on(OT_CONNECTION_LED);

k_work_init(&on_connect_work, on_ot_connect);
k_work_init(&on_disconnect_work, on_ot_disconnect);

openthread_set_state_changed_cb(on_thread_state_changed);
openthread_start(openthread_get_default_context());

dk_set_led_off(DK_LED1);
client->on_connect = golioth_on_connect;
golioth_system_client_start();

k_sem_take(&connected, K_FOREVER);

dk_set_led_off(DK_LED2);

k_timer_start(&my_timer, K_SECONDS(sensor_interval), K_SECONDS(sensor_interval));
Expand Down
4 changes: 2 additions & 2 deletions west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ manifest:
- name: nrf
repo-path: sdk-nrf
remote: nrfconnect
revision: v1.9.1
revision: v2.1.0
import:
path-prefix: deps
name-allowlist:
Expand All @@ -35,7 +35,7 @@ manifest:
# Golioth repository.
- name: golioth
path: deps/modules/lib/golioth
revision: v0.2.0
revision: v0.5.0
url: https://github.com/golioth/zephyr-sdk.git

# QCBOR
Expand Down

0 comments on commit 091ca1e

Please sign in to comment.