Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including arduino-serial-messenger with all npm packages installed. Try it out:

var arduinoSerialMessenger = require("arduino-serial-messenger")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

arduino-serial-messenger v0.0.4

A messenger API for communicating with Arduion over serial port

Arduino Serial Messenger

NodeJS library for communication with Arduino using Serial Port.

Installation

npm install arduino-serial-messenger

Generating Arduino Sources

const messengerLib = require("arduino-serial-messenger");
const ArduinoGenerator = messengerLib.ArduinoGenerator;
const FixedSizeType = messengerLib.MessageType.FixedSizeMessageType;
const VariableSizeType = messengerLib.MessageType.VariableSizeMessageType;

let generator = new ArduinoGenerator([
    new FixedSizeType(0, 0),
    new FixedSizeType(1, 2),
    new VariableSizeType(2, 10)
]);

generator.generateCode(__dirname);

When you execute the above code it will generate two files:

  • Messenger.h
  • Messenger.cpp

Both files can be directly included in your arduino project folder and loaded to the arduino script using:

#include "Messenger.h"

See a sample of a simple Arduino program using Messenger.

Using Messenger in your javascript

const messengerLib = require("arduino-serial-messenger");
const SerialMessenger = messengerLib.SerialMessenger;
const FixedSizeType = messengerLib.MessageType.FixedSizeMessageType;
const VariableSizeType = messengerLib.MessageType.VariableSizeMessageType;
const Message = messengerLib.Message;

const MESSAGE0 = new FixedSizeType(0, 0);
const MESSAGE1 = new FixedSizeType(1, 2);
const MESSAGE2 = new VariableSizeType(2, 10);

let messenger = new SerialMessenger({
    path: "/dev/ttyUSB0",
    messagetypes: [
        MESSAGE0,
        MESSAGE1,
        MESSAGE2
    ]
});

messenger.on("unknowntype", function (data) {
    console.log("Unknown message type received: " + data);
});

messenger.on("message", function (message) {
    if (message.getMessageType().isFixedSize()) {
        console.log(`Received fixed size message type: ${message.getMessageType().getTypeId()} with data: ${message.getData()}`);
    } else {
        console.log("Received variable size message with data: " + message.getData());
    }
});

function sendMessage() {
    messenger.sendMessage(new Message(MESSAGE0));
}

setTimeout(sendMessage, 2000);

The path in SerialMessenger configuration is a path to serial port device. For more details see the documentation for serialport. In case you need a different parameters for serial port you can initialize it yourself and pass it as a second parameter.

Remember that Arduion gets restarted when a serial port connects to it and it takes a while to load. You need to either wait a safe amount of time to start communicating with it or keep sending ack messages until you get one back.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free