Maker.io main logo

Smart alarm system for indoor agriculture

135

2023-05-24 | By M5Stack

Share

Wifi M5Stack

* Thanks for the source code and project information provided by @Huixiang Wee

1

 

Story

Driving question: How can we make up for the lack of manpower in the agricultural sector.

The agricultural sector faces a lack of manpower, and we want to use the m5stack to reduce the need of manpower by using it to detect hardware errors

22

2

main flowchart and rfid security flow chart

4

sensor and alarm flow chart

youtube link:

 

Code

Smart Alarm System For Indoor Agriculture Python

An alarm will sound when there is a significant deviation between expected values and sensor values. or if the system cannot correct minor deviations in value. It will also suggest possible hardware errors that could cause the error.

Copy Code
from m5stack import *
from m5ui import *
from uiflow import *
import time
import unit

setScreenColor(0x222222)
env2_1 = unit.get(unit.ENV2, unit.PAHUB0)
light_1 = unit.get(unit.LIGHT, unit.PORTB)
rfid_1 = unit.get(unit.RFID, unit.PAHUB1)
pahub_0 = unit.get(unit.PAHUB, unit.PORTA)


sensors = None
light = None
moisture_error = None
Temp_error = None
light_error = None
error_level = None
Temperature = None
rfid_time = None
security = None
alarm_off = None
temp_Time = None
error = None
Aircon = None
i = None
moisture = None
Light_time = None
card = None



aircon = M5TextBox(10, 10, "Air-con temp:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
aircon_temp = M5TextBox(120, 10, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
temp = M5TextBox(10, 35, "Temperature: ", lcd.FONT_Default, 0xFFFFFF, rotate=0)
Temp_data = M5TextBox(120, 35, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
light_label = M5TextBox(10, 60, "Light:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
Light_Data = M5TextBox(120, 60, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
test2 = M5TextBox(120, 110, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
test1 = M5TextBox(10, 110, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
humidity = M5TextBox(10, 85, "humidity", lcd.FONT_Default, 0xFFFFFF, rotate=0)
humidity_data = M5TextBox(120, 85, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
rfid_text = M5TextBox(135, 135, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
broken_aircon = M5TextBox(200, 35, "Broken air-con", lcd.FONT_Default, 0xffffff, rotate=0)
broken_light = M5TextBox(200, 60, "Broken light", lcd.FONT_Default, 0xffffff, rotate=0)
s1 = M5TextBox(10, 135, "security status", lcd.FONT_Default, 0xFFFFFF, rotate=0)
alarm = M5TextBox(41, 224, "alarm", lcd.FONT_Default, 0x33cc00, rotate=0)

from numbers import Number


# Describe this function...
def moisture_detect():
  global sensors, light, moisture_error, Temp_error, light_error, error_level, Temperature, rfid_time, security, alarm_off, temp_Time, error, Aircon, i, moisture, Light_time, card
  if moisture + 5 < (env2_1.humidity) or moisture - 5 > (env2_1.humidity):
    moisture_error = 2
  elif moisture + 2 < (env2_1.humidity):
    if 0 <= 30:
      moisture_error = 1
    elif 0 > 30:
      moisture_error = 2
  elif moisture - 2 > (env2_1.humidity):
    if 0 <= 30:
      moisture_error = 1
    elif 0 > 30:
      moisture_error = 2
  else:
    moisture_error = 0

# Describe this function...
def Temp_detect():
  global sensors, light, moisture_error, Temp_error, light_error, error_level, Temperature, rfid_time, security, alarm_off, temp_Time, error, Aircon, i, moisture, Light_time, card
  if Temperature + 5 < (env2_1.temperature) or Temperature - 5 > (env2_1.temperature):
    broken_aircon.show()
    Temp_error = 2
  elif Temperature + 2 < (env2_1.temperature):
    if temp_Time <= 30:
      temp_Time = (temp_Time if isinstance(temp_Time, Number) else 0) + 1
      Temp_error = 1
    elif temp_Time > 30:
      Temp_error = 2
  elif Temperature - 2 > (env2_1.temperature):
    if temp_Time <= 30:
      temp_Time = (temp_Time if isinstance(temp_Time, Number) else 0) + 1
      Temp_error = 1
    elif temp_Time > 30:
      Temp_error = 2
      broken_aircon.show()
  else:
    Temp_error = 0
    temp_Time = 0
  if temp_Time % 5 == 0 and temp_Time > 0:
    if temp_Time <= 30:
      if Temperature + 2 < (env2_1.temperature):
        Aircon = (Aircon if isinstance(Aircon, Number) else 0) + -1
      elif Temperature - 2 > (env2_1.temperature):
        Aircon = (Aircon if isinstance(Aircon, Number) else 0) + 1

# Describe this function...
def light_detect():
  global sensors, light, moisture_error, Temp_error, light_error, error_level, Temperature, rfid_time, security, alarm_off, temp_Time, error, Aircon, i, moisture, Light_time, card
  if light + 200 < (light_1.analogValue):
    light_error = 2
  elif light - 200 > (light_1.analogValue):
    light_error = 2
    broken_light.show()
  else:
    light_error = 0
    broken_light.hide()

# Describe this function...
def error_lvl():
  global sensors, light, moisture_error, Temp_error, light_error, error_level, Temperature, rfid_time, security, alarm_off, temp_Time, error, Aircon, i, moisture, Light_time, card
  sensors = [light_error, Temp_error, moisture_error]
  error_level = 0
  for error in sensors:
    if error == 2 or error_level == 2:
      rgb.setColorAll(0xff0000)
      error_level = 2
      break
    elif error == 1 or error_level == 1:
      rgb.setColorAll(0xff6600)
      error_level = 1
      break
    else:
      error_level = 0
      rgb.setColorAll(0x33cc00)
  if error_level == 2:
    if alarm_off == False:
      speaker.tone(1800, 200)
    else:
      wait_ms(200)
  else:
    wait_ms(200)

# Describe this function...
def home_screen():
  global sensors, light, moisture_error, Temp_error, light_error, error_level, Temperature, rfid_time, security, alarm_off, temp_Time, error, Aircon, i, moisture, Light_time, card
  if light_error == 2:
    Light_Data.setColor(0xff0000)
  elif light_error == 0:
    Light_Data.setColor(0xffffff)
  if moisture_error == 2:
    humidity_data.setColor(0xff0000)
  elif moisture_error == 1:
    humidity_data.setColor(0xff6600)
  elif moisture_error == 0:
    humidity_data.setColor(0xffffff)
  if Temp_error == 2:
    Light_Data.setColor(0xff0000)
    broken_aircon.show()
  elif Temp_error == 1:
    Temp_data.setColor(0xff6600)
  elif Temp_error == 0:
    Temp_data.setColor(0xffffff)
    broken_aircon.hide()

# Describe this function...
def variableSet():
  global sensors, light, moisture_error, Temp_error, light_error, error_level, Temperature, rfid_time, security, alarm_off, temp_Time, error, Aircon, i, moisture, Light_time, card
  light = 800
  Temperature = 24
  Aircon = 22
  temp_Time = 0
  Light_time = 0
  error_level = 0
  moisture = 71
  security = False
  alarm_off = False
  rfid_time = 0

# Describe this function...
def rfid():
  global sensors, light, moisture_error, Temp_error, light_error, error_level, Temperature, rfid_time, security, alarm_off, temp_Time, error, Aircon, i, moisture, Light_time, card
  if rfid_1.isCardOn():
    if rfid_1.isCardOn():
      for i in ['32ff5d1282', '32ff5d9082', '324e5d1582']:
        if (rfid_1.readUid()) == i:
          card = True
          break
        else:
          card = False
      if card == True:
        security = True
        rfid_time = 0
      else:
        security = False
  elif rfid_time <= 10:
    rfid_time = (rfid_time if isinstance(rfid_time, Number) else 0) + 1
  elif rfid_time > 10:
    security = False
  else:
    security = False


def buttonA_wasPressed():
  global sensors, light, moisture_error, Temp_error, light_error, error_level, Temperature, rfid_time, security, alarm_off, temp_Time, Aircon, moisture, Light_time, card, i, error
  if security == True:
    if alarm_off == False:
      alarm_off = True
      alarm.setColor(0xff0000)
    elif security == True:
      alarm_off = False
      alarm.setColor(0x33cc00)
  pass
btnA.wasPressed(buttonA_wasPressed)


speaker.setVolume(0)
rgb.setBrightness(10)
pahub_0.select(0, 1)
pahub_0.select(1, 1)
broken_light.hide()
broken_aircon.hide()
variableSet()
while True:
  rfid()
  moisture_detect()
  Temp_detect()
  light_detect()
  error_lvl()
  home_screen()
  aircon_temp.setText(str(Aircon))
  Temp_data.setText(str(env2_1.temperature))
  Light_Data.setText(str(light_1.analogValue))
  humidity_data.setText(str(env2_1.humidity))
  test2.setText(str(temp_Time))
  test1.setText(str(sensors))
  rfid_text.setText(str(security))
  wait_ms(800)
  wait_ms(2)
Mfr Part # U001-C
ENV III UNIT WITH TEMPERATURE HU
M5Stack Technology Co., Ltd.
Mfr Part # U021
LIGHT UNIT WITH PHOTO-RESISTANCE
M5Stack Technology Co., Ltd.
Mfr Part # U031-B
MINI RFID UNIT 2 WS1850S
M5Stack Technology Co., Ltd.
Mfr Part # U040-B
I2C HUB 1 ~ 6 EXPANSION PCA9548A
M5Stack Technology Co., Ltd.
Mfr Part # K001-V26
ESP32 BASIC CORE IOT V2.6 DEVKIT
M5Stack Technology Co., Ltd.
More Info
View More Details
Mfr Part # K007-V26
DEV KIT FIRE IOT PSRAM V2.6
M5Stack Technology Co., Ltd.
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.