Hello everyone, In the last article I talked about the implementation of a database system using Tangle and IPFS. Today I am going to present a use case for my database. I will create a recycling system. The recycling industry faces a serious challenge, The quantities of recycled materials is inconsistent and hard to estimate. So we will use Blockchain to solve this problem.

Application Scenario

Consumer#A wants to recycle his laptop, smart-phone or watch… whatever. Consumer#A scans his consumed product using NFC on his smartphone and go to our smart Trash. Our smart trash scan the NFC tag and send Consumer#A some MIOTA as rewards for recycling.

Diagram will be explained in details next

How It works

Assume a laptop factory interested in recycling his laptop after consumption by the user.

1- Factory well send all his laptop data and IDs to our system to be saved on our system. (IDs will be the key to access each laptop)

2- All laptops will be saved with states like:’ not found, found by Consumer #a, Found at trash#B’

The database will look like this:

3- All laptops will have NFC tags to write the ID.

4-Now consumer will scan the laptop he wants to recycle. Once you scan the laptop an API call will set the consumer as the owner of the recycled laptop.

Scan product using your phone

5- Whenever the consumer puts his laptop to our trash he will earn tokens as rewards.

The smart Trash scan product and check its owner to give token.

API implementation

First, I will start with the API design it will look like this:

Recycler Sequence Diagram.

How each API endpoint work:-

‘init’ get-call to initialize the MAM root and get the database ready to use (it shouldn’t be made as API call but this will change on the second release).

Route:

app.route(‘/init’)

.get(recycler.init);

Controller:

exports.init = async (req,res) =>{

const response = await manageClients.init()

res.json('DB initialized with some clients with root',response )

}

Implementation:( will initialize the root of MAM and add client dummy data )

const init = async()=>{ products['HELLODRAYMANRNFELVTJREBCXJETCFEUGXBZHEGHCJHIYIFPQEQGDXILJZYUQMOYSELXIG9KUYOSYBFAY'] = { 'name':'product01', 'address':'HELLODRAYMANRNFELVTJREBCXJETCFEUGXBZHEGHCJHIYIFPQEQGDXILJZYUQMOYSELXIG9KUYOSYBFAY', 'owner':false, 'producer':'client01' } products['AB9CCUACOSC9VHQQFSVUGOVLAGNYJYDPROPBYTZIGNVOF9KMFNXBMSUGFFA9HTVHBPKPGGIBGSRJPUXZK'] = { 'name':'product02', 'address':'AB9CCUACOSC9VHQQFSVUGOVLAGNYJYDPROPBYTZIGNVOF9KMFNXBMSUGFFA9HTVHBPKPGGIBGSRJPUXZK', 'owner':false, 'producer':'client01' } products['LHSQELEGSRM9IAQCUBPXOLYXDMFOAPNJFS9JPOJBSRUDJAIRTWHTZCJCFLVYVRJPGPNXEWDDRGTCGKRAB'] = { 'name':'product03', 'address':'LHSQELEGSRM9IAQCUBPXOLYXDMFOAPNJFS9JPOJBSRUDJAIRTWHTZCJCFLVYVRJPGPNXEWDDRGTCGKRAB', 'owner':false, 'producer':'client01' } const ipfsHash = await addIPFS.execute(products) const root = await mamManage.send(ipfsHash) return root }

‘addClient’ post-call producer/recycler register to my service.

Route:

app.route('/addClient') .post(recycler.addClient);

Controller:

exports.addClient = async (req,res) =>{

const response = await manageClients.addNewClient(req.body.root,req.body.products)

res.json("added new client succefuly "+response)

}

Implementation:

const addNewClient = async (_root,_products)=>{

const currentPropretiesRoot = _root

const currentPropretiesHash = await mamManage.fetch(currentPropretiesRoot)

const currentPropretiesString = await catIPFS.execute(getLastHash(currentPropretiesHash))

const currentPropretiesJSON = JSON.parse(currentPropretiesString)

const productsJSON = JSON.parse(JSON.stringify(_products))

for(let i = 0; i < productsJSON.length;i++){

currentPropretiesJSON[productsJSON[i].address] = productsJSON[i]

}

const newPropretiesHash = await addIPFS.execute(currentPropretiesJSON)

const newPropretiesRoot = await mamManage.send(newPropretiesHash)

return newPropretiesRoot

}

‘addOwner’ post-call will make the consumer owner of a certain product.

Route:

app.route('/addOwner')

.post(recycler.addOwner);

Controller:

exports.addOwner = async (req,res) =>{

const response = await manageClients.addNewOwner(req.body.root,req.body.productAddress,req.body.ownerAddress)

res.json("added new owner "+ response)

}

Implementation:

const addNewOwner = async(_root,productAddress,ownerAddress)=>{

const proprietiesHash = await mamManage.fetch(_root)

const currentPropretiesString = await catIPFS.execute(getLastHash(proprietiesHash))

const currentPropretiesJSON = JSON.parse(currentPropretiesString)

currentPropretiesJSON[productAddress].owner = ownerAddress

const newPropretiesHash = await addIPFS.execute(currentPropretiesJSON)

const root = await mamManage.send(newPropretiesHash)

return root

}

‘giveReward’ post-call will send a reward to the owner of the product.

Route:

app.route('/giveReward')

.post(recycler.giveReward);

Controller:

exports.giveReward = async (req,res) =>{

const response = await giveReward.execute(req.body.root,req.body.productAddress)

res.json(response)

}

Implementation:

const sendToken = require('./sendToken')

const fetchToMap = require('./fetchToMap')

giveRewards = async(root,productAddress) =>{

const OwnerAddrress = await fetchToMap.execute(root)

console.log(OwnerAddrress[productAddress].owner)

await sendToken.execute(OwnerAddrress[productAddress].owner,1)

return "Send 1 i. Congrats"

}

Demo

Now, To demonstrate my project I made an android app for consumers and for smart Trash. Short video (1:16)

You can find the android app & API on the following repository: https://github.com/yehia67/Recycling-System-IOTA

What To Do Next

For the next release, I will add Datamarketplace and accountHub to my project to create a full system and may use ESP32 for the smart trash.