To automate our robot and link it to physical events, we connect a Raspberry Pi Pico W microcontroller to hardware buttons, connect it to Wifi, and program it to trigger speech over the Peerbots API.
Before we write code, let’s look at the key concepts:
A function is a reusable block of code that performs a specific action. If you find yourself writing the same code more than twice, it’s best to wrap it in a function. Functions can take inputs called parameters.
An API is a set of functions that allow developers to communicate with a service. We use the Peerbots API to trigger expressions on the robot face device programmatically.
An API Key is a unique passcode string sent with every API request to authorize and identify who is sending the message. This tells the face device which account the trigger is coming from.
/v1/send-message/{username} endpoint by entering parameters and clicking execute.You can run code to programmatically send messages to your face device:
import requests
api_key = "YOUR_API_KEY"
username = "ROBOT_FACE_USERNAME"
url = f"https://api.peerbots.org/v1/send-message/{username}"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"message": "Hello! I am a social robot."
}
response = requests.post(url, json=payload, headers=headers)
print("Response:", response.status_code, response.text)
Task
I authorize-tested the API endpoint in the documentation siteTask
I ran a test script on my computer to trigger a speech message