Mesh All the Things

Because an "Internet of Thing" would kinda suck.

Mesh networks are amazing — they're crazy efficient, extendable, resilient, autonomous, and decentralized. Lucky for us, Pinoccio has all of this baked right in. Let's take a quick look at each of these perks, and then learn how to send messages around our Troop.

Crazy Efficient

Pinoccio Scouts use an extremely low-power mesh radio that's 14 times more efficient than a standard Wifi radio, and has about the same range. This means that for the same amount of energy, we can run 14 Pinoccio Scouts, versus just one Wifi device.

14 mesh networked radios vs 1 wifi device

Extendable & Resilient

That mesh looks pretty cute when it's all pack together, but we can also extend the mesh to connect to a remote location, or provide coverage only where we need it. If some of the nodes are unavailable, we'll just route messages around them.

Wifi devices can't talk directly to each other, so they all have to stay huddled around an access point.

14 mesh networked radios vs 1 wifi device

Autonomous & Decentralized

What happens to this whole setup when we turn the Wifi off?

Wifi and Bluetooth devices — which typically depend on a central hub to communicate — are in big trouble! Not only are they cut off from the internet, they're cut off from each other, like stranded ships, passing in the night.

Our Pinoccio Scouts, on the other hand, may barely even notice. A Troop (mesh) may not need to be connected to the internet to do its job — many tasks can be handled entirely within the network, since Scouts can just talk directly to each other. This decentralization and internet-independence lets Pinoccio Troops operate completely autonomously.

14 mesh networked radios vs 1 wifi device

Sending & Receiving Messages in ScoutScript

Now that we've got this really robust mesh network set up (Pinoccio does all of that stuff automatically), how do we send messages between Scouts? There are two ways to do it in ScoutScript, and both are super easy — let's take a look.

Direct Messages

Send direct messages with
message.scout(scoutId,"foo")
and the receiving Scout will run
on.message.scout(fromId,"foo")

Group Messages

Send group messages with
message.group(groupId,"foo")
and the receiving Scouts will run
on.message.group(groupId,fromId,"foo")

Groups

We use groups to send messages to multiple Scouts. By default, every Scout in the Troop has joined groups 1 through 10. This means that any message sent to groups 1 through 10 will go to every Scout in the Troop, which is very handy! Scouts can join groups with mesh.joingroup(groupId), or leave them with mesh.leavegroup(groupId).

Defining Message Handlers

As we've learned, when a Scout receives a message, it calls the appropriate on.message handler function. You must define these functions to handle messages. Inside the function, you can access the arguments passed in with arg(1), arg(2), etc.

Let's look at some examples...

Direct Message Example

Let's define the direct message handler on Scout 4.
> function on.message.scout {
  led.sethex(arg(2));
}

Now, if Scout 1 runs
message.scout(4,"FF0000")
Scout 4 will get the message, and run our handler,
on.message.scout(1,"FF0000")
where arg(1) = 1, and arg(2) = "FF0000", so we get
led.sethex("FF0000")
and the LED turns red!

Group Message Example

Let's define the group message handler on each Scout in Group 7.
> function on.message.group {
  if (arg(1) == 7) {
    led.green;
  }
}

Now, if Scout 1 runs
message.group(7)
each Scout in Group 7 will get the (blank) message, and run its handler,
on.message.group(7,1)
and since arg(1) = 7, the LED turns green!

Final Notes and Nuggets

  • Both messaging functions let you send multiple messages, e.g. message.scout(4,"foo","bar").
  • As we learned in the group message example above, you don't have to send "foo", e.g. message.scout(4). The receiving Scout will still run its message handler.
  • Scouts don't receive the messages they send, even if they send them to a group that they, themselves, belong to.
  • Remember, by default, all Scouts have joined groups 1 through 10.
  • If a Scout sends a message to a group that it doesn't belong to, it will automatically join that group, and then send the message.
  • You have a ton of group ids available! Scouts can send messages to groups 1 through 65000.

That's it! You can read all about these messaging functions and more in the Mesh Networking section of the ScoutScript documentation.