[PYTHON] Control LED bulbs from a microcomputer (Intel Edison) (2)

Continuing from previous, we will challenge the control of Bluetooth-controlled LED bulbs from a microcomputer. This time, I will try to control from the program language (Python).

Thing you want to do

slide.png

--1st step: Control by typing a command from the microcomputer (previous) --2nd step: Control by running a program on the microcomputer

In this article, we will work on the 2nd step.

environment

Procedure to control

It is assumed that the settings of the microcomputer (Intel Edison), the connection with the Bluetooth control type LED bulb (PLAYBULB), and the control of the bulb using gatttools are completed (previous. / items / 7d6afca7b81ea46f9474)). In this article, we will make it possible to do what we did with gatttools (device control through the Bluetooth LE GATT protocol) in a programming language. I used Python as the language because "the environment and information on Edison are substantial". I think that other languages can be used as long as it is a programming language that can control GATT.

[1] Installation of Python and Pip (Optional)

I installed standalone Python (3.5.3) and Pip from source (for a different reason than this one). For the method, I referred to Intel official website ..

[2] Installation of BluePy

There are several ways to use GATT functionality through Python. This time, I used Bluepy.

--Pexpect ([Edison official page](https://software.intel.com/en-us/articles/using-the-generic-attribute-profile-gatt-in-bluetooth-low-energy-with-your-intel] -edison) has an article) -PyGatt (wrapper of gatttools * It doesn't work well in your environment) -Bluepy (wrapper of gatttools)

$ pip3 install bluepy

[3] Implementation and execution of programs that handle GATT

Now that we're ready, we're ready to implement it. Here, let's create a program that repeatedly changes the color every 3 seconds.

light_control.py


# -*- coding: utf-8 -*-
from bluepy import btle
from bluepy.btle import BTLEException
import time

LIGHT_MAC_ADDR="DB:22:4B:13:AC:E6"

if __name__ == '__main__':
    # connect to device
    tPeripheral = btle.Peripheral(deviceAddr=LIGHT_MAC_ADDR)
    # find characteristic
    tCharList = tPeripheral.getCharacteristics()
    tChar = next(tChar for tChar in tCharList if tChar.getHandle()==0x001b)    

    while (True):
        tChar.write(bytes([0, 255, 0, 0]))  # red
        time.sleep(3)
        tChar.write(bytes([0, 0, 255, 0]))  # green
        time.sleep(3)
        tChar.write(bytes([0, 0, 0, 255]))  # blue
        time.sleep(3)

There are several ways to get the characteristic (I understand it in the form of "an object that controls a certain function"), but I took the method of getting all the characteristics and then matching the handles. For handles and control commands, refer to the table published in the article Previous.

When the program is completed, try running it with the following command

$ python3 light_controll.py

Did the color of the LED bulb change every 3 seconds?

Summary

I was able to control the LED bulb (PLAYBULB) using my own program (Python) and library (bluepy). As an application

-** Change color depending on time ** (Link with microcomputer clock) -** Change color depending on tomorrow's weather ** (link with weather API) -** Change the color when your family is near your home ** (Link with smartphone GPS)

Etc. can be realized. It may be installed in the living room with a small Intel Edison and used as a family information light. If you know that there is such an interesting application, please let me know.

I tried using the light bulb function of the LED light bulb twice, but next time I would like to use another speaker function.

Recommended Posts

Control LED bulbs from a microcomputer (Intel Edison) (1)
Control LED bulbs from a microcomputer (Intel Edison) (2)
Control the Matrix LED panel from ROS
Infrared remote control reception with Intel Edison
Startup Intel Edison
Send accelerometer value from Eaglet + Intel Edison to Kinesis