Friday, December 25, 2015

Serial Programming the Pet Tutor Pro

We recently bought a Pet Tutor Pro system for a variety of training purposes, such as to stop door dashing and to stop continually dive bombing us at 5:30am to wake us up. We also thought this system would come in handy as an automatic feeder for our cat if we are out-of-town for a couple of days.

Limitations of Documented "Programming" Methods

According to the documentation, it seemed like there were to ways to "program" the system: (1) purchasing the windows software and using whatever features were implemented in that software or (2) using the SDK. With the windows software you are forced to use windows (an inconvenience and possibly an additional expense) and you are limited to its features; i.e., you cannot extend it (e.g., have more than four TOD triggers).

The SDK, which allows you to program a Light Blue Bean (bluetooth-enabled microcontroller) to interact with the feeder, also has its own problems. First, there is example code, but not much documentation. Second, direct communication with the feeder is impossible, you have to talk to their iOS/Android app that then talks to the feeder. So, this method would require purchasing the bluetooth upgrade, having a tablet (or phone) running the Pet Tutor app, and leaving the tablet running all the time so that the app could talk to the feeder.

Serial Interface

Due to the above limitations, we were considering returning the system; however, Wes, the owner of the company, was extremely interactive and let us know about the serial interface that would allow us to control the smart remote through a computer.

We keep a small server running at home for different purposes, so it was simple to leave the smart remote plugged into this server. Through this server, we can issue commands to the smart-remote, via the serial interface, to issue feed cycles. Our server is currently running Raspbian Jessie Lite, though the below steps should be pretty much the same for whatever flavor of Linux you are running.


The smart remote connects via to the server via usb, so you just have to figure what TTY device node corresponds to the smart remote so that you can communicate with it.
$ dmesg | grep -i tty
This will show the serial ports (nodes with prefix "tty"). The smart remote was the only serial port on my server and it was located at /dev/ttyACM0. If there are multiple ports and you are unsure about which one corresponds to the smart remote, run lsusb before and after the device is plugged in and look in at the full dmesg output.

If you have pyserial, you can also simply run:
$ python -m
According to Wes, the settings the serial port should be:
  • baud 115,200
  • bits 8
  • parity none
  • stop bits 1
  • handshake no
  • terminate each ASCII string with <cr> only i.e. no <LF>
You can view and change the default settings using the stty utility or a package called minicom. If you will be using pyserial, you can change the settings in your code, so I don't think the default settings really matter.

Issuing Commands

Commands to the feeder can be issued in several ways. For instance, you can run an interactive minicom session. During the minicom session you can also physically press buttons on the smart remote to see the commands that are issued along with the feedback from the receiver.

For quick testing purposes, you can also simply write some text directly to the serial port (though you won't see any feedback this way). For example,
$ echo "00SGL_CYC\r" > /dev/ttyACM0
Currently, 00SGL_CYC is the only active command that I am aware of. This will issue one feed cycle. The amount that comes out in a single feed cycle will depend on the food. Our cat food is a bit small, so it can spit out several pieces; however, for larger food it seems to be 1-2 pieces. After issuing the feed command, you should see the feedback 00SGL_CNF (the message was sent) 00SGL_ACK (feed cycle completed). I have also 00SGL_ERR occur when the feeder was empty.

We created a simple python script that issues N feed commands via the serial interface (we will keep pet tutor-related scripts here). A script like this enables you to set cronjobs so that you can automatically issue feeds at certain times/intervals. For example, while we were away for a couple days, we set a cronjob so that the Pet Tutor would automatically feed at 7am and 6pm. In addition, we will use it to shift the breakfast time of our cat, Lily, so that she does not wake us up quite so early.