import network
import time
from machine import Pin
from machine import ADC
import dht
#Lux Converter
GAMMA = 0.7;
RL10 = 85;
def converter(analogValue):
voltage = analogValue / 1024. * 3.3
resistance = 5000 * voltage / (1 - voltage / 3.3)
return round(pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA)), 2)
#Pin Allocation
sensor = dht.DHT22(Pin(27))
button = Pin(25, Pin.IN, Pin.PULL_UP)
motion = Pin(34,Pin.IN)
adc_pin = Pin(2)
adc = ADC(adc_pin)
adc.width(10)
#Page Initialization
prev_weather = ""
print(prev_weather)
print("Initiating Sleep Tracking")
print(prev_weather)
motion_record = set()
#Time Measurement
hour = 0
minute = 0
#Required to Represent One Motion Instance
cap = 0
# Each loop = ~5 Minutes In Real Life
while True:
button_state = button.value()
if button_state == False:
print("Recording Successful - Good Morning!")
if hour > 0:
print(f"You Slept for {hour}h and {minute}m")
else:
print(f"You Slept for {minute} Minutes")
print("")
break
if cap == 0:
if motion.value() == True:
print(f"Motion Detected!")
cap = 3
else:
cap -= 1
print("Measuring weather conditions... ", end="")
sensor.measure()
if sensor.temperature() >= 0 and sensor.temperature() <= 30:
message = (f"Temp:{sensor.temperature()}C - Humidity: {sensor.humidity()}%")
else:
message = "--- Invalid ---"
print(message)
print("Measuring light conditions... ", end="")
print(" Light Level:",converter(adc.read()),"lux")
print("")
minute += 5
if minute == 60:
minute = 0
hour += 1
time.sleep(1)
#Made by A. E.
Lux Conditions
0.1 - Full Moon
10 - Twilight
50 - Device Monitor ~1m Away
100 - Dim Building Lighting
400 - Standard Office Lighting
> 1000 - Daylight
Temperature
Light
'Awake'
Movement