My hope in writing this instructable is that you can, of course, make your own tweeting pet bowl, but also that this can be used as a reference. That being said, this is my first instructable, so I appreciate any feedback that would help make future instructables better.
So let's get started!
Step 1: What You Need
2 x XBee Module
2 x XBee Adapter
1 x USB FTDI Cable
1 x Force-Sensitive Resistor
1 x 10K Ohm Resistor
1 x 2 AA Battery Holder
2 x AA Battery
1 x Small Perfboard
10 x Nuts
1 x Dog Bowl
? x Extra Wire
We'll be using Python for this project so you'll need to have it installed on the computer you plan to hook up the receiver to. If you don't already have Python and/or the other dependencies installed, don't worry: I will cover how to install Python later in the instructable.
All you really need are some basic soldering skills and a little patience.
Step 2: Build the XBee Adapters
Step 3: Configuring and Testing the XBee's
Now it’s time to configure your XBee radios. Take your FTDI cable and connect it to the XBee with the black cable on GND. See the first image above.
Now open up X-CTU. You’ll start on the PC Settings tab. Select the COM port you are using and make sure that the settings are as follows:
• Baud: 9600
• Flow Control: NONE
• Data Bits: 8
• Parity: NONE
• Stop Bits: 1
Click on the Test/Query button to ensure that X-CTU can communicate with the XBee.
Time to upgrade your XBee’s. Click on the Modem Configuration Tab. Click the Download New Versions… button to make sure you have the newest version firmware. If you don’t, it will be automatically downloaded for you. Now click on the Read button and the current settings of the XBee will be displayed. Go over to the Version drop down menu and select the newest available version, then check the Always Update Firmware Check box, and then finally click Write. Follow these steps again with your second XBee and then un-check the Always Update Firmware box.
Now we will configure our transmitter. Connect the XBee you wish to use as a transmitter to your computer and press the Read button inside X-CTU. You’ll see a list of all the setting that XBee has. Navigate through the list and make the following changes:
• MY = 1
• SM = 4
• ST = 3
• SP = 68B0
• D0 = 2
• IT = 13
• IR = 1
So what do these setting mean? The MY attribute is an identifier for the XBee and if we were using more than one transmitter each would have its own MY value. SM sets the type of sleep mode. Here we are using cyclic sleep mode. ST defines the sleep time. We’re setting it to 3 which means we’ll have 3 milliseconds after waking up before we go back to sleep. During this awake time the XBee will transmit data to the receiver. The SP value defines the sleep period. As its name suggests, this is the amount of time the radio will sleep for. I’ve set a hexadecimal value of 0x68B0 or 26800 in decimal. This tells the XBee we want it to sleep for (SP * 10 msec) = 268 seconds before waking up and transmitting. Next, setting D0 to 2 means that we want the digital zero pin to be in ADC mode. Then, we define the number of samples per packet, IT, to 0x13 or 19 ADC samples per packet. And finally, we set IR to 1 msec between each ADC sampling.
The receiving XBee will not need any additional settings, so once it’s firmware has been updated you’re all set. However, I do recommend labeling the radio in some way so that you can keep track of which is the transmitter and which is the receiver.
Resetting a Sleeping XBee
If you want to make changes to the XBee later on and you get error message saying “Action Required,” then there is a good chance your XBee cannot reset itself. There is an easy fix to this. Once the message pops up, touch the GND pin and the RST pin together on the XBee adapter; the message should disappear and your XBee’s settings should successfully be changed.
Step 4: Add Remaining Components
If you look at the images above, the red and black lines mark the positive and negative buses respectively. The green connection are just all common connections. The yellow and orange wire go to the Vref and Vcc on the XBee and connect to the positive bus. The green wire connects to GND on the XBee and goes to the group the bus. And the blue wire connects to the AD0 on the XBee and connects in between the FSR and 10K Ohm resistor.
Be advised that you must be extremely careful when soldering to the FSR! If you hold the iron on the sensor too long you could melt the plastic and permanently damage the sensor.
Step 5: Modifying the Bowl
The idea is have the bowl sit primarily on the FSR. I'm using nuts in this case as a sort of stilt. Then to help balance the bowl, I use additional nuts around the perimeter of the bowl. The idea is to have the bowl rest on the FSR while reducing wobbling.
Here's what I did. First, I removed the rubber feet from the bottom of the bowl. I was lucky enough to have feet that had prongs on the back of them and easily allowed me to screw the nuts onto the feet. I did this to four of the feet and then used hot glue to attach them to the perimeter of the bowl. Next, I peeled off the thin layer of plastic on the back of the FSR to reveal a sticky area. I attached the FSR to the center of the bottom of the bowl and then used a small piece of double sided tape to attach a nut to the center of the FSR. Finally, I hot glued another nut on top of that nut.
After this primary set up, I decided to tuck the electronics inside of inner "compartment" of the bowl and used some small velcro squares to attach the XBee with perfboard and the batteries inside.
Step 6: Set Up ATwitter Application
Now you should see your application on the “Your App” page. Click on it and you’ll see a lot of different information. What we want are four codes that the python script requires to connect to twitter via oauth. First two are the Consumer Key and the Consumer Secret Key. They are both located on the main setting page. The second two are the Access Token and Access Token Secret. To get these to click on the link on right that says “My Access Token.”
Hold onto these values. You’ll need them for the python script configuration step.
Step 7: Installing Python
If you don't have python installed on your computer follow these steps to get it up and running and then follow the instructions in the dependencies section. If you have python already, skip on down to dependencies.
Head over to the Python downloads page at http://www.python.org/download/ to get the newest version. I used version 2.7.2 and if you have windows, installing is as easy as clicking on the executable and following the steps to install. I recommend installing the root drive of computer (usually C:). Once installed, you're ready to add some dependencies that our script will need in order to run.
The first thing we're going to need is the pyserial plugin that will allow your computer to communicate with the XBee through a serial port. You can get this from here . Once you've downloaded it, just double click on the executable and install it.
Next, we're going to install simplejson. The python-twitter library will need to have this to connect to the Twitter API. Installing this is slightly more difficult. First, download the package here and then extract the archive. Once extracted, open up the command prompt and change your directory such that you are inside the simplejson directory. The easiest way I found to do this was navigate in windows explorer to the simplejson folder and then copied the address. (In case you don't know, changing directories in command prompt is achieved by typing "cd C:\Users\Chris\..."). Once you're in the correct folder, you'll want to type:
C:\python27\python.exe setup.py install
This command should successfully install the simplejson package.
Lastly, we need the library that allows python to communicate with Twitter. This python library can be found here . Locate the download link on the left side of page and click it. The installation of this is very similar to the installation of simplejson. Extract the archive and open command prompt. Navigate to the python-twitter directory and copy the address. Then go your command window and change directories. Here we need to type two commands to install the library. First type:
C:\python27\python.exe setup.py build
hit enter and then type
C:\python27\python.exe setup.py install
And that's it. Now let's look at the actual script that will tweet for us.
Step 8: Understanding the Software
This script is simply for you to use in order to take preliminary measurements. I'm not going to explain what is going on in this script because it is literally copied and pasted out of the tpb.py script. We'll use this in the next step to help us configure our final main script.
This piece of code is a small set of functions designed to help handle the incoming packets from the XBee. It makes parsing data really simple. We won't go through this line by line because understanding it's functionality is trivial to this project.
Finally, we come to the main script for this project. This is actually what will be getting the XBee's ADC reading and Tweeting when necessary. Let's take a closer look.
We start by importing all the necessary external dependencies: the serial library, the XBee library, and the Twitter library. Next we define some initial variables that we'll use later on. Theses are commented on the script as to what each is for and their names should be self explanatory.
Next, we'll initialize the twitter API. The call twitter.Api(...) takes four arguments. The first two are the Consumer Key and Consumer Secret respectively. The second two are Access Token and Access Token Secret. These are the strings of characters we got when we set up the twitter application.
Then we have to open the serial connection with the receiver XBee. We set up a serial object with the serial port and the baud rate and then we open the connection.
Now we'll define our main program loop which I just call mainLoop. First thing we'll do in our main loop is get a packet from the XBee or else we'll return out of the function. Then we'll parse the packet by calling a function contained in the XBee library. Now we'll set up and array to contain the ADC reading from all the samples of the packet except for the first one (ADC reading can sometimes be screwy on the first read). Then we'll average those values. Then, we check if the if the averaged value is less than the value we set as the Threshold value. If it is we tweet and increment the warning the number otherwise we set the warning number to zero.
And finally, we put a while loop with a true condition to keep the script running indefinitely and call the mainLoop function.
Step 9: Configure the Software
Skip down to the line where the Twitter API is initialized and copy and paste your Consumer Key, Consumer Secret, Access Token, and Access Token Secret into the arguments of the twitter.Api function call.
The final step to configuring the script is to set the BASELINE and THRESHOLD values. These are the ADC readings when the bowl is empty and when it has an amount to water in it that you feel it needs more. To set these values, we're going to use the helper.py script. First, make sure you have your receiver hooked up to your computer and your transmitter powered and the bowl empty. Now navigate to where to you have the helper.py script and double click on it to run it. You'll see a command window open. About every 5 minutes you'll see a sensor reading and an average reading. Be patient and let the script run for a few cycles. Then note the average values. They should be the same or close the same. Take that value and store it as your BASELINE value in the tpb.py script.
Now, fill the bowl up with water or food to the point where you would normally refill it. Again, allow a couple of cycles to happen and record the average number as the THRESHOLD value.
You should be all set up! Now you have a few options as how to make the script run on you computer. One way is to just click on the script whenever you want to use it. Another way is setting it to run on start up.
Step 10: Enjoy
P.S. I'd like to extend a special thanks to Ladyada, as this project would not have been possible without the guidance found on her site.