• No se han encontrado resultados

W: WATERSHIP DOWN Y: YOGY BEAR

X: WEBSITES DE INTERES

77. LA NIÑERA

So far, we have been using the mraa library to work with PWM and change the brightness level for the different LEDs and colors within an RGB LED. However, in the first chapter, we also installed the wiring-x86 library. We can change just a few lines of our object-oriented code to replace the mraa library with the wiring-x86 one to change the brightness levels for the red, green and blue components.

There is an important difference between the mraa library and the wiring-x86 library when working with PWM. The former works with floating point values from 0.0f to 1.0f to set the output duty cycle percentage, but the latter works with values from 0 to 255 inclusive to set this value. Thus, when working with the wiring-x86 library, we don't need to translate the desired brightness level to an output duty cycle percentage and we can use the brightness level value to specify the value for PWM. As a result, the code is simpler in this case.

The following lines shows the code for a Board class followed by the new version of the AnalogLed class that works with the wiring-x86 library instead of using mraa. The code file for the sample is iot_python_chapter_04_04.py.

from wiringx86 import GPIOGalileoGen2 as GPIO class Board: def set_brightness(self, value):

brightness_value = value

Chapter 4 if brightness_value > 255:

brightness_value = 255 elif brightness_value < 0:

brightness_value = 0

self.gpio.analogWrite(self.pin, brightness_value) self.brightness_value = brightness_value

print("{0} LED connected to PWM Pin #{1} set to brightness {2}.".format(self.name, self.pin, brightness_value))

We just needed to change a few lines from the previous code of the AnalogLed class. The new lines that interact with the wiring-x86 library are highlighted in the previous code. The constructor, that is, the __init__ method, saves a reference to the Board.gpio class attribute in self.gpio and calls its pinMode method with the received pin as its pin argument and self.gpio.PWM as its mode argument.

This way, we configure the pin to be an output PWM pin. All the Led instances will save a reference to the same Board.gpio class attribute that created an instance of the GPIO class, specifically, the wiringx86.GPIOGalileoGen2 class with its debug argument set to False to avoid unnecessary debug information for the low-level communications.

The set_brightness method calls the analogWrite method for the GPIO instance (self.gpio) to set the output duty-cycle for the pin configured as PWM output to brightness_value. The self.pin attribute specifies the pin value for the analogWrite method call. Because brightness_value is already a value between 0 and 255 (inclusive), it is valid value for the analogWrite method.

The rest of the code for our RESTful API remains the same one that we have used for the previous example. There is no need to make changes to this class because it will automatically work with the new AnalogLed class and there were no changes in the arguments for its constructor or its set_brightness methods.

The following line will start the HTTP server and our new version of the RESTful API that works with the wiring-x86 library. Don't forget that you need to transfer the Python source code file to the Yocto Linux with an SFTP client, as explained in the previous chapter.

python iot_python_chapter_04_04.py

We can make the same HTTP requests we made in our previous example to check that we can achieve exactly the same results with the wiring-x86 library.

Test your knowledge

1. PWM stands for:

1. Pin Work Mode.

2. Pulse Weight Modulation.

3. Pulse Width Modulation.

2. In the Intel Galileo Gen 2 board, the pins labeled with the following symbol as a prefix for the number can be used as PWM output pins:

1. Hash sign (#).

2. Dollar sign ($).

3. Tilde symbol (~).

3. A 100% duty cycle (always signal in the ON status) in a PWM pin will generate a steady voltage equal to:

1. 0 V.

2. The voltage specified in the position in which the IOREF jumper is located.

3. 6 V.

4. A 0% duty cycle (always signal in the OFF status) in a PWM pin will generate a steady voltage equal to:

1. 0 V.

2. The voltage specified in the position in which the IOREF jumper is located.

3. 6 V.

5. A 50% duty cycle in a PWM pin with a LED connected to it will generate the same result as a steady voltage equal to:

1. 0 V.

2. Half the voltage specified in the position in which the IOREF jumper is located.

3. 6 V * 0.5 = 3 V.

Chapter 4

Summary

In this chapter, we worked with Tornado web server, Python, the HTTPie command-line HTTP client, and the mraa and wiring-x86 libraries. As in the previous chapters, we took advantage of Python's object-oriented features and we generated many versions of RESTful APIs that allowed us to interact with the board in computers and devices connected to the LAN.

We could compose and send HTTP requests that printed number in LEDs, changed the brightness levels for three LEDs and generated millions of colors with an RGB LED.

Now that we created our first RESTful APIs that made is possible for computers and devices to interact with our IoT device, we can take advantage of additional features that allow us to read digital inputs and analog values, which is the topic of the next chapter.

Working with Digital Inputs,