Jia Juan KooMatt dooJoon Lee ThongThaddeus Kee
Published

Sustainable Vending Machines

Making vending machines more energy efficient and sustainable.

BeginnerFull instructions provided423
Sustainable Vending Machines

Things used in this project

Hardware components

Mini RFID Unit RC522 Module Sensor
M5Stack Mini RFID Unit RC522 Module Sensor
only cashless payment
×1
ENV II Unit with Temperature Humidity Pressure Sensor(SHT30+BMP280)
M5Stack ENV II Unit with Temperature Humidity Pressure Sensor(SHT30+BMP280)
ensure the temperature of the machine stays optimal for chilled drinks
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
turns the spiral thing in the machine to dispense the product
×1
Light Unit with Photo-resistance
M5Stack Light Unit with Photo-resistance
in the day, the vending machine lights will be dimmer
×1
PIR Sensor Human Body Infrared PIR Motion Sensor (AS312)
M5Stack PIR Sensor Human Body Infrared PIR Motion Sensor (AS312)
checks if people are at/using the machine. If no one is there, lights of the vending machine will dim, saving energy.
×1
I2C Hub 1 to 6 Expansion TCA9548A Module
M5Stack I2C Hub 1 to 6 Expansion TCA9548A Module
×1

Software apps and online services

M5stack Uiflow

Story

Read more

Code

m5stack_1

Python
- User interface for vending machine
- Input from PIR sensor to auto-dim/ auto-bright
- 2-way communication with m5stack_2 via ESP NOW
- Activates servo motor if payment successful
from m5stack import *
from m5ui import *
from uiflow import *
import espnow
import wifiCfg
import time
import unit

setScreenColor(0x222222)
servo_0 = unit.get(unit.SERVO, unit.PORTA)
pir_0 = unit.get(unit.PIR, unit.PORTB)


mac = None
Data_send = None
quantity = None
price = None

wifiCfg.wlan_ap.active(True)
wifiCfg.wlan_sta.active(True)
espnow.init()

