In the previous tutorial, I described a bit about what Apple HomeKit is and the basic configuration of the ESP module. In addition, there was information about the structure of the project and the configuration of new accessories in the Home application. Therefore, in this article I will not repeat this information once again, if you meet the topic of combining ESP microcontrollers and the HomeKit platform for the first time then I refer you to the previous material.
The topic of this tutorial will be the integration of an ESP8266 module with a line of RGB LEDs, designated WS2812B, connected to its lead. This extremely popular design has gained considerable popularity due to its low price and simplicity of implementation. The luminous elements are connected in series, so that only one pin of the microcontroller is used for communication. However, you need to remember that the power supply is connected in parallel. Besides, diode rulers can consume quite a lot of current, a single element emitting white light can need as much as 60mA, this is because at the white wave of light all silicon structures – red, green and blue – shine, and each of them consumes about 20mA. With a few RGB LEDs we do not need to keep this in mind, but with more, the current consumption increases, in which case it will be advisable to power the LEDs from a separate power source. In this case, using a USB socket, in the computer can end badly, in extreme cases we can damage them irreparably.
WS2812B diode control
If the development environment is ready, you can proceed to physically connect the LEDs to the ESP module. For the purpose of the example, I connected a diode ruler with four lighted elements to the D4 pin of my microcontroller, the whole thing is powered from the USB port of the computer. Note that with a larger number of LEDs, an external power supply with more power is advisable.
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
#define NEOPIN D4
#define NUMPIXELS 4
In the main code of the project we need to take a closer look at really only two definitions – NEOPIN and NUMPIXELS. The first one defines the pin to which the signal controlling the diodes is connected, in my example it is D4. The second definition specifies the number of diodes connected to the ESP, I connected four elements, that’s why the code included the number 4. Interestingly, in the definition of the number of diodes you can specify a value greater than the number of physically connected elements. However, it should be remembered that in such a case the ESP module, will drive diodes that are not physically present, which takes valuable time for the microcontroller.
The files my_accessory.c and wifi_info.h are not much different from the example in the previous tutorial. Here, too, we can modify the basic configuration data, such as the device name (in the example, it’s RGB LED) and the access password needed for the first startup. We specify the information about the network the ESP is to connect to in the header file.
Controlling LEDs from the app
Once the ESP module is programmed, you can proceed to add a new accessory in the Home application. The process looks identical to the previous material. However, the process of controlling LEDs itself deserves a moment’s attention. As you can see in the video we have quite a lot of possibilities, we can successfully control brightness, color, as well as color temperature. In addition, you can create several shortcuts to specific glow colors.
As you can see, the control of the WS2812B RGB LEDs, is quite simple and does not require too much code intervention. All you need to do is insert your own parameters and configuration information, and then program the chip. Once the module is up and running, connect it to Apple HomeKit and you’re done.
Also see:
The next material on Apple HomeKit is not yet available.
Sources:
- https://github.com/adafruit/Adafruit_NeoPixel
- https://github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266