Kevin LeeCayden Theseirahaoweeeen
Published

Automatic Shutting System

An efficient addon to pre-existing freezers in supermarkets, to reduce the cost of the freezers by automatically closing the doors.

IntermediateShowcase (no instructions)130
Automatic Shutting System

Things used in this project

Hardware components

M5Stack FIRE IoT Development Kit (PSRAM 2.0)
M5Stack FIRE IoT Development Kit (PSRAM 2.0)
×2
Time-of-Flight (ToF) VL53L0X Laser Ranging Unit (MCP4725/)
M5Stack Time-of-Flight (ToF) VL53L0X Laser Ranging Unit (MCP4725/)
×1
PIR Sensor Human Body Infrared PIR Motion Sensor (AS312)
M5Stack PIR Sensor Human Body Infrared PIR Motion Sensor (AS312)
×1
ENV III Unit with Temperature Humidity Air Pressure Sensor (SHT30+QMP6988)
M5Stack ENV III Unit with Temperature Humidity Air Pressure Sensor (SHT30+QMP6988)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1

Software apps and online services

M5Stack UIFlow
Qubitro
Qubitro

Hand tools and fabrication machines

Scissors, Free Fall
Scissors, Free Fall
Hot glue gun (generic)
Hot glue gun (generic)
Tape, Clear
Tape, Clear
Tape, Double Sided
Tape, Double Sided

Story

Read more

Code

Code for M5Stack 1

Python
This code manages the main logic for the automatic shutting system. And communicates with both the 2nd M5Stack and the dashboard.
from m5stack import *
from m5ui import *
from uiflow import *
import espnow
import wifiCfg
import time
from m5mqtt import M5mqtt
import json

import unit


setScreenColor(0x222222)
tof_0 = unit.get(unit.TOF, unit.PAHUB1)
pir_0 = unit.get(unit.PIR, unit.PORTB)
pahub0 = unit.get(unit.PAHUB, unit.PORTA, 0x70)
env3_1 = unit.get(unit.ENV3, unit.PAHUB0)


modes = None
temp = None
state = None
current_temp = None
temp_state = None
distance = None
last_msg = None
temp_variable = None
Warning2 = None
critical_temp = None
Human = None
j = None
beep_return = None
i = None

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

