1

I'm working on a college project where we are using IoT sensors (raspberry pi) to monitor the supply chain conditions, think temp, pressure, humidity etc. This data is being transmitted via MQTT to a Ubunutu VM. The VM is also running a Ganache Ethereum testnet, with a smart contract powered dashboard which can be updated during the supply chain process. E.g. manufactuer can ititate the contract then pass it onto the distributor. This works fine and so does the MQTT data, I'm currently displaying said data using MQTT explorer. However we are required to store this data directly on chain. This is to prove transparency and accuracy during shipment. I can't figure out how to achieve this.

Can anyone share any insight? Maybe there is a better way this data can be stored someway?

1 Answer 1

2

One way to achieve this is to create a smart contract that acts as the storage interface. The smart contract should hold a data structure for your data, and have functions to write to and read from the data structure. obviously you should prevent unwanted access. then whenever you want to store a data on chain you send a transaction from the device to the smart contract and save the data.

this is a simple code generated by ChatGPT that shows a smart contract for storing the temperature on specific dates:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TemperatureLogger {
    // Event to emit when a temperature is logged
    event TemperatureLogged(uint256 date, int temperature);

    // Mapping of date to temperature
    mapping(uint256 => int) public temperatureRecords;

    // Function to log temperature
    function logTemperature(uint256 _date, int _temperature) public {
        temperatureRecords[_date] = _temperature;
        emit TemperatureLogged(_date, _temperature);
    }

    // Function to get temperature by date
    function getTemperature(uint256 _date) public view returns (int) {
        return temperatureRecords[_date];
    }
}

Keep in mind that if you deploy this on a mainnet network, the transaction fees could accumulate and get expensive. you could implement batch saves for the data to save fees.

2
  • 1
    Thanks. So somehow within the MQTT publish python script I could have a feature which also sends the data to the smart contract? Sounds ideal It's all proof of concept and won't leave the testnet. Apr 16 at 14:45
  • 1
    Yes you can use libraries like web3-py to send transactions to the smart contract on the specific chain.
    – Bastin
    Apr 16 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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