Last active
June 11, 2016 19:46
-
-
Save makevoid/e577c423c15c265e75a7 to your computer and use it in GitHub Desktop.
Solether - solar powered autonomous entity that lets you charge your phone for ethers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*jslint node:true, vars:true, bitwise:true, unparam:true */ | |
| /*jshint unused:false */ | |
| /*global */ | |
| // Solether - solar powered autonomous entity | |
| var mraa = require('mraa'); | |
| var _ = require('underscore') // docs: http://underscorejs.org/ | |
| var Web3 = require('web3'); | |
| var account, balance, c, eth, geth_url, provider, web3; | |
| var loop_time = 1000 // ms (check every second) | |
| var balance = null | |
| var relayPin = new mraa.Gpio(12) | |
| var reset_loops = 0 | |
| var light_is_on = false | |
| var reset_after = 30 // seconds | |
| c = console | |
| // geth_url = 'http://127.0.0.1:8545' | |
| geth_url = "http://v.mkvd.net:8080" | |
| provider = new Web3.providers.HttpProvider(geth_url) | |
| web3 = new Web3(provider) | |
| eth = web3.eth | |
| c.log("web3 version:", web3.version.api) | |
| //c.log("geth version:", web3.version.node); | |
| // account = eth.coinbase; | |
| account = eth.accounts[2]; | |
| c.log("main account address:", account) | |
| var account_to_watch = "0xe078de0afdf56936580521aac192fe0b2924c4f3" | |
| account = account_to_watch | |
| balance = eth.getBalance(account); | |
| c.log("balance: ", Number(balance), "[account]") | |
| var addr2watch = account | |
| var check_balance = function(){ | |
| bal = eth.getBalance(account); | |
| var ethers = Number(bal) | |
| // console.log(".") | |
| console.log("b: ", ethers) | |
| // this is the simplest approach, just check if the balance if different from the previous one, meaning you can deposit any amount to charge your device | |
| // you could check if the price is increased by X amount or better if you got a transaction exactly of x ethz | |
| if (balance && balance != ethers) | |
| activate() | |
| if (light_is_on) | |
| reset_loops += 1 | |
| balance = ethers | |
| if (reset_loops >= reset_after) { | |
| reset_loops = 0; | |
| relayPin.write(0) // set the relay pin to low, this will trigger the relay! | |
| _.delay(function(){ relayPin.write(1) }, 5000) // this is like setTimeout(check_balance, loop_Time) | |
| light_is_on = false; | |
| console.log("time's up, switching it off") | |
| } | |
| _.delay(check_balance, loop_time) // this is like setTimeout(check_balance, loop_Time) | |
| } | |
| var activate = function() { | |
| console.log("Address balance changed, new one is:", balance, " ") | |
| console.log("Here's your charge!") | |
| light_is_on = true | |
| relayPin.write(0) // set the relay pin to low, this will trigger the relay! | |
| _.delay(function(){ relayPin.write(1) }, 100) // this is like setTimeout(check_balance, loop_Time) | |
| } | |
| var main = function() { | |
| console.log('MRAA Version: ' + mraa.getVersion()); | |
| relayPin.dir(mraa.DIR_OUT); | |
| relayPin.write(1); // I have my relay as normally closed (NC). otherwise, if it's normally open (NO) you have to invert the 0 and 1 in all the .write() calls | |
| console.log("EtherSwitch initialized - v0.2.0 - watching address:", addr2watch) | |
| check_balance() | |
| } | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment