Creating Data Generator


# Group 1 : Vlad, Dinesh, Geerthan, Jai, Rohan

import random, time

start_id = 111

def generator() -> int:
    '''    
    This generator gives you a uniform random number in a 0 to 10    
    '''
    return random.randint(0, 10)

def create_data():
    global start_id
    start_id += 1

    payload = {
        'Request Time': time.asctime(),
        'LOD': generator()
    }
    
    return payload

def print_data(payload):
    print('__________________________________________')
    print(f'Time Requested: {payload["Request Time"]}')
    print(f'LOD Milliseconds: {payload["LOD"]}')
    

Creating a Publisher


# Group 1 : Vlad, Dinesh, Geerthan, Jai, Rohan

import paho.mqtt.client as mqtt
import group_1_util as util
import json, time
import random

client = mqtt.Client()
client.connect('localhost', 1883)

client.loop_start()
for x in range(100): #run loop for 100 times
    dictionary = util.create_data()
    data = json.dumps(dictionary)
    
    if(random.randint(0, 100) == 100): #About 1 in 100 chance
        client.publish('FinalProject', payload=data)
        print(f'Message sent')

    else:
        print(f'No Message sent')
    time.sleep(0.3)
client.loop_stop()
client.disconnect()

Creating a Subscriber


# Group 1 : Vlad, Dinesh, Geerthan, Jai, Rohan

import paho.mqtt.client as mqtt
import group_1_util as util
import json
import time, random
from tkinter import *

yStart = 100
xStart = 600
xFinish = 625
valueText = ""
color = ""
green = '#6CC644'
red = '#BD2C00'

window = Tk()
window.option_add('*Font', '8')
window.title("Group 1 Final Project")
window.geometry('800x800')

canvas = Canvas(window)
canvas.create_text(80, 25, text = "Group 1 Final Project")

def putLabel(obj):
    global yStart, xStart, xFinish, color, green, red
    if obj["Request Time"] is None:
        obj["Request Time"] = time.asctime()

    canvas.create_text(150, yStart, text = f'Requested Time: {obj["Request Time"]}')

    if obj["LOD"] is None:
        obj["LOD"] = util.generator()

    elif obj["LOD"] is not None:
        if obj["LOD"] >= 0 and obj["LOD"] <= 5: color = green elif obj["LOD"] > 5 and obj["LOD"] <= 10: color = red if obj["LOD"] == 0 : valueText = 'Invalid number' elif obj["LOD"] > 0 and obj["LOD"] <= 10:
            valueText = f'{obj["LOD"]}'
            canvas.create_rectangle(
            xStart, yStart - (obj["LOD"] * 5), 
            xFinish, (yStart + 10), fill = color)
    
    canvas.create_text(400, yStart, text = f'\t LOD Milliseconds: {valueText}')

    yStart += 100
    window.update()

def conversion(client, userdata, message):
    data = message.payload.decode("utf-8")
    obj = json.loads(data)
    putLabel(obj)

client = mqtt.Client()
client.on_message = conversion
client.connect('localhost', 1883)
client.subscribe('FinalProject')

print(f'Subscriber is listening')

canvas.pack(fill=BOTH, expand=1)

client.loop_start()
window.mainloop()

About Me

Rohan Juneja's photo

Rohan Juneja

I am a student at Centennial College studying Software Engineering Technology – Interactive gaming.

I worked on various software projects during school. I am detail-oriented individual with strong organizational ability. I am skilled at working closely with others in a team environment and have a flexible approach to work.

Contact Details

Phone: +1 (343) 235-2301
Email: me@rohanjuneja.com
Web: rohanjuneja.com

Toronto, ON
Canada

Add me on my social profiles to stay connected to me.

Contact Me

Video Demo: