Hubert TohLim YongChunJosh LimR Gonzales
Created December 13, 2022

Smart Energy Efficient Urban Farming

Our aim is to make urban farming more convenient and more power efficient, as high energy cost is the reason why urban farming is expensive.

1,213
Smart Energy Efficient Urban Farming

Things used in this project

Story

Read more

Code

Main M5stack code

Python
For main menu and dashboard of main m5stack
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)
env2_0 = unit.get(unit.ENV2, unit.PAHUB1)
pahub_0 = unit.get(unit.PAHUB, unit.PORTA)
tof_0 = unit.get(unit.TOF, unit.PAHUB2)
earth_1 = unit.get(unit.EARTH, unit.PORTB)


mac = None
Data_send = None
Tof = None
Sunroof = None
Aircon = None
Moist = None
count = None

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

button_block_B = M5TextBox(141, 219, "Soil", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
button_block_A = M5TextBox(39, 219, "Aircon", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
button_block_C = M5TextBox(217, 219, "Sunroof", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
Info1 = M5TextBox(13, 19, "label0", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
Info2 = M5TextBox(12, 56, "label1", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
Value1 = M5TextBox(180, 16, "label2", lcd.FONT_UNICODE, 0xFFFFFF, rotate=0)
Value2 = M5TextBox(174, 54, "label3", lcd.FONT_UNICODE, 0xFFFFFF, rotate=0)
label0 = M5TextBox(12, 185, "label0", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
image0 = M5Img(115, 86, "res/menu.png", True)


# Sending Data
def SendData():
  global mac, Data_send, Tof, Sunroof, Aircon, Moist, count
  Tof = int((20 - 1.33 * ((tof_0.distance) / 10)))
  if Tof <= 0:
    Tof = 0
  m5mqtt.publish(str('80a11249-f342-463a-a89c-4d98b4b1297a'), str((json.dumps(({'temp':(env2_0.temperature),'press':(env2_0.pressure),'hum':(env2_0.humidity),'moi':(earth_1.analogValue),'ToF':Tof,'Aircon':Aircon,'Moist':Moist})))), )
  wait(1)

# sunroof
def Sunroof_status():
  global mac, Data_send, Tof, Sunroof, Aircon, Moist, count
  if Data_send >= str(600):
    Sunroof = 'Open'
  else:
    Sunroof = 'Close'

# Temp and aircon status
def Temp_check():
  global mac, Data_send, Tof, Sunroof, Aircon, Moist, count
  if (env2_0.temperature) >= 30:
    Aircon = 'High power'
    rgb.setColorAll(0xff0000)
  elif (env2_0.temperature) >= 12 and (env2_0.temperature) <= 30:
    Aircon = 'Normal'
    rgb.setColorAll(0x33ff33)
  else:
    Aircon = 'Low power'
    rgb.setColorAll(0x33ccff)

# Describe this function...
def buttonC():
  global mac, Data_send, Tof, Sunroof, Aircon, Moist, count
  button_block_C.setColor(0xff0000)
  wait_ms(150)
  button_block_C.setColor(0xffffff)
  image0.hide()
  count = 0
  while True:
    Sunroof_status()
    Info1.setText('Luminosity :')
    Info2.setText('Sunroof Status :')
    Value1.setText(str(Data_send))
    Value2.setText(str(Sunroof))
    if Sunroof == 'Close':
      if count == 0:
        image0.changeImg("res/closed1.png")
        count = 1
      elif count == 1:
        image0.changeImg("res/closed1.png")
      elif count == 2:
        image0.hide()
        image0.changeImg("res/closed1.png")
        count = 1
    elif Sunroof == 'Open':
      if count == 0:
        image0.changeImg("res/open1.png")
        count = 2
      elif count == 1:
        image0.hide()
        image0.changeImg("res/open1.png")
        count = 2
      elif count == 2:
        image0.changeImg("res/open1.png")
    SendData()
    if btnB.wasPressed():
      buttonB()
    elif btnA.wasPressed():
      buttonA()
    wait_ms(2)

# Describe this function...
def buttonB():
  global mac, Data_send, Tof, Sunroof, Aircon, Moist, count
  button_block_B.setColor(0xff0000)
  wait_ms(150)
  button_block_B.setColor(0xffffff)
  image0.hide()
  count = 0
  while True:
    Soil_Status()
    Info1.setText('Moisture level :')
    Info2.setText('Soil Status :')
    Value1.setText(str(earth_1.analogValue))
    Value2.setText(str(Moist))
    if Moist == 'Very Moist':
      if count == 0:
        image0.changeImg("res/moist.png")
        count = 1
      elif count == 1:
        image0.changeImg("res/moist.png")
      elif count == 2:
        image0.hide()
        image0.changeImg("res/moist.png")
        count = 1
    elif Moist == 'Normal':
      if count == 0:
        image0.changeImg("res/soil.png")
        count = 2
      elif count == 1:
        image0.hide()
        image0.changeImg("res/soil.png")
        count = 2
      elif count == 2:
        image0.changeImg("res/soil.png")
    SendData()
    if btnA.wasPressed():
      buttonA()
    elif btnC.wasPressed():
      buttonC()
    wait_ms(2)

# Moisture
def Soil_Status():
  global mac, Data_send, Tof, Sunroof, Aircon, Moist, count
  if (earth_1.analogValue) >= 250:
    Moist = 'Very Moist'
  else:
    Moist = 'Normal'

# Describe this function...
def buttonA():
  global mac, Data_send, Tof, Sunroof, Aircon, Moist, count
  button_block_A.setColor(0xff0000)
  wait_ms(150)
  button_block_A.setColor(0xffffff)
  image0.hide()
  while True:
    Temp_check()
    Info1.setText('Temperature :')
    Info2.setText('Aircon Status :')
    Value1.setText(str((str((env2_0.temperature)) + str('℃'))))
    Value2.setText(str(Aircon))
    if Aircon == 'Normal':
      image0.changeImg("res/normal.png")
    elif Aircon == 'High power':
      image0.changeImg("res/high.png")
    elif Aircon == 'Low power':
      image0.changeImg("res/low.png")
    SendData()
    if btnB.wasPressed():
      buttonB()
    elif btnC.wasPressed():
      buttonC()
    wait_ms(2)



def recv_cb(_):
  global mac,Data_send,Tof,Sunroof,Aircon,Moist,count
  mac, _, Data_send = espnow.recv_data(encoder='str')

  pass
espnow.recv_cb(recv_cb)



wifiCfg.doConnect('Bruh', 'Tekashi69')
if wifiCfg.wlan_sta.isconnected():
  m5mqtt = M5mqtt('80a11249-f342-463a-a89c-4d98b4b1297a', 'broker.qubitro.com', 1883, '80a11249-f342-463a-a89c-4d98b4b1297a', 'KB$dvSNu3epRW3NXko9yur5GAPABE3SQ5qdijGKx', 300)
  m5mqtt.start()
  Aircon = 'Nothing'
  Moist = 'Nothing'
  Value1.hide()
  Value2.hide()
  Info1.setText('To Start')
  Info2.setText('Press One Of The Buttons')
  image0.changeImg("res/menu.png")
  if Data_send == None:
    label0.setText('Not Connected')
    label0.setColor(0xff0000)
    Data_send = '0'
  else:
    label0.setText('Connected')
    label0.setColor(0x33ff33)
  while True:
    if btnA.wasPressed():
      label0.hide()
      buttonA()
    elif btnB.wasPressed():
      label0.hide()
      buttonB()
    elif btnC.wasPressed():
      label0.hide()
      buttonC()
    Soil_Status()
    Temp_check()
    SendData()
    wait_ms(2)

Secondary M5stack Code

Python
Use for control of motor and sending luminosity data to main m5tack
from m5stack import *
from m5ui import *
from uiflow import *
import wifiCfg
import espnow
from m5mqtt import M5mqtt
import time
import json

import unit


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


Sunroof = None
LED = None
Count_Close = None
i = None

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

label1 = M5TextBox(64, 51, "Sunroof :", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
Sun = M5TextBox(165, 50, "label2", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
label0 = M5TextBox(40, 10, "Luminosity:", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
lum = M5TextBox(165, 10, "lum", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)




wifiCfg.doConnect('Bruh', 'Tekashi69')
espnow.add_peer('f0:08:d1:c7:39:5d', id=1)
Sunroof = 'Nothing'
if wifiCfg.wlan_sta.isconnected():
  m5mqtt = M5mqtt('66196535-111d-492d-93a6-f1ed75d4b210', 'broker.qubitro.com', 1883, '66196535-111d-492d-93a6-f1ed75d4b210', 'DjjXAGJrCdks9pH-dlMhFzd9ZGjQksZzQcI3TnD5', 300)
  m5mqtt.start()
  wait(2)
  servo_0.write_angle(0)
  LED = 'OFF'
  Count_Close = 0
  while True:
    if (light_0.analogValue) >= 600:
      rgb.setColorAll(0x000000)
      LED = 'OFF'
      if Count_Close == 1:
        for i in range(180, -1, -5):
          servo_0.write_angle(i)
          wait(0.25)
        Sunroof = 'Opening'
        Count_Close = 0
      elif Count_Close == 0:
        Sunroof = 'Open'
      Sunroof = 'Open'
    elif (light_0.analogValue) <= 600:
      rgb.setColorAll(0xcc33cc)
      rgb.setBrightness(100)
      LED = 'ON'
      if Count_Close == 0:
        for i in range(0, 181, 5):
          servo_0.write_angle(i)
          wait(0.25)
        Sunroof = 'Closing'
        Count_Close = 1
      elif Count_Close == 1:
        Sunroof = 'Close'
      Sunroof = 'Close'
    Sun.setText(str(Sunroof))
    espnow.send(id=1, data=str((light_0.analogValue)))
    lum.setText(str(light_0.analogValue))
    m5mqtt.publish(str('66196535-111d-492d-93a6-f1ed75d4b210'), str((json.dumps(({'lum':(light_0.analogValue),'LED':LED,'Sun':Sunroof})))), )
    wait(1)
    wait_ms(2)

Credits

Hubert Toh

Hubert Toh

1 project • 3 followers
Lim YongChun

Lim YongChun

1 project • 3 followers
Josh Lim

Josh Lim

1 project • 3 followers
R Gonzales

R Gonzales

1 project • 3 followers

Comments