Matthew TanHongheng QianDaksh B
Published

Automatic System for Urban Farming​

We aim to reduce the time spent and reduce the costs of urban farming?

BeginnerWork in progress128
Automatic System for Urban Farming​

Things used in this project

Story

Read more

Code

M5Stack Code

MicroPython
from m5stack import *
from m5ui import *
from uiflow import *
import time
import unit

setScreenColor(0x222222)
pahub_2 = unit.get(unit.PAHUB, unit.PORTA)
pbhub_2 = unit.get(unit.PBHUB, unit.PAHUB1)
env2_2 = unit.get(unit.ENV2, unit.PAHUB0)


UpperLimit = None
LightValue = None
EarthValue = None
LowerLimit = None
Upper_LowerLimit = None



label0 = M5TextBox(0, 0, "Temperature", lcd.FONT_Default, 0x000fff, rotate=0)
label1 = M5TextBox(0, 27, "Humidity", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label2 = M5TextBox(308, 0, "C", lcd.FONT_Default, 0x1d00ff, rotate=0)
label3 = M5TextBox(306, 26, "%", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label4 = M5TextBox(180, 0, "Text", lcd.FONT_Default, 0x000fff, rotate=0)
label5 = M5TextBox(180, 26, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label6 = M5TextBox(0, 170, "Upper Limit:", lcd.FONT_Default, 0xff0000, rotate=0)
label7 = M5TextBox(0, 200, "Lower Limit:", lcd.FONT_Default, 0x003bff, rotate=0)
label8 = M5TextBox(105, 170, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label9 = M5TextBox(105, 200, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label10 = M5TextBox(242, 224, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label11 = M5TextBox(64, 224, "+", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label12 = M5TextBox(157, 224, "-", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label13 = M5TextBox(0, 57, "Moisture level", lcd.FONT_Default, 0xFFFFFF, rotate=0)
image0 = M5Img(205, 116, "res/default.jpg", True)
label14 = M5TextBox(180, 57, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label16 = M5TextBox(180, 86, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label15 = M5TextBox(0, 86, "Brightness", lcd.FONT_Default, 0xFFFFFF, rotate=0)

from numbers import Number


# Describe this function...
def ReadLightValue():
  global UpperLimit, LightValue, EarthValue, LowerLimit, Upper_LowerLimit
  LightValue = pbhub_2.analogRead(0)
  return LightValue

# Describe this function...
def ReadEarthValue():
  global UpperLimit, LightValue, EarthValue, LowerLimit, Upper_LowerLimit
  EarthValue = pbhub_2.digitalRead(2, 0)
  return EarthValue


def buttonA_wasPressed():
  global UpperLimit, LightValue, EarthValue, LowerLimit, Upper_LowerLimit
  if Upper_LowerLimit == 0:
    UpperLimit = (UpperLimit if isinstance(UpperLimit, Number) else 0) + 1
  elif Upper_LowerLimit == 1:
    LowerLimit = (LowerLimit if isinstance(LowerLimit, Number) else 0) + 1
  pass
btnA.wasPressed(buttonA_wasPressed)

def buttonB_wasPressed():
  global UpperLimit, LightValue, EarthValue, LowerLimit, Upper_LowerLimit
  if Upper_LowerLimit == 0:
    UpperLimit = (UpperLimit if isinstance(UpperLimit, Number) else 0) + -1
  elif Upper_LowerLimit == 1:
    LowerLimit = (LowerLimit if isinstance(LowerLimit, Number) else 0) + -1
  pass
btnB.wasPressed(buttonB_wasPressed)

def buttonC_wasPressed():
  global UpperLimit, LightValue, EarthValue, LowerLimit, Upper_LowerLimit
  if Upper_LowerLimit == 0:
    Upper_LowerLimit = 1
  else:
    Upper_LowerLimit = 0
  pass
btnC.wasPressed(buttonC_wasPressed)


UpperLimit = 37
LowerLimit = 17
Upper_LowerLimit = 0
while True:
  pbhub_2.setColor(1, 0, 3, 0x993399)
  label8.setText(str(UpperLimit))
  label9.setText(str(LowerLimit))
  label4.setText(str(env2_2.temperature))
  label5.setText(str(env2_2.humidity))
  if Upper_LowerLimit == 0:
    label10.setText('Upper Limit')
  else:
    label10.setText('Lower Limit')
  if (env2_2.temperature) > UpperLimit:
    image0.changeImg("res/default.jpg")
  elif (env2_2.temperature) < LowerLimit:
    image0.changeImg("res/default.jpg")
  else:
    image0.changeImg("res/default.jpg")
  if 700 < ReadLightValue():
    pbhub_2.setBrightness(1, 100)
    label16.setText('4/4')
  elif 500 < ReadLightValue():
    pbhub_2.setBrightness(1, 75)
    label16.setText('3/4')
  elif 300 < ReadLightValue():
    pbhub_2.setBrightness(1, 50)
    label16.setText('2/4')
  elif 0 < ReadLightValue():
    pbhub_2.setBrightness(1, 25)
    label16.setText('1/4')
  if 0 == ReadEarthValue():
    pass
  else:
    speaker.sing(220, 1/2)
    wait_ms(300)
  if 0 == ReadEarthValue():
    label14.setText('True')
  else:
    label14.setText('False')
  wait_ms(2)

Credits

Matthew Tan
1 project • 0 followers
Hongheng Qian
1 project • 0 followers
Daksh B
1 project • 0 followers

Comments