Created
October 7, 2024 02:24
-
-
Save patrickelectric/bae25eab1504a877f53ce99bf51f3653 to your computer and use it in GitHub Desktop.
Speed test with esp32-c3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <WiFi.h> | |
#include <zenoh-pico.h> | |
#define SSID "aaa" | |
#define PASS "aaa" | |
// Client mode values | |
#define MODE "peer" // Change to "peer" or "client" as needed | |
#define CONNECT "" // If empty, it will scout in 'peer' mode | |
// Base topic and suffix | |
constexpr const char* BASE_TOPIC = "demo/example/topic/"; | |
constexpr int TOPIC_SUFFIX = 1; | |
z_owned_session_t s; | |
z_owned_subscriber_t sub; | |
z_owned_publisher_t pub; | |
char topic_a[256]; | |
char topic_b[256]; | |
void data_handler(z_loaned_sample_t *sample, void *arg) { | |
(void)(arg); | |
Serial.println("data_handler called"); | |
// Extract key | |
z_view_string_t keystr; | |
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); | |
// Extract payload | |
z_owned_string_t value; | |
z_bytes_to_string(z_sample_payload(sample), &value); | |
Serial.printf(">> [Subscriber] Received ('%.*s': '%.*s')\n", | |
(int)z_string_len(z_view_string_loan(&keystr)), | |
z_string_data(z_view_string_loan(&keystr)), | |
(int)z_string_len(z_string_loan(&value)), | |
z_string_data(z_string_loan(&value))); | |
z_string_drop(z_string_move(&value)); | |
// Publish 'Potato' to topic B | |
const char* text = "Potato"; | |
z_owned_bytes_t payload; | |
z_bytes_from_static_buf(&payload, reinterpret_cast<const uint8_t*>(text), strlen(text)); | |
if (z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL) < 0) { | |
Serial.println("Error while publishing data"); | |
} else { | |
Serial.println("Sent 'Potato'!"); | |
} | |
} | |
void setup() { | |
// Initialize Serial for debugging | |
Serial.begin(115200); | |
while (!Serial) { | |
delay(100); | |
} | |
// Set WiFi in STA mode and trigger attachment | |
Serial.print("Connecting to WiFi..."); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(SSID, PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
} | |
Serial.println("OK"); | |
// Build topic names based on the single constexpr variable | |
sprintf(topic_a, "%s%d", BASE_TOPIC, TOPIC_SUFFIX); | |
sprintf(topic_b, "%s%d", BASE_TOPIC, TOPIC_SUFFIX + 1); | |
// Initialize Zenoh Session and other parameters | |
z_owned_config_t config; | |
z_config_default(&config); | |
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); | |
if (strcmp(CONNECT, "") != 0) { | |
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); | |
} | |
// Open Zenoh session with retry logic | |
Serial.print("Opening Zenoh Session..."); | |
int open_result = -1; | |
while (open_result < 0) { | |
open_result = z_open(&s, z_config_move(&config), NULL); | |
if (open_result < 0) { | |
Serial.println("Unable to open session! Retrying in 1 second..."); | |
delay(1000); | |
} else { | |
Serial.println("Session opened successfully."); | |
} | |
} | |
// Declare Zenoh subscriber for topic A with retry logic | |
Serial.print("Declaring Subscriber on "); | |
Serial.print(topic_a); | |
Serial.println(" ..."); | |
z_owned_closure_sample_t callback; | |
z_closure_sample(&callback, data_handler, NULL, NULL); | |
z_view_keyexpr_t ke_sub; | |
z_view_keyexpr_from_str_unchecked(&ke_sub, topic_a); | |
int sub_result = -1; | |
while (sub_result < 0) { | |
sub_result = z_declare_subscriber(&sub, z_session_loan(&s), z_view_keyexpr_loan(&ke_sub), | |
z_closure_sample_move(&callback), NULL); | |
if (sub_result < 0) { | |
Serial.println("Unable to declare subscriber. Retrying in 1 second..."); | |
delay(1000); | |
} else { | |
Serial.println("Subscriber declared successfully."); | |
} | |
} | |
// Declare Zenoh publisher for topic B with retry logic | |
Serial.print("Declaring Publisher on "); | |
Serial.print(topic_b); | |
Serial.println(" ..."); | |
z_view_keyexpr_t ke_pub; | |
z_view_keyexpr_from_str_unchecked(&ke_pub, topic_b); | |
int pub_result = -1; | |
while (pub_result < 0) { | |
pub_result = z_declare_publisher(&pub, z_session_loan(&s), z_view_keyexpr_loan(&ke_pub), NULL); | |
if (pub_result < 0) { | |
Serial.println("Unable to declare publisher. Retrying in 1 second..."); | |
delay(1000); | |
} else { | |
Serial.println("Publisher declared successfully."); | |
} | |
} | |
Serial.println("Zenoh setup finished!"); | |
} | |
// Process Zenoh events | |
void loop() { | |
if (zp_read(z_session_loan(&s), NULL) < 0) { | |
Serial.println("Error during zp_read"); | |
} | |
if (zp_send_keep_alive(z_session_loan(&s), NULL) < 0) { | |
Serial.println("Error sending keep alive"); | |
} | |
if (zp_send_join(z_session_loan(&s), NULL) < 0) { | |
Serial.println("Error sending join"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment