编程语言
首页 > 编程语言> > 乐鑫M5GO自制智能家居系统(程序结构精简版/群除我佬版)

乐鑫M5GO自制智能家居系统(程序结构精简版/群除我佬版)

作者:互联网

乐鑫M5GO自制智能家居系统(程序结构精简版/群除我佬版)

这一款是基于乐鑫的M5GO Lite,以及相关传感器。
编译平台选择的是UIFlow,模块性编译,好上手,对新手比较友好!下附地址:
https://flow.m5stack.com/
上一篇是萌新简单的编写
这次有了组员大佬的加持,程序果然精简,厉害了不少。

设计思路

1.通过M5Go的监测温度,湿度,气压的ENV Uint监测环境条件,天气小助手;
2.外加光线监测模块(Light Unit),监测环境光强,提醒是否适宜睡眠;
3.声音模块监测呼噜声,及时给出提醒;
4.也可以附加人体红外成像单元,监测人体睡眠时的热成像照片;
5.通过之后再M5Go显示分析结果,发出柔和的LED光线和音乐,或者提醒的LED光线及声音。
6.当适宜睡眠时给出相应提示,不适宜时,可以通过红外光电对管(Mini Infrared Unit)反馈调整家庭电器(空调,加湿器)。
7.所有信息都将通过Wifi反馈到手机,手机也可以通过小程序来调节红外光电对管,反馈调整家庭电器(空调,加湿器)。

程序

经过大佬加持,程序使用起来更加丝滑,图形模块可以看附件,这里列出来程序代码。

from m5stack import *
from m5ui import *
from uiflow import *
import unit
remoteInit()
import machine
setScreenColor(0x111111)
light0 = unit.get(unit.LIGHT, unit.PORTB)
env2 = unit.get(unit.ENV, unit.PORTA)




Sun = M5Circle(253, 199, 120, 0xff7800, 0x000000)
cloud1 = M5Circle(167, 231, 40, 0x57adff, 0x57adff)
cloud2 = M5Circle(261, 206, 80, 0x57adff, 0x57adff)
cloud3 = M5Circle(283, 136, 40, 0x57adff, 0x57adff)
theme = M5TextBox(36, 24, "Text", lcd.FONT_Comic,0xFFFFFF, rotate=0)
z1 = M5TextBox(64, 154, "Z", lcd.FONT_DejaVu24,0xFFFFFF, rotate=320)
z2 = M5TextBox(112, 111, "Z", lcd.FONT_DejaVu40,0xFFFFFF, rotate=340)
z3 = M5TextBox(181, 80, "Z", lcd.FONT_DejaVu56,0xFFFFFF, rotate=0)
temlabel = M5TextBox(38, 89, "Temperature", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0)
damplabel = M5TextBox(38, 122, "Humidity", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0)
presslabel = M5TextBox(37, 156, "Pressure", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0)
temp = M5TextBox(181, 87, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0)
damp = M5TextBox(181, 123, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0)
press = M5TextBox(179, 157, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0)
weatherinfo = M5TextBox(37, 196, "Text", lcd.FONT_DejaVu24,0xFFFFFF, rotate=0)

import random

light = None
bright = None
interface = None
t = None
h = None
p = None
j = None
alarm = None
qrupdated = None
R = None
G = None
B = None

