Shebin Jose JacobDarkmatter
Published © MIT

Advanced Trash Management with IOTA

Building cleaner, greener and smarter cities with IOTA.

AdvancedFull instructions provided15 hours748

Things used in this project

Story

Read more

Schematics

Schema

Layout bb qd8u7v3vnu uivdw7vlqh

Code

sensorread

Python
#Developed by CodersCafe
from datetime import datetime
import time
import schedule
import RPi.GPIO as GPIO
  
#Setup sensor as input
sensor1 = 16
sensor2  = 12
  
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor1,GPIO.IN)
GPIO.setup(sensor2,GPIO.IN)

# Import the PyOTA library
import iota

# Import json
import json

# Define IOTA address where all transactions  are stored, replace with your own address.
# IOTA addresses can be created with the IOTA Wallet
Addr = b"RLLQQVU9ZPWF9EPOVTJ9AXVJOBQWJWDPGGMALZQANY9GWR99XPITQJQBVLYCX9XLGIGLB9TBUNDTDWYBZACGWGQSLZ"

# Create IOTA object, specify full node to be used when sending transactions.
api = iota.Iota("https://nodes.thetangle.org:443")
  
# Define static variable
city = "Smart City"
  
#Define the post function
def datapost():
   FinalBundle = api.send_transfer(depth=3, transfers=[pta], min_weight_magnitude=14)['bundle']
   FinalBundle = api.send_transfer(depth=3, transfers=[ptb], min_weight_magnitude=14)['bundle']
   print("Sucess")
  
#Schedule dataposting at 7 am
schedule.every().day.at("07:00").do(datapost)
  
#Main loop
try:
   while True:
       # Show welcome message
       print("\nWelcome to Smart City")
       print("Press Ctrl+C to exit the system")
       # Get bin number
       bin_number1 = sensor1
       print ("bin number = ",bin_number1)
       bin_number2 = sensor2
       print ("bin number = ",bin_number2)
       # Get status from bins
       id1 = GPIO.input(sensor1)
       if id1==1:
           status="Full"
       else:
           status="Not Full"
       id2 = GPIO.input(sensor2)
       if id1==1:
           status="Full"
       else:
           status="Not Full"        
       # Create json data to be uploaded to the tangle
       data1 = {'city': city, 'bin_number': bin_number1,'Status': status}
       data2 = {'city': city, 'bin_number': bin_number2,'Status': status}
       # Define new IOTA transaction
       pta = iota.ProposedTransaction(address = iota.Address(Addr),
                                     message = iota.TryteString.from_unicode(json.dumps(data1)),
                                     tag     = iota.Tag(b'SMARTCITY'),
                                     value   = 0)
       ptb = iota.ProposedTransaction(address = iota.Address(Addr),
                                     message = iota.TryteString.from_unicode(json.dumps(data2)),
                                     tag     = iota.Tag(b'SMARTCITY'),
                                     value   = 0)
       schedule.run_pending()
       time.sleep(50) 
# Clean up function when user press Ctrl+C (exit)
except KeyboardInterrupt:
   GPIO.cleanup()

displaydata

Python
#Developed by CodersCafe
# 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'RLLQQVU9ZPWF9EPOVTJ9AXVJOBQWJWDPGGMALZQANY9GWR99XPITQJQBVLYCX9XLGIGLB9TBUNDTDWYBZACGWGQSLZ')]

# 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 = [ "city", "bin_number","Status", "last_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("Please wait while retrieving data 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
   last_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 ["city","bin_number","Status"]):
       # Add table row with json values
       x.add_row([json_data['city'], json_data['bin_number'], json_data['Status'], last_time])

# Sort table by cleaned datetime
x.sortby = "last_time"

# Print table to terminal
print(x)

Credits

Shebin Jose Jacob

Shebin Jose Jacob

4 projects • 14 followers
I know only one language, the language of 0's and 1's
Darkmatter

Darkmatter

3 projects • 5 followers
God of Tech....

Comments