Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arduino examples #650

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@
#define SSID "SSID"
#define PASS "PASS"

#define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode
#if CLIENT_OR_PEER == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for not using the CLIENT_OR_PEER approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically the #if was ignored and compiler complained that #mode and #connect were redefined.

// Client mode values (comment/uncomment as needed)
#define MODE "client"
#define CONNECT "" // If empty, it will scout
#elif CLIENT_OR_PEER == 1
#define MODE "peer"
#define CONNECT "udp/224.0.0.225:7447#iface=en0"
#else
#error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value."
#endif
// Peer mode values (comment/uncomment as needed)
// #define MODE "peer"
// #define CONNECT "udp/224.0.0.225:7447#iface=en0"

#define KEYEXPR "demo/example/**"
#define VALUE ""
Expand Down Expand Up @@ -97,10 +93,14 @@ void setup() {
}
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);

// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) {
Serial.println("Unable to start read and lease tasks\n");
z_close(z_session_move(&s), NULL);
while (1) {
;
}
}
Serial.println("Zenoh setup finished!");

delay(300);
Expand Down
25 changes: 13 additions & 12 deletions examples/arduino/z_pub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@
#define SSID "SSID"
#define PASS "PASS"

#define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode
#if CLIENT_OR_PEER == 0
// Client mode values (comment/uncomment as needed)
#define MODE "client"
#define CONNECT "" // If empty, it will scout
#elif CLIENT_OR_PEER == 1
#define MODE "peer"
#define CONNECT "udp/224.0.0.225:7447#iface=en0"
#else
#error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value."
#endif
// Peer mode values (comment/uncomment as needed)
// #define MODE "peer"
// #define CONNECT "udp/224.0.0.225:7447#iface=en0"

#define KEYEXPR "demo/example/zenoh-pico-pub"
#define VALUE "[ARDUINO]{ESP32} Publication from Zenoh-Pico!"

z_owned_session_t s;
z_owned_publisher_t pub;
static int idx = 0;

Expand Down Expand Up @@ -64,7 +61,6 @@ void setup() {

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
z_owned_session_t s;
if (z_open(&s, z_config_move(&config), NULL) < 0) {
Serial.println("Unable to open session!");
while (1) {
Expand All @@ -73,9 +69,14 @@ void setup() {
}
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);
// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) {
Serial.println("Unable to start read and lease tasks\n");
z_close(z_session_move(&s), NULL);
while (1) {
;
}
}

// Declare Zenoh publisher
Serial.print("Declaring publisher for ");
Expand Down
24 changes: 11 additions & 13 deletions examples/arduino/z_pull.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@
#define SSID "SSID"
#define PASS "PASS"

#define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode
#if CLIENT_OR_PEER == 0
// Client mode values (comment/uncomment as needed)
#define MODE "client"
#define CONNECT "" // If empty, it will scout
#elif CLIENT_OR_PEER == 1
#define MODE "peer"
#define CONNECT "udp/224.0.0.225:7447#iface=en0"
#else
#error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value."
#endif
// Peer mode values (comment/uncomment as needed)
// #define MODE "peer"
// #define CONNECT "udp/224.0.0.225:7447#iface=en0"

#define KEYEXPR "demo/example/**"

const size_t INTERVAL = 5000;
const size_t SIZE = 3;

z_owned_session_t s;
z_owned_subscriber_t sub;
z_owned_ring_handler_sample_t handler;

void setup() {
Expand Down Expand Up @@ -65,7 +62,6 @@ void setup() {

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
z_owned_session_t s;
if (z_open(&s, z_config_move(&config), NULL) < 0) {
Serial.println("Unable to open session!");
while (1) {
Expand All @@ -74,16 +70,18 @@ void setup() {
}
Serial.println("OK");

// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) {
printf("Unable to start read and lease tasks\n");
Serial.println("Unable to start read and lease tasks\n");
z_close(z_session_move(&s), NULL);
return;
while (1) {
;
}
}

printf("Declaring Subscriber on '%s'...\n", KEYEXPR);
z_owned_closure_sample_t closure;
z_ring_channel_sample_new(&closure, &handler, SIZE);
z_owned_subscriber_t sub;
z_view_keyexpr_t ke;
z_view_keyexpr_from_str(&ke, KEYEXPR);
if (z_declare_subscriber(&sub, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&closure),
Expand Down
28 changes: 15 additions & 13 deletions examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@
#define SSID "SSID"
#define PASS "PASS"

#define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode
#if CLIENT_OR_PEER == 0
// Client mode values (comment/uncomment as needed)
#define MODE "client"
#define CONNECT "" // If empty, it will scout
#elif CLIENT_OR_PEER == 1
#define MODE "peer"
#define CONNECT "udp/224.0.0.225:7447#iface=en0"
#else
#error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value."
#endif
// Peer mode values (comment/uncomment as needed)
// #define MODE "peer"
// #define CONNECT "udp/224.0.0.225:7447#iface=en0"

#define KEYEXPR "demo/example/zenoh-pico-queryable"
#define VALUE "[ARDUINO]{ESP32} Queryable from Zenoh-Pico!"

z_owned_session_t s;
z_owned_queryable_t qable;

void query_handler(const z_loaned_query_t *query, void *arg) {
z_view_string_t keystr;
z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr);
Expand Down Expand Up @@ -88,7 +87,6 @@ void setup() {

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
z_owned_session_t s;
if (z_open(&s, z_config_move(&config), NULL) < 0) {
Serial.println("Unable to open session!");
while (1) {
Expand All @@ -97,17 +95,21 @@ void setup() {
}
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);
// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) {
Serial.println("Unable to start read and lease tasks\n");
z_close(z_session_move(&s), NULL);
while (1) {
;
}
}

// Declare Zenoh queryable
Serial.print("Declaring Queryable on ");
Serial.print(KEYEXPR);
Serial.println(" ...");
z_owned_closure_query_t callback;
z_closure_query(&callback, query_handler, NULL, NULL);
z_owned_queryable_t qable;
z_view_keyexpr_t ke;
z_view_keyexpr_from_str_unchecked(&ke, KEYEXPR);
if (z_declare_queryable(&qable, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_query_move(&callback),
Expand Down
28 changes: 15 additions & 13 deletions examples/arduino/z_sub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@
#define SSID "SSID"
#define PASS "PASS"

#define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode
#if CLIENT_OR_PEER == 0
// Client mode values (comment/uncomment as needed)
#define MODE "client"
#define CONNECT "" // If empty, it will scout
#elif CLIENT_OR_PEER == 1
#define MODE "peer"
#define CONNECT "udp/224.0.0.225:7447#iface=en0"
#else
#error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value."
#endif
// Peer mode values (comment/uncomment as needed)
// #define MODE "peer"
// #define CONNECT "udp/224.0.0.225:7447#iface=en0"

#define KEYEXPR "demo/example/**"

z_owned_session_t s;
z_owned_subscriber_t sub;

void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
Expand Down Expand Up @@ -75,7 +74,6 @@ void setup() {

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
z_owned_session_t s;
if (z_open(&s, z_config_move(&config), NULL) < 0) {
Serial.println("Unable to open session!");
while (1) {
Expand All @@ -84,17 +82,21 @@ void setup() {
}
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);
// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) {
Serial.println("Unable to start read and lease tasks\n");
z_close(z_session_move(&s), NULL);
while (1) {
;
}
}

// Declare Zenoh subscriber
Serial.print("Declaring Subscriber on ");
Serial.print(KEYEXPR);
Serial.println(" ...");
z_owned_closure_sample_t callback;
z_closure_sample(&callback, data_handler, NULL, NULL);
z_owned_subscriber_t sub;
z_view_keyexpr_t ke;
z_view_keyexpr_from_str_unchecked(&ke, KEYEXPR);
if (z_declare_subscriber(&sub, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&callback),
Expand Down
Loading