GithubHelp home page GithubHelp logo

bhanditz / skirnir Goto Github PK

View Code? Open in Web Editor NEW

This project forked from densaugeo/skirnir

0.0 2.0 0.0 122 KB

Link Arduino to Nodejs, reliably

License: GNU Lesser General Public License v3.0

Makefile 0.17% JavaScript 2.71% C++ 97.12%

skirnir's Introduction

Skirnir

Link Arduino to Nodejs, reliably. Can automatically connect to devices in a given folder and detect new devices. Linux-only, since it manages serial settings through stty.

License: LGPL Build Status

Installation

Nodejs

Run npm install git://github.com/Densaugeo/Skirnir.git or add git://github.com/Densaugeo/Skirnir.git to your package.json dependencies.

Arduino

Add contents of src/ to your project folder and `#include "Skirnir.hpp" in your main .cpp file.

Usage

Nodejs

Minimal code to automatically connect to any device found in /dev, send [1, 2, 3], and log any response (messages will be padded with zeroes, because Skirnir packets always carry 45 bytes of data):

var Skirnir = require('skirnir');

var skirnir = new Skirnir({dir: '/dev', autoscan: true, autoadd: true});

setInterval(function() {
  for(var i in skirnir.connections) {
    skirnir.connections[i].send(new Buffer([1, 2, 3]));
  }
}, 1000);

skirnir.on('message', function(e) {
  console.log(new Buffer(e.data));
});

Arduino

The following minimal code can maintain a connection and echo received packets back to the PC:

#include "Skirnir.hpp"

unsigned char packet_decoded[45];
uint16_t last_ping = 0;

Skirnir a_skirnir = Skirnir(&Serial);

void setup() {
  Serial.begin(9600);
}

void loop() {
  // Start heartbeat every 2s
  if((uint16_t) millis() - last_ping > 2000) {
    last_ping = millis();

    a_skirnir.heartbeat();
  }
  
  // Returns true if a valid packet is found
  if(a_skirnir.receive_until_packet(packet_decoded)) {
    // Echo packet back to PC
    a_skirnir.send45(packet_decoded);
  }
}

Larger packets

Normally, Skirnir sends data in 45-byte packets, and ignores extra data if trying to .send() more than 45 bytes. The Skirnir180 class works the same as Skirnir, but is also able to send 180-byte packets, at the cost of a couple hundred bytes of memory. Skirnir180::send() will still send smaller payloads in the same 45-byte packets as Skirnir::send().

License

LGPL

skirnir's People

Contributors

densaugeo avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.