Julius SimSomeoneram221 / Zoey Tanjanis
Published

Smart Inventory Management System(SIMS)

Welcome to our Smart Inventory Management Systems project:)

BeginnerFull instructions provided1,084
Smart Inventory Management System(SIMS)

Things used in this project

Story

Read more

Custom parts and enclosures

IDE for the project

app used to program the M5Stack microcontroller

Schematics

Worker Management System Circuit

Manages the login history of the workers

Security System Circuit

basically keeps the inventory manager secure

Inventory Manager Circuit

Manages how much space there is inside a storeroom for physical stocks

Code

Inventory Manager Code

Python
Python code for the inventory manager
from m5stack import *
from m5ui import *
from uiflow import *
import unit

setScreenColor(0x222222)
rgb_0 = unit.get(unit.RGB, unit.PORTB)
tof_0 = unit.get(unit.TOF, unit.PORTA)



mainTitle = M5Title(title="Inventory Manager", x=90, fgcolor=0xffffff, bgcolor=0x9500ff)

greenCircle = M5Circle(25, 74, 15, 0x00ff19, 0x00ff45)
redCircle = M5Circle(25, 122, 15, 0xff0000, 0xff0000)
selectCircle = M5Circle(320, 74, 7, 0xffae00, 0xffae00)
titleGreen = M5TextBox(45, 65, "Green Threshold:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
titleRed = M5TextBox(45, 115, "Red Threshold:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)

grnThresVal = M5TextBox(223, 65, "...", lcd.FONT_DejaVu18, 0x1eff00, rotate=0)
redThresVal = M5TextBox(204, 115, "...", lcd.FONT_DejaVu18, 0xff0000, rotate=0)

barOutline = M5Rect(15, 150, 194, 40, 0x8d8d8d, 0x000000)
percentageBar = M5Rect(22, 155, 180, 30, 0x1eff00, 0x08ff00)
percentageLabel = M5TextBox(215, 162, "% Full", lcd.FONT_Default, 0xFFFFFF, rotate=0)

#Button indicators
toggle_1 = M5Img(227, 204, "res/toggle_1.png", True)
toggle_1.hide()
toggle_2 = M5Img(227, 204, "res/toggle_2.png", True)

label0 = M5TextBox(51, 198, "+", lcd.FONT_DejaVu40, 0xFFFFFF, rotate=0)
rectangle0 = M5Rect(145, 215, 30, 5, 0xFFFFFF, 0xFFFFFF)

#debug_label = M5TextBox(120, 170, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
#TOF_label = M5TextBox(130, 180, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)

STATE = 1 #Used to switch between increment and decrementing threshold values

thresholds = [100, 50] #Threshold, in cm for red and green thressholds
valLabels = [redThresVal, grnThresVal]

INCREASE_AMT = 5 #How much the values increment or decrement by


def updateLabel(textLabl, newVal):
    textLabl.setText(str(newVal))


def incrementVal(amt):
    def incrementer():
        global thresholds
        global STATE
        global INCREASE_AMT

        if STATE == 0:
            if thresholds[STATE]+amt <= thresholds[toggleState(STATE)]:
                return
        elif STATE == 1:
            if thresholds[STATE]+amt >= thresholds[toggleState(STATE)]:
                return
        if thresholds[STATE]+amt <= 0:
            return

        thresholds[STATE] += amt

        updateLabel(valLabels[STATE], str(thresholds[STATE]) + "cm")
        #updateLabel(debug_label, "INCREMENT")
    return incrementer

def toggleState(val):
    if val == 0:
        return 1
    return 0

def changeState():
    global STATE
    STATE = toggleState(STATE)

    if STATE == 1:
        selectCircle.setPosition(y=74)
        toggle_1.hide()
        toggle_2.show()
    else:
        selectCircle.setPosition(y=122)
        toggle_2.hide()
        toggle_1.show()

    #updateLabel(debug_label, "TOGGLE STATE")


start_color = [255, 0, 0]
end_color = [0, 255, 0]
def getColor(percentage):
    r_diff = end_color[0] - start_color[0]
    g_diff = end_color[1] - start_color[1]
    b_diff = end_color[2] - start_color[2]

    current_color = [
        round(start_color[0] + r_diff * percentage),
        round(start_color[1] + g_diff * percentage),
        round(start_color[2] + b_diff * percentage)
    ]

    hex_color = hex(current_color[0])
    for color_index in range(1,3):
        hex_current = hex(current_color[color_index])
        hex_color += hex_current[2:] + "0" * (4-len(hex_current))

    return int(hex_color)


btnA.wasPressed(incrementVal(INCREASE_AMT))
btnB.wasPressed(incrementVal(-INCREASE_AMT))
btnC.wasPressed(changeState)

updateLabel(valLabels[0], str(thresholds[0]) + "cm")
updateLabel(valLabels[1], str(thresholds[1]) + "cm")


while True:
    tof_val = tof_0.distance/10 - 2.3 #Distance between ToF sesor and object (cm)
    #updateLabel(TOF_label, tof_val)

    percent_full = tof_val/thresholds[0]
    if percent_full > 1:
        percent_full = 1
    elif percent_full < 0:
        percent_full = 0

    percent_full = (1 - percent_full)

    percentageBar.setBgColor(getColor(percent_full))
    percentageBar.setBorderColor(getColor(percent_full))

    barWidth = int(180*(percent_full))
    percentageBar.setSize(barWidth, 30)

    #Converting % number into 2dp string
    percent_full = str(percent_full*100)
    if len(percent_full) > 4:
        percent_full = percent_full[:5]

    updateLabel(percentageLabel, str(percent_full)+"% Full")


    if tof_val <= thresholds[1]:
        rgb_0.setColorFrom(1, 3, 0x00ff00)
    else:
        rgb_0.setColorFrom(1, 3, 0xff0000)

    wait_ms(100)

Worker Management System Code

Python
Python code for the data logger
from m5stack import *
from m5ui import *
from uiflow import *
import espnow
import wifiCfg
import unit
import json
import math

setScreenColor(0x222222)


angle_1 = unit.get(unit.ANGLE, unit.PORTB)
wifiCfg.wlan_ap.active(True)
wifiCfg.wlan_sta.active(True)
espnow.init()

cards = []
index_labels = []
id_labels = []
entrytime_labels = []

#Code to create default/template for card page
CARDS_PER_PAGE = 5
for i in range(CARDS_PER_PAGE):
  cards += [M5Rect(10, 8+i*35, 300, 30, 0x222222, 0xff0000)]
  index_labels += [M5TextBox(18, 17+i*35, "X", lcd.FONT_Default, 0xFFFFFF, rotate=0)]
  id_labels += [M5TextBox(40, 18+i*35, "XXXXXX", lcd.FONT_Default, 0xFFFFFF, rotate=0)]
  entrytime_labels += [M5TextBox(170, 18+i*35, "ENTER", lcd.FONT_Default, 0xFFFFFF, rotate=0)]

page_index_label = M5TextBox(140, 188, "[X/Y]", lcd.FONT_Default, 0xffc400, rotate=0)
sel_label = M5TextBox(130, 219, "SELECT", lcd.FONT_Default, 0xFFFFFF, rotate=0)
prv_label = M5TextBox(50, 219, "PREV", lcd.FONT_Default, 0xFFFFFF, rotate=0)
nxt_label = M5TextBox(229, 219, "NEXT", lcd.FONT_Default, 0xFFFFFF, rotate=0)
empty_page_label = M5TextBox(96, 87, "Empty Page.", lcd.FONT_DejaVu18, 0xFF0000, rotate=0)


#Creating info page
user_label = M5TextBox(60, 30, "USER:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
user_icon = M5Img(10, 17, "res/user_icon.png", True)

date_label = M5TextBox(60, 74, "DATE:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
date_icon = M5Img(10, 60, "res/date_icon.png", True)

rfid_label = M5TextBox(60, 118, "RFID:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
rfid_icon = M5Img(10, 105, "res/scan_icon.png", True)

#type_label = M5TextBox(80, 160, "TYPE:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
#rfid_icon = M5Img(30, 160, "res/door_icon.png", True)

mac_label = M5TextBox(0, 215, "MAC ADDR: ", lcd.FONT_Default, 0xFFFFFF, rotate=0)

#for menu switching
huge_box = M5Rect(0, 0, 320, 250, 0x222222, 0x222222)



CURRENT_PAGE = 1
SELECTED_INDEX = 0

CURRENT_MENU = "CARDS"
DATA_RECEIVED = None

log_db = [
]

menu_list = {
    "CARDS": [
        page_index_label,
        prv_label,
        sel_label,
        nxt_label,
        empty_page_label,
    ],
    "INFO": [
        user_label,
        user_icon,

        date_label,
        date_icon,

        rfid_label,
        rfid_icon,

        mac_label,
    ],
}

def show_menu(menuName: str):
    global CURRENT_MENU, CURRENT_PAGE
    huge_box.show()

    for menuItem in menu_list[menuName]:
        menuItem.show()
    if menuName == "CARDS":
        prepare_page(CURRENT_PAGE)
    CURRENT_MENU = menuName

def getPageItems(pageNum: int):
    return CARDS_PER_PAGE if len(log_db)/CARDS_PER_PAGE >= pageNum else len(log_db) - (pageNum-1)*CARDS_PER_PAGE


def getMaxPage():
    max_page = math.ceil(len(log_db)/CARDS_PER_PAGE)
    max_page = 1 if max_page <= 0 else max_page

    return max_page

def set_selector(index: int):
    global SELECTED_INDEX, CARDS_PER_PAGE

    SELECTED_INDEX = index
    for i in range(getPageItems(CURRENT_PAGE)):
        cards[i].setBorderColor(0xFF0000)
    cards[SELECTED_INDEX].setBorderColor(0x00FF00)

def prepare_page(pageNum: int):
    global CURRENT_PAGE, CARDS_PER_PAGE, log_db

    page_items = getPageItems(pageNum)

    if SELECTED_INDEX+1 > page_items and page_items != 0:
        set_selector(page_items-1)

    for i in range(5-page_items):
        cards[4-i].hide()
        index_labels[4-i].hide()
        id_labels[4-i].hide()
        entrytime_labels[4-i].hide()

    if getPageItems(CURRENT_PAGE) == 0:
        empty_page_label.show()
    else:
        empty_page_label.hide()

    for i in range(page_items):
        cards[i].show()
        index_labels[i].show()
        id_labels[i].show()
        entrytime_labels[i].show()

    page_beginning_index = (CARDS_PER_PAGE*pageNum)-CARDS_PER_PAGE
    page_data = log_db[page_beginning_index:page_beginning_index+(CARDS_PER_PAGE)]
    for i in range(page_items):
        index_labels[i].setText(str(page_beginning_index+i+1))

        username = page_data[i]["username"]
        if len(username) > 12:
            username = username[:13] + "..."
        id_labels[i].setText(username)
        entrytime_labels[i].setText(page_data[i]["datetime"]["date"] + " " + page_data[i]["datetime"]["time"])

    page_index_label.setText("[" + str(pageNum) + "/" + str(getMaxPage()) + "]")

    CURRENT_PAGE = pageNum


def recv_cb(_):

    global mac, DATA_RECEIVED, log_db
    mac, _, DATA_RECEIVED = espnow.recv_data(encoder='str')
    DATA_RECEIVED = DATA_RECEIVED.replace("'", '"')


    rgb.setBrightness(20)
    rgb.setColorAll(0x00ff00)

    log_db += [json.loads(DATA_RECEIVED)]

    if CURRENT_MENU == "CARDS":
        prepare_page(CURRENT_PAGE)

    rgb.setColorAll(0x000000)


set_selector(0)
show_menu("CARDS")
espnow.recv_cb(recv_cb)




def btnA_Press():
    global CURRENT_MENU

    if CURRENT_MENU == "CARDS":
        return movePage(-1)()
    else:
        return

def btnC_Press():
    global CURRENT_MENU

    if CURRENT_MENU == "CARDS":
        return movePage(1)()
    else:
        return

def movePage(increment):

  def setPage():
      if CURRENT_PAGE + increment > getMaxPage():
          return
      if CURRENT_PAGE + increment == 0:
          return
      prepare_page(CURRENT_PAGE+increment)

  return setPage


def btnB_Press():
    global CURRENT_MENU

    def showInfo():
        global CURRENT_PAGE, SELECTED_INDEX, log_db

        index = ((CARDS_PER_PAGE*CURRENT_PAGE)-CARDS_PER_PAGE) + SELECTED_INDEX
        show_menu("INFO")

        entry_data = log_db[index]
        user_label.setText("USER: " + entry_data["username"])
        date_label.setText("DATE: " + entry_data["datetime"]["date"] + " " + entry_data["datetime"]["time"])
        rfid_label.setText("RFID: " + entry_data["rfid"])
        #type_label.setText("TYPE: " + entry_data["type"])
        mac_label.setText("MAC ADDR: " + entry_data["mac"])

    def goBack():
        global CURRENT_PAGE

        show_menu("CARDS")

    if CURRENT_MENU == "CARDS" and getPageItems(CURRENT_PAGE) > 0:
        return showInfo()
    else:
        return goBack()

btnA.wasPressed(btnA_Press)
btnC.wasPressed(btnC_Press)
btnB.wasPressed(btnB_Press)


while True:
    percentage = angle_1.read() / 1024
    selection = math.ceil(percentage*5)-1

    if getPageItems(CURRENT_PAGE) != 0:
        if selection < 0:
            selection = 0
        if selection > getPageItems(CURRENT_PAGE)-1:
            selection = getPageItems(CURRENT_PAGE)-1

        if SELECTED_INDEX != selection:
            set_selector(selection)
    wait_ms(100)

Security System Code

Python
Python Code for the security system
from m5stack import *
from m5ui import *
from uiflow import *
import time
import unit
setScreenColor(0x000000)

servo_2 = unit.get(unit.SERVO, unit.PORTB)
rfid_5 = unit.get(unit.RFID, unit.PORTA)

rectangle0 = M5Rect(0, 27, 320, 30, 0x010101, 0x0000ff)

flavourText = M5TextBox(71, 107, "Text", lcd.FONT_DejaVu24, 0x00ff00, rotate=0)
scanCard = M5TextBox(40, 31, "Pls scan card", lcd.FONT_DejaVu24, 0x0000ff, rotate=0)
mainTitle = M5Title(title="Security System", x=105, fgcolor=0xFFFFFF, bgcolor=0x9500ff)
nameText = M5TextBox(71, 207, "Text", lcd.FONT_DejaVu24, 0x00ff00, rotate=0)
rightArrow = M5Img(207, 60, "res/right_arrow.png", True)


#Ram: "a23fd114d"
allowed_uids = ['7295712f2', '929811219', '626de711f9', '178b83ecf3']
names = ['Julius Sim', "Ong Roy", "Janis Foo", "Zoey"]
is_showing = True
scanCard.show()

def hideBox():
  global is_showing
  if not is_showing:
    return

  is_showing = False
  rectangle0.hide()
  scanCard.hide()
  rightArrow.hide()

def showBox():
  global is_showing
  if is_showing:
    return

  is_showing = True
  rectangle0.show()
  scanCard.show()
  rightArrow.show()

while True:

  servo_2.write_angle(0)
  flavourText.hide()
  showBox()

  if rfid_5.isCardOn():
    scanCard.hide()
    hideBox()
    rfid = rfid_5.readUid()
    if (rfid) in allowed_uids:
      flavourText.show()
      flavourText.setText("Access Granted")
      flavourText.setColor(0x00ff00)

      nameText.setText(names[allowed_uids.index(rfid)])
      nameText.show()

      rgb.setColorAll(0x33ff33)
      rgb.setBrightness(20)
      servo_2.write_angle(90)
      wait(3)
    else:
      flavourText.show()
      flavourText.setText('Invalid person')
      flavourText.setColor(0xff0000)
      speaker.tone(300, 200)

      rgb.setColorAll(0xff0000)
      rgb.setBrightness(20)
      wait(1)
    rgb.setBrightness(0)

  nameText.hide()
  wait_ms(100)

Credits

Julius Sim

Julius Sim

1 project • 1 follower
ur average hackster.io user. pls dun stalk
Someone

Someone

1 project • 1 follower
ram2

ram2

1 project • 1 follower
21 / Zoey Tan

21 / Zoey Tan

0 projects • 1 follower
read
janis

janis

0 projects • 0 followers

Comments