Josekutty Shaju
Published © Apache-2.0

Where Is My Bus?

A live bus tracking system with IOTA to avoid unnecessary waiting at bus stops.

AdvancedFull instructions provided20 hours2,333
Where Is My Bus?

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
RFID Module (Generic)
×1
RFID reader (generic)
×1

Software apps and online services

IOTA Tangle
IOTA Tangle

Story

Read more

Schematics

Schema

Code

rfid_post

Python
# 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()

data_read

Python
# 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) 

Credits

Josekutty Shaju

Josekutty Shaju

1 project • 3 followers
Metamorphosis

Comments