Serial-Synapse

Serial-Synapse © MIT

I made a Node.js module to create an easy interface between your custom hardware and node AND instantly put it online via a socket API too!

  • 1,246 views
  • 0 comments
  • 6 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
Works with any board that uses CmdMessenger style protocols - which of course includes nearly all Arduino boards!
×1

Apps and online services

About this project

What is Serial-synapse?

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.

How to do it?

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.

Getting answers

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.

Getting sensor data without pulling

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.

Code

Github: RedBot Serial Synapse Example
https://github.com/hlfshell/redbot-synapse-example
Github: serial-synapse-socket
Use this module to insta-wrap any serial-synapse objects and create a web socket API for it.
Github: serial-synapse
serial-synapse's github home

Comments

Similar projects you might like

Attiny85/84 with Bluetooth

by Robin

  • 12,632 views
  • 0 comments
  • 54 respects

Master Slave I2C Connection

by PIYUSH_K_SINGH

  • 3,734 views
  • 0 comments
  • 8 respects

Serial Pong

Project showcase by Cody Brooks

  • 2,625 views
  • 2 comments
  • 12 respects

ESP8266 ESP-01 Webserver

by Robin

  • 100,367 views
  • 18 comments
  • 131 respects

LCD (Liquid Crystal Display)

by MicroBob

  • 30,781 views
  • 41 comments
  • 101 respects

Smart Parking

Project showcase by Kaustubh Agarwal

  • 28,258 views
  • 14 comments
  • 43 respects
Add projectSign up / Login