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)
Comments