The time is now capable to write down a definition of what a smart city is. Like its ancestors, smart city now has all the factors in place to become a reality. The cities that grew out of the agricultural revolution, and then later the industrial revolution, were driven by similar factors — increased population and technological advances that facilitated and drove a change in living conditions. The modern city has these same factors, but on a much larger scale; the global population, as mentioned earlier, is growing exponentially and we need ways of dealing with it.
The pressure of population on resources is a major factor to smarten up our current city models. And, the technology to do so is finally here in the form of hyper-connectivity, improved and cheaper sensors, AI, and data analytics. The clue here is in the data; data is the new energy in the modern smart city. Data, or rather the analysis and application of these data, will be the pivot upon which the smart city turns. So a smart city should have a combination of ways technology improves important aspects and tasks of cities, like monitoring water and air, waste management, parking, lights, and vehicles.
In this project we focus on improving smart waste management in a district ( encompassing many smart cities) with help of smart bins and IOTA tangle to accelerate the process of smart waste management and reduce congestion, pollution and help in improving energy optimization.
One of the major issues in waste management is non-optimized garbage collection truck routes.Truck routes that are not optimized lead to the use of excessive fuel and produce congestion in densely populated cities. In addition, some bins may end up being overfilled and others under-filled as a result.So what we are actually going to solve is the optimization of garbage collection truck routes so as to avoid the congestion and excessive fuel usage.
This is how the project is going to work. We are connecting fill level sensors in the waste bins which continuously monitors the fill level inside the bin. At every morning at a fixed time (say, 7.00 am) the data is uploaded to the tangle. A garbage truck driver is able to retrieve data from the node using a suitable device and he can optimize the truck route.
Hardware Components1. IR Proximity Sensor
The principle of an IR sensor working as an Object Detection Sensor can be explained using the following figure. An IR sensor consists of an IR LED and an IR Photodiode; together they are called as Photo – Coupler or Opto – Coupler.
When the IR transmitter emits radiation, it reaches the object and some of the radiation reflects back to the IR receiver. Based on the intensity of the reception by the IR receiver, the output of the sensor is defined.
The IR proximity sensor works as the fill level sensors. The proximity sensor is placed inside the bin about the top of the bin.
Once the bin is filled the sensor returns 1 and if not it returns 0. (The ultrasonic sensor can be used instead and is widely used. For simplicity we are using IR proximity sensor.)
2.Raspberry Pi
The raspberry pi is the board which is used to post the data to the tangle. For every bins we are connecting a Raspberry Pi zero with an infrared proximity sensor. The sensor monitors the fill level and the Raspberry Pi zero post the data to the tangle everyday morning at a prefixed time.
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 3.5 or above if not previously installed. Then install other libraries like PyOTA and PrettyTable.
If pip is installed enter following commands in the terminal to install the libraries.
pip install pyota
pip install 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 with the sensor. This python code reads the data from the sensor continuously and sends the bin number and the status of the bin to tangle.
sensorread.py
#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("Success")
#Schedule data posting at 7 am
schedule.every().day.at("07:00").do(datapost)
#Main loop
try:
while True:
# Show welcome message
print("\n Welcome 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()
The other python code is used by the garbage truck driver or any other authorized person. This code reads data from the tangle and displays data of bins in a comprehensive manner.
displaydata.py
#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)
5. Working
Connect the IR sensor to the Raspberry Pi zero and place the fill level senor inside the waste bin. Then run the code sensorread.py in the Raspberry Pi zero and keep it working forever and it will return the fill level status to tangle everyday morning at a fixed time seamlessly.
Then run the code displaydata.py in an android device or a computer (or another Raspberry Pi). It will display the data with bin numbers and their fill level status. By taking a look on the data driver can optimize the truck route.
Comments