Connect with Python
Send a message
Let's have your robot send its first message to the cloud! We'll start by sending a GPS position from your robot. Create a file called robot.py
and copy in the code below.
This code sample sends latitude and longitude data of type sensor_msgs/NavSatFix
to the /location
topic.
from freedomrobotics.link import Link
# Connect to the cloud with Link
# you can instantiate multiple Link objects by using different names (core, my_node1)
freedom = Link("core")
# Send your GPS position
freedom.message("/location", \
"sensor_msgs/NavSatFix", \
{"latitude": 37.778454,"longitude": -122.389171})
Next, run your code with python robot.py
. The message will show up in the Freedom App in your STREAM dashboard, showing a map with the location of your device.
In addition to the freedom.message
method for sending data, the Python module has a freedom.log
method for logging and a freedom.image
method for sending images. You can read more in the Python module reference.
Receive a message
Let's now send a simple command to your robot.
Set up a callback
To prepare your robot to receive and respond to a command, we'll need to set up a callback. Create a file called callback.py
and copy in the code below.
from time import sleep
from freedomrobotics.link import Link
keep_alive = True
# Create a callback to handle remote messages
def callback(msg):
global keep_alive
freedom.log("info", "I heard " + str(msg) )
if msg["topic"] == "/commands":
if msg["message"]["data"] == "mission":
freedom.log("info", "Running mission...")
elif msg["message"]["data"] == "shutdown":
freedom.log("info", "Shutting down...")
keep_alive = False
# Connect to the cloud with Link
freedom = Link(name="Core", command_callback=callback)
# Run until told to shut down
while keep_alive:
sleep(1)
Run the code with python callback.py
. Your robot is now waiting patiently for a remote command.
Test the callback
To check that the callback works, we'll send a command to your device using Freedom's REST API.
If you haven't already, click Log In in the top right corner of this page so we can populate the values for your token, secret, account, and device for this example.
To create a token, proceed to MENU → TEAM SETTINGS and click the TOKENS button (this might take a few moments). You'll see options to create DEVICE, USER, SHARING, and CUSTOM tokens. For this example:
- Select the Custom Tab.
- Click Create.
A modal will appear to select the REST calls available to you. Ensure PUT is selected for this example.


- Create a file on your machine called test.sh.
- Populate the file with the example curl command below.
TOKEN="<<USER_TOKEN>>"
SECRET="<<USER_SECRET>>"
ACCOUNT="<<ACCOUNT>>"
DEVICE="<<DEVICE>>"
HEADERS="-H content-type:application/json -H mc_token:$TOKEN -H mc_secret:$SECRET"
DATA="[{
\"platform\": \"custom\",
\"utc_time\": `date +%s`,
\"topic\": \"/commands\",
\"type\": \"std_msgs/String\",
\"expiration_secs\": 60,
\"message\": {\"data\":\"mission\"}
}]"
curl -v $HEADERS -d "$DATA" -X PUT "<<API_URL>>accounts/$ACCOUNT/devices/$DEVICE/commands"
- Run the file using bash.
bash test.sh
The message will be received in the callback you set up. Once your robot receives the command, it will log Running mission...
.

Change mission
to shutdown
and send a new command. Your application will now shut down remotely.
Success! You now know how to use Freedom to upload data and communicate with your robot through the cloud.
What's next?
If you would like to read walkthroughs of usage for key aspects of the system, the guides for Monitoring, Alerts and Integrations, and Teleop and Control are a great place to start.
If you prefer reviewing detailed API specs and references, you can do that also.
If you like to poke around applications, go to the Freedom App and explore!
Updated about 4 years ago