ethereum-smart-chargingpoint

The flow connects to the local Geth node running on the RPi and it retrieves a smart-contract stored on the blockchain.

Using the "watch event" tool featured by Geth, Ethereum and the web3.js library, checks if any cryptocurrency is sent to the account connected to the plug and it triggers the charging when a transaction is received.

The flow then, based on the consumption data obtained by a smart-meter throgh MQTT, calculates the accumulated Wh and enables the charging according to the price of electricity and the amount sent via transaction.

When the charging credit is over, the flow switches off the charging point.

[{"id":"80e8fda7.a9045","type":"tab","label":"Complete"},{"id":"df7ea571.67ff48","type":"debug","z":"80e8fda7.a9045","name":"Power","active":false,"console":"false","complete":"payload","x":319,"y":745,"wires":[]},{"id":"3f2e9dee.4e1a62","type":"function","z":"80e8fda7.a9045","name":"Wh","func":"//variables Wh accumulated\nvar timestep = 10/(60*60);\nvar Wh = msg.payload*timestep;\n\n// initialise the counter to 0 if it doesn't exist already\nvar count = flow.get('count')||0;\ncount += Wh;\n\n// store the value back\nflow.set('count',count);\n\n// make it part of the outgoing msg object\nmsg.count = count;\n\nreturn msg\n\n\n","outputs":"1","noerr":0,"x":404,"y":646.5,"wires":[["d0f857fc.91e2e8","d02d412b.1d23"]]},{"id":"d02d412b.1d23","type":"johnny5","z":"80e8fda7.a9045","name":"Relay","func":"var relay = new five.Relay(10);\nvar price = 50; // upc/Wh\nvar light_on = false;\nvar Wh_start, consumption, total;\n\nnode.on('input', function(msg){\n\n\n    if (light_on !== true & msg.data > 0) {\n        relay.on();\n        node.send({payload:'Light is ON'});\n        light_on = true;\n        Wh_start = flow.get('count');\n        node.send({payload:\"Initial accumulated value\"+ Wh_start +\"Wh\"});\n        total = msg.data/price;\n        node.send({payload:\"Purchased electricity\"+ total +\"Wh\"})\n    }\n\n    if (light_on === true){\n    \n        consumption = (flow.get('count')-Wh_start);\n        node.log(\"consumption calculated\")\n        node.send({payload:\"Consumption:\"+ consumption+ \"Wh\"});\n    }\n    \n    if (consumption > total){\n            relay.off();\n            node.send({payload:'Light is OFF'});\n            light_on = false;\n            consumption=0;\n    }\n});\n\nreturn;","board":"2961200a.cef94","noerr":0,"x":609,"y":591.5,"wires":[["c388717e.9c398"]]},{"id":"a4727c2a.fbaef","type":"inject","z":"80e8fda7.a9045","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":202,"y":539.5,"wires":[["261b5bca.a2bb84"]]},{"id":"d0f857fc.91e2e8","type":"debug","z":"80e8fda7.a9045","name":"Wh accumulated","active":true,"console":"false","complete":"count","x":558,"y":733.5,"wires":[]},{"id":"261b5bca.a2bb84","type":"function","z":"80e8fda7.a9045","name":"Watch Event","func":"//variables\nvar Web3 = global.get('web3');\nvar abi = [{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"standard\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"},{\"name\":\"_extraData\",\"type\":\"bytes\"}],\"name\":\"approveAndCall\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"},{\"name\":\"\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"addr\",\"type\":\"address\"}],\"name\":\"getBalance\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialSupply\",\"type\":\"uint256\"},{\"name\":\"tokenName\",\"type\":\"string\"},{\"name\":\"decimalUnits\",\"type\":\"uint8\"},{\"name\":\"tokenSymbol\",\"type\":\"string\"}],\"payable\":false,\"type\":\"constructor\"},{\"payable\":false,\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"}];\nvar main_account = \"0xa4ea55ff755474e3bfe019335ad900cd160cf420\";//accounts[0];\nvar relay1_account = \"0x046457074bb15c0d0d0f5ff5a69a6f1c353fda03\"; //accounts[1];\nvar relay2_account = \"0x7560b2a36513abd17cf339592ff52f4387de2c7c\"; //accounts[2];\n\nvar amount, web3, accounts, main_account, relay1_account, relay2_account, reset_after;\n\n\n//web3.js, checking if node is running\nweb3 = new Web3(new Web3.providers.HttpProvider(\"http://localhost:8545\"));\n\nif (!web3.isConnected()) {\n    node.status({fill:\"red\",shape:\"ring\",text:\"node not connected\"});\n\n} else {\n    node.status({fill:\"green\",shape:\"ring\",text:\"node connected\"});\n}\n\n//fetching smart contract UPC\nvar UPCoin = web3.eth.contract(abi);\nvar contract = UPCoin.at('0x5c62b492ae99db9c2a174f3328ddd2f98773291a');\n\n//watching event Transfer\nvar filterR1 = contract.Transfer({\n    to: relay1_account\n    }, {\n        fromBlock: 'latest',             //we are searching only latest block\n        toBlock: 'latest'\n    });\n\nfilterR1.watch(function(error, result) {\n        if (!error) {\n          node.log(\"###########  watch ################# result ####################################\");\n          node.log(result);\n          node.log(\"###########  watch ################# end of result #############################\");\n          amount = (result.args.value);\n          node.send({data:amount});\n        }\n    })\nnode.on('close', function() {\n    \nfilterR1.stopWatching();\n\n});\n\nreturn msg;\n","outputs":1,"noerr":0,"x":395,"y":553.5,"wires":[["f4296145.11db7","d02d412b.1d23"]]},{"id":"f4296145.11db7","type":"debug","z":"80e8fda7.a9045","name":"Transaction","active":true,"console":"false","complete":"data","x":565,"y":491.5,"wires":[]},{"id":"7b4f10dd.7d767","type":"mqtt in","z":"80e8fda7.a9045","name":"emon/emontx3/power1","topic":"emon/emontx3/power1","qos":"2","broker":"8a9d92d6.75627","x":187,"y":683.5,"wires":[["3f2e9dee.4e1a62","df7ea571.67ff48"]]},{"id":"c388717e.9c398","type":"debug","z":"80e8fda7.a9045","name":"Status 1","active":true,"console":"false","complete":"payload","x":801,"y":591.5,"wires":[]},{"id":"2961200a.cef94","type":"nodebot","z":"","name":"Arduino UNO","username":"","password":"","boardType":"firmata","serialportName":"/dev/ttyACM0","connectionType":"local","mqttServer":"","socketServer":"","pubTopic":"","subTopic":"","tcpHost":"","tcpPort":"","sparkId":"","sparkToken":"","beanId":"","impId":"","meshbluServer":"https://meshblu.octoblu.com","uuid":"","token":"","sendUuid":""},{"id":"8a9d92d6.75627","type":"mqtt-broker","z":"a5ce04b2.5a31f8","broker":"localhost","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"willTopic":"","willQos":"0","willRetain":null,"willPayload":"","birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":""}]

Flow Info

Created 7 years, 9 months ago
Updated 6 years, 9 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • debug (x4)
  • function (x2)
  • inject (x1)
  • mqtt in (x1)
  • mqtt-broker (x1)
Other
  • johnny5 (x1)
  • nodebot (x1)
  • tab (x1)

Tags

  • ethereum
  • smart-contract
  • blockchain
  • event
  • smart-plug
  • mqtt
  • web3.js
  • johnny-five
  • openenergymonitor
  • raspberrypi
  • arduino
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option