Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js
The Raspberry Pi 2 has a number of input and output channels, or general purpose I/O (GPIO) ports, available for programmatic control. Python has been one of the more popular languages on the Raspberry Pi since its introduction and remains so with the Pi 2. A quick web search for Raspberry Pi Python will return a large number of results.
The default NOOB installation that we're using for these articles comes with the RPi.GPIO library installed. RPi.GPIO contains everything you'll need to manipulate the GPIO pins to implement a wide range of projects. We'll walk you through the basics of the library in this article and finish up with an example or two in the final installment.
Before we get into the code, we should explain a little about the GPIO pins on the Raspberry Pi 2. The RaspberryPi.org site has a much more detailed write-up of the GPIO pins here. For now, we'll just go over the basics available on the main GPIO header. The first types of pins are single input and output lines. These eight individual lines must be configured programmatically as either input or output and are typically used to read a switch or drive an LED. You can also connect to I2C and SPI devices plus traditional serial communication. One additional pin is available for pulse-width modulation (PWM) used in controlling motors.
One of the best ways to investigate any Python library is from the command prompt. Launching Python 2 from the Programming menu will open a new window where any legal Python statement is fair game.
This will load the RPi.GPIO module and make all methods and properties available. Typing the command dir(GPIO) will show all attributes of the GPIO module. Figure 1 shows the output of this command.
Figure 1: The output of the dir(GPIO) command
Another way to accomplish the same thing is to use the autocomplete function available from Python. Figure 2 shows what happens when you type part of a command and then press the tab key. In this case, the first available option after typing GPIO.set is displayed. You could just as easily type GPIO period-tab to see the first available option.
Figure 2: Pressing the tab key activates the autocomplete function
You will find a complete description of all GPIO methods on the sourceforge.net project site. It includes a number of example programs showing off a variety of ways to use the GPIO pins. At the simplest level, you issue single commands such as GPIO.output(pin, value) where pin represents the specific pin and value would be either a zero or one, depending on the wiring. Conversely, you would use the following statement to read a value of an input:
pinstate = GPIO.input(pinnumber)
If you need to wait for a switch action you could use the wait_for_edge() function or the event_detected() function if you don't want to block program execution. We'll look at several of these techniques in the final installment of this series.