Page 1 of 1

Litter Robot waste drawer notofication

Posted: Sun Apr 09, 2017 7:21 am
by macmuchmore
I purchased a Litter Robot and it has a light that comes on when the waste drawer is full. I would like to use my Pi to send me a notification (text/email/tweet/whatever) when the light comes on so that I know I need to empty the drawer.

I am totally new to making my Pi do something (I have only been using it as a backup computer for web surfing). I was hoping I could get help (instructions/suggestions on where I can find the answers) with the following:

1) what light sensor to use (should I use something other than a light sensor?)
2) how to connect the sensor to the pi and make it work
3) writing the script/s to send the notification when the light comes on

Thanks in advance for any guidance/help provided.

Re: Litter Robot waste drawer notofication

Posted: Sun Apr 09, 2017 7:59 am
by macmuchmore
So I found this:

http://videos.cctvcamerapros.com/digita ... ry-pi.html

and I think I can use it with the following code and a light sensor:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

for i in range(0,5):
print GPIO.input(4)

Please let me know if I am on the right track...

Re: Litter Robot waste drawer notofication

Posted: Sun Apr 09, 2017 11:28 am
by pcmanbob
Hi.
So I think you should start by learning how to connect switches to your Pi gpio, then you will have some were to start with your project.
https://github.com/raspberrypilearning/ ... rksheet.md
https://github.com/raspberrypilearning/ ... up_down.md
https://sourceforge.net/p/raspberry-gpi ... asicUsage/

once you have mastered that then you be better equipped to decide how to go about this project.

so are you going to mount the Pi on the robot ?
are you willing to modify the robot to allow connection to the Pi gpio ?

these are just the first of many questions you will have to ask yourself, you will have to make many decisions and probably do lots of research along the way. but this should get you started .

good luck with your project.

Re: Litter Robot waste drawer notofication

Posted: Thu Apr 13, 2017 7:03 pm
by macmuchmore
pcmanbob wrote:Hi.
So I think you should start by learning how to connect switches to your Pi gpio, then you will have some were to start with your project.
https://github.com/raspberrypilearning/ ... rksheet.md
https://github.com/raspberrypilearning/ ... up_down.md
https://sourceforge.net/p/raspberry-gpi ... asicUsage/

once you have mastered that then you be better equipped to decide how to go about this project.

so are you going to mount the Pi on the robot ?
are you willing to modify the robot to allow connection to the Pi gpio ?

these are just the first of many questions you will have to ask yourself, you will have to make many decisions and probably do lots of research along the way. but this should get you started .

good luck with your project.
thanks for the reply. the pi will be mounted on the litter robot with the sensor in front of the light for the litter tray. what i want is to have the pi notify me when the light comes on. i have the sensor hooked up to the pi correctly and now i just need help with the code.

just to start out, i am just using the following code to make sure the sensor can detect the light.

import RPI.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)
for i in range(0,5):
print GPIO.input(4)

my problem is a syntax error on "GPIO" in the print statement (in red). any help would be appreciated as this is my first python script.

thanks

Re: Litter Robot waste drawer notofication

Posted: Thu Apr 13, 2017 7:26 pm
by pcmanbob
So the reason you get an error message is because you have not indented the print line, indenting is very important in python.

so try this code it will just keep printing the gpio status until you press Ctrl + c

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.IN)

while True:
	print GPIO.input(4)
	time.sleep (0.1)

if you have a look at this thread viewtopic.php?f=32&t=178930&p=1140947&h ... t#p1140947
I posted a short explanation about python indenting on Fri Mar 31, 2017 9:10 am near the bottom of the thread.

Re: Litter Robot waste drawer notofication

Posted: Thu Apr 13, 2017 7:52 pm
by macmuchmore
pcmanbob wrote:So the reason you get an error message is because you have not indented the print line, indenting is very important in python.

so try this code it will just keep printing the gpio status until you press Ctrl + c

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.IN)

while True:
	print GPIO.input(4)
	time.sleep (0.1)

if you have a look at this thread viewtopic.php?f=32&t=178930&p=1140947&h ... t#p1140947
I posted a short explanation about python indenting on Fri Mar 31, 2017 9:10 am near the bottom of the thread.
I apologize for not showing my code with the indentions... I have them in place in my script. However, thank you for your code example. I tried it and unfortunately I am still getting a syntax error on the "GPIO" in the print line. When I run the "check module", it stops with a red highlight of the "GPIO" and has a popup error message for the incorrect syntax.

Re: Litter Robot waste drawer notofication

Posted: Thu Apr 13, 2017 7:57 pm
by macmuchmore
macmuchmore wrote: I apologize for not showing my code with the indentions... I have them in place in my script. However, thank you for your code example. I tried it and unfortunately I am still getting a syntax error on the "GPIO" in the print line. When I run the "check module", it stops with a red highlight of the "GPIO" and has a popup error message for the incorrect syntax.
Ok, so on a whim, I executed the code in a terminal window. It ran and started displaying zeros. I tried shining a light on the sensor (I expected the value to change), but nothing happened. Now I am wondering if I have an issue with the way it is wired.... I will double check and post back if I have an issue with it or not.

Re: Litter Robot waste drawer notofication

Posted: Thu Apr 13, 2017 7:58 pm
by Douglas6
If you are using Python 3, you'll need to do this:

Code: Select all

   print(GPIO.input(4))
That will work also with Python 2.

Re: Litter Robot waste drawer notofication

Posted: Thu Apr 13, 2017 8:12 pm
by macmuchmore
Douglas6 wrote:If you are using Python 3, you'll need to do this:

Code: Select all

   print(GPIO.input(4))
That will work also with Python 2.
Thanks! That fixed my syntax error. Now I just need to figure out why it is displaying only zeros when I shine a light on the sensor.

Re: Litter Robot waste drawer notofication

Posted: Thu Apr 13, 2017 9:34 pm
by macmuchmore
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.

Re: Litter Robot waste drawer notofication

Posted: Thu Apr 13, 2017 9:45 pm
by macmuchmore
Instead of continuously reporting the Visible value, It would be ok to have the Pi check the value a lot less - like once an hour for example. If it is just as easy to do it continuously, then that works as well. I guess what I am saying is that instantaneous notification is not a huge deal.... ;-)

Re: Litter Robot waste drawer notofication

Posted: Sun May 06, 2018 6:11 am
by metracom
My cats Litter Robot is controlled by a Raspberry Pi. https://twitter.com/LitterBoxTweets