npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cylon-imp

v0.5.0

Published

Cylon module for the Electric Imp

Downloads

13

Readme

Cylon.js For Imp

Cylon.js (http://cylonjs.com) is a JavaScript framework for robotics, physical computing, and the Internet of Things using Node.js

This repository contains the Cylon adaptor for the Electric Imp (https://electricimp.com/) platform.

Want to use Ruby on robots? Check out our sister project Artoo (http://artoo.io)

Want to use the Go programming language to power your robots? Check out our sister project Gobot (http://gobot.io).

Build Status Code Climate Test Coverage

How to Install

Install the module with:

$ npm install cylon cylon-imp

You'll also need to setup your Imp, for this you can go here: Electric Imp Getting Started

Once you have completed the Getting Started process and have a connected Imp, you need to go to the Electric Imp IDE page and create a new model that you can attach your Imp to. You can check how that process works in detail on the Electric Imp IDE docs

How to Use

Blink LED: Digital GPIO

var Cylon = require('cylon');

Cylon.robot({
  connections: {
    imp: { adaptor: 'imp', agentUrl: 'https://agent.electricimp.com/45QYZvoB41bu', module: 'cylon-imp' }
  },

  devices: {
    led: { driver: 'led', pin: 1 }
  },

  work: function(my) {
    every((1).second(), function() {
      my.led.toggle();
    });
  }
}).start()

How to Connect

We need to setup the Agent code and Device code so we can control the Imp over the internet. First we need to setup the agent that will serve as the endpoint URL for our Imp.

Setting up the Agent

Inside the Electric Imp IDE, on the left side (for the default layout), you'll find the Agent section, you need to paste the following code:

// Log the URLs we need
server.log("Agent URL: " + http.agenturl());

saved_response <- null;

function requestHandler(request, response) {
  local jsonRes = {};
  try {
    // check if the user sent led as a query parameter

    if ("mode" in request.query) {
      local value = ("value" in request.query) ? request.query.value : 0;
      local pin = ("pin" in request.query) ? request.query.pin : 0;
      local address = ("address" in request.query) ? request.query.address : null;
      local buffer = ("buffer" in request.query) ? request.query.buffer : null;
      local bytes = ("bytes" in request.query) ? request.query.bytes : null;

      device.send(request.query.mode, {
        "mode": request.query.mode,
        "pin": pin,
        "value": value,
        "address": address,
        "buffer": buffer,
        "bytes": bytes
      })
    }
    // send a response back saying everything was OK.
    //response.send(200, jsonRes);
    saved_response = response;
  } catch (ex) {
    response.send(500, "Internal Server Error: " + ex);
  }
}

function responseHandler(data) {
  saved_response.send(200, http.jsonencode(data));
}

// register the HTTP handler
http.onrequest(requestHandler);

// register device response listener for digital and analog reads
device.on("response", responseHandler);

This will allow use to send commands to the Imp, specify modes, setup pins and consotrl I2C devices. You can also find this code in the utils section of this repo.

Setting up the Device firmware

Same as with the Agent code we do this inside the Electric Imp IDE, but now on the right side viewport, there you'll see the Device section, same as before paste the following code:

// create a global variabled called led and assign pin9 to it
pins <- {
  "1": hardware.pin1,
  "2": hardware.pin2,
  "5": hardware.pin5,
  "7": hardware.pin7,
  "8": hardware.pin8,
  "9": hardware.pin9,
};

function digitalWrite(args) {
  pins[args.pin].configure(DIGITAL_OUT);
  pins[args.pin].write(args.value.tointeger());

  agent.send("response", { "data": null });
}

function digitalRead(args) {
  pins[args.pin].configure(DIGITAL_IN);
  local readVal = pins[args.pin].read();

  agent.send("response", { "data": readVal });
}

function analogWrite(args) {
  pins[args.pin].configure(ANALOG_OUT);
  pins[args.pin].write(args.value.tofloat());

  agent.send("response", { "data": null });
}

function analogRead(args) {
  pins[args.pin].configure(ANALOG_IN);
  local readVal = pins[args.pin].read();

  agent.send("response", { "data": readVal });
}

function pwmWrite(args) {
  local freq = 0.02;

  if ("freq" in args) {
    freq = args.freq;
  }

  pins[args.pin].configure(PWM_OUT, freq, 1);
  pins[args.pin].write(args.value.tofloat());

  agent.send("response", { "data": null });
}

function i2cWrite(args) {
  hardware.i2c89.configure(CLOCK_SPEED_400_KHZ);
  local i2c = hardware.i2c89;

  local buffer = split(args.buffer, ",");
  local binaryAddress = 0x00;
  local binaryBuffer = "";

  binaryAddress += args.address.tointeger() << 1;

  foreach(val in buffer){
    binaryBuffer += val.tointeger().tochar();
  }

  i2c.write(binaryAddress, binaryBuffer);

  agent.send("response", { "data": null });
}

function i2cRead(args) {
  hardware.i2c89.configure(CLOCK_SPEED_400_KHZ);
  local i2c = hardware.i2c89;

  local buffer = split(args.buffer, ",");
  local binaryAddress = 0x00;
  local binaryBuffer = "";
  local bytesLength = args.bytes.tointeger();

  binaryAddress += args.address.tointeger() << 1;

  foreach(val in buffer){
    binaryBuffer += val.tointeger().tochar();
  }

  //i2c.write(binaryAddress, binaryBuffer);
  local data = i2c.read(binaryAddress, binaryBuffer, bytesLength)
  local dataArray = [];

  foreach(val in data) {
    dataArray.push(val);
  }

  agent.send("response", { "data": dataArray });
}

agent.on("digitalWrite", digitalWrite);
agent.on("digitalRead", digitalRead);
agent.on("analogWrite", analogWrite);
agent.on("analogRead", analogRead);
agent.on("pwmWrite", pwmWrite);
agent.on("i2cWrite", i2cWrite);
agent.on("i2cRead", i2cRead);

This will execute code in your Imp device when you make calls to the Agent using the agent URL, you can check how to do this in the following section.

Your Imp device is one click away of being ready to communicate and execute code, just click on the Build and Run button on the top of the IDE and you should be ready to go.

Talking to your imp through the Agent URL

In order to use Cylon to communicate with your Imp we'll need the agent URL. You can find this URL right above the Agent viewport of the IDE, alternatively you can look for it in the log after you click Build and Run.

Once you have this url, which should like something along the lines of https://agent.electricimp.com/45QYZvoB41bu, you can pass it along the adaptor parameters in your Cylon code.

This is how you setup the connection using the agent url in Cylon:

var Cylon = require('cylon');

Cylon.robot({
  connections: {
    imp: { adaptor: 'imp', agentUrl: 'https://agent.electricimp.com/45QYZvoB41bu', module: 'cylon-imp' }
  },

// Rest of code ...
}).start()

Documentation

We're busy adding documentation to our web site at http://cylonjs.com/ please check there as we continue to work on Cylon.js

Thank you!

Contributing

For our contribution guidelines, please go to https://github.com/hybridgroup/cylon/blob/master/CONTRIBUTING.md .

Release History

For the release history, please go to https://github.com/hybridgroup/cylon-imp/blob/master/RELEASES.md .

License

Copyright (c) 2014-2015 The Hybrid Group. Licensed under the Apache 2.0 license.