Temperature of earth goes on rising everyday. One of the major reasons for this effect is uncontrolled carbon dioxide emission. A major part of carbon dioxide emission is contributed by vehicles. So to ensure our sustainable existence it is time to rethink and control carbon dioxide emission. One thing we can do is to reduce use of private vehicles and promote public transport. Then to promote an effective public transport we should solve the problems now deteriorating the use of public transport.
A major issue preventing use of public transport is the unavailability of bus timings and their status ( whether they are running, where are they now etc). So in our solution we try to develop a live bus tracking system (both for bus owners (to help fleet management) and for other common users) with IOTA protocol.
This is how the solution is going to work. All bus stations are equipped with a generic RFID reader with an information about the bus station. And all the buses have a RFID module with a unique identifier. When a bus reaches a bus station the authorized person will swipe the RFID module above reader which reads the data. The data of the bus with the data of the bus station is posted to the tangle. A person going in specific route can go to the bus station and can scan the QR code and get the information of buses in the route and their live status.
Hardware Needed1. RFID module
In this project we use MFRC522 module as RFID module. Connect the RFID to raspberry pi zero according to the figure shown.
If needed follow this tutorial to set up RFID with raspberry pi.
2.Raspberry Pi
The raspberry pi is the board which is used to post the data to the tangle. Whenever a bus reaches a station and an RFID data is generated the board post the data to the tangle.
Software Needed
1. Python
Python is an interpreted, high-level, general-purpose programming language. We use python to build the whole project.
2. PyOTA: The IOTA Python API Library
This is the official Python library for the IOTA Core. It implements both the official API, as well as newly-proposed functionality (such as signing, bundles, utilities and conversion).
3. PrettyTable
PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables.
PROCEDURE1. Setup Raspberry Pi
Setup Raspberry Pi by installing Raspbian OS or any other suitable operating systems. If needed follow this tutorial.
2. Install required Software
Download and install Python or above if not previously installed. Then install other libraries like PyOTA and PrettyTable .
3. Create IOTA Address
We can create an IOTA wallet address using IOTA mobile wallet. You will find a QR code when generating new addresses using the IOTA wallet or by searching an existing address at https://thetangle.org .
4. Code
We have two python codes for this project. One which is running on the Raspberry Pi zero which is connected to RFID reader which reads and post data to tangle whenever bus reaches a bus station
# Import datetime library
from datetime import datetime
# Import GPIO library
import RPi.GPIO as GPIO
# Import simplified version of the MFRC522 library
import SimpleMFRC522
# Import the PyOTA library
import iota
# Import json
import json
# Define IOTA address where all transactionsare stored, replace with your own address.
# IOTA addresses can be created with the IOTA Wallet
Address = b"GTZUHQSPRAQCTSQBZEEMLZPQUPAA9LPLGWCKFNEVKBINXEXZRACVKKKCYPWPKH9AWLGJHPLOZZOYTALAWOVSIJIYVZ"
api = iota.Iota("https://nodes.thetangle.org:443")
# Define static variable
network = "Live Bus Tracking"
# Create RFID reader object
reader = SimpleMFRC522.SimpleMFRC522()
#define bus station number
station_number=1
try:
while True:
# Show welcome message
print("\nWelcome to the Hotel IOTA cleaning log system")
print("Press Ctrl+C to exit the system")
id, text = reader.read()
# Create json data to be uploaded to the tangle
data = {'Bus': str(id), 'station_number': station_number}
# Define new IOTA transaction
pt = iota.ProposedTransaction(address = iota.Address(Address),
message = iota.TryteString.from_unicode(json.dumps(data)),
tag = iota.Tag(b'BUSTRACK'),
value = 0)
# Print waiting message
print("\nID card detected...Sending transaction...Please wait...")
# Send transaction to the tangle
FD = api.send_transfer(depth=3, transfers=[pt], min_weight_magnitude=14)['bundle']
# Print confirmation message
print("\nTransaction sucessfully completed, have a nice day")
# Clean up function when user press Ctrl+C (exit)
except KeyboardInterrupt:
print("cleaning up")
GPIO.cleanup()
The other code is used to retrieve the data posted on the tangle. While retrieving the data the user will be able to see all the buses in the fixed route with the bus stations they have passed and the passing time.
# Imports from the PyOTA library
from iota import Iota
from iota import Address
from iota import Transaction
from iota import TryteString
# Import json library
import json
# Import datetime libary
import datetime
# Import from PrettyTable
from prettytable import PrettyTable
# Define IOTA address where all transactions are stored, replace with your own address.
address = [Address(b'GTZUHQSPRAQCTSQBZEEMLZPQUPAA9LPLGWCKFNEVKBINXEXZRACVKKKCYPWPKH9AWLGJHPLOZZOYTALAWOVSIJIYVZ')]
# Define full node to be used when retrieving cleaning records
iotaNode = "https://nodes.thetangle.org:443"
# Create an IOTA object
api = Iota(iotaNode)
# Create PrettyTable object
x = PrettyTable()
# Specify column headers for the table
x.field_names = ["Bus", "Station", "Passing time"]
# Find all transacions for selected IOTA address
result = api.find_transactions(addresses=address)
# Create a list of transaction hashes
myhashes = result['hashes']
# Print wait message
print("Fetching bus info from the tangle...")
# Loop trough all transaction hashes
for txn_hash in myhashes:
# Convert to bytes
txn_hash_as_bytes = bytes(txn_hash)
# Get the raw transaction data (trytes) of transaction
gt_result = api.get_trytes([txn_hash_as_bytes])
# Convert to string
trytes = str(gt_result['trytes'][0])
# Get transaction object
txn = Transaction.from_tryte_string(trytes)
# Get transaction timestamp
timestamp = txn.timestamp
# Convert timestamp to datetime
passing_time = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
# Get transaction message as string
txn_data = str(txn.signature_message_fragment.decode())
# Convert to json
json_data = json.loads(txn_data)
# Check if json data has the expected json tag's
if all(key in json.dumps(json_data) for key in ["Bus","station_number"]):
# Add table row with json values
x.add_row([json_data['Bus'], json_data['station_number'], passing_time])
# Sort table by cleaned datetime
x.sortby = "last_cleaned"
# Print table to terminal
x.border = True
print(x)
Comments