All - In case you have not figured it out, I have never programmed anything in my life, so I
REALLY appreciate the help I am receiving. I am really enjoying this and am learning a lot about python (I really need to learn it apparently).
I think I failed to adequately describe the process I am trying to create, so let me start over by describing the scenario for my project and what I am trying to achieve.
With the litter box, there is a light that comes on when the litter needs to be emptied. This light stays on until the litter box is emptied. Once the box is emptied, the light goes out. What I would like to do is have the light sensor identify when the light comes on, then send one notification every 4 hours until the light goes out. The light going out would reset the process for the next time the light comes on.
Ok - so I have taken a different approach and have found code for the light sensor that outputs specific values (I have tested it and it works great), but I am not sure how to incorporate it into the code that does the push notifications.
The code that works with my sensor when run, prints once on screen the values for Full Spectrum(IR + Visible), Infrared, and Visible light. Here is the code:
Code: Select all
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# TSL2561 address, 0x39(57)
# Select control register, 0x00(00) with command register, 0x80(128)
# 0x03(03) Power ON mode
bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
# TSL2561 address, 0x39(57)
# Select timing register, 0x01(01) with command register, 0x80(128)
# 0x02(02) Nominal integration time = 402ms
bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)
time.sleep(0.5)
# Read data back from 0x0C(12) with command register, 0x80(128), 2 bytes
# ch0 LSB, ch0 MSB
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)
# Read data back from 0x0E(14) with command register, 0x80(128), 2 bytes
# ch1 LSB, ch1 MSB
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)
# Convert the data
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]
# Output data to screen
print "Full Spectrum(IR + Visible) :%d lux" %ch0
print "Infrared Value :%d lux" %ch1
print "Visible Value :%d lux" %(ch0 - ch1)
What I think I need to do is to add to this to make it continuously get the Visible Value and then use this value as the trigger for sending the push message, i.e. if the value is >x trigger the push notification, then wait for the value to drop below a certain threshold (indicating the light is off) so that it can then be ready to send a notification the next time the light comes on.
The code that does the push notifications is for a door sensor:
Code: Select all
# ------------- Begin doorSensor.py ------------------ #
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
# set this to Application ID from Instapush
appID = ""
# set this to the Application Secret from Instapush
appSecret = ""
# leave this set to DoorAlert unless you named your event something different in Instapush
pushEvent = "DoorAlert"
# set this to what you want the push message to say
pushMessage = "Door Opened!"
# use StringIO to capture the response from our push API call
buffer = StringIO()
# use Curl to post to the Instapush API
c = pycurl.Curl()
# set Instapush 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 dictionary structure for the JSON data to post to Instapush
json_fields = {}
# setup JSON values
json_fields['event']=pushEvent
json_fields['trackers'] = {}
json_fields['trackers']['message']=pushMessage
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 that is sent
#c.setopt(c.VERBOSE, True)
# setup an indefinite loop that looks for the door to be opened / closed
while True:
# door open detected
GPIO.wait_for_edge(23, GPIO.RISING)
print("Door Opened!\n")
# 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)
# door closed detected
GPIO.wait_for_edge(23, GPIO.FALLING)
print("Door Closed!\n")
# cleanup
c.close()
GPIO.cleanup()
# -------------------- End doorSensor.py -------------------- #
I assume I need to remove the code that has to do with the door sensor, add the code for my light sensor, and make the light sensor continuously report its visible light value, and have code that says if the value is over a certain number, trigger the push. I have installed pycurl and set up the push notification at the instapush website (so I have the instapush variables).
I understand I am asking for a lot of help... any suggestions how/where I can learn to do this relatively quickly will be greatly appreciated.