User manual
a) When the target node does hear the rpc() call, the ACK packet is sent automacally (by the SNAP
firmware – you do not send the ACK from your script).
b) If the target node does not hear the rpc() call, then it does not know to send the ACK packet. This means
the source node will not hear an ACK, and so it will meout and retry a configurable number of mes.
Going back two examples, instead of modifying the askSensorReading() funcon in node B’s script to take an
addional “who” parameter, and calling
mcastRpc(1, 1, "askSensorReading", address_of_node_B)
node A could simply call:
rpc(address_of_node_B, "askSensorReading")
Nodes C-Z would ignore the funcon call (although they may be helping route the funcon call to node B
without any addional configuraon required by the user).
The askSensorReading() funcon could also benefit from the use of rpc() instead of mcastRpc().
Instead of telling all nodes in group 1 and 1 hop away what the sensor reading is via:
mcastRpc(1, 1, "tellSensorReading", value)
the script could instead send the results back to the original requestor only via:
rpc(rpcSourceAddr(), "tellSensorReading", value)
Funcon rpcSourceAddr() is another built-in funcon that, when called from a funcon that was invoked
remotely, returns the SNAP Address of the calling node. (If you call rpcSourceAddr() locally at some arbitrary
point-in-me, such as within the HOOK_STARTUP or HOOK_GPIO handler, then it simply returns None.)
Note that the funcons being invoked, by either an rpc() call or an mcastRpc() call, could be built-in (such as
readPin(), readAdc(), random()) or could be user-defined (defined within the currently loaded SNAPpy script).
Callback(callback, remoteFunction, remoteFunctionArgs )
Both the previous methods allowed one node to ask another node to perform a funcon and then send the
result of that funcon back to the first node. In each case, the first node called the askSensorReading() funcon,
whose only purpose was to call a separate funcon (readSensor()) and then send back the value from that.
It turns out that SNAP has a built-in funcon to do just that, the callback funcon.
Going back to a previous example, and expanding on it a lile:
def readSensor():
return readAdc(0) * SENSOR_GAIN + SENSOR_OFFSET
def askSensorReading():
value = readSensor()
mcastRpc(1, 1, "tellSensorReading", value)
In a scenario like this, roune readSensor() is pulling its own weight – it is encapsulang some of the sensor
complexity, thus hiding it from the rest of the system.
Somemes, however, the raw sensor readings are sufficient (or the calculaons are so complex that they need
to be offloaded to a bigger computer).
This results in the code being changed to something more like:
def readSensor():
SNAP® Network Operang System 35