Shebin Jose Jacob
Published © Apache-2.0

Building Smart Cities with Less Sound Pollution

You can close your eyes, but not your ears.

IntermediateFull instructions provided5 hours6,812

Things used in this project

Story

Read more

Schematics

Arduino with pi

Arduino with sensor

Code

Read.ino

Arduino
For reading sound sensor data
int soundSensor=A0;
int dB;
void setup() {
 pinMode(soundSensor,INPUT);
 Serial.begin(9600);
 
}

void loop() {

  int Data=analogRead(soundSensor); 
  Serial.println(Data);
  dB = (Data+83.2073) / 11.003;//decibal value
  Serial.println(dB);
  Serial.write(dB);//Send data to the raspberry pi 3
  delay(100);
  } 

Upload.py

Python
For storing data to the tangle
from datetime import datetime
import schedule
import time
# Import GPIO library
import RPi.GPIO as GPIO
import serial
#Setup sensor as input
# Import the PyOTA library
import iota
# Import json
import json
# Define IOTA address where all transactions (cleaning records) are stored, replace with your own address.
# IOTA addresses can be created with the IOTA Wallet
CleaningLogAddr = b"XXXXXXXXXHKBWTCGKXTJGWHXEYJONN9MZZQUQLSZCLHFAWUWKHZCICTHISXBBAKGFQENMWMBOVWJTCMEWXKQDTJCV9"
# Create IOTA object, specify full node to be used when sending transactions.
# Notice that not all nodes in the field.deviota.com cluster has enabled attaching transactions to the tangle
# In this case you will get an error, you can try again later or change to a different full node.
api = iota.Iota("https://nodes.thetangle.org:443")
# Define static variable
city = "Smart District"
sector=0
def datapost():
   FB = api.send_transfer(depth=3, transfers=[pta], min_weight_magnitude=14)['bundle']
   print("success")
schedule.every(1).minutes.do(datapost)
# Main loop,
try:
   while True:
       ser = serial.Serial("/dev/ttyUSB0", 9600)#communication with pi
       status=ser.read()
       st=str(status)
       # Create json data to be uploaded to the tangle
       data1 = {'city': city, 'sector':sector,'Status': st}
       # Define new IOTA transaction
       pta = iota.ProposedTransaction(address = iota.Address(CleaningLogAddr),
                                     message = iota.TryteString.from_unicode(json.dumps(data1)),
                                     tag     = iota.Tag(b'SMARTDISTRICT'),
                                     value   = 0)
       schedule.run_pending() 
       time.sleep(1) 
except KeyboardInterrupt:
   GPIO.cleanup()

Display.py

Python
For displaying data in the terminal
 # 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'XXXXXXXXXXXXXXCGKXTJGWHXEYJONN9MZZQUQLSZCLHFAWUWKHZCICTHISXBBAKGFQENMWMBOVWJTCMEWXKQDTJCV9')]
# 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", "sector","Status"]
# 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 sound records 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
  clean_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","sector","Status"]):
      # Add table row with json values
      x.add_row([json_data['city'], json_data['sector'], json_data['Status'], clean_time])
# Sort table by cleaned datetime
x.sortby = "status"
# Print table to terminal
print(x)

Credits

Shebin Jose Jacob

Shebin Jose Jacob

34 projects • 102 followers
CEO, Co-Founder | Coders Cafe

Comments