Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cryptocracy/0e012e525256997f0d48 to your computer and use it in GitHub Desktop.
Save cryptocracy/0e012e525256997f0d48 to your computer and use it in GitHub Desktop.
BitSwitch for Intel Edison sample using Node.js
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:false */
/*global */
var mraa = require('mraa'); //require mraa
var request = require('request');
var _ = require('underscore')
var addr2watch = '19e2eU15xKbM9pyDwjFsBJFaSeKoDxp8YT' // bitcoin address to watch
//var loop_time = 10000 // ms (check every 10 sec)
var loop_time = 3000 // ms (check every 3 sec)
var balance = null
var relayPin = new mraa.Gpio(13)
var check_balance = function(){
// this version uses blockchain.info direct api - but I could've used blockr.io, chain.com or the awesome bitcore-explorers library
request('https://blockchain.info/q/addressbalance/'+addr2watch, function (error, response, body) {
if (!error && response.statusCode == 200) {
var satoshi = parseInt(body)
var btc = satoshi*Math.pow(10, -8)
// this is the simplest approach, just check if the balance if different
// you could check if the price is increased by X amount or better if you got a transaction exactly of x btc
if (balance && balance != btc)
activate()
balance = btc
}
_.delay(check_balance, loop_time)
})
}
var activate = function() {
console.log("Address balance changed, new one is:", balance, "BTC")
console.log("Let there be light!")
relayPin.write(0);
}
var main = function() {
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console
relayPin.dir(mraa.DIR_OUT);
relayPin.write(1);
console.log("check_balance() initialized")
check_balance()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment