serial-synapse

Keith Chester
3 min readJun 20, 2016

--

I’ve used Johnny-5 with some great success and highly recommend it to people looking to bridge node.js and their hardware projects. For a lot of JavaScript developers it’s the first time they’re seeing their code do something physically outside of a computer — an addictive feeling I constantly push people to experience. Recent projects, however, required the ability to control custom hardware with custom on board programming. I liked my solution at the time and decided to wrap it into a module — serial-synapse.

The not-so-simple short explanation? Given a protocol (in this case, I’m using the developer friendly CmdMessenger protocol, which has libraries for Arduino and other popular MCU boards) allow a developer (programmer? hacker? embedded engineer?) to write their control code with a few exposed endpoints with said protocol. With some agreed-upon standards, you can handle asynchronous messaging between the secondary (the MCU, such as a robot) and the master (node.js on, say, a Raspberry Pi?) The benefit of this approach is that time-essential behaviors can still be managed on the embedded side — great for when you need that reaction time to prevent a robot from plowing into a guest — and still program higher behavior in node.

This might not make sense, but let’s take a robot for example. Here’s a sub $100 robot kit I recently got on sale at Sparkfun— it’s great for some quick fun. Let’s say I wanted to wrap this in serial-synapse. Well, first I’d head over to codebender.cc (it has a nice quick interface for this bot and other Arduino boards) and include in CmdMessenger to handle parsing the serial messaging. This means if I get a message along the lines of:

0,arg,arg,arg;

CmdMessenger would execute the function associated with enum 0, and your function could then pull piece by piece the expected arguments into it. CmdMessenger looks for the “;” as a terminator for commands. For serial-synapse, we do this for commands:

0,uuid,arg,arg;

We still use the CmdMessenger style, but now expect a uuid (generated by serial-synapse, and is just a 6–8 character alphanumeric string). We expect the function to reply back starting with that uuid and data:

uuid,data,data,data

…and from this, we know that our function we fired off in node has responded.

This might not make sense until you see it in practice. So take this example using the aforementioned robot, which breaks out functions for the RedBot — drive, setLeftMotor, setRightMotor, etc — we can now call these methods like this:

synapse.addCommand({
name: "drive",
identifier: 2
});
synapse.drive(255, function(err){
console.log("The motor is driving!");
});

…and that’s it. When the response message comes in, the callback is called, just like any normal node library. It’s as if your code was entirely executing locally. Define what identifies the required function, and what it returns, and suddenly you have an API to your physical embedded project.

And if you want to get sensor data without pulling, there’s also Update Handlers — which ping you anytime the device has data to send. In the example code earlier, I have the accelerometer data broadcast with an identifier of 10 every quarter second. Then I can capture and handle that data just by doing:

synapse.addUpdateHandler({
name: 'accelerometer',
identifier: 10,
returns: ["x", "y", "z"],
then: function(data){
console.log("Accelerometer returned", data);
}
});

…which, every time an accelerometer update comes in, which looks like:

10,234.3,134.2,somerealisticvalue

…serial-synapse would capture, parse, and call our update handler, and print out:

Accelerometer returned    {
"x": 234.3,
"y": 134.2,
"z": somerealisticvalue
}

Of course, console.logs are fun, but you can easily see where your logic code could go.

Since serial-synapse does this in a predictable manner, I also created a really quick wrapper that would expose a serial-synapse instance via web sockets, called serial-synapse-socket. The robot example uses it as well, making that robot WiFi controlled with little effort!

Hopefully some of you out there will find this as useful as I will.

--

--

Keith Chester

Developer + R&D for Fusion Marketing. Hardware maker. Node programmer. Entrepreneur. Curious fellow.