main.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from machine import Pin
  2. from neopixel import NeoPixel
  3. from time import sleep_ms
  4. from onewire import OneWire
  5. from ds18x20 import DS18X20
  6. def main():
  7. # init buttons
  8. button1 = Pin(18,Pin.IN,Pin.PULL_DOWN)
  9. button2 = Pin(19,Pin.IN,Pin.PULL_DOWN)
  10. # init leds
  11. leds = []
  12. for i in range(0,6):
  13. leds.append(Pin(2+i,Pin.OUT))
  14. # init neopixel
  15. pin = Pin(16,Pin.OUT)
  16. pin.off()
  17. sleep_ms(2000)
  18. pin.on()
  19. pixel = NeoPixel(pin,1)
  20. pixel[0] = (0,0,0)
  21. pixel.write()
  22. # init onewire tempsensor
  23. temp = DS18X20(OneWire(Pin(17)))
  24. sensors = temp.scan()
  25. # program
  26. while True:
  27. if button1.value():
  28. for led in leds:
  29. led.on()
  30. sleep_ms(100)
  31. elif button2.value():
  32. for led in leds:
  33. led.off()
  34. sleep_ms(100)
  35. else:
  36. for sensor in sensors:
  37. temp.convert_temp()
  38. value = temp.read_temp(sensor)
  39. print(f"Temperature: {round(value,2)}°C")
  40. if value >= 35.0:
  41. pixel[0] = (0,255,0)
  42. pixel.write()
  43. elif value < 35.0 and value >= 30.0:
  44. pixel[0] = (255,255,0)
  45. pixel.write()
  46. else:
  47. pixel[0] = (255,0,0)
  48. pixel.write()
  49. sleep_ms(1000)
  50. if __name__ == "__main__":
  51. main()