summaryrefslogtreecommitdiff
path: root/pico-tempguard.py
diff options
context:
space:
mode:
Diffstat (limited to 'pico-tempguard.py')
-rw-r--r--pico-tempguard.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/pico-tempguard.py b/pico-tempguard.py
new file mode 100644
index 0000000..18478a5
--- /dev/null
+++ b/pico-tempguard.py
@@ -0,0 +1,80 @@
+from machine import Pin, PWM, ADC
+import utime
+import _thread
+
+def led_blink_nr(nr):
+ internal_led = machine.Pin(25, machine.Pin.OUT)
+ for i in range(0,nr):
+ internal_led.on()
+ utime.sleep(0.3)
+ internal_led.off()
+ utime.sleep(0.3)
+
+def get_temperature():
+ temp_sensor = ADC(4)
+ temperature = temp_sensor.read_u16()
+ to_volts = 3.3 / 65535
+ temperature = temperature * to_volts
+ celsius_degrees = 27 - (temperature - 0.706) / 0.001721
+ return celsius_degrees
+
+def buzz():
+ buzzer = PWM(Pin(15))
+ buzzer.freq(1000)
+ buzzer.duty_u16(1000)
+ utime.sleep(0.1)
+ buzzer.duty_u16(0)
+
+def alarm():
+ for i in range (0,3):
+ buzzer = PWM(Pin(15))
+ buzzer.freq(400)
+ buzzer.duty_u16(1000)
+ utime.sleep(0.5)
+ buzzer.duty_u16(0)
+ buzzer.freq(800)
+ buzzer.duty_u16(1000)
+ utime.sleep(0.5)
+ buzzer.duty_u16(0)
+
+def calc_mean(temp):
+ tr=0
+ for i in range(0,len(temp)):
+ tr=tr+temp[i]
+ return tr/10
+
+
+t = [0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0]
+lock = _thread.allocate_lock()
+
+def second_thread():
+ #print temperature every 5 s
+ while True:
+ #if not lock.locked():
+ lock.acquire()
+ bn = calc_mean(t)
+ lock.release()
+ if get_temperature() >= bn+1:
+ alarm()
+ led_blink_nr(bn)
+ utime.sleep(5)
+
+def main():
+ _thread.start_new_thread(second_thread, ())
+ count = 0
+
+ #get temperture reading every 1 s
+ while True:
+ lock.acquire()
+ #buzz()
+ t[count%10] = get_temperature()
+ lock.release()
+
+ count = count + 1
+ #if count == 10:
+ # count = 0
+
+ utime.sleep(1)
+
+if __name__=="__main__":
+ main()