title0 = M5Title(title="vending_ui", x=3, fgcolor=0xFFFFFF, bgcolor=0x0000FF)
label0 = M5TextBox(190, 33, "Item", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
label1 = M5TextBox(145, 78, "Quantity:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
quantity_lbl = M5TextBox(253, 78, "Text", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
label2 = M5TextBox(134, 198, "+", lcd.FONT_DejaVu40, 0xFFFFFF, rotate=0)
label3 = M5TextBox(58, 198, "-", lcd.FONT_DejaVu40, 0xFFFFFF, rotate=0)
label4 = M5TextBox(207, 208, "Checkout", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
label5 = M5TextBox(146, 109, "Price:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
price_lbl = M5TextBox(252, 109, "Text", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
mac_lbl = M5TextBox(1, 20, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
image0 = M5Img(16, 52, "res/default.jpg", True)

from numbers import Number




def recv_cb(_):
  global mac,Data_send,quantity,price
  mac, _, Data_send = espnow.recv_data(encoder='str')
  lcd.setBrightness(100)
  servo_0.write_angle(0)
  wait(1)
  servo_0.write_angle(90)

  pass
espnow.recv_cb(recv_cb)


def buttonA_wasPressed():
  global mac, Data_send, quantity, price
  quantity = (quantity if isinstance(quantity, Number) else 0) + -1
  price = 2.5 * quantity
  pass
btnA.wasPressed(buttonA_wasPressed)

def buttonB_wasPressed():
  global mac, Data_send, quantity, price
  quantity = (quantity if isinstance(quantity, Number) else 0) + 1
  price = 2.5 * quantity
  pass
btnB.wasPressed(buttonB_wasPressed)

def buttonC_wasPressed():
  global mac, Data_send, quantity, price
  espnow.add_peer('98:f4:ab:6b:a1:71', id=1, channel=0, ifidx=0, encrypt=False)
  espnow.send(id=1, data=str(price))
  quantity = 1
  price = 2.5
  pass
btnC.wasPressed(buttonC_wasPressed)


mac_lbl.setText(str(espnow.get_mac_addr()))
quantity = 1
price = 2.5
while True:
  if (pir_0.state) == 0:
    lcd.setBrightness(1)
  else:
    lcd.setBrightness(100)
  quantity_lbl.setText(str(quantity))
  price_lbl.setText(str(price))
  wait_ms(2)

m5stack_2

Python
- User interface for payment process, input data from sensors, product sales data
- Collect input from RFID, light, env sensors
- Processes payment
- 2-way communication with m5stack_1 via ESP NOW
- Product sales data storage via IoT service EZData
from m5stack import *
from m5ui import *
from uiflow import *
import espnow
import wifiCfg
from flow import ezdata
import unit


setScreenColor(0x222222)
rfid0 = unit.get(unit.RFID, unit.PAHUB0)
light0 = unit.get(unit.LIGHT, unit.PORTB)
env21 = unit.get(unit.ENV2, unit.PAHUB1)


mac = None
Data_send = None
stock = None
transactions = None
sold = None
quantity = None

wifiCfg.wlan_ap.active(True)
wifiCfg.wlan_sta.active(True)
espnow.init()
wifiCfg.autoConnect(lcdShow=False)

price = M5TextBox(179, 109, "Text", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
title0 = M5Title(title="paymentSystem", x=3, fgcolor=0xFFFFFF, bgcolor=0xff0047)
label3 = M5TextBox(66, 109, "Total Price:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
stock_lbl = M5TextBox(117, 27, "stock_lbl", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label5 = M5TextBox(0, 77, "Transactions:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
sales_lbl = M5TextBox(142, 82, "sales_lbl", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label1 = M5TextBox(0, 22, "Stock left:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
label6 = M5TextBox(9, 112, "Internal Temp:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label7 = M5TextBox(52, 134, "Lighting:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
lighting = M5TextBox(115, 134, "lighting", lcd.FONT_Default, 0xFFFFFF, rotate=0)
temp = M5TextBox(267, 112, "temp", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label8 = M5TextBox(0, 49, "Stock sold:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
sold_lbl = M5TextBox(124, 54, "sold_lbl", lcd.FONT_Default, 0xFFFFFF, rotate=0)
scan = M5TextBox(41, 160, "Please scan to pay", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
label2 = M5TextBox(41, 214, "Back", lcd.FONT_Default, 0xFFFFFF, rotate=0)

from numbers import Number


# Describe this function...
def stats_hide():
  global mac, Data_send, stock, transactions, sold, quantity
  label1.hide()
  label5.hide()
  sold_lbl.setPosition(x=999)
  stock_lbl.setPosition(x=999)
  sales_lbl.setPosition(x=999)
  label2.setPosition(x=999)
  label6.setPosition(x=999)
  label7.hide()
  label8.hide()
  lighting.setPosition(x=999)
  temp.setPosition(x=999)

# Describe this function...
def stats_show():
  global mac, Data_send, stock, transactions, sold, quantity
  label1.show()
  label5.show()
  stock_lbl.setText(str(stock))
  sales_lbl.setText(str(transactions))
  sold_lbl.setText(str(sold))
  sold_lbl.setPosition(x=120)
  sales_lbl.setPosition(x=142)
  stock_lbl.setPosition(x=120)
  label2.setPosition(x=41)
  label6.setPosition(x=10)
  label7.show()
  label8.show()
  lighting.setPosition(x=115)
  temp.setPosition(x=115)
  lcd.setBrightness(100)
  while not (btnA.isPressed()):
    lighting.setText(str(light0.analogValue))
    temp.setText(str(env21.temperature))

# Describe this function...
def payment_hide():
  global mac, Data_send, stock, transactions, sold, quantity
  label3.hide()
  scan.hide()
  price.hide()
  label2.setPosition(x=999)
  lcd.setBrightness(0)

# Describe this function...
def payment_show():
  global mac, Data_send, stock, transactions, sold, quantity
  label3.show()
  scan.show()
  price.show()
  label2.setPosition(x=41)
  lcd.setBrightness(100)



def recv_cb(_):
  global mac,Data_send,stock,transactions,sold,quantity
  mac, _, Data_send = espnow.recv_data(encoder='str')
  payment_show()
  scan.setText('Please scan to pay')
  price.setText(str(Data_send))
  while not (btnA.isPressed()):
    if rfid0.isCardOn():
      espnow.add_peer('98:f4:ab:6b:a3:85', id=2)
      espnow.send(id=2, data=str('test'))
      scan.setText('Payment successful!')
      speaker.tone(800, 200)
      transactions = (transactions if isinstance(transactions, Number) else 0) + 1
      ezdata.setData('m4kkE3iXSzN01jLI6QN0D9bytn23prU5', 'transactions', transactions)
      quantity = float((float(Data_send) / 2.5))
      stock = (stock if isinstance(stock, Number) else 0) + int((quantity * -1))
      sold = (sold if isinstance(sold, Number) else 0) + int(quantity)
      ezdata.setData('m4kkE3iXSzN01jLI6QN0D9bytn23prU5', 'stock_sold', sold)
      break
  payment_hide()

  pass
espnow.recv_cb(recv_cb)


def buttonB_wasPressed():
  global mac, Data_send, stock, transactions, sold, quantity
  payment_hide()
  stats_show()
  pass
btnB.wasPressed(buttonB_wasPressed)

def buttonA_wasPressed():
  global mac, Data_send, stock, transactions, sold, quantity
  payment_hide()
  stats_hide()
  pass
btnA.wasPressed(buttonA_wasPressed)


lcd.setBrightness(100)
stock = 10
transactions = int((ezdata.getData('m4kkE3iXSzN01jLI6QN0D9bytn23prU5', 'transactions')))
sold = int((ezdata.getData('m4kkE3iXSzN01jLI6QN0D9bytn23prU5', 'stock_sold')))
payment_hide()
stats_hide()
quantity = 0

Credits

Jia Juan Koo

Jia Juan Koo

1 project • 3 followers
Matt doo

Matt doo

0 projects • 3 followers
I love engineering
Joon Lee Thong

Joon Lee Thong

0 projects • 2 followers
Thaddeus Kee

Thaddeus Kee

0 projects • 2 followers

Comments