summaryrefslogtreecommitdiff
path: root/pico-tempguard.py
blob: 18478a5af03d3d12700d56b0a97299c75c49d3d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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()