# Mausefalle
# by (c) 2023 Frank Schürmann / www.schuermann.ws
from machine import Pin, Timer
from time import sleep
from picozero import pico_led
from picozero import Servo # importing Servo class to easily control the servo motor
# Hardware and connections used:
# Servo GND to Raspberry Pi Pico GND
# Servo V+ to Raspberry Pi Pico 3.3 V
# Servo PWM pin to GPIO Pin 15
from machine import Pin, I2C # importing Display SSD1306 class
from ssd1306 import SSD1306_I2C
import framebuf, sys
# Pins LEDs
led_green = Pin(14, Pin.OUT, value=0)
led_red = Pin(13, Pin.OUT, value=0)
led_blue = Pin(12, Pin.OUT, value=0)
# Pins Buttons und Kontakte
button1 = Pin(1, Pin.IN, Pin.PULL_UP)
contact1 = Pin(5, Pin.IN, Pin.PULL_UP)
contact2 = Pin(9, Pin.IN, Pin.PULL_UP)
# Pin Servo / bzw. Magnetschalter / Auslösung Falle
servo_gate = Servo(15)
# Pins Ansteuerung und Auflösung Display SSD1306
display_scl_pin = 27
display_sda_pin = 26
display_res_x = 128
display_res_y = 64
# Globale Variablen
catch_status = 0
maingate_passed = 0
# FUNKTIONEN ----------------------------------------------------------------
# FUNKTION INITIALISISERUNG DISPLAY
def display_init():
# Initialize I2C device
global display_scl_pin
global display_sda_pin
i2c_dev = I2C(1, scl=Pin(display_scl_pin), sda=Pin(display_sda_pin), freq=200000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
if not i2c_addr:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0]))
print("I2C Configuration: {}".format(i2c_dev))
return i2c_dev
#FUNKTION DISPLAY LÖSCHEN
def display_clear():
global display
display.fill(0)
display.show()
#FUNKTION SUESSES VIECH ANZEIGEN
def display_rat():
global display
# Bytearray generiert bei: https://www.donskytech.com/micropython-interfacing-with-ssd1306-oled-display/ und https://www.donskytech.com/micropython-interfacing-with-ssd1306-oled-display/
# Bytearray der Maus-Grafik
buffer = bytearray(b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x80\xff\xdf\xff\xff\xff\xff\xff\xff\xff\x80\xff\x7f\xff\xff\xff\xff\xff\xff\xff\x80\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x80\xfd\xff\xff\xff\xff\xfc\x03\xff\xff\x80\xf9\xff\xff\xff\xff\xf0\x01\xff\xff\x80\xfb\xff\xff\xff\xff\xe0\x00\xbf\xff\x80\xf7\xff\xff\xff\xff\xc0\x00\x4f\xff\x80\xe7\xff\xff\xff\xff\xc0\x00\x07\xff\x80\xe7\xff\xff\xff\xff\x80\x00\x27\xff\x80\xcf\xff\xff\xfc\xff\x80\x00\x23\xff\x80\xcf\xff\xfe\x00\x01\x80\x00\x23\xff\x80\xcf\xff\xf0\x00\x00\x80\x00\x23\xff\x80\xcf\xff\xc0\x00\x00\x80\x00\x21\xff\x80\x9f\xff\x80\x00\x00\x80\x00\x21\xff\x80\x9f\xff\x00\x00\x00\x80\x00\x23\xff\x80\x9f\xfe\x00\x00\x00\x40\x00\x03\xff\x80\x9f\xfc\x00\x00\x00\x40\x00\x43\xff\x80\x9f\xf8\x01\xc0\x00\x20\x00\x87\xff\x80\x9f\xf8\x00\x70\x00\x10\x00\x27\xff\x80\x8f\xf0\x00\x38\x00\x04\x00\x1f\xff\x80\x8f\xf0\x00\x18\x00\x00\x00\x0f\xff\x80\xcf\xe0\x00\x18\x00\x00\x00\x07\xff\x80\xc7\xe0\x00\x18\x00\x00\x00\x01\xff\x80\xc7\xe0\x00\x08\x00\x00\x00\x00\xff\x80\xe3\xe0\x00\x18\x00\x00\x00\x20\x7f\x80\xe1\xe0\x00\x18\x00\x00\x00\x70\x3f\x80\xf0\xc0\x00\x10\x00\x00\x00\x70\x1f\x80\xf8\x00\x00\x21\xf8\x00\x00\x00\x1f\x80\xfe\x00\x00\x3e\x1e\x00\x00\x00\x1f\x80\xff\x00\x00\x00\x03\x00\x00\x00\x21\x80\xff\xc0\x00\x00\x01\x00\x00\x00\x21\x80")
# Framebuffer Class https://docs.micropython.org/en/latest/library/framebuf.html
fb = framebuf.FrameBuffer(buffer, 73, 32, framebuf.MONO_HLSB)
oled.fill(0)
oled.blit(fb, 96, 0)
oled.show()
# FUNKTION SERVO UND LEDs ZURÜCKSETZEN
def reset():
global maingate_passed
global catch_status
servo_gate.mid()
led_green.off()
led_blue.off()
led_red.off()
catch_status = 0
maingate_passed = 0
# FUNKTION SHOW STANDBY & EINWANDFREIE FUBKTION ANZEIGEN
def standby():
print ("Warten auf Aktivierung der Falle")
global display
error = True
while button1.value() == 1:
# Bereitschaft - grüne LED blinkt
if contact1.value() == 1 and contact2.value() == 1:
if error == True:
error = False
# Display Text ausgeben
display_clear()
display.text("OK - Warte", 5, 5)
display.text("auf Aktivierung", 5, 15)
display.show()
led_green.on()
sleep(0.5)
led_green.off()
sleep(0.5)
# Bei Störung von contact1 Fehlercode 1 x rot blinken
elif contact1.value() == 0 and contact2.value() == 1:
error = True
# Display Text ausgeben
display_clear()
display.text("ERROR", 5, 5)
display.text("Sensor 1", 5, 15)
display.show()
led_red.on()
sleep(0.5)
led_red.off()
sleep(0.5)
# Bei Störung von contact2 Fehlercode 2 x rot blinken
elif contact1.value() == 1 and contact2.value() == 0:
error = True
# Display Text ausgeben
display_clear()
display.text("ERROR", 5, 5)
display.text("Sensor 2", 5, 15)
display.show()
led_red.on()
sleep(0.2)
led_red.off()
sleep(0.2)
led_red.on()
sleep(0.2)
led_red.off()
sleep(0.5)
# FUNKTION FALLE SCHARF
def trap_armed():
global maingate_passed
global catch_status
led_green.on()
print ("Falle scharf gestellt")
# Display Texte ausgeben
global display
display_clear()
display.text("FALLE SCHARF", 5, 5)
display.text("GESTELLT!", 5, 15)
display.show()
# Warten auf Maus - Alarmkontakte-Auswertung
while catch_status == 0:
if contact1.value() == 0 and contact2.value() == 1 and maingate_passed == 0:
led_green.off()
led_red.off()
led_blue.on()
maingate_passed = 1
print ("Falle betreten")
# Display Texte ausgeben
global display
display_clear()
display.text("FALLE BETRETEN", 5, 5)
display.text("", 5, 15)
display.show()
elif contact1.value() == 1 and contact2.value() == 0 and maingate_passed == 1:
led_green.off()
led_blue.off()
led_red.on()
print ("AUSLÖSUNG")
catch_status = 1
# FUNKTION MAUS GEFANGEN
def guest_catched():
servo_gate.max()
print ("GEFANGEN!")
# Display Texte ausgeben
global display
display_clear()
display.text("FALLE", 5, 5)
display.text("AUSGELOEST!", 5, 15)
display.show()
print ("Email send")
# FUNKTON NEUSTART
def restart():
print ("Warten auf Neustart")
while button1.value() == 1:
sleep(3)
while button1.value() == 0:
sleep(1)
print ("Neustart")
# MAIN PROGRAM --------------------------------------------------------------
# Display initialisieren
i2c_dev = display_init()
display = SSD1306_I2C(display_res_x, display_res_y, i2c_dev)
# Hauptprogramm
while True:
# Globaler Reset
reset()
# Warten auf Aktivierung
standby()
# Falle scharf gestellt & Warten auf Maus
trap_armed()
# Gefangen!
guest_catched()
# Warten auf Neustart - 3 Sekunden Status-Button drücken!
restart()