Sending external requests
Misty as a security guard (part 2)
Welcome back!
In Part 1, we:
- looked at the three main capabilities Misty uses in her security guard skill
- explored the tech behind Misty’s face recognition capabilities
- learned how to code Misty to use face recognition in general and in her security guard skill
So what’s next? In Part 2 of Misty as a security guard, we learn how Misty uses web services.
In the dark ages before the IoT revolution, when you talked about an internet-connected device, you probably meant a personal computer with a web browser. These days, your light bulb might need a WiFi connection, or even your baby’s sock. And today’s internet-connected devices are more likely to be sending your workout data to the cloud than they are to be sending an email when you hit Return.
As you probably well know, much of the traffic between devices and web services doesn’t directly involve humans at all. Thanks to the magic of web APIs, developers (and therefore devices) can programmatically interact with remote servers, adding capabilities to their products as simply as you can call POST
.
An internet-connected robot like Misty is no exception to this happy collaboration. Along with her own impressive robot capabilities, Misty can use pretty much any web service you like. This is where we take Misty from a lone device to a robot who commands a fleet of web services and IoT devices to pull off anything you can imagine. Ready to see how?
Sending external requests with Misty
If you’re familiar with the conventions of REST APIs, then you already know the basics of how Misty interacts with web servers.
Misty’s API includes a function for sending HTTP requests to external resources. In Misty’s skills, you use this call to identify an HTTP verb, a uniform resource identifier (URI), and the data to send in the body or as the parameters of a request. When required, you can also send authentication information, token data, and a message about the MIME type of the expected response.
In your skill code, you call the function misty.SendExternalRequest()
to send a request to a web API. As an example of how this works, here’s how you send a GET
request to the APIXU API for data about the current weather in Paris:
If the resource requires authentication, you can replace the null
values passed as the third and fourth arguments with an authentication type and token string. The empty JSON string at the end is reserved for request parameters or a body of data to send along with the request.
With the request sent, you can set up the callback function to handle the response.
Setting up the callback
The misty.SendExternalRequest()
command includes an optional argument for naming a function to call when response data is ready. If you don’t use this argument, a default function, _SendExternalRequest()
, handles your data. You determine what this callback does with the data by defining the callback in your skill.
When the request above succeeds, the callback function receives an object that includes the response data as a JSON string. You can parse this string to use specific values from the response in your skill. In this example, the value of _data.current.condition.text
is a string with a phrase describing the weather in Paris. The misty.Debug()
command tells Misty to print a debug message with this phrase.
Security guards and external requests
In Part 1, we learned how Misty’s security guard skill uses data from face recognition events to know when a stranger is present. But for a security guard, just knowing something’s wrong isn’t enough. When Misty sees a stranger, she should do something about it.
So what reaction makes sense for a 14-inch security guard who weighs less than seven pounds? Unless you consider bruised shin bones to be an effective deterrent, Misty probably shouldn’t try to stop the intruder physically. She’ll need to think beyond the limits of her chassis. And with her cloud capabilities, that’s just what she does.
In the security guard skill, Misty uses If This Then That (IFTTT), a web-based service for mediating communication between apps and devices that don’t normally talk to each other. With IFTTT you can create conditional statements such that a specific event in one app automatically triggers an action in another. What’s more, you can associate custom events with webhooks to trigger actions by sending web requests to specific URLs.
This is how the security guard skill works. When Misty sees an intruder, she sends external requests to IFTTT webhooks for custom events named blink_intruder
and text_intruder
. The first triggers a Phillips Hue light in the room to start blinking and the second sends a warning SMS to Misty’s owner. These requests are placed in the callback function for face recognition events, within the conditional block that executes when Misty doesn’t recognize a face.
For each request made this way, the _SendExternalRequest()
callback sends a debug message containing the response data.
So you can see how, for a robot Misty’s size, it’s all about getting creative. And for Misty, this creativity is second nature. Misty — and you, as her developer — have virtually limitless options for solving problems in her skills.
What’s next?
So far we’ve covered:
- how to set up face recognition events to detect when a stranger is in the room
- how to do something about it by sending requests to web services and interacting with IoT devices
In the final post, we’ll look at Misty’s image capture capabilities. We’ll learn how to use Misty’s API to take pictures of intruders, and we’ll examine how the security guard skill uses a Raspberry Pi to print the mugshots Misty captures. Can’t wait? Check out the repo for the security guard skill on GitHub, or see what other developers in the Misty community are building.
Read this article (and others) on the Misty Robotics blog.