How I Feed My Cats with Linux
Everything I know about serial, I learned from the excellent “Serial Howto” by David S. Lawyer and Greg Hankins. It's a thick document, with a lot of interesting, low-level information about the RS-232 standard.
Although the BASIC Stamp communicates with bstamp over a serial connection, the serial port provided with the Homework Board is not a good candidate for true serial communication. Parallax wired the port in a nontraditional way. For one thing, all commands sent to the port are echoed back to the host, which makes two-way communication difficult.
The RS-232 standard dictates that the electrical signals that travel along our cable be +/- 12V. Because of this, if we hook up a serial connection directly to our Stamp I/O pins, we most likely will burn it out, as it expects +5V. The solution is to use an intermediary to lower the 12V signal coming from the PC to 5V and boost the 5V signal coming from the Stamp to 12V. Such a chip does exist, and it is called a MAX232. Luckily, you can get a MAX232-based RS-232-compliant adapter specifically built for solderless breadboards from a Texan named Al Williams. The device is called the RS-1, and a link to his Web site is included in the on-line Resources for this article.
Starting with the 2.4 kernel, Linux names serial ports as /dev/ttyS0, 1, 2, 3 and so on. These device files act like any other file. You open them, read or write to them and close them. The OS buffers reads and writes with a 16k buffer, which is more than adequate for most serial communication. This is good; you don't have to worry about losing bits simply because you weren't reading at the exact moment your device sent them across the wire. It also means you need to flush the buffers explicitly on the OS side when you're ready to send.
Because the port is treated as a file, you need to set the permissions accordingly. In my case, because I ultimately want a CGI program to drive the feeder, I made apache the owner. If you're in a secure environment, you always could chmod 777 /dev/ttyS0, but this obviously is insecure. It's best to decide up front what you want to do with your port and set the permissions in as secure a way as possible.
Because Linux treats our serial port as a file, it's easy to use Python to talk to the Stamp. Python's file object makes it simple to read and write files:
>>> f = open("/tmp/cotton.txt",'w') >>> f.write("Cotton loves treats!") >>> f.close() >>> f = open("/tmp/cotton.txt",'r') >>> f.read() 'Cotton loves treats!' >>> f.close()
As you can see, however, although it's easy to open and close a file, doing so could get tricky if that file actually is a serial port. Fortunately for us, our Python script needs to write only a letter at a time to tell the feeder to dispense a treat. That said, I wanted to use as robust a method of communication as possible, and all this opening and closing worried me, as I see this project as something that always will be a work in progress. Maybe the cats will want to hit a button that sends us a message at work, who knows? The point is, I wanted something that was more serial-aware than a straight file handle. Luckily, someone else wanted the exact same thing. Chris Liechti has been nice enough to create PySerial for exactly this sort of situation. Here's an example of PySerial in action:
>>> import serial >>> sp = serial.Serial(0) >>> sp.portstr '/dev/ttyS0' >>> sp.write("F") >>> sp.readline()
We don't actually open /dev/ttyS0, we open 0. PySerial is smart enough to know we mean the first serial port and opens it accordingly. This also means that PySerial is cross-platform, so you don't have to know that your port is /dev/ttyS0 on one machine and /dev/ttya on another. It's all handled by PySerial.
Now that Python is talking over the serial port, we need to get it on-line. I admit, I'm not terribly fond of Python in a CGI environment. Don't let that stop you though; there's a working group whose mission it is to see the CGI libraries improved, and several Python Web frameworks make CGI unnecessary. In addition, mod_python, in its latest release, has included Python Server Pages (PSP), a PHP-like syntax for mixing Python directly into an HTML page. In short, you have a lot of options when it comes to using Python on-line. For our purposes, however, the Python CGI library is more than enough to keep our kittens well fed.
Here's a brief CGI example for a bare-bones cat feeder:
#!/usr/bin/env python import serial import cgi class Feeder: def __init__(self): self.port = serial.Serial(0) def feed(self): self.port.write("B") print 'Content-Type: text/html' print # Blank line marking end of HTTP headers cgiParameters = cgi.FieldStorage() control = Feeder() control.feed() print "<p>Thanks for feeding the kittens!"
First of all, I import the PySerial and CGI modules and then I declare a class feeder. The constructor for the class opens the serial port. The class has one method, feed, which sends an arbitrary character, in this case B, down the wire to the feeder. On the other end, PBASIC is listening for the character B and dispenses a treat when it sees it.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
|Working with Command Arguments||May 28, 2016|
|Secure Desktops with Qubes: Installation||May 28, 2016|
|CentOS 6.8 Released||May 27, 2016|
|Secure Desktops with Qubes: Introduction||May 27, 2016|
|Chris Birchall's Re-Engineering Legacy Software (Manning Publications)||May 26, 2016|
|ServersCheck's Thermal Imaging Camera Sensor||May 25, 2016|
- Secure Desktops with Qubes: Introduction
- Secure Desktops with Qubes: Installation
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Working with Command Arguments
- CentOS 6.8 Released
- The Italian Army Switches to LibreOffice
- Linux Mint 18
- ServersCheck's Thermal Imaging Camera Sensor
- Chris Birchall's Re-Engineering Legacy Software (Manning Publications)
- Petros Koutoupis' RapidDisk
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide