@@ -9,6 +9,7 @@ LOG_MODULE_REGISTER(app_sensors, LOG_LEVEL_DBG);
99
1010#include <golioth/client.h>
1111#include <golioth/stream.h>
12+ #include <zcbor_encode.h>
1213#include <zephyr/drivers/gpio.h>
1314#include <zephyr/kernel.h>
1415
@@ -24,11 +25,7 @@ LOG_MODULE_REGISTER(app_sensors, LOG_LEVEL_DBG);
2425static struct golioth_client * client ;
2526/* Add Sensor structs here */
2627
27- /* Formatting string for sending sensor JSON to Golioth */
28- #define JSON_FMT "{\"counter\":%d}"
29-
3028/* Callback for LightDB Stream */
31-
3229static void async_error_handler (struct golioth_client * client ,
3330 const struct golioth_response * response ,
3431 const char * path ,
@@ -45,8 +42,8 @@ static void async_error_handler(struct golioth_client *client,
4542void app_sensors_read_and_stream (void )
4643{
4744 int err ;
48- char json_buf [256 ];
4945
46+ /* Golioth custom hardware for demos */
5047 IF_ENABLED (CONFIG_ALUDEL_BATTERY_MONITOR , (
5148 read_and_report_battery (client );
5249 IF_ENABLED (CONFIG_LIB_OSTENTUS , (
@@ -55,35 +52,56 @@ void app_sensors_read_and_stream(void)
5552 ));
5653 ));
5754
55+ /* Send sensor data to Golioth */
5856 /* For this demo, we just send counter data to Golioth */
59- static uint8_t counter ;
57+ static uint16_t counter ;
6058
61- /* Send sensor data to Golioth */
62- /* For this demo we just fake it */
63- snprintk (json_buf , sizeof (json_buf ), JSON_FMT , counter );
64- LOG_DBG ("%s" , json_buf );
59+ /* Encode sensor data using CBOR serialization */
60+ uint8_t cbor_buf [13 ];
6561
62+ ZCBOR_STATE_E (zse , 1 , cbor_buf , sizeof (cbor_buf ), 1 );
63+
64+ bool ok = zcbor_map_start_encode (zse , 1 ) &&
65+ zcbor_tstr_put_lit (zse , "counter" ) &&
66+ zcbor_uint32_put (zse , counter ) &&
67+ zcbor_map_end_encode (zse , 1 );
68+
69+ if (!ok ) {
70+ LOG_ERR ("Failed to encode CBOR." );
71+ return ;
72+ }
73+
74+ size_t cbor_size = zse -> payload - cbor_buf ;
75+
76+ LOG_DBG ("Streaming counter: %d" , counter );
77+
78+ /* Stream data to Golioth */
6679 err = golioth_stream_set_async (client ,
6780 "sensor" ,
68- GOLIOTH_CONTENT_TYPE_JSON ,
69- json_buf ,
70- strlen ( json_buf ) ,
81+ GOLIOTH_CONTENT_TYPE_CBOR ,
82+ cbor_buf ,
83+ cbor_size ,
7184 async_error_handler ,
7285 NULL );
7386 if (err ) {
7487 LOG_ERR ("Failed to send sensor data to Golioth: %d" , err );
7588 }
7689
90+ /* Golioth custom hardware for demos */
7791 IF_ENABLED (CONFIG_LIB_OSTENTUS , (
7892 /* Update slide values on Ostentus
7993 * -values should be sent as strings
8094 * -use the enum from app_sensors.h for slide key values
8195 */
82- snprintk (json_buf , sizeof (json_buf ), "%d" , counter );
83- slide_set (UP_COUNTER , json_buf , strlen (json_buf ));
84- snprintk (json_buf , sizeof (json_buf ), "%d" , 255 - counter );
85- slide_set (DN_COUNTER , json_buf , strlen (json_buf ));
96+ char sbuf [32 ];
97+
98+ snprintk (sbuf , sizeof (sbuf ), "%d" , counter );
99+ slide_set (UP_COUNTER , sbuf , strlen (sbuf ));
100+ snprintk (sbuf , sizeof (sbuf ), "%d" , 65535 - counter );
101+ slide_set (DN_COUNTER , sbuf , strlen (sbuf ));
86102 ));
103+
104+ /* Increment for the next run */
87105 ++ counter ;
88106}
89107
0 commit comments