1

I have the variable w1_temp, which i need to send to contract I have the following code:

var W1Temp = require('w1temp')
var Web3 = require('web3')
var web3 = new Web3()

web3.setProvider(new web3.providers.HttpProvider('http://localhost:8042'))
var contractAddress = '0x414c49c9552c4bd41401ff0a67ff84431a900c66'
var ABI = JSON.parse(
'[ { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "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", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" } ] ')

var StoreIntegerValue = web3.eth.contract(ABI).at(contractAddress)

W1Temp.setGpioData(7)
W1Temp.getSensor('28-0316612396ff').then(function (sensor) {
    var _sensorData = sensor.getTemperature();
});

eth.sendTransaction({
    from: 'a57f7e5347bd04076dd02c02212c0221522289ff38940e', 
    to: '0x414c49c9552c4bd41401ff0a67ff84431a900c66',
    value: web3.toWei(100,'finney'),
    data: web3.toHex('_sensorData')
})

But I’m almost sure that it is not correct Please help to make it work

enter image description here

0

Since you have the contract ABI you can create an instance pointing to contract's address and call methods directly.

var StoreIntegerValue=web3.eth.contract(ABI).at(contractAddress);

W1Temp.setGpioData(7)
W1Temp.getSensor('28-0316612396ff').then(function (sensor){
  var _sensorData = sensor.getTemperature();

  StoreIntegerValue.setSensorData(_sensorData, { 
    from: "0x123412341234.." 
  });
});

But you should ask the person who provided the ABI which methods are available, their parameters and requisites.

  • It still does not work, whenever I call the contract it returns the same data that I sent to execute it. As I understood the value sent into the contract can be anything. Here is my smart contract. 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; } } – Nurbolat K Apr 10 '18 at 13:59
  • I do not understand your issue. If you call setSensorData with a new value, after the transaction is mined it should return the new value when you call getSensorData. Perhaps the require is failing and causing the transaction to revert, for test just comment out the require sentences. Also try specifying the gas { from: "0x..", gas: 1000000 }. – Ismael Apr 10 '18 at 17:24
  • I do not call sensor data, it is written in read from contract field. Also when i send transaction, it says “It seems transaction will fail. If you submit it it may consume all the gas you provide”, and then lets me send the transaction. – Nurbolat K Apr 11 '18 at 10:38
  • And what is the require? How to find it? Is it artifacts? – Nurbolat K Apr 11 '18 at 10:39
  • In your contract code there's a require(msg.sender == owner); this causes that only the contract owner can send data to the contract. – Ismael Apr 11 '18 at 13:19

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.