Skip to content

Instantly share code, notes, and snippets.

View imp-guru's full-sized avatar

imp-guru

  • Green Bay, WI
View GitHub Profile
@imp-guru
imp-guru / Agent
Last active August 29, 2015 14:15
Read a UART via Agent Function
uart_response <- "dummy"
device.on("resp", function(data) {
server.log(data)
});
function readUart(cmd) {
device.send("req", cmd)
// Blocking wait to receive response from the imp
@imp-guru
imp-guru / agent
Created February 10, 2015 13:36
Serial CMD and Response via http
// Log the URLs we need
server.log("API Protocol " + http.agenturl() + "?cmd=");
function requestHandler(request, response) {
try {
// check if the user sent led as a query parameter
if ("cmd" in request.query) {
device.send("req", request.query.cmd)
// Blocking wait to receive response from the imp
@imp-guru
imp-guru / hall_effect_agent
Last active August 29, 2015 14:08
Hall Effect Counter with rate calculation
// Create a data stream at data.sparkfun.com, then enter your keys here:
local publicKey = "mKO3DYdxD7UO7dRElmbb"
local privateKey = "kzlbm9jJmPUAvpB1ZyEE"
local buffer_depth = 300
local total = 0
local nvm_storage = server.load();
if (nvm_storage.len() != 0)
{
total = nvm_storage.total
@imp-guru
imp-guru / mows_broken.js
Last active August 29, 2015 14:06
Broken version of MOWS
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.mows=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var websocket = require('websocket-stream')
module.exports = function buildWebsocket(url, opts) {
opts.protocol = 'mqttv3.1';
opts.type = Uint8Array;
return websocket(url, opts);
};
@imp-guru
imp-guru / mows_fixed
Created September 16, 2014 12:25
Working version of Browserified MOWS
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.mows=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var websocket = require('websocket-stream')
module.exports = function buildWebsocket(url, opts) {
opts.protocol = 'mqttv3.1';
opts.type = Uint8Array;
return websocket(url, opts);
};
@imp-guru
imp-guru / Si7020_agent_push2sparkfun
Last active August 29, 2015 14:06
Electric Imp Agent Code for Si7020 Breakout Board w/ upload to data.sparkfun.com
// Create a data stream at data.sparkfun.com, then enter your keys here:
local publicKey = "8dX9xNk1w3sOv044oz1m" // These are fake codes, please enter yours
local privateKey = "pzbnrbq6XvCzWn556pDE" // These are fake codes, please enter yours
function sample(params) {
// Extract bits from the data sent by the agent
// Combine the 2 bytes of data from Si7020 into a single uint16
// then convert to float. Do this for both temp and humidity
local rh_code = (params[0]*256+params[1]).tofloat();
local temp_code = (params[2]*256+params[3]).tofloat();
@imp-guru
imp-guru / read_Si7020_device
Created September 12, 2014 17:47
Electric Imp Device Code for Si7020 Breakout Board
// Define the Sensor Address
const ADDR = 0x80;
// Define register map
const HUMIDITY = "\xE5";
const TEMP_FROM_RH = "\xE0";
// Specify the update rate in seconds
const update_rate = 60;
@imp-guru
imp-guru / simple_uart_rx
Last active August 29, 2015 14:05
A simple example for receiving data from a UART on Electric Imp and outputting it to the server console line by line
// Create a message buffer
local msg = ""
// A function that is called everytime that the UART receives a byte
function serialRx() {
//Pull a byte from the UART FIFO
local c = hardware.uart1289.read();
// Determine if this byte is a carriage return (triggers to log the line)
@imp-guru
imp-guru / periodic_sample
Created July 31, 2014 03:28
Periodic Sampling for Electric Imp
function readTemp() {
// Do your periodic stuff here...
// schedule imp to wakeup in .5 seconds and do it again.
imp.wakeup(0.5, readTemp);
}
// start the loop
readTemp();
@imp-guru
imp-guru / simple_rest_api
Created July 30, 2014 15:01
Create a REST API for Electric Imp using Blocking device.on()
// ----------------------Agent Code-----------------------
function requestHandler(request, response) {
try {
// Send a read request to the device
device.send("getRequest", request);
// Blocking wait to receive response from the imp
device.on("getResponse", function(data) {
response.send(200,data);
});