Skip to main content

device.onconnect(callback)

Registers a function that will be called when the device connects to the agent

Availability

Agent

Parameters

Name Type Description
callback Function The function that will be called automatically when the imp-enabled device connects

Returns

Nothing

Description

This method is used to specify a function which which will be called automatically whenever an agent is notified that its paired device has connected to the server. The registered function has no parameters.

Only one callback function can be registered at any one time — registering another callback will stop the current one from being called. In the agent’s initial state, no device.onconnect() callback is registered. Until the agent is restarted, a registered callback can’t be removed, only replaced.

Be aware that device connection starts early in its start-up cycle, so this method may trigger before the Squirrel virtual machine is fully up and running on the device. Bear this in mind if you intend to use the callback to send data to the device: the device’s incoming data handler function may not yet be running when the data is sent.

Example Code

The following agent code registers two in-line callback functions that log, respectively, the string "Device connected to agent" and "Device disconnected from agent" whenever these events takes place. The device code sets the imp to go into a deep sleep after ten seconds. Deep sleep lasts for 20 seconds, after which the imp warm boots. Upon a warm boot, the imp is disconnected and remains in that stats until Squirrel attempts to contact the server — which here takes place in the first line of the device code.