Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!

Sleep-aid Device (SaD)

This project aims to develop a solution to increase the amount of sleep people get.

BeginnerShowcase (no instructions)115
Sleep-aid Device (SaD)

Things used in this project

Story

Read more

Code

Information + Curtain

Python
This is the M5Stack code for one of the M5Stacks used, which is the receiving of information, sending it to a website called Qubitro, and closing a "curtain" whenever the light level is not low
from m5stack import *
from m5ui import *
from uiflow import *
import wifiCfg
import time
from m5mqtt import M5mqtt
import json

import unit

setScreenColor(0x222222)
servo_0 = unit.get(unit.SERVO, unit.PORTB)
pbhub_1 = unit.get(unit.PBHUB, unit.PAHUB1)
env2_0 = unit.get(unit.ENV2, unit.PAHUB0)


Curtain = None
Default = None

wifiCfg.doConnect('', '')

Temperature = M5TextBox(10, 40, "Temperature   :", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
Temperature_num = M5TextBox(217, 45, "Text", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
Light = M5TextBox(10, 78, "Light               :", lcd.FONT_DejaVu24, 0xffffff, rotate=0)
Light_num = M5TextBox(218, 83, "Text", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
low = M5Img(110, 116, "res/low.jpg", True)
med = M5Img(110, 116, "res/med.jpg", True)
high = M5Img(110, 116, "res/high.jpg", True)
label0 = M5TextBox(12, 10, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)



def buttonA_wasPressed():
  global Curtain, Default
  if Curtain == 1:
    servo_0.write_angle(180)
    wait(0.5)
    servo_0.write_angle(150)
    wait(0.5)
    servo_0.write_angle(120)
    wait(0.5)
    servo_0.write_angle(90)
    wait(0.5)
    servo_0.write_angle(60)
    wait(0.5)
    servo_0.write_angle(30)
    wait(0.5)
    servo_0.write_angle(0)
    wait(0.5)
    Curtain = 0
  pass
btnA.wasPressed(buttonA_wasPressed)


if wifiCfg.wlan_sta.isconnected():
  label0.setText('Connected')
  m5mqtt = M5mqtt('9402493f-2545-4879-abc6-6ef9fb9257aa', 'broker.qubitro.com', 1883, '9402493f-2545-4879-abc6-6ef9fb9257aa', '1738566147726002426692ebf82c94d4843a63b15fd1216629a62034500647639e81bd76e7c2925096fe47833d58176e3d2', 60)
  m5mqtt.start()
  Curtain = 0
  while True:
    m5mqtt.publish(str('9402493f-2545-4879-abc6-6ef9fb9257aa'), str((json.dumps(({'Light value':int((pbhub_1.analogRead(1))),'Temperature':(env2_0.temperature)})))), 0)
    wait(1)
    if int((pbhub_1.analogRead(1))) <= 150:
      Light_num.setText('High')
      high.show()
    elif int((pbhub_1.analogRead(1))) <= 450:
      Light_num.setText('Medium')
      med.show()
    else:
      Light_num.setText('Low')
      low.show()
    Temperature_num.setText(str(env2_0.temperature))
    if int((pbhub_1.analogRead(1))) <= 450 and Curtain == 0:
      wait(0.5)
      servo_0.write_angle(0)
      wait(0.5)
      servo_0.write_angle(30)
      wait(0.5)
      servo_0.write_angle(60)
      wait(0.5)
      servo_0.write_angle(90)
      wait(0.5)
      servo_0.write_angle(120)
      wait(0.5)
      servo_0.write_angle(150)
      wait(0.5)
      servo_0.write_angle(180)
      wait(0.5)
      Curtain = 1
    wait_ms(2)

Temperature Adjuster

Python
This is the code of the other M5stack. With the use of the angle unit, the user is able to "adjust" the temperature to increase or decrease it.
from m5stack import *
from m5ui import *
from uiflow import *
import unit

setScreenColor(0x222222)
angle_0 = unit.get(unit.ANGLE, unit.PORTB)






Name = M5TextBox(50, 38, "Temperature Adjuster", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
increase = M5TextBox(30, 161, "Increasing temperature...", lcd.FONT_DejaVu18, 0xff0000, rotate=0)
decrease = M5TextBox(27, 180, "Decreasing temperature...", lcd.FONT_DejaVu18, 0x00c0ff, rotate=0)
temp_cold = M5Img(110, 60, "res/coolTemp.png", True)
temp_hot = M5Img(110, 60, "res/Hightemp.png", True)
DefaultText1 = M5TextBox(69, 224, "Turn right to increase temp", lcd.FONT_Default, 0xff0000, rotate=0)
DefaultText2 = M5TextBox(71, 200, "Turn left to decrease temp", lcd.FONT_Default, 0x00c0ff, rotate=0)


increase.hide()
decrease.hide()
temp_hot.hide()
temp_cold.hide()
while True:
  if int((angle_0.read())) > 660:
    increase.show()
    decrease.hide()
    temp_hot.show()
    temp_cold.hide()
    DefaultText1.hide()
    DefaultText2.hide()
  elif int((angle_0.read())) < 580:
    decrease.show()
    increase.hide()
    temp_cold.show()
    temp_hot.hide()
    DefaultText1.hide()
    DefaultText2.hide()
  elif int((angle_0.read())) >= 580 and int((angle_0.read())) <= 660:
    DefaultText1.show()
    DefaultText2.show()
    temp_cold.hide()
    temp_hot.hide()
    increase.hide()
    decrease.hide()
  wait_ms(2)

Credits

Y J A
1 project • 1 follower
Contact
Jin Yan De
1 project • 0 followers
Contact
Krittitee “TeRRa” Chatsomsanga
1 project • 0 followers
Contact
jincheng
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.