micro-ROS / micro-ROS/micro_ros_arduino

Trouble establishing a udp transport between esp32 and ros2

Open
#1,954 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
576
Forks
146
PR merge metrics
No merged PRs in 30d

Description

- Hardware description: ESP32
- RTOS: FreeRTOS
- Installation type: Arduino ide
- Version or commit hash: humble

Im trying to set up a bridge between esp32 and ROS2 via udp , I dumped the below code in the arduino ide and flashed it to the esp32

```
#include
#include
#include
#include
#include
#include

// Configuration
const char* ssid = "Smooth";
const char* password = "surriyaa";
const char* agent_ip = "192.168.84.197";
const uint16_t agent_port = 8888;

// ROS2 Objects
rcl_publisher_t publisher;
std_msgs__msg__Int32 msg;
rclc_executor_t executor;
rclc_support_t support;
rcl_allocator_t allocator;
rcl_node_t node;

// Enhanced Error Handling
#define RCCHECK(fn) { \
rcl_ret_t temp_rc = fn; \
if((temp_rc != RCL_RET_OK)){ \
Serial.printf("Error in %s:%d: %d\n", __FUNCTION__, __LINE__, (int)temp_rc); \
delay(1000); \
ESP.restart(); \
}}

void setup() {
Serial.begin(115200);
while (!Serial) { delay(10); }

// 1. WiFi Connection
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
unsigned long wifiTimeout = millis() + 30000; // 30s timeout
while (WiFi.status() != WL_CONNECTED && millis() < wifiTimeout) {
delay(500);
Serial.print(".");
}

if (WiFi.status() != WL_CONNECTED) {
Serial.println("\nWiFi Failed!");
while(1) delay(1000);
}

Serial.println("\nWiFi Connected!");
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("RSSI: "); Serial.println(WiFi.RSSI());

// 2. Alternative Network Verification
Serial.println("Verifying network configuration...");
if (WiFi.localIP() == INADDR_NONE) {
Serial.println("Invalid IP Address!");
while(1) delay(1000);
}

if (WiFi.gatewayIP() == INADDR_NONE) {
Serial.println("No Gateway Found!");
while(1) delay(1000);
}

// 3. micro-ROS Initialization
Serial.println("Initializing micro-ROS transport...");
set_microros_wifi_transports((char*)ssid, (char*)password, (char*)agent_ip, agent_port);
delay(2000); // Critical delay

// 4. micro-ROS Core Init
Serial.println("Initializing micro-ROS core...");
allocator = rcl_get_default_allocator();
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
RCCHECK(rclc_node_init_default(&node, "micro_ros_esp32_node", "", &support));

// 5. Create Publisher
Serial.println("Creating publisher...");
RCCHECK(rclc_publisher_init_default(
&publisher,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
"micro_ros_esp32_publisher"
));

// 6. Create Executor
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));

Serial.println("Setup complete!");
}

void loop() {
static unsigned long last_pub = 0;
if (millis() - last_pub > 1000) {
msg.data = millis() / 1000;
RCCHECK(rcl_publish(&publisher, &msg, NULL));
Serial.print("Published: "); Serial.println(msg.data);
last_pub = millis();
}

RCCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
delay(10); // Prevent watchdog triggers
}
```

In serial monitor the result im getting is ,

Connecting to WiFi.
WiFi Connected!
IP: 192.168.84.166
Gateway: 192.168.84.123
RSSI: -20
Verifying network configuration...
Initializing micro-ROS transport...
Initializing micro-ROS core...
Error in setup:74: 1

And in the terminal inside microros workspace the code I entered is,
`ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888 -v6`

and the result im getting is,

[1744058348.463167] info | UDPv4AgentLinux.cpp | init | running... | port: 8888
[1744058348.463392] info | Root.cpp | set_verbose_level | logger setup | verbose_level: 6

I tried several methods to make the udp connection between ros2 and esp32 but its being tough, It'll be helpful if anyone let me know what Im doing wrong and how to change it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Arduino sketch's set_microros_wifi_transports and rclc_support_init calls, then compare the ESP32 network details with the micro_ros_agent udp4 command on port 8888. Done means rclc_support_init succeeds and the agent receives the publisher's messages.

Written by the indexing model from the issue text.

Assessment

Tech stack
arduino, c
Domain
embedded-iot, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.