Syed Dareel AlqadryJeremy Leong
Published

Smart Farming System

An auto irrigation for crops with environment monitoring and a pest control system

IntermediateFull instructions provided28 days342
Smart Farming System

Things used in this project

Story

Read more

Schematics

Block Diagram (Main and Secondary M5Stack)

Flowchart (Main M5Stack)

Flowchart (Secondary M5Stack)

Main M5Stack Blockly Code

Secondary M5Stack Blockly Code

Circuit Layout

Code

Main M5Stack Code

Python
from m5stack import *
from m5ui import *
from uiflow import *
import unit
remoteInit()
setScreenColor(0x222222)
Watering_2 = unit.get(unit.WATERING, unit.PORTB)
env2_1 = unit.get(unit.ENV2, unit.PORTA)
pir_0 = unit.get(unit.PIR, unit.PORTE)


SoilMoisture = None
Temperature = None



SoilMoistureValue = M5TextBox(70, 107, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
TemperatureValue = M5TextBox(208, 107, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
PressureValue = M5TextBox(197, 222, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
SoilPic = M5Img(53, 30, "res/soil70px.png", True)
HumidityValue = M5TextBox(70, 222, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
HumidityLabel = M5TextBox(58, 129, "Humidity", lcd.FONT_Default, 0xFFFFFF, rotate=0)
PressureLabel = M5TextBox(190, 129, "Pressure", lcd.FONT_Default, 0xFFFFFF, rotate=0)
SoilMoistureLabel = M5TextBox(43, 9, "Soil Moisture", lcd.FONT_Default, 0xFFFFFF, rotate=0)
TemperatureLabel = M5TextBox(180, 9, "Temperature", lcd.FONT_Default, 0xFFFFFF, rotate=0)
TempPic = M5Img(190, 30, "res/balance70px.png", True)
HumidPic = M5Img(52, 148, "res/humid70px.png", True)
PressPic = M5Img(190, 148, "res/press70px.png", True)




def gauge_1_callback():
  global SoilMoisture, Temperature, Watering_2, env2_1, pir_0 
  return SoilMoisture
def gauge_2_callback():
  global SoilMoisture, Temperature, Watering_2, env2_1, pir_0 
  return Temperature
def gauge_3_callback():
  global SoilMoisture, Temperature, Watering_2, env2_1, pir_0 
  return env2_1.humidity
def gauge_4_callback():
  global SoilMoisture, Temperature, Watering_2, env2_1, pir_0 
  return env2_1.pressure
while True:
  SoilMoisture = Watering_2.get_adc_value()
  Temperature = env2_1.temperature
  SoilMoistureValue.setText(str(SoilMoisture))
  TemperatureValue.setText(str(Temperature))
  PressureValue.setText(str(env2_1.pressure))
  HumidityValue.setText(str(env2_1.humidity))
  if Temperature > 30:
    TempPic.changeImg("res/hot70px.png")
  elif Temperature < 25:
    TempPic.changeImg("res/cold70px.png")
  else:
    TempPic.changeImg("res/balance70px.png")
  if SoilMoisture < 1550:
    Watering_2.set_pump_status(0)
  else:
    Watering_2.set_pump_status(1)
  wait_ms(2)

Secondary M5Stack Code

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

setScreenColor(0x222222)
servo_0 = unit.get(unit.SERVO, unit.PORTA)
light_0 = unit.get(unit.LIGHT, unit.PORTB)
pir_1 = unit.get(unit.PIR, unit.PORTA)


Degree = None



LightValue = M5TextBox(138, 95, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
LightImage = M5Img(125, 9, "res/sun70px.png", True)
Crop = M5TextBox(106, 207, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
CropImg = M5Img(125, 126, "res/crops70px.png", True)

from numbers import Number




Degree = 90
while True:
  LightValue.setText(str(light_0.analogValue))
  if (light_0.analogValue) > 700:
    LightImage.changeImg("res/hotsun70px.png")
    while Degree < 180:
      Degree = (Degree if isinstance(Degree, Number) else 0) + 10
      servo_0.write_angle(Degree)
      wait(0.5)
  else:
    LightImage.changeImg("res/sun70px.png")
    while Degree > 90:
      Degree = (Degree if isinstance(Degree, Number) else 0) + -10
      servo_0.write_angle(Degree)
      wait(0.5)
  if (pir_1.state) == 1:
    speaker.setVolume(0.1)
    speaker.tone(1800, 200)
    CropImg.changeImg("res/pest70px.png")
    Crop.setText('Pest Detected!')
  else:
    CropImg.changeImg("res/crops70px.png")
    Crop.setText('Crops are safe!')
  wait_ms(2)

Credits

Syed Dareel Alqadry

Syed Dareel Alqadry

1 project • 0 followers
Jeremy Leong

Jeremy Leong

0 projects • 0 followers

Comments