• No results found

Determining the darkness level with analog inputs and the mraa library

In document Internet of Things With Python (Page 194-197)

We will create a new DarknessSensor class to represent the photoresistor included in the voltage divider and connected to our board, specifically, to an analog input pin. As we already wrote code to read and transform an analog input, we will use the previously created VoltageInput class. The following lines show the code for the new DarknessSensor class that works with the mraa library. The code file for the sample is iot_python_chapter_06_02.py.

import mraa import time

class DarknessSensor:

# Light level descriptions

light_extremely_dark = "extremely dark"

light_very_dark = "very dark"

light_dark = "just dark"

light_no_need_for_a_flashlight = \ "there is no need for a flashlight"

# Maximum voltages that determine the light level extremely_dark_max_voltage = 2.0

very_dark_max_voltage = 3.0 dark_max_voltage = 4.0

def __init__(self, analog_pin):

self.voltage_input = VoltageInput(analog_pin) self.voltage = 0.0

self.ambient_light = self.__class__.light_extremely_dark self.measure_light()

def measure_light(self):

self.voltage = self.voltage_input.voltage

if self.voltage < self.__class__.extremely_dark_max_voltage:

self.ambient_light = self.__class__.light_extremely_dark elif self.voltage < self.__class__.very_dark_max_voltage:

self.ambient_light = self.__class__.light_very_dark elif self.voltage < self.__class__.dark_max_voltage:

self.ambient_light = self.__class__.light_dark else:

self.ambient_light = self.__class__.light_no_need_for_a_

flashlight

We have to specify the analog pin number to which the voltage divider, which includes the photoresistor, is connected when we create an instance of the

DarknessSensor class in the analog_pin required argument. The constructor, that is, the __init__ method, creates a new VoltageInput instance with the received analog_pin as its analog_pin argument and saves its reference in the voltage_

input attribute. Then, the constructor creates and initializes two attributes: voltage and ambient_light. Finally, the constructor calls the measure_light method.

The class defines a measure_light method that saves the voltage value retrieved by checking the self.voltage_input.voltage property in the voltage attribute (self.voltage). This way, the code can check whether the value stored in the voltage attribute is lower than the three maximum voltage values that determine the light level and sets the appropriate value for the ambient_light attribute (self.

ambient_light).

The class defines the following three class attributes that determine the maximum voltage values that determine each light level:

• extremely_dark_max_voltage: If the retrieved voltage is lower than 2V, it means that the environment is extremely dark

• very_dark_max_voltage: If the retrieved voltage is lower than 3V, it means that the environment is very dark

• dark_max_voltage. If the retrieved voltage is lower than 4V, it means that the environment is just dark

The values are configured for a specific photoresistor and environment conditions. You might need to set different values based on the voltage values retrieved with the photoresistor included in the voltage divider.

Once you run the sample, you can check the voltage values and make the necessary adjustments to the voltage values stored in the previously explained class attributes. Remember that the voltage value will be higher, that is, closer to 5V, when the incident light increases. Thus, the darkest environment, the lower the measured voltage.

Chapter 6 Our goal is to convert a quantitative value, specifically, a voltage value, into

a qualitative value, that is, a value that explains the real situation in a real

environment. The class defines the following four class attributes that specify the light level descriptions and determine one of the four light levels in which a voltage value will be converted after we call the measure_light method:

• light_extremely_dark

• light_very_dark

• light_dark

• light_no_need_for_a_flashlight

Now, we can write the code that uses the new DarkSensor class to create an instance for the photoresistor included in the voltage divider and easily print a description of the light conditions. The new class uses the previously created VoltageInput class to make the necessary calculations to map the read value into a voltage value, and then, transforms it into a qualitative value that provides us with a description of the light conditions. Now, we will write a loop that will check whether the light conditions changed every two seconds. The code file for the sample is iot_python_

chapter_06_02.py.

if __name__ == "__main__":

darkness_sensor = DarknessSensor(0) last_ambient_light = ""

while True:

darkness_sensor.measure_light()

new_ambient_light = darkness_sensor.ambient_light if new_ambient_light != last_ambient_light:

# The ambient light value changed last_ambient_light = new_ambient_light

print("Darkness level: {0}".format(new_ambient_light)) # Sleep 2 seconds

time.sleep(2)

The first line creates an instance of the previously coded DarknessSensor class with 0 as the value of the analog_pin argument and saves the instance in the darkness_sensor local variable. This way, the instance will use an instance of the VoltageInput class to read the analog values from the pin labeled A0. Then, the code initializes the last_ambient_light local variable with an empty string.

Then, the code runs a loop forever, that is, until you interrupt the execution by pressing Ctrl + C or the button to stop the process in case you are using a Python IDE with remote development features to run the code in your board. The loop calls the darkness_sensor.measure_light method to retrieve the current light conditions and saves the updated darkness_sensor.ambient_light value in the new_ambient_light local variable. Then, the code checks whether the

new_ambient_light value is different from last_ambient_light. In case they are different, it means that the ambient light has changed, and therefore, it sets the value for last_ambient_light equal to new_ambient_light, and prints the ambient light description stored in new_ambient_light.

The loop prints the ambient light description only when it changes from the last printed value, and checks the ambient light every two seconds. The following line will start the example. Don't forget that you need to transfer the Python source code file to the Yocto Linux with an SFTP client.

python iot_python_chapter_06_02.py

After you run the example, perform the following actions:

• Use a smartphone or a flashlight to induce light over the photoresistor

• Use your hand to generate a shadow over the photoresistor

• Reduce the light in the environment, but not the minimum, just make it a bit dark

• Reduce the light in the environment to the minimum, a complete dark environment with no light at all

As a result of the previous actions, you should see the following output:

Darkness level: there is no need for a flashlight Darkness level: just dark

Darkness level: very dark Darkness level: extremely dark

Firing actions when the environment

In document Internet of Things With Python (Page 194-197)