Posts a message from the agent to the device
Agent
Name | Type | Description |
---|---|---|
messageName | String |
The name of the message
|
data | Any |
The data to be passed to the device
|
Nothing
This method works in partnership with agent.on() to allow messages and data to be sent from the agent to its imp. For example, if the agent calls:
device.send("setspeed", data);
and the device has already called:
agent.on("setspeed", messageHandler);
then on receipt of the message setspeed
from the agent, the device’s messageHandler() function will be called and be passed the contents of the variable data, which can be any serializable Squirrel table, array, string, integer, float or boolean. See the Developer Guide Squirrel Data Serialization for Squirrel’s data serialization rules. The message handler function will always be called with exactly one parameter: the data passed to device.send(). The data parameter in both messageHandler() and device.send() is not optional. You must include a value, even if you do not plan to use it in the handler.
For a more in-depth look at the imp’s use of events and event triggers, please see Event-Driven Programming.
For more information on agent-device and device-agent messaging, please see Effective Internet-agent-device Communication.
For sample code that can be used to manage messaging between agent and device, see the imp API Cookbook recipe Agent-Device Communications.
The following agent code’s startTime() function uses device.send() to issue a ‘pong’ message to the device in response to the arrival of a ‘ping’ message from the device.