Send an event to the Device as soon as it's connected

Hello,

I was trying to figure out how to make the Agent to send an event to the Device as soon as it’s connected. Here’s the problem. I want to “start” the application running on my STM32 board as soon as the device is connected to the Agent. So I thought to check when the device is connected from the agent with:

device.onconnect

and sends an event to the device, so the onconnect callback function become:

device.onconnect( function () { device.send(“StartEvent”, “string”); });

and in the device there is something like:

agent.on( “StartEvent”, SerialTransmit );

where SeriaqlTransmit is a function in the device that sends the string to the serial port of the STM32 board.
I thought this worked but it didn’t. When the callback function called an error is generated in the Device side:

ERROR: no handler for device.send()

But I am sure that the event handler is set in the device side.

Is there something I am not getting?
Thanks.

This is because the agent.on() hasn’t yet registered with the device Squirrel at the point at which the “StartEvent” message arrives.

device.onconnect() fires as soon as the device connects, which is not necessarily the point at which the device’s Squirrel VM has reached the agent.on() line.

FWIW, for this kind of thing, I get the device to send a message to the agent that it’s ready (ie. code is running, and functions and variables have been set up etc) then the agent messages back to say ‘start sending data’ (or whatever). Possibly not the quickest way, but it seems to work.

“The chicken and the egg” problem…

Synchronisation between device and agent has been tricky for me too. If you are designing something robust, in your test cases you’ll need to see how the pair behave when:

  • you click “Build and Run” with new device code, when the device is online,
  • you click “Build and Run” without new device code, when the device is online,
  • you click “Build and Run” when the device is offline but comes online at a later date
  • the agent has an unsolicited restart while the device remains online
  • the agent has an unsolicited restart while the device is offline