import machine
from utime import sleep
import math
import ssd1306
i2c: I2C = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0))
oled_width: int = 128
oled_height: int = 64
oled: SSD1306_I2C = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
led: Pin = machine.Pin(21, machine.Pin.OUT)
motor: Pin = machine.Pin(20, machine.Pin.OUT)
alarm: Pin = machine.Pin(4, machine.Pin.OUT)
alarmState = False
ntc: ADC = machine.ADC(28)
ldr: ADC = machine.ADC(27)
def getTemp() -> float:
reading: float = ntc.read_u16()
temp: float = 1 / (math.log(1 / (2**16. / reading - 1)) / 3950 + 1.0 / 298.15) - 273.15
print(str(temp) + " C.")
return temp
def getLux() -> float:
reading: float = ldr.read_u16()
RL10: int = 50
GAMMA: float = 0.7
voltage: float = reading / (2**16-1) * 5
resistance: float = 2000 * voltage / (1 - voltage / 5)
lux: float = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))
print(str(lux) + " Lux")
return lux
def main() -> None:
while True:
if getTemp() >= 37:
motor.value(1)
else:
motor.value(0)
if getTemp() >= 50:
alarmState = True
alarm.value(1)
else:
alarmState = False
alarm.value(0)
if getLux() <= 500:
led.value(1)
else:
led.value(0)
oled.fill(0)
oled.text("Temperature:", 0, 0)
oled.text("{:.2f} C".format(getTemp()), 0, 10)
if alarmState:
oled.text("HIGH TEMPS!!!!!", 0, 20)
oled.text("Light:", 0, 30)
oled.text("{:.2f} Lux".format(getLux()), 0, 40)
oled.show()
sleep(0.5)
if __name__ == "__main__":
main()