Using ngrok with ROS
Few days ago i have discovered ngrok and if this your first time hearing about it. Get ready because it’s a life changer. ngrok allows you to expose a web server running on your local machine behind NATs and firewalls to the internet. No more port forwarding via routers and all this hassle. Using just a simple command as
1
ngrok tcp 22
You will be able to ssh into your machine from anywhere in the world. For me this is very helpful to access all the robots im working with remotely. And it’s not always possible to find someone on the other side who can do the port forwarding thing from the router.
In this blog post i will show you how to use ngrok to expose your ROS bridge. But first what is ROS bridge?
Rosbridge provides a JSON API to ROS functionality for non-ROS programs. There are a variety of front ends that interface with rosbridge, including a WebSocket server for web browsers to interact with.
ngrok_ros
I’m working on a Robotics project with some Web developers to develop a remote dashboard for the robots. So we are using Rosbridge heavily in this project. I thought why not to create a ROS package to act as ngrok bridge.
This package offers a ROS service to create tunnels using ngrok. You can clone it to your workspace from here and build it easily using catkin_make or catkin_build. Then you can run it using rosrun
1
rosrun ngrok_ros ngrok_ros_bridge.py
Now it’s ready to receive requests on the ROS service /ngrok_ros/start_tunnel or a String message on the topic /ngrok_ros/start_tunnel. The String message data should be in this format address,protocol,name. And the service is using the ROS service StartTunnel in the ngrok_ros package. Which also has this format.
1
2
3
4
5
string addr
string proto
string name
---
string public_url
For example you can call it using the command line to create tunnel like this.
1
2
3
rosservice call /ngrok_ros/start_tunnel "addr: '9090'
proto: 'tcp'
name: 'ros_bridge'"
and you should receive back the public url which is something like this.
tcp://2.tcp.ngrok.io:17661 Now let’s start the ROS bridge on the same port and use the ngrok public port as an external port for the ros bridge.
1
roslaunch rosbridge_server rosbridge_websocket.launch _port:=9090 websocket_external_port:=17661 --screen
WebViz
You can visualize your ROS topics using Webviz to verify that everything is working as expected. Just replace the public url at the end of the following link.
https://webviz.io/app/?rosbridge-websocket-url=ws://2.tcp.ngrok.io:17661
Comments powered by Disqus.