I modified some code that I found readily available (see below). My raspberry is connected to a relay that when triggered sends a push event and emails me. It current is sensing a normally closed contact on the relay. I also have the ability to use the normally open side of the relay. Unfortunately it appears my code is being triggered by some phantom voltage being picked up by the raspberry. I removed the relay and used a magnetic contact and still had the same issue.
What is the best way to deal with this? Should I connect the NO contact and also test for that condition? Or convert the 16vAC down to a tolerable GPIO pin? Though since this is a doorbell there might always be ~3VAC
Code: Select all
import pycurl, json
from StringIO import StringIO
import RPi.GPIO as GPIO
#setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)
# set to pull-up (normally closed position)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#setup InstaPush variables
# add your Instapush Application ID
appID = "<redacted>"
# add your Instapush Application Secret
appSecret = "<redacted>"
pushEvent = "DoorbellStatus"
pushMessage = "Doorbell Rang"
# use this to capture the response from our push API call
buffer = StringIO()
# use Curl to post to the Instapush API
c = pycurl.Curl()
# set API URL
c.setopt(c.URL, 'https://api.instapush.im/v1/post')
#setup custom headers for authentication variables and content type
c.setopt(c.HTTPHEADER, ['x-instapush-appid: ' + appID,
'x-instapush-appsecret: ' + appSecret,
'Content-Type: application/json'])
# create a dict structure for the JSON data to post
json_fields = {}
# setup JSON values
json_fields['event']=pushEvent
json_fields['trackers'] = {}
json_fields['trackers']['message']=pushMessage
#print(json_fields)
postfields = json.dumps(json_fields)
# make sure to send the JSON with post
c.setopt(c.POSTFIELDS, postfields)
# set this so we can capture the resposne in our buffer
c.setopt(c.WRITEFUNCTION, buffer.write)
# uncomment to see the post sent
#c.setopt(c.VERBOSE, True)
# setup an indefinite loop that looks for the door to be opened / closed
while True:
GPIO.wait_for_edge(23, GPIO.RISING)
print("Doorbell Rang\n")
execfile ("gmailimage.py")
# in the door is opened, send the push request
c.perform()
# capture the response from the server
body= buffer.getvalue()
# print the response
print(body)
# reset the buffer
buffer.truncate(0)
buffer.seek(0)
# print when the door in closed
GPIO.wait_for_edge(23, GPIO.FALLING)
print("Doorbell Free\n")
# cleanup
c.close()
GPIO.cleanup()