Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
GawainYuvenidth Gerald
Published

Urban Farming Overseer

Urban Farming Overseer aims to increase local food production in Singapore

AdvancedProtip2 hours401
Urban Farming Overseer

Things used in this project

Story

Read more

Schematics

Circuit Diagram (Primary m5stack)

Circuit Diagram (Secondary m5stack)

Block Diagram (primary m5stack)

Block Diagram (Secondary m5stack)

Code

Python Code (Primary m5stack)

Python
from m5stack import *
from m5ui import *
from uiflow import *
from flow import ezdata
import wifiCfg
import time
import unit


setScreenColor(0x0068b6)
pahub_0 = unit.get(unit.PAHUB, unit.PORTA)
env2_0 = unit.get(unit.ENV2, unit.PAHUB1)
rgb_1 = unit.get(unit.RGB, unit.PORTB)
pbhub_1 = unit.get(unit.PBHUB, unit.PAHUB0)


changeTempThreshhold = None
lightValueCleanWater = None
angle = None
warningCount = None
lowerTemp = None
contaminationLevel = None
tempWarning = None
humidityWarning = None
moistureWarning = None
contaminationWarning = None
listOfWarning = None
upperTemp = None
humidity = None
moisture = None
i = None
lightSensorRecalibrate = None
temp = None
startTime = None
breakLoop = None
angleForCalc = None
currentTime = None
duration = None

wifiCfg.autoConnect(lcdShow=False)
ezdata.startQueue()

TempLabel = M5TextBox(98, 25, "Temp:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
HumidityLabel = M5TextBox(76, 47, "Humidity:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
MoistureLabel = M5TextBox(11, 66, "Moisture Of Plot A:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
FishTankContaminationLevel = M5TextBox(37, 112, "Contamination:", lcd.FONT_Default, 0xFFFFFF, rotate=0)
temperatureValue = M5TextBox(158, 26, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
humidityValue = M5TextBox(158, 47, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
moistureValue = M5TextBox(158, 66, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
lightValue = M5TextBox(158, 112, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
upperThresholdLabel = M5TextBox(167, 10, "Set Upper Threshold", lcd.FONT_Default, 0xFFFFFF, rotate=0)
celcius = M5TextBox(205, 25, "℃", lcd.FONT_UNICODE, 0xFFFFFF, rotate=0)
celciusLowerThresh = M5TextBox(63, 25, "℃", lcd.FONT_UNICODE, 0xFFFFFF, rotate=0)
celciusUpperThresh = M5TextBox(256, 25, "℃", lcd.FONT_UNICODE, 0xFFFFFF, rotate=0)
FishFarmLabel = M5TextBox(100, 85, "Fish Farm", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
PlantFarmLabel = M5TextBox(99, 0, "Plant Farm", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
Warnings = M5TextBox(101, 131, "Warnings", lcd.FONT_DejaVu24, 0xff0000, rotate=0)
confirm = M5TextBox(242, 224, "Confirm", lcd.FONT_Default, 0xFFFFFF, rotate=0)
lowMoisture = M5Img(167, 159, "res/wilting.png", False)
temperatureImg = M5Img(0, 159, "res/Highertemp.png", False)
humidImg = M5Img(82, 159, "res/higherHumid.jpg", False)
contam = M5Img(250, 159, "res/contam.png", False)
tempHigher = M5TextBox(233, 24, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
tempLower = M5TextBox(47, 24, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
lowerThresholdLabel = M5TextBox(8, 8, "Set Lower Threshold", lcd.FONT_Default, 0xFFFFFF, rotate=0)

import math
from numbers import Number


# Describe this function...
def sendata():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, i, lightSensorRecalibrate, temp, startTime, breakLoop, angleForCalc, currentTime, duration
  ezdata.setDataAsync(ezdata_set_NQnEkcb, ezdata_set_fail_NQnEkcb, 'sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'contamination', contaminationLevel)
  ezdata.setDataAsync(ezdata_set_TqDQxcb, ezdata_set_fail_TqDQxcb, 'sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'humidity', humidity)
  ezdata.setDataAsync(ezdata_set_zkeBBcb, ezdata_set_fail_zkeBBcb, 'sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'moisture', moisture)
  ezdata.setDataAsync(ezdata_set_ffHFDcb, ezdata_set_fail_ffHFDcb, 'sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'temp', temp)
  wait(20)

# Describe this function...
def checkTemp():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, i, lightSensorRecalibrate, temp, startTime, breakLoop, angleForCalc, currentTime, duration
  if temp < lowerTemp or temp > upperTemp:
    tempWarning = True
    if temp < lowerTemp:
      temperatureImg.changeImg("res/coolerTemp.jpg")
    elif temp > upperTemp:
      temperatureImg.changeImg("res/Highertemp.jpg")
  else:
    tempWarning = False
    temperatureImg.hide()

# Describe this function...
def checkHumidity():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, i, lightSensorRecalibrate, temp, startTime, breakLoop, angleForCalc, currentTime, duration
  if humidity < 70 or humidity > 90:
    humidityWarning = True
    if humidity < 70:
      humidImg.changeImg("res/lowHumid.jpg")
    elif humidity > 90:
      humidImg.changeImg("res/higherHumid.jpg")
  else:
    humidityWarning = False
    humidImg.hide()

# Describe this function...
def checkMoisture():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, i, lightSensorRecalibrate, temp, startTime, breakLoop, angleForCalc, currentTime, duration
  if moisture < 50:
    moistureWarning = True
    lowMoisture.changeImg("res/wilting.jpg")
    rgb_1.setColorFrom(1, 3, 0xff0000)
  else:
    rgb_1.setColorFrom(1, 3, 0x33ff33)
    moistureWarning = False
    lowMoisture.hide()

# Describe this function...
def changeThresholdOfTemp():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, i, lightSensorRecalibrate, temp, startTime, breakLoop, angleForCalc, currentTime, duration
  TempLabel.hide()
  HumidityLabel.hide()
  MoistureLabel.hide()
  FishTankContaminationLevel.hide()
  temperatureValue.hide()
  humidityValue.hide()
  moistureValue.hide()
  lightValue.hide()
  FishFarmLabel.hide()
  PlantFarmLabel.hide()
  Warnings.hide()
  confirm.show()
  lowerThresholdLabel.show()
  celciusLowerThresh.show()
  celcius.hide()
  temperatureImg.hide()
  humidImg.hide()
  lowMoisture.hide()
  contam.hide()
  while True:
    if btnC.isPressed():
      breakLoop = True
    else:
      angleForCalc = getAngle()
      lowerTemp = math.floor(5 + angleForCalc * (upperTemp - 5))
      tempLower.setText(str(lowerTemp))
    if breakLoop == True and (btnC.isReleased()):
      breakLoop = False
      break
  lowerThresholdLabel.hide()
  celciusLowerThresh.hide()
  upperThresholdLabel.show()
  celciusUpperThresh.show()
  tempLower.hide()
  tempHigher.show()
  while True:
    if btnC.isPressed():
      breakLoop = True
    else:
      angleForCalc = getAngle()
      upperTemp = math.floor(lowerTemp + angleForCalc * (45 - lowerTemp))
      tempHigher.setText(str(upperTemp))
    if breakLoop == True and (btnC.isReleased()):
      breakLoop = False
      break
  celcius.show()
  upperThresholdLabel.hide()
  celciusUpperThresh.hide()
  tempHigher.hide()
  tempLower.hide()
  TempLabel.show()
  HumidityLabel.show()
  MoistureLabel.show()
  FishTankContaminationLevel.show()
  temperatureValue.show()
  humidityValue.show()
  moistureValue.show()
  lightValue.show()
  Warnings.show()
  FishFarmLabel.show()
  PlantFarmLabel.show()
  confirm.hide()
  startTime = time.ticks_ms()

# Describe this function...
def checkContam():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, i, lightSensorRecalibrate, temp, startTime, breakLoop, angleForCalc, currentTime, duration
  if contaminationLevel > 30 and lightSensorRecalibrate == False:
    contaminationWarning = True
    contam.changeImg("res/contam.jpg")
  else:
    contaminationWarning = False
    contam.hide()

# Describe this function...
def getAngle():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, i, lightSensorRecalibrate, temp, startTime, breakLoop, angleForCalc, currentTime, duration
  # max value= 736
  # min value(offset)= 15
  # 736-15
  angle = (100 - math.floor((((pbhub_1.analogRead(1)) - 15) / 721) * 100)) / 100
  return angle

# Describe this function...
def checkWarnings():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, i, lightSensorRecalibrate, temp, startTime, breakLoop, angleForCalc, currentTime, duration
  warningCount = 0
  listOfWarning = [tempWarning, humidityWarning, moistureWarning, contaminationWarning]
  for i in listOfWarning:
    if i == True:
      warningCount = (warningCount if isinstance(warningCount, Number) else 0) + 1
  if warningCount == 0:
    rgb.setColorAll(0x000000)
  elif warningCount == 1:
    speaker.tone(1800, 300)
    rgb.setColorAll(0xff0000)
    wait_ms(50)
    speaker.tone(1800, 300)
    rgb.setColorAll(0x000000)
    wait_ms(50)
  elif warningCount == 2:
    speaker.tone(1800, 150)
    rgb.setColorAll(0xff0000)
    wait_ms(50)
    speaker.tone(1800, 150)
    wait_ms(50)
    speaker.tone(1800, 150)
    wait_ms(50)
    rgb.setColorAll(0x000000)
  elif warningCount == 3:
    speaker.tone(1800, 75)
    rgb.setColorAll(0xff0000)
    wait_ms(50)
    speaker.tone(1800, 75)
    wait_ms(50)
    speaker.tone(1800, 75)
    wait_ms(50)
    speaker.tone(1800, 75)
    wait_ms(50)
    rgb.setColorAll(0x000000)
  else:
    speaker.tone(1800, 35)
    rgb.setColorAll(0xff0000)
    wait_ms(50)
    speaker.tone(1800, 35)
    wait_ms(50)
    speaker.tone(1800, 35)
    wait_ms(50)
    speaker.tone(1800, 35)
    wait_ms(50)
    speaker.tone(1800, 35)
    rgb.setColorAll(0x000000)


def ezdata_set_NQnEkcb(r=None):
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  pass
def ezdata_set_fail_NQnEkcb():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  pass

def ezdata_set_TqDQxcb(r=None):
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  pass
def ezdata_set_fail_TqDQxcb():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  pass

def ezdata_set_zkeBBcb(r=None):
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  pass
def ezdata_set_fail_zkeBBcb():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  pass

def ezdata_set_ffHFDcb(r=None):
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  pass
def ezdata_set_fail_ffHFDcb():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  pass

def buttonA_wasPressed():
  global changeTempThreshhold, lightValueCleanWater, angle, warningCount, lowerTemp, contaminationLevel, tempWarning, humidityWarning, moistureWarning, contaminationWarning, listOfWarning, upperTemp, humidity, moisture, lightSensorRecalibrate, temp, startTime, i, breakLoop, angleForCalc, currentTime, duration
  changeTempThreshhold = True
  pass
btnA.wasPressed(buttonA_wasPressed)


# max value= 753
# min value (brightest) ~= 28
# dirty value=
lightValueCleanWater = pbhub_1.analogRead(2)
lowerTemp = 20
upperTemp = 30
lightSensorRecalibrate = False
startTime = time.ticks_ms()
tempLower.setText(str(lowerTemp))
tempHigher.setText(str(upperTemp))
confirm.hide()
lowerThresholdLabel.hide()
celciusLowerThresh.hide()
upperThresholdLabel.hide()
celciusUpperThresh.hide()
tempLower.hide()
tempHigher.hide()
while True:
  temp = float(("%.2f"%((env2_0.temperature))))
  humidity = float(("%.2f"%((env2_0.humidity))))
  # max value= 760
  # min value= 260
  moisture = math.floor(100 - (((pbhub_1.analogRead(0)) - 260) / 500) * 100)
  # max value= lightValueCleanWater
  # min value = 28
  # dirty value=
  contaminationLevel = math.floor((((pbhub_1.analogRead(2)) - lightValueCleanWater) / (753 - lightValueCleanWater)) * 100)
  if contaminationLevel < 0:
    if contaminationLevel > -15:
      contaminationLevel = 0
    else:
      lightSensorRecalibrate = True
  elif contaminationLevel > 100:
    if contaminationLevel < 115:
      contaminationLevel = 100
    else:
      lightSensorRecalibrate = True
  if lightSensorRecalibrate == True:
    lightValue.setText('Recalibrate sensor')
  else:
    lightValue.setText(str((str(contaminationLevel) + str('%'))))
  temperatureValue.setText(str(temp))
  humidityValue.setText(str((str(humidity) + str('%'))))
  moistureValue.setText(str((str(moisture) + str('%'))))
  checkTemp()
  checkHumidity()
  checkMoisture()
  checkContam()
  checkWarnings()
  if changeTempThreshhold == True:
    changeThresholdOfTemp()
    changeTempThreshhold = False
  currentTime = time.ticks_ms()
  duration = currentTime - startTime
  wait_ms(2)

Python Code (Secondary m5stack)

Python
from m5stack import *
from m5ui import *
from uiflow import *
from flow import ezdata
import wifiCfg
import time
import machine
import unit
remoteInit()

setScreenColor(0x222222)
rfid_0 = unit.get(unit.RFID, unit.PORTA)


RFID = None

wifiCfg.autoConnect(lcdShow=False)

label0 = M5TextBox(11, 36, "Text", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
label1 = M5TextBox(11, 86, "Text", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)

















ezdata.setData('sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'RFID', 'No Users In Room')
while True:
  RFID = 0
  label0.setText('Please tap your identity card!')
  label1.setText('')
  if rfid_0.isCardOn():
    RFID = rfid_0.readUid()
    rgb.setColorAll(0xffff00)
    if RFID == '72a81f12d7':
      if (ezdata.getData('sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'RFID')) != ((str('JONATHAN (RFID:)     ') + str(RFID))):
        ezdata.setData('sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'RFID', ((str('JONATHAN (RFID:)     ') + str(RFID))))
        label0.setText('Welcome, Jonathan')
        label1.setText('Please confirm you are wearing your sanitary suit/kit')
        rgb.setColorAll(0x33cc00)
        speaker.tone(1800, 200)
        wait(5)
        rgb.setColorAll(0x000000)
      else:
        ezdata.removeData('sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'RFID')
        label0.setText('Goodbye, Jonathan')
        rgb.setColorAll(0x33cc00)
        speaker.tone(1800, 200)
        wait(5)
        rgb.setColorAll(0x000000)
        wait(1)
        ezdata.setData('sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'RFID', 'No Users In Room')
        machine.reset()
    elif rfid_0.isCardOn():
      RFID = rfid_0.readUid()
      rgb.setColorAll(0xffff00)
      if RFID != '72a81f12d7':
        ezdata.setData('sxWXkcxxPbRYk2RoQAHlQMBSxVshSQxH', 'RFID(INVALID)', ((str('UNAUTHORIZED USER (RFID:)     ') + str(RFID))))
        RFID = rfid_0.readUid()
        label0.setText('Unauthorized user')
        for count in range(4):
          rgb.setColorAll(0xff0000)
          speaker.tone(1800, 250)
          wait(0.25)
          rgb.setColorAll(0x000000)
          wait(0.1)
  wait_ms(2)

Credits

Gawain
1 project • 0 followers
Yuvenidth Gerald
0 projects • 0 followers

Comments