I have a script that sends me a firebase notification when the doorbell is pressed. It was working fine when it was on the breadboard. After connecting the project to the doorbell it was randomly sending notifications.
So i've stripped the project back and I think it's something on the Pi as I currently just have a wire on GPIO18 and Ground so i can simulate a button press. However the button press is triggered by simply touching the wire or when another wire is attached to the GPIO pin.
I've got my script below:
from os import system, name from pyfcm import FCMNotification import json from firebase import firebase import RPi.GPIO as GPIO import time from datetime import datetime import calendar buttonPin = 18 chimePin = 22 GPIO.setmode(GPIO.BCM) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(chimePin, GPIO.OUT) push_service = FCMNotification(api_key="MY_API_KEY") def send_fcm_messages(reg_ids, body): try: results = push_service.notify_multiple_devices(registration_ids=reg_ids, message_title='Ding Dong', message_body=body) except pyfcm.errors.FCMServerError as e: time.sleep(0.5) results = send_fcm_messages(reg_ids, body) i = 0 for k,v in results.items(): if k == "failure": if v == 1: print("remove device") f.delete('/devices', reg_ids[i]) i += 1 return results while True: input_state = GPIO.input(buttonPin) if input_state == False: GPIO.output(chimePin, GPIO.HIGH) time.sleep(0.1) GPIO.output(chimePin, GPIO.LOW) system('clear') d = datetime.utcnow() unixtime = calendar.timegm(d.utctimetuple()) url = 'https://my-firebase-url' f = firebase.FirebaseApplication(url, None) # your variables are already assigned before this data = str(unixtime) f.put("/dings/", data, data) dateTimeStr = datetime.fromtimestamp(unixtime).strftime('%a %d/%m/%Y %H:%M:%S') devices = f.get('/devices', None); reg_ids = list() for k,v in devices.items(): reg_ids.append(k) result = send_fcm_messages(reg_ids, dateTimeStr) print('Doorbell Pressed ' + dateTimeStr)
Door Chime Circuit: