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 <micro_ros_arduino.h>
#include <WiFi.h>
#include <rcl/rcl.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <std_msgs/msg/int32.h>
// 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.