Internet of Things with Python
上QQ阅读APP看书,第一时间看更新

Counting from 1 to 9 with LEDs, Python code and the mraa library

Once we finish the wirings and we make sure that all the components and the wires are in the right place, we can write our first version of the Python code to count from 1 to 9 with the LEDs, transfer it to the board via SFTP and execute it.

We will write a few lines of Python code that will use the mraa library to run the following steps to count from 1 to 9, with a 3 seconds pause between each step:

  • Turn on LED1
  • Turn on LED1 and LED2
  • Turn on LED1, LED2 and LED3
  • Turn on LED1, LED2, LED3 and LED4
  • Turn on LED1, LED2, LED3, LED4 and LED5
  • Turn on LED1, LED2, LED3, LED4, LED5 and LED6
  • Turn on LED1, LED2, LED3, LED4, LED5, LED6 and LED7
  • Turn on LED1, LED2, LED3, LED4, LED5, LED6, LED7 and LED8
  • Turn on LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8 and LED9

The following lines show the Python code that performs the previously explained actions. The code file for the sample is iot_python_chapter_03_02.py.

import mraa
import time


if __name__ == "__main__":
    print ("Mraa library version: {0}".format(mraa.getVersion()))
    print ("Mraa detected platform name: {0}".format(mraa.getPlatformName()))

    # Configure GPIO pins #1 to 9 to be output pins
    output = []
    for i in range(1, 10):
        gpio = mraa.Gpio(i)
        gpio.dir(mraa.DIR_OUT)
        output.append(gpio)

    # Count from 1 to 9
    for i in range(1, 10):
        print("==== Turning on {0} LEDs ====".format(i))
        for j in range(0, i):
            output[j].write(1)
            print("I've turned on the LED connected to GPIO Pin #{0}.".format(j + 1))
        time.sleep(3)

Once we transfer the file to the board, we can run the previous code with the following command on the board's SSH terminal:

python iot_python_chapter_03_02.py

We have used many print statements to make it easy for us to understand what is going on with messages on the console. The following lines show the generated output after we run the code:

Mraa library version: v0.9.0
Mraa detected platform name: Intel Galileo Gen 2
Setting GPIO Pin #1 to dir DIR_OUT
Setting GPIO Pin #2 to dir DIR_OUT
Setting GPIO Pin #3 to dir DIR_OUT
Setting GPIO Pin #4 to dir DIR_OUT
Setting GPIO Pin #5 to dir DIR_OUT
Setting GPIO Pin #6 to dir DIR_OUT
Setting GPIO Pin #7 to dir DIR_OUT
Setting GPIO Pin #8 to dir DIR_OUT
Setting GPIO Pin #9 to dir DIR_OUT
==== Turning on 1 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
==== Turning on 2 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
I've turned on the LED connected to GPIO Pin #2.
==== Turning on 3 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
I've turned on the LED connected to GPIO Pin #2.
I've turned on the LED connected to GPIO Pin #3.
==== Turning on 4 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
I've turned on the LED connected to GPIO Pin #2.
I've turned on the LED connected to GPIO Pin #3.
I've turned on the LED connected to GPIO Pin #4.
==== Turning on 5 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
I've turned on the LED connected to GPIO Pin #2.
I've turned on the LED connected to GPIO Pin #3.
I've turned on the LED connected to GPIO Pin #4.
I've turned on the LED connected to GPIO Pin #5.
==== Turning on 6 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
I've turned on the LED connected to GPIO Pin #2.
I've turned on the LED connected to GPIO Pin #3.
I've turned on the LED connected to GPIO Pin #4.
I've turned on the LED connected to GPIO Pin #5.
I've turned on the LED connected to GPIO Pin #6.
==== Turning on 7 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
I've turned on the LED connected to GPIO Pin #2.
I've turned on the LED connected to GPIO Pin #3.
I've turned on the LED connected to GPIO Pin #4.
I've turned on the LED connected to GPIO Pin #5.
I've turned on the LED connected to GPIO Pin #6.
I've turned on the LED connected to GPIO Pin #7.
==== Turning on 8 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
I've turned on the LED connected to GPIO Pin #2.
I've turned on the LED connected to GPIO Pin #3.
I've turned on the LED connected to GPIO Pin #4.
I've turned on the LED connected to GPIO Pin #5.
I've turned on the LED connected to GPIO Pin #6.
I've turned on the LED connected to GPIO Pin #7.
I've turned on the LED connected to GPIO Pin #8.
==== Turning on 9 LEDs ====
I've turned on the LED connected to GPIO Pin #1.
I've turned on the LED connected to GPIO Pin #2.
I've turned on the LED connected to GPIO Pin #3.
I've turned on the LED connected to GPIO Pin #4.
I've turned on the LED connected to GPIO Pin #5.
I've turned on the LED connected to GPIO Pin #6.
I've turned on the LED connected to GPIO Pin #7.
I've turned on the LED connected to GPIO Pin #8.
I've turned on the LED connected to GPIO Pin #9.

The following nine pictures show the sequence of LEDs that are turned on in the breadboard by executing the Python code.

Counting from 1 to 9 with LEDs, Python code and the mraa library

First, the code declares an empty list named output. Then, a for loop creates nine instances of the mraa.Gpio class and each of them represent a general purpose Input/Output pin on the board. We pass i as an argument for the pin parameter, and therefore, each instance represents the pin number equal to i of the GPIO pins in the board. After we create the instance, we call the dir method to configure the pin to be an output pin, that is, to set is direction to the mraa.DIR_OUT value. Then we call the append method for the output list to add the mraa.Gpio instance (gpio) to the output list. It is important to understand that range(1, 10) generates the following list: [1, 2, 3, 4, 5, 6, 7, 8, 9]. Thus, our for loop will start with i equal to 1 and its last iteration will be with i equal to 9.

output = []
for i in range(1, 10):
    gpio = mraa.Gpio(i)
    gpio.dir(mraa.DIR_OUT)
    output.append(gpio)

Another for loop determines the number of LEDs to be turned on. We use range(1, 10) to generate the same list than in the previous loop. The first line within the for loop calls a print method to display the number of LEDs that we are going to turn on in the iteration. A loop within the loop uses range(0, i) to generate the list of indexes of the elements in the output list that we have to turn on for the iteration of the main for loop (i).

The inner loop uses j as its variable and the code within this inner loop just calls the write method for each mraa.Gpio instance, output[j], with 1 as an argument for the value required parameter. This way, we send a high value (1) to the pin that is equal to j + 1, configured for digital output. If j is equal to 0, the first element of the output list is the mraa.Gpio instance that is configured for pin 1 (j + 1). Because each pin from 1 to 9 has an LED connected to it, the result of a high value in one or more pins are LEDs turned on. Then, the code prints a message indicating the LED number that has been turned on.

Once the inner loop finishes, a call to time.sleep with 3 as the value for the seconds argument delays the execution for three seconds. This way, the LED or LEDs stay turned on during this delay before the outer loop performs another iteration.

for i in range(1, 10):
    print("==== Turning on {0} LEDs ====".format(i))
    for j in range(0, i):
        output[j].write(1)
        print("I've turned on the LED connected to GPIO Pin #{0}.".format(j + 1))
    time.sleep(3)

The following picture shows the console output printed on an SSH terminal in a laptop, the 9 LEDs turned on in the protoboard connected to the board that is running Python code.

Counting from 1 to 9 with LEDs, Python code and the mraa library