Sample project that reads sensor measurements (temperature and humidity), stores them in a MongoDB database, and, every 10 records, sends the hashes of the readings to the Blockchain (Solidity contract).
The simple frontend allows you to view the data, hashes, and direct links to Etherscan.
- Backend: Node.js + Express
- Database: MongoDB + Mongoose
- Blockchain: Solidity contract (Sepolia testnet)
- Frontend: Simple HTML, CSS, and JavaScript
- Others: CORS, REST API
- Clone the repository
git clone https://github.com/tiagogalvao7/iot-blockchain.git
cd iot-blockchain- Create
.envwith the variables:INFURA_URL=https://sepolia.infura.io/v3/YOUR_PROJECT_ID PRIVATE_KEY=PRIVATE_KEY CONTRACT_ADDRESS=CONTRACT_ADDRESS
- Install dependency
npm install - Configure variables (MongoDB URI, Ethereum private key, contract address).
- Start server
npm start
- Receive measurements (temperature + humidity) via POST.
- Save data and generate SHA256 hash for each record.
- For every 10 unsent records, send the hashes in batches to the Blockchain.
- Save the transaction's
txHashin MongoDB. - Endpoint to list data + hash + direct link to Etherscan.
POST /api/sensor-data→ Sends new measurement.GET /api/sensor-data→ Lists all measurements.GET /api/sensor-data/with-etherscan→ Lists measurements + Etherscan link.
Here's a quick look at the table in action:
