其他分享
首页 > 其他分享> > 乐鑫M5GO自制睡眠小助手!新手。。。轻打脸

乐鑫M5GO自制睡眠小助手!新手。。。轻打脸

作者:互联网

M5GO自制睡眠小助手!新手。。。轻打脸

这一款是基于乐鑫的M5GO Lite,以及相关传感器。
编译平台选择的是UIFlow,模块性编译,好上手,对新手比较友好!下附地址:
https://flow.m5stack.com/

程序设计思路

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

思路图

代码

更多的其实可以通过UIFlow扩展;
在UiFlow上面可以有清晰的图像模块,更方便观看!这里就贴出了代码


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




label0 = M5TextBox(10, 52, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
label1 = M5TextBox(2, 14, "Text", lcd.FONT_Comic,0xFFFFFF, rotate=0)
label4 = M5TextBox(166, 126, "Z", lcd.FONT_DejaVu24,0xFFFFFF, rotate=0)
label3 = M5TextBox(204, 93, "Z", lcd.FONT_DejaVu40,0xFFFFFF, rotate=0)
label2 = M5TextBox(245, 55, "Z", lcd.FONT_DejaVu56,0xFFFFFF, rotate=0)
label9 = M5TextBox(217, 103, "T:", lcd.FONT_Default,0xFFFFFF, rotate=0)
label10 = M5TextBox(216, 145, "P:", lcd.FONT_Default,0xFFFFFF, rotate=0)
label11 = M5TextBox(216, 184, "H:", lcd.FONT_Default,0xFFFFFF, rotate=0)
label6 = M5TextBox(256, 103, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
label7 = M5TextBox(254, 143, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
label5 = M5TextBox(135, 152, "Z", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0)
label8 = M5TextBox(254, 184, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
circle1 = M5Circle(190, 139, 40, 0xFFFFFF, 0xFFFFFF)
rectangle1 = M5Rect(145, 134, 30, 2, 0xFFFFFF, 0xFFFFFF)
circle0 = M5Circle(123, 112, 15, 0xFFFFFF, 0xFFFFFF)
circle2 = M5Circle(189, 70, 15, 0xFFFFFF, 0xFFFFFF)
circle3 = M5Circle(130, 173, 15, 0xFFFFFF, 0xFFFFFF)
circle4 = M5Circle(198, 207, 15, 0xFFFFFF, 0xFFFFFF)
circle5 = M5Circle(250, 169, 15, 0xFFFFFF, 0xFFFFFF)
circle7 = M5Circle(56, 61, 20, 0xff7301, 0x0f0f0f)
circle6 = M5Circle(254, 111, 15, 0xFFFFFF, 0xFFFFFF)
circle8 = M5Circle(72, 118, 15, 0xFFFFFF, 0xFFFFFF)
circle9 = M5Circle(92, 102, 15, 0xFFFFFF, 0xFFFFFF)
circle10 = M5Circle(121, 93, 30, 0xFFFFFF, 0xFFFFFF)
circle11 = M5Circle(153, 111, 20, 0xFFFFFF, 0xFFFFFF)
circle12 = M5Circle(123, 111, 25, 0xFFFFFF, 0xFFFFFF)
circle13 = M5Circle(93, 115, 15, 0xFFFFFF, 0xFFFFFF)
circle14 = M5Circle(168, 122, 15, 0xFFFFFF, 0xFFFFFF)
rectangle0 = M5Rect(81, 139, 3, 3, 0xFFFFFF, 0xFFFFFF)
rectangle2 = M5Rect(104, 139, 3, 3, 0xFFFFFF, 0xFFFFFF)
rectangle3 = M5Rect(123, 140, 3, 3, 0xFFFFFF, 0xFFFFFF)
rectangle4 = M5Rect(161, 139, 3, 3, 0xFFFFFF, 0xFFFFFF)
rectangle5 = M5Rect(148, 138, 3, 3, 0xFFFFFF, 0xFFFFFF)
label12 = M5TextBox(173, 25, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0)

import random

x = None
j = None
t = None
h = None
p = None
R = None
y = None
G = None
B = None
i = None
rand = None
tai = None

def _E5_A4_A9_E6_B0_94_E5_B0_8F_E5_8A_A9_E6_89_8B2():
  global x, j, t, h, p, R, y, G, B, i, rand, tai
  pass


def buttonC_wasPressed():
  global j, t, h, p, R, y, G, x, B, rand, i, tai
  try:
    req = urequests.request(method='GET', url='http://www.weather.com.cn/weather40d/101090301.shtml', headers={'Content-Type':'text/html'})
    lcd.print((req.text), 0, 0, 0xffffff)
  except:
    lcd.print((req.status_code), 0, 0, 0xffffff)
  pass
btnC.wasPressed(buttonC_wasPressed)

def buttonA_wasPressed():
  global j, t, h, p, R, y, G, x, B, rand, i, tai
  while True:
    R = random.randint(0, 255)
    G = random.randint(0, 255)
    B = random.randint(0, 255)
    label0.setText(str(light0.digitalValue))
    if x == 1:
      rgb.setBrightness(y)
      rgb.setColorAll(0xffffff)
    else:
      if (light0.analogValue) < 300:
        rgb.setColorAll(0x000000)
        label1.setText('天黑了,碎觉觉')
        circle0.hide()
        circle1.hide()
        circle2.hide()
        circle3.hide()
        circle4.hide()
        circle5.hide()
        circle6.hide()
        label2.setColor(0xffffff)
        label3.setColor(0xffffff)
        label4.setColor(0xffffff)
        label5.setColor(0xffffff)
        speaker.setVolume(1)
        speaker.tone(100, 200)
        speaker.sing(196, 1)
        speaker.sing(175, 1)
        speaker.sing(165, 1)
        speaker.sing(147, 1)
        speaker.sing(131, 1)
        speaker.sing(247, 1)
        speaker.sing(220, 1)
      else:
        rgb.setColorAll(0xff0000)
        label1.setText('天亮了,起床了')
        for i in range(255, -1, -1):
          rgb.setColorFrom(6 , 10 ,(R << 16) | (G << 8) | B)
          rgb.setColorFrom(1 , 5 ,(R << 16) | (G << 8) | B)
          rgb.setBrightness(i)
          label2.hide()
          label3.hide()
          label4.hide()
          label5.hide()
          circle0.setBgColor(0xff0000)
          circle1.setBgColor(0xff0000)
          circle2.setBgColor(0xff0000)
          circle3.setBgColor(0xff0000)
          circle4.setBgColor(0xff0000)
          circle5.setBgColor(0xff0000)
          circle6.setBgColor(0xff0000)
          speaker.setVolume(1)
          speaker.tone(800, 200)
          speaker.setVolume(2)
          speaker.sing(889, 1)
          speaker.sing(988, 1)
          speaker.sing(523, 2)
          speaker.sing(587, 2)
          speaker.sing(659, 2)
          speaker.sing(698, 2)
          speaker.sing(784, 2)
          speaker.setVolume(3)
          speaker.tone(1800, 200)
      wait(1)
    wait_ms(2)
  pass
btnA.wasPressed(buttonA_wasPressed)

def buttonB_wasPressed():
  global j, t, h, p, R, y, G, x, B, rand, i, tai
  while True:
    label6.setText(str(env2.temperature))
    label7.setText(str(env2.pressure))
    label8.setText(str(env2.pressure))
    wait(0.1)
    if (env2.humidity) > 50:
      circle7.setBgColor(0x000000)
      rgb.setColorAll(0x3333ff)
      rectangle0.setBorderColor(0x3333ff)
      rectangle2.setBorderColor(0x3366ff)
      rectangle3.setBorderColor(0x3333ff)
      rectangle5.setBorderColor(0x3333ff)
      rand = random.randint(2, 50)
      rectangle0.setSize(height=30)
      rand = random.randint(2, 50)
      rectangle2.setSize(height=30)
      rand = random.randint(2, 50)
      rectangle3.setSize(height=30)
      rand = random.randint(2, 50)
      rectangle5.setSize(height=30)
    else:
      rectangle0.setBorderColor(0x000000)
      rectangle2.setBorderColor(0x000000)
      rectangle3.setBorderColor(0x000000)
      rectangle5.setBorderColor(0x000000)
      circle1.setBgColor(0xff6600)
      rgb.setColorAll(0xff6600)
      for tai in range(20, 31):
        lcd.circle(56, 61, tai, color=0xff6600)
        lcd.circle(56, 61, (tai - 1), color=0x000000)
        wait(0.05)
      lcd.circle(56, 61, 30, color=0x000000)
    if (env2.temperature) < 28 and (env2.temperature) > 23:
      label0.setColor(0xffff00)
      label0.setText('今天美美哒!')
    else:
      if (env2.temperature) < 28:
        label0.setColor(0x33ccff)
        label0.setText('要冻成狗了!')
      else:
        label0.setColor(0xff0000)
        label0.setText('要热成汪了!')
    wait_ms(2)
  pass
btnB.wasPressed(buttonB_wasPressed)

def _remote_光线强度:():
  global j, t, h, p, R, y, G, x, B, rand, i, tai, light0, env2, ir0, 天气小助手2 
  j = light0.analogValue
  return j
def _remote_Bright(x):
  global j, t, h, p, R, y, G, B, rand, i, tai, light0, env2, ir0, 天气小助手2 
  rgb.setBrightness(y)

def _remote_温度:():
  global j, t, h, p, R, y, G, x, B, rand, i, tai, light0, env2, ir0, 天气小助手2 
  t = env2.temperature
  return t
def _remote_开灯/关灯(x):
  global j, t, h, p, R, y, G, B, rand, i, tai, light0, env2, ir0, 天气小助手2 
  if x == 1:
    rgb.setBrightness(y)
    rgb.setColorAll(0xffffff)

def _remote_湿度:():
  global j, t, h, p, R, y, G, x, B, rand, i, tai, light0, env2, ir0, 天气小助手2 
  h = env2.humidity
  return h
def _remote_压强:():
  global j, t, h, p, R, y, G, x, B, rand, i, tai, light0, env2, ir0, 天气小助手2 
  p = env2.pressure
  return p
def _remote_睡眠小助手():
  global j, t, h, p, R, y, G, x, B, rand, i, tai, light0, env2, ir0, 天气小助手2 
  while True:
    R = random.randint(0, 255)
    G = random.randint(0, 255)
    B = random.randint(0, 255)
    label0.setText(str(light0.digitalValue))
    if x == 1:
      rgb.setBrightness(y)
      rgb.setColorAll(0xffffff)
    else:
      if (light0.analogValue) < 300:
        rgb.setColorAll(0x000000)
        label1.setText('天黑了,碎觉觉')
        circle0.hide()
        circle1.hide()
        circle2.hide()
        circle3.hide()
        circle4.hide()
        circle5.hide()
        circle6.hide()
        label2.setColor(0xffffff)
        label3.setColor(0xffffff)
        label4.setColor(0xffffff)
        label5.setColor(0xffffff)
        speaker.setVolume(1)
        speaker.tone(100, 200)
        speaker.sing(196, 1)
        speaker.sing(175, 1)
        speaker.sing(165, 1)
        speaker.sing(147, 1)
        speaker.sing(131, 1)
        speaker.sing(247, 1)
        speaker.sing(220, 1)
      else:
        rgb.setColorAll(0xff0000)
        label1.setText('天亮了,起床了')
        for i in range(255, -1, -1):
          rgb.setColorFrom(6 , 10 ,(R << 16) | (G << 8) | B)
          rgb.setColorFrom(1 , 5 ,(R << 16) | (G << 8) | B)
          rgb.setBrightness(i)
          label2.hide()
          label3.hide()
          label4.hide()
          label5.hide()
          circle0.setBgColor(0xff0000)
          circle1.setBgColor(0xff0000)
          circle2.setBgColor(0xff0000)
          circle3.setBgColor(0xff0000)
          circle4.setBgColor(0xff0000)
          circle5.setBgColor(0xff0000)
          circle6.setBgColor(0xff0000)
          speaker.setVolume(1)
          speaker.tone(800, 200)
          speaker.setVolume(2)
          speaker.sing(889, 1)
          speaker.sing(988, 1)
          speaker.sing(523, 2)
          speaker.sing(587, 2)
          speaker.sing(659, 2)
          speaker.sing(698, 2)
          speaker.sing(784, 2)
          speaker.setVolume(3)
          speaker.tone(1800, 200)
      wait(1)
    wait_ms(2)

def _remote_天气小助手():
  global j, t, h, p, R, y, G, x, B, rand, i, tai, light0, env2, ir0, 天气小助手2 
  while True:
    label6.setText(str(env2.temperature))
    label7.setText(str(env2.pressure))
    label8.setText(str(env2.pressure))
    wait(0.1)
    if (env2.humidity) > 50:
      circle7.setBgColor(0x000000)
      rgb.setColorAll(0x3333ff)
      rectangle0.setBorderColor(0x3333ff)
      rectangle2.setBorderColor(0x3366ff)
      rectangle3.setBorderColor(0x3333ff)
      rectangle5.setBorderColor(0x3333ff)
      rand = random.randint(2, 50)
      rectangle0.setSize(height=30)
      rand = random.randint(2, 50)
      rectangle2.setSize(height=30)
      rand = random.randint(2, 50)
      rectangle3.setSize(height=30)
      rand = random.randint(2, 50)
      rectangle5.setSize(height=30)
    else:
      rectangle0.setBorderColor(0x000000)
      rectangle2.setBorderColor(0x000000)
      rectangle3.setBorderColor(0x000000)
      rectangle5.setBorderColor(0x000000)
      circle1.setBgColor(0xff6600)
      rgb.setColorAll(0xff6600)
      for tai in range(20, 31):
        lcd.circle(56, 61, tai, color=0xff6600)
        lcd.circle(56, 61, (tai - 1), color=0x000000)
        wait(0.05)
      lcd.circle(56, 61, 30, color=0x000000)
    if (env2.temperature) < 28 and (env2.temperature) > 23:
      label0.setColor(0xffff00)
      label0.setText('今天美美哒!')
    else:
      if (env2.temperature) < 28:
        label0.setColor(0x33ccff)
        label0.setText('要冻成狗了!')
      else:
        label0.setColor(0xff0000)
        label0.setText('要热成汪了!')
    wait_ms(2)

lcd.qrcode('http://flow-remote.m5stack.com/?remote=undefined', 100, 50, 176)

补充

我们的声音模块与人体红外模块,由于没有相关传感器,目前还在改进中;
其中红外线控制空调部分, 尝试了用uiflow里的PWM模仿IRRemote里面的一些函数,但发现用uiflow控制低电平反而会变亮,而且是偶尔,而且微秒级计时几乎没用,我们可能要改用Arduino来编译这部分。

参考网址与资料

·参赛队伍 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

标签:轻打,rand,env2,0xFFFFFF,lcd,speaker,M5GO,乐鑫,tai
来源: https://blog.csdn.net/weixin_46378298/article/details/106072989