def updateinterface():
  global light, bright, interface, t, h, p, j, alarm, qrupdated, R, G, B
  if interface == 3:
    if not qrupdated:
      lcd.fill(0xffffff)
      lcd.qrcode('http://flow-remote.m5stack.com/?remote=undefined', 72, 32, 176)
      qrupdated = True
  else:
    qrupdated = False
    lcd.clear()
    theme.show()
    if interface == 1:
      R = random.randint(0, 255)
      G = random.randint(0, 255)
      B = random.randint(0, 255)
      if (light0.analogValue) < 300:
        rgb.setColorAll(0x000000)
        theme.setText('Good Night')
        z1.show()
        z2.show()
        z3.show()
        if not alarm:
          speaker.setVolume(1)
          speaker.tone(988, 469)
          speaker.tone(880, 469)
          speaker.tone(740, 469)
          speaker.tone(880, 469)
          speaker.tone(784, 703)
          speaker.tone(740, 234)
          speaker.tone(659, 469)
          speaker.tone(587, 1406)
          alarm = True
      else:
        if alarm:
          theme.setText('Good Morning')
          rgb.setBrightness(20)
          rgb.setColorFrom(6 , 10 ,(R << 16) | (G << 8) | B)
          rgb.setColorFrom(1 , 5 ,(R << 16) | (G << 8) | B)
          Sun.show()
          speaker.setVolume(2)
          speaker.tone(117, 156)
          speaker.tone(147, 156)
          speaker.tone(175, 156)
          speaker.tone(233, 156)
          speaker.tone(294, 156)
          speaker.tone(349, 156)
          speaker.tone(466, 469)
          speaker.tone(294, 156)
          speaker.tone(349, 156)
          speaker.tone(466, 156)
          speaker.tone(523, 1406)
        else:
          theme.setText('Energetic!')
          Sun.show()
    else:
      if (env2.humidity) > 50:
        rgb.setColorAll(0x3366ff)
        cloud1.show()
        cloud2.show()
        cloud1.show()
      else:
        rgb.setColorAll(0xff6600)
        Sun.show()
      if (env2.temperature) < 28 and (env2.temperature) > 23:
        weatherinfo.setColor(0xffff00)
        weatherinfo.setText('Beautiful!')
      else:
        if (env2.temperature) < 28:
          weatherinfo.setColor(0x33ccff)
          weatherinfo.setText('Too Cold!')
        else:
          weatherinfo.setColor(0xff0000)
          weatherinfo.setText('Too Hot!')
      theme.setText('Weather')
      temp.setText(str(env2.temperature))
      damp.setText(str(env2.humidity))
      press.setText(str(env2.pressure))
      temlabel.show()
      damplabel.show()
      presslabel.show()
      temp.show()
      damp.show()
      press.show()


def buttonA_wasPressed():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B
  interface = 1
  pass
btnA.wasPressed(buttonA_wasPressed)

def buttonB_wasPressed():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B
  interface = 2
  alarm = False
  pass
btnB.wasPressed(buttonB_wasPressed)

def buttonC_wasPressed():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B
  interface = 3
  alarm = False
  pass
btnC.wasPressed(buttonC_wasPressed)

def _remote_开关灯(light):
  global interface, t, h, p, j, alarm, qrupdated, bright, R, G, B, light0, env2, updateinterface 
  if light == 1:
    rgb.setBrightness(10)
    rgb.setColorAll(0xffffff)
  else:
    rgb.setColorAll(0x000000)

def _remote_Light(bright):
  global interface, t, h, p, j, alarm, qrupdated, light, R, G, B, light0, env2, updateinterface 
  rgb.setBrightness(bright)

def _remote_温度():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B, light0, env2, updateinterface 
  t = env2.temperature
  return t
def _remote_湿度():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B, light0, env2, updateinterface 
  h = env2.humidity
  return h
def _remote_压强():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B, light0, env2, updateinterface 
  p = env2.pressure
  return p
def _remote_光线强度():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B, light0, env2, updateinterface 
  j = light0.analogValue
  return j
def _remote_睡眠小助手():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B, light0, env2, updateinterface 
  interface = 1

def _remote_天气小助手():
  global interface, t, h, p, j, alarm, qrupdated, bright, light, R, G, B, light0, env2, updateinterface 
  interface = 2

interface = 3
qrupdated = False
alarm = False
while True:
  updateinterface()
  wait(1)
  wait_ms(2)

附件:RemoteControl.m5f
CSDN上也有RemoteControl.m5f资源。

参考网址与资料

·参赛队伍 B 站教学视频: https://space.bilibili.com/443675010spm_id_from=333.788.b_765f757069 6e666f.2

·参赛队伍硬件信息一览:
https://docs.m5stack.com/#/

·参考成品一览: https://m5stack.com/pages/blog-page

·乐鑫公司详情:
https://www.espressif.com/zhhans/products/hardware/development-boards

·编译平台 UIFLOW: https://docs.m5stack.com/#/zh_CN/uiflow/uiflow_home_page

·Arduino 开发平台: https://docs.m5stack.com/#/zh_CN/arduino/arduino_home_page

·UIFLOW-GITHUB 参考资料: https://github.com/m5stack/UIFlow-Code

·Micro Python 参考资料 https://www.icloud.com/keynote/0W35w4QK26yW7S5eNWwHHYvHA

标签:精简版,show,env2,alarm,程序结构,智能家居,qrupdated,bright,interface
来源: https://blog.csdn.net/weixin_46378298/article/details/106089755