0

I have a PC with two nodes running on it, and a Raspberry Pi which also has Geth running. Problem is how to write a smart contract so that sensor to send the measurements to the blockchain periodically, and also is it possible to access this chain from anywhere, given that I know the private key of the account?

Thank you

0

A smart contract lives on the blockchain and cannot perform any operations outside of the blockchain world (see also this excellent article).

You need an oracle for this. An oracle is some piece of software living outside the blockchain and communicates with it through some API (e.g. web3 or ethjsonrpc).

So in your case you should write a small script (I prefer python for those kind of things) which fetches the sensor data and writes it on the blockchain e.g. by calling a smart contract function. If you just need to store a simple integer value (Solidity doesn't support floating point numbers, so perhaps you need to modify the sensor data accordingly) the following smart contract could help you:

pragma solidity ^0.4.10;

contract StoreIntegerValue {
    address owner;
    int sensorData;

    function StoreIntegerValue() {
        owner = msg.sender;
    }

    function setSensorData(int _sensorData) {
        require(msg.sender == owner);
        sensorData = _sensorData;
    }

    function getSensorData() constant returns (int) {
        require(msg.sender == owner);
        return sensorData;
    }
}

To store the value just send a transaction calling setSensorData and to get the value call getSensorData.

Hope it helps.

  • Thank you, but I’m new to Ethereum, so could you please, explain how to add oracle and how to modify sensor data? Thanks again – Nurbolat K Feb 8 '18 at 17:10
0

Just to follow up on joffi's answer. I've provided a python program you could use on your RPi to submit data to the StoreIntegerValue contract. Please note that a few things may need to be changed for your particular case.

from web3 import Web3, HTTPProvider
from solc import compile_source
from web3.contract import ConciseContract

#Please replace with you actual web3 provider (for more info: http://web3py.readthedocs.io/en/stable/providers.html)
w3 = Web3(HTTPProvider('http://127.0.0.1:8545'))

#abi can be generated form the command line with solc or online with Remix IDE
abi = '''
[
    {
        "constant": false,
        "inputs": [
            {
                "name": "_sensorData",
                "type": "int256"
            }
        ],
        "name": "setSensorData",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getSensorData",
        "outputs": [
            {
                "name": "",
                "type": "int256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    }
]
'''

#Replace with real deployed contract address
address = Web3.toChecksumAddress("0xeab01dba3ef110d5584f3315f2b3e4a86d04eb94")
StoreIntegerValue = w3.eth.contract(
    address, abi=abi, ContractFactoryClass=ConciseContract)

#Replace with real account address for raspi
raspi = 0x907c1a1c053ac1d01bda773b9a36b8ffd00c1cc4

#Example function to submit data to the block chain
def submitSensorData(data):
    #note that data must be an integer,
    StoreIntegerValue.setSensorData(int(data), transact={'from': raspi})

#For more info about using Python to interact with the blockchain, checkout the web3.py documentation http://web3py.readthedocs.io/en/stable/index.html
  • Thank you for your answer, but could you clarify some points: what is abi? how to know the web3 provider? and does this script send data to contract periodically or by request? Again thank you very much, it helped me a lot – Nurbolat K Apr 6 '18 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.