๐ปThe Town Radio System

Table of contents
- ๐ฏ The Big Idea
- ๐๏ธ What Are Topics in ROS2?
- ๐ฆ Letโs Build WeatherBot
- ๐ Step 1: Create the weather_bot Package
- ๐ Step 2: Create the Publisher Node
- ๐ง Code Breakdown (In Plain Town-Speakโข)
- ๐ง Step 3: Update setup.py
- ๐จ Step 4: Build the Town Again
- ๐งช Step 5: Run WeatherBot
- ๐ Step 6: Spy on the Radio (Without a Listener)
- ๐ก Step 7: Explore Other Topic Commands
- ๐งฐ Folder Recap: Robot Town So Far
- ๐๏ธ Mini Project: Expand WeatherBot
- ๐ What Weโve Learned
- ๐ Coming Up Next: Listening Ears (Subscribing to Topics)

๐ฏ The Big Idea
In Robot Town so far:
๐ค Each bot could speak, but to no one in particular.
๐ข Now weโre building a public radio system โ a topic โ where a bot can announce things.
๐ง Any bot that wants to listen just needs to tune in to the topic.
Now, weโll only build the broadcaster โ the publisher.
๐๏ธ What Are Topics in ROS2?
Imagine a radio station in the town square:
A bot publishes (announces) something to a topic
Anyone subscribed to that topic hears it
In ROS2:
Topics are named message channels
Bots publish messages to these channels
Others can subscribe to them (we'll do that in the next article!)
๐ฆ Letโs Build WeatherBot
Our new robot will:
Live in its own house (
weather_bot
)Publish temperature updates to a topic called
/weather
Use ROS2 messages (
std_msgs.msg.String
)
๐ Step 1: Create the weather_bot
Package
Navigate to your workspace:
cd ~/robot_town_ws/src
ros2 pkg create --build-type ament_python weather_bot --dependencies rclpy std_msgs
New workspace layout:
robot_town_ws/
โโโ src/
โโโ mayor_bot/
โโโ door_bot/
โโโ chatter_bot/
โโโ greeter_bot/
โโโ weather_bot/
โโโ weather_bot/
๐ Step 2: Create the Publisher Node
Create a new Python file:
touch ~/robot_town_ws/src/weather_bot/weather_bot/weather_publisher.py
And paste this code:
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import random
class WeatherBot(Node):
def __init__(self):
# Initialize the node with the name 'weather_bot'
super().__init__('weather_bot')
# Create a publisher object to send messages on the 'weather' topic
# Parameters explained:
# - String: the type of message we will publish (std_msgs/String)
# - 'weather': the name of the topic where messages are published
# - 10: the queue size or history depth for outgoing messages;
# if messages are sent faster than the receiver can handle,
# this limits how many messages are buffered before dropping
self.publisher = self.create_publisher(String, 'weather', 10)
# Create a timer that triggers the publish_weather method every 2 seconds
# Parameters explained:
# - 2.0: interval in seconds between timer callbacks
# - self.publish_weather: the callback function to execute on timer
self.timer = self.create_timer(2.0, self.publish_weather)
# Log an informational message that WeatherBot has started broadcasting
# Explanation of chained function:
# self.get_logger() returns the node's logging interface
# .info() sends an info-level message to the console/output
self.get_logger().info("WeatherBot is now broadcasting on /weather")
def publish_weather(self):
# Generate a random integer temperature between 15 and 35 (inclusive)
temp = random.randint(15, 35)
# Create a new String message instance to hold our temperature data
message = String()
# Set the 'data' field of the message with a formatted temperature string
message.data = f"Current temperature: {temp}ยฐC"
# Publish the message to the 'weather' topic using the publisher
# Explanation: The publisher sends the message so any subscribers
# listening on 'weather' will receive this update
self.publisher.publish(message)
# Log the announcement using the node's logger (similar chaining as above)
self.get_logger().info(f"๐ป Announced: {message.data}")
def main(args=None):
rclpy.init(args=args)
node = WeatherBot()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
๐ง Code Breakdown (In Plain Town-Speakโข)
Code | What It Does |
self.publisher = create_publisher(...) | Installs a loudspeaker in the town square |
self.timer = create_timer(...) | Says something every 2 seconds |
random.randint(...) | Fakes a temperature sensor |
String() | ROS2 message that holds simple text |
publisher.publish(message) | Shouts on the /weather topic |
rclpy.spin(node) | Keeps the bot alive and running |
๐ง Step 3: Update setup.py
Open weather_bot/
setup.py
, and add this to entry_points
:
entry_points={
'console_scripts': [
'weather_publisher = weather_bot.weather_publisher:main',
],
},
This gives us an easy terminal command to run the bot.
๐จ Step 4: Build the Town Again
cd ~/robot_town_ws
colcon build
source install/setup.bash
Why source again? It refreshes your terminal with the latest commands from the new build โ like reloading a new map in the control center.
๐งช Step 5: Run WeatherBot
Make sure your workspace is built and sourced:
cd ~/robot_town_ws
source install/setup.bash
Then run the bot using its entry point:
ros2 run weather_bot weather_publisher
You should see something like:
[INFO] [weather_bot]: WeatherBot is now broadcasting on /weather
[INFO] [weather_bot]: ๐ป Announced: Current temperature: 21ยฐC
[INFO] [weather_bot]: ๐ป Announced: Current temperature: 19ยฐC
...
๐ Congratulations!
Your bot is now shouting into the town square every few seconds, broadcasting weather reports!
๐ Step 6: Spy on the Radio (Without a Listener)
Even without a subscriber bot, we can eavesdrop using ros2 topic echo
.
Open a new WSL terminal, source the workspace again:
cd ~/robot_town_ws
source install/setup.bash
Now run:
ros2 topic echo /weather
Youโll see live messages:
data: 'Current temperature: 25ยฐC'
---
data: 'Current temperature: 30ยฐC'
---
๐ง This is like a radio technician tuning into the broadcast, just to see whatโs being aired.
๐ก Step 7: Explore Other Topic Commands
Letโs explore a few more diagnostic tools ROS2 gives us:
๐งพ List All Topics
ros2 topic list
Output:
/weather
/parameter_events
/rosout
/weather
is your broadcast. The others are ROS2 internals โ like system logs and configs.
๐ฆ Check the Message Type of a Topic
ros2 topic info /weather
Output:
Type: std_msgs/msg/String
Publisher count: 1
Subscriber count: 0
You can see how many bots are publishing or listening.
๐งฐ Folder Recap: Robot Town So Far
Hereโs how your Robot Town is shaping up:
robot_town_ws/
โโโ src/
โโโ mayor_bot/ --> Greets citizens
โโโ door_bot/ --> Opens/closes the door
โโโ greeter_bot/ --> Friendly greeter
โโโ chatter_bot/ --> Chatty bot, says hi
โโโ weather_bot/ --> Publishes weather updates
Each folder is a house for a bot, and each bot does its job as part of town life.
๐๏ธ Mini Project: Expand WeatherBot
Hereโs your challenge! ๐งช
๐ก Add humidity to the broadcast!
๐ฆ Format:
"Current temperature: 28ยฐC | Humidity: 56%"
๐ป Then run
ros2 topic echo /weather
and see the full broadcast message!
๐ Bonus: Try making the bot publish every random interval instead of a fixed 2 seconds. Use
random.uniform(1.0, 5.0)
for delays!
๐ What Weโve Learned
Concept | Town Analogy | ROS2 Term |
Public radio system | Town square loudspeaker | Topic |
Bot speaking on radio | Announcing info | Publisher |
Message format | Radio sentence | std_msgs/msg/String |
Peeking into messages | Tuning into radio | ros2 topic echo |
๐ Coming Up Next: Listening Ears (Subscribing to Topics)
What if your bots had ears to hear the town radio? In this next article, weโll give our bots the power to listen by subscribing to ROS2 topics. Meet ListenerBot, tuning in to WeatherBotโs broadcasts to stay informed.
Our fun mini project features UmbrellaBot, which springs into action and opens its umbrella whenever rain is announced. Get ready to make your bots truly reactive! โ๐
Ready to give robots ears as well as voices? ๐๐
Letโs build our first listener bot next!
Subscribe to my newsletter
Read articles from gayatri kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by