diff options
author | Robert Scheibe <mail@robert-scheibe.de> | 2022-04-10 20:44:52 +0200 |
---|---|---|
committer | Robert Scheibe <mail@robert-scheibe.de> | 2022-04-10 20:44:52 +0200 |
commit | 3d72d8aa2b5f8a4c04b680b77f6a502be94db3b7 (patch) | |
tree | d21b2a7b22b0da844be92bf4ac6afb5c7696dfd0 |
-rw-r--r-- | README.txt | 8 | ||||
-rw-r--r-- | pico-tempguard.py | 80 |
2 files changed, 88 insertions, 0 deletions
diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..8ffb95e --- /dev/null +++ b/README.txt @@ -0,0 +1,8 @@ +press button on pico and start device by pluggin in USB cable +picotool load rp2-pico-20220117-v1.18.uf2 +restart +rshell -p /dev/ttyACM0 --buffer-size 512 +cp pico-tempguard.py /pyboard/main.py + +pip install mpremote +mpremote reset; mpremote run pico-tempguard.py 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() |