label0 = M5TextBox(130, 50, "-", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label1 = M5TextBox(130, 80, "-", lcd.FONT_Default, 0xFFFFFF, rotate=0)
DataTransmit = M5Title(title="DataTransmit", x=6, fgcolor=0xFFFFFF, bgcolor=0x0000FF)
label2 = M5TextBox(14, 55, "Detection Range:", lcd.FONT_DefaultSmall, 0xFFFFFF, rotate=0)
label3 = M5TextBox(14, 80, "Door distance:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label4 = M5TextBox(169, 197, "Last State: ", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label5 = M5TextBox(25, 214, "Timer:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label10 = M5TextBox(176, 80, "0", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
label13 = M5TextBox(176, 41, "Detection Temp:", lcd.FONT_DefaultSmall, 0xFFFFFF, rotate=0)
label6 = M5TextBox(169, 165, "-", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
label14 = M5TextBox(272, 41, "0", lcd.FONT_DefaultSmall, 0xFFFFFF, rotate=0)
label7 = M5TextBox(76, 205, "0", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
label15 = M5TextBox(290, 41, "°C", lcd.FONT_DefaultSmall, 0xFFFFFF, rotate=0)
label8 = M5TextBox(169, 217, "-", lcd.FONT_DefaultSmall, 0xFFFFFF, rotate=0)
rectangle0 = M5Rect(25, 136, 60, 60, 0xFFFFFF, 0xFFFFFF)
label9 = M5TextBox(169, 144, "Human Presence:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label11 = M5TextBox(210, 83, "°C", lcd.FONT_UNICODE, 0xFFFFFF, rotate=0)
image0 = M5Img(253, 60, "res/fire-small.png", False)
label12 = M5TextBox(176, 60, "Temp:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
line0 = M5Line(M5Line.PLINE, 10, 120, 310, 120, 0xFFFFFF)
line1 = M5Line(M5Line.PLINE, 160, 30, 160, 230, 0xFFFFFF)
Add = M5TextBox(54, 225, "Add", lcd.FONT_Default, 0xFFFFFF, rotate=0)
Minus = M5TextBox(130, 225, "Minus", lcd.FONT_Default, 0xFFFFFF, rotate=0)
Modes = M5TextBox(220, 225, "Modes", lcd.FONT_Default, 0xFFFFFF, rotate=0)

from numbers import Number
import math


# Describe this function...
def MQTT():
  global modes, temp, state, current_temp, temp_state, distance, last_msg, temp_variable, Warning2, critical_temp, Human, j, beep_return, i
  if (tof_0.distance) < distance:
    state = 'Opened'
  else:
    state = 'Closed'
  current_temp = env3_1.temperature
  m5mqtt.publish(str('f8e49263-8704-43e4-a1b5-9d366e81806d'), str((json.dumps(({'human':(pir_0.state),'distance':(tof_0.distance),'temperature':current_temp,'door-state':state,'warning':Warning2,'temp-state':temp_state})))), )
  wait(1)

# Describe this function...
def temp_warnings():
  global modes, temp, state, current_temp, temp_state, distance, last_msg, temp_variable, Warning2, critical_temp, Human, j, beep_return, i
  if (env3_1.temperature) > temp + 5:
    temp_state = 'Critical'
    label10.setColor(0xff0000)
    label11.setColor(0xff0000)
    espnow.send(id=1, data=str('temp-warn'))
    wait(1)
    last_msg = 'temp-warn'
    image0.show()
    temp_variable = False
    while (env3_1.temperature) > temp + 5:
      espnow.send(id=1, data=str('temp-warn'))
      wait(2)


def multiBtnCb_CB():
  global modes, temp, state, current_temp, temp_state, distance, last_msg, temp_variable, Warning2, critical_temp, Human, j, beep_return, i
  espnow.send(id=1, data=str('test'))
  DataTransmit.setBgColor(0xff0000)
  wait_ms(100)
  DataTransmit.setBgColor(0x3333ff)
  DataTransmit.show()
  espnow.send(id=1, data=str('sound fazbear'))
  label8.setText('Test')
  pass
btn.multiBtnCb(btnC,btnB,multiBtnCb_CB)

def multiBtnCb_AB():
  global modes, temp, state, current_temp, temp_state, distance, last_msg, temp_variable, Warning2, critical_temp, Human, j, beep_return, i
  if modes:
    label2.setColor(0x33cc00)
    label13.setColor(0xffffff)
    distance = 60
    label0.setColor(0xff0000)
    label0.setText(str(distance))
    wait_ms(30)
    label0.setColor(0xffffff)
  else:
    label13.setColor(0x33cc00)
    label2.setColor(0xffffff)
    temp = 32
    label14.setColor(0xff0000)
    label14.setText(str(temp))
    wait_ms(30)
    label14.setColor(0xffffff)
  pass
btn.multiBtnCb(btnA,btnB,multiBtnCb_AB)

def multiBtnCb_AC():
  global modes, temp, state, current_temp, temp_state, distance, last_msg, temp_variable, Warning2, critical_temp, Human, j, beep_return, i
  label0.hide()
  label1.hide()
  label2.hide()
  label3.hide()
  label4.hide()
  label5.hide()
  label6.hide()
  label7.hide()
  label8.hide()
  label9.hide()
  label10.hide()
  label11.hide()
  label12.hide()
  label13.hide()
  label14.hide()
  label15.hide()
  rectangle0.hide()
  line0.hide()
  line1.hide()
  Add.show()
  Minus.show()
  Modes.show()
  wait(1)
  label0.show()
  label1.show()
  label2.show()
  label3.show()
  label4.show()
  label5.show()
  label6.show()
  label7.show()
  label8.show()
  label9.show()
  label10.show()
  label11.show()
  label12.show()
  label13.show()
  label14.show()
  label15.show()
  rectangle0.show()
  line0.show()
  line1.show()
  Add.hide()
  Minus.hide()
  Modes.hide()
  pass
btn.multiBtnCb(btnA,btnC,multiBtnCb_AC)

def buttonA_wasPressed():
  global modes, temp, state, current_temp, temp_state, distance, last_msg, temp_variable, Warning2, critical_temp, Human, j, beep_return, i
  if modes:
    label2.setColor(0x33cc00)
    label13.setColor(0xffffff)
    distance = (distance if isinstance(distance, Number) else 0) + -1
    if distance < 0:
      distance = 0
    label0.setColor(0xff0000)
    label0.setText(str(distance))
    wait_ms(30)
    label0.setColor(0xffffff)
  else:
    temp = (temp if isinstance(temp, Number) else 0) + -1
    label13.setColor(0x33cc00)
    label2.setColor(0xffffff)
    label14.setColor(0xff0000)
    label15.setColor(0xff0000)
    label14.setText(str(temp))
    wait_ms(30)
    label14.setColor(0xffffff)
    label15.setColor(0xffffff)
  pass
btnA.wasPressed(buttonA_wasPressed)

def buttonB_wasPressed():
  global modes, temp, state, current_temp, temp_state, distance, last_msg, temp_variable, Warning2, critical_temp, Human, j, beep_return, i
  if modes:
    label2.setColor(0x33cc00)
    label13.setColor(0xffffff)
    distance = (distance if isinstance(distance, Number) else 0) + 1
    label0.setColor(0xff0000)
    label0.setText(str(distance))
    wait_ms(30)
    label0.setColor(0xffffff)
  else:
    temp = (temp if isinstance(temp, Number) else 0) + 1
    label13.setColor(0x33cc00)
    label2.setColor(0xffffff)
    label14.setColor(0xff0000)
    label15.setColor(0xff0000)
    label14.setText(str(temp))
    wait_ms(30)
    label14.setColor(0xffffff)
    label15.setColor(0xffffff)
  pass
btnB.wasPressed(buttonB_wasPressed)

def buttonC_wasPressed():
  global modes, temp, state, current_temp, temp_state, distance, last_msg, temp_variable, Warning2, critical_temp, Human, j, beep_return, i
  if modes:
    modes = False
    label2.setColor(0xffffff)
    label13.setColor(0x33cc00)
  else:
    modes = True
    label13.setColor(0xffffff)
    label2.setColor(0x33cc00)
  pass
btnC.wasPressed(buttonC_wasPressed)


wifiCfg.doConnect('cayden', '12345678')
if wifiCfg.wlan_sta.isconnected():
  espnow.add_peer('78:21:84:93:ac:4d', id=1)
  m5mqtt = M5mqtt('f8e49263-8704-43e4-a1b5-9d366e81806d', 'broker.qubitro.com', 1883, 'f8e49263-8704-43e4-a1b5-9d366e81806d', 'Qq67mrTj9AZOi5CfPcEE246GgHeDLPm-6lbupCX6', 300)
  m5mqtt.start()
  modes = True
  distance = 60
  temp = 26
  Add.hide()
  Minus.hide()
  Modes.hide()
  while True:
    critical_temp = temp + 5
    label14.setText(str(temp))
    if modes:
      label2.setColor(0x33cc00)
      label13.setColor(0xffffff)
    else:
      label13.setColor(0x33cc00)
      label2.setColor(0xffffff)
    current_temp = env3_1.temperature
    label10.setText(str(round(current_temp)))
    espnow.add_peer('78:21:84:93:ac:4d', id=1)
    rectangle0.setBgColor(0x000000)
    Warning2 = 0
    label0.setText(str(distance))
    label6.setText(str(pir_0.state))
    label1.setText(str(tof_0.distance))
    Human = pir_0.state
    j = 0
    MQTT()
    wait(1)
    if current_temp > critical_temp:
      temp_warnings()
    elif current_temp > temp and current_temp < critical_temp:
      temp_state = 'Not Optimal'
      label10.setColor(0xff6600)
      label11.setColor(0xff6600)
      wait(1)
      last_msg = 'temp-warn'
      image0.show()
      temp_variable = True
    else:
      temp_state = 'Optimal'
      image0.hide()
      label10.setColor(0xffffff)
      label11.setColor(0xffffff)
      temp_variable = True
    wait(1)
    if temp_variable:
      if (tof_0.distance) < distance and Human == 0:
        wait(1)
        espnow.send(id=1, data=str('close door'))
        last_msg = 'close door'
        label8.setText(str(last_msg))
        wait(2)
      elif (tof_0.distance) < distance and Human == 1:
        espnow.send(id=1, data=str('open door'))
        label8.setText(str(last_msg))
        last_msg = 'open door'
        beep_return = True
        wait(1)
        for count in range(3):
          MQTT()
          wait(1)
          espnow.send(id=1, data=str('open door'))
          label8.setText(str(last_msg))
          last_msg = 'open door'
          j = (j if isinstance(j, Number) else 0) + 1
          for i in range(1, 11):
            label1.setText(str(tof_0.distance))
            label6.setText(str(pir_0.state))
            label7.setText(str(i))
            wait(1)
          Human = pir_0.state
          if Human == 0:
            beep_return = False
            espnow.send(id=1, data=str('close door'))
            last_msg = 'close door'
            label8.setText(str(last_msg))
            wait(2)
            break
          elif (tof_0.distance) < distance:
            label1.setText(str(tof_0.distance))
            wait(1)
            espnow.send(id=1, data=str('beep'))
            last_msg = 'beep'
            label8.setText(str(last_msg))
            beep_return = True
            if j == 1:
              Warning2 = 1
              rectangle0.setBgColor(0x33ff33)
            elif j == 2:
              Warning2 = 2
              rectangle0.setBgColor(0xffcc66)
            else:
              Warning2 = 3
              rectangle0.setBgColor(0xff0000)
            wait(1)
            MQTT()
          elif (tof_0.distance) >= distance:
            label1.setText(str(tof_0.distance))
            beep_return = False
            espnow.send(id=1, data=str('close door'))
            last_msg = 'close door'
            label8.setText(str(last_msg))
            wait(1)
            break
        label7.setText('0')
        if beep_return:
          wait(2)
          espnow.send(id=1, data=str('sound fazbear'))
          last_msg = 'sound fazbear'
          label8.setText(str(last_msg))
          beep_return = False
          wait(12)
          espnow.send(id=1, data=str('close door'))
          last_msg = 'close door'
          label8.setText(str(last_msg))
          wait(1)
      if last_msg != 'close image' or last_msg != 'temp-warn':
        wait(2)
        espnow.send(id=1, data=str('close image'))
        last_msg = 'close image'
        label8.setText(str(last_msg))
    wait_ms(2)

Code for M5Stack 2

Python
Is used to control the motor and output visual and audio warnings and feedback.
from m5stack import *
from m5ui import *
from uiflow import *
import espnow
import wifiCfg
import time
import unit


setScreenColor(0x222222)
servo0 = unit.get(unit.SERVO, unit.PORTB)


mac = None
Data_send = None

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

rectangle0 = M5Rect(37, 180, 150, 75, 0xd03531, 0xFFFFFF)
label4 = M5TextBox(71, 193, "Emengency", lcd.FONT_Default, 0xFFFFFF, rotate=0)
Close = M5TextBox(140, 224, "Close", lcd.FONT_Default, 0xFFFFFF, rotate=0)
Open = M5TextBox(49, 224, "Open", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label3 = M5TextBox(18, 71, "Default", lcd.FONT_Default, 0xFFFFFF, rotate=0)
SpeakerTest = M5TextBox(207, 224, "SpeakerTest", lcd.FONT_Default, 0xFFFFFF, rotate=0)
image0 = M5Img(215, 50, "res/Closed-s.png", True)
line0 = M5Line(M5Line.PLINE, 38, 217, 186, 217, 0xFFFFFF)
title0 = M5Title(title="DataReceive", x=3, fgcolor=0xFFFFFF, bgcolor=0x0000FF)
label0 = M5TextBox(205, 32, "Mac Address", lcd.FONT_DefaultSmall, 0xFFFFFF, rotate=0)
line1 = M5Line(M5Line.PLINE, 1, 172, 320, 172, 0xFFFFFF)
label1 = M5TextBox(18, 145, "No Data Recieved", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label2 = M5TextBox(18, 50, "Fridge State:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
image1 = M5Img(130, 71, "res/Warning.png", False)
label7 = M5TextBox(18, 124, "Data Received:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
line2 = M5Line(M5Line.PLINE, 15, 105, 122, 105, 0xFFFFFF)


# Describe this function...
def FazBear():
  global mac, Data_send
  rgb.setColorAll(0xff0000)
  image1.show()
  speaker.sing(392, 1)
  wait_ms(40)
  speaker.sing(448, 1)
  wait_ms(20)
  rgb.setColorAll(0x000000)
  image1.hide()
  speaker.sing(392, 1/2)
  wait_ms(20)
  speaker.sing(330, 1)
  wait_ms(40)
  rgb.setColorAll(0xff0000)
  image1.show()
  speaker.sing(330, 1)
  wait_ms(20)
  speaker.sing(330, 1)
  wait_ms(20)
  rgb.setColorAll(0x000000)
  image1.hide()
  speaker.sing(294, 1/2)
  wait_ms(20)
  speaker.sing(330, 1)
  wait_ms(20)
  rgb.setColorAll(0xff0000)
  image1.show()
  speaker.sing(349, 1/2)
  wait_ms(20)
  speaker.sing(330, 1)
  wait_ms(200)
  rgb.setColorAll(0x000000)
  image1.hide()
  speaker.sing(349, 1)
  wait_ms(40)
  speaker.sing(294, 1)
  wait_ms(20)
  rgb.setColorAll(0xff0000)
  image1.show()
  speaker.sing(392, 1/2)
  wait_ms(10)
  speaker.sing(330, 1)
  wait_ms(100)
  rgb.setColorAll(0x000000)
  image1.hide()
  speaker.sing(262, 1)
  wait_ms(40)
  speaker.sing(220, 1)
  wait_ms(20)
  rgb.setColorAll(0xff0000)
  image1.show()
  speaker.sing(294, 1/2)
  wait_ms(10)
  speaker.sing(196, 1)
  rgb.setColorAll(0x000000)
  image1.hide()

# Describe this function...
def close():
  global mac, Data_send
  rgb.setColorAll(0x33ff33)
  image0.changeImg("res/Closed-s.png")
  label3.setText('Door is Closed')
  image1.show()
  servo0.write_angle(180)
  wait(1)
  image1.hide()
  servo0.write_angle(0)
  rgb.setColorAll(0x000000)



def recv_cb(_):
  global mac,Data_send
  mac, _, Data_send = espnow.recv_data(encoder='str')
  if wifiCfg.wlan_sta.isconnected():
    label1.setText(str(Data_send))
    if Data_send == 'close door':
      close()
    elif Data_send == 'sound fazbear':
      FazBear()
    elif Data_send == 'open door':
      image0.changeImg("res/Open-small.png")
      label3.setText('Door is open')
    elif Data_send == 'beep':
      image0.changeImg("res/Open-small.png")
      label3.setText('beep')
      image1.show()
      rgb.setColorAll(0xff0000)
      speaker.tone(1800, 200)
      wait(1)
      image1.hide()
      rgb.setColorAll(0x000000)
    elif Data_send == 'test':
      label1.setText('Test Sucssesful')
    elif Data_send == 'close image':
      image0.changeImg("res/Closed-s.png")
      label3.setText('Door is Closed')
    elif Data_send == 'temp-warn':
      image1.show()
      rgb.setColorAll(0xff6600)
      speaker.tone(1800, 200)
      wait(1)
      image1.hide()
      rgb.setColorAll(0x000000)

  pass
espnow.recv_cb(recv_cb)


def buttonA_wasPressed():
  global mac, Data_send
  Open.setColor(0x000000)
  servo0.write_angle(0)
  wait(1)
  Open.setColor(0xffffff)
  pass
btnA.wasPressed(buttonA_wasPressed)

def buttonB_wasPressed():
  global mac, Data_send
  Close.setColor(0x000000)
  servo0.write_angle(180)
  wait(1)
  Close.setColor(0xffffff)
  pass
btnB.wasPressed(buttonB_wasPressed)

def buttonC_wasPressed():
  global mac, Data_send
  SpeakerTest.setColor(0xff0000)
  FazBear()
  SpeakerTest.setColor(0xffffff)
  pass
btnC.wasPressed(buttonC_wasPressed)


wifiCfg.doConnect('cayden', '12345678')
image0.show()
label0.setText(str(espnow.get_mac_addr()))
close()

Credits

Kevin Lee

Kevin Lee

1 project • 1 follower
Cayden Theseira

Cayden Theseira

1 project • 2 followers
haoweeeen

haoweeeen

0 projects • 1 follower

Comments