Smart alarm clock for .NET
Recently I became interested in the idea of "Smart home". I think it would be very convenient to control lights of their own applications. To be able to set the time on/off lights or to describe any scenario of control of appliances.
I turned my attention to the system Noolite. It enables wireless control of electrical appliances. In his series it has different power blocks, different types of control panels. Among the products Noolite is there an adapter Pc118.
It allows you to manage the power series units via USB. Noolite provides a detailed and accessible documentation for the device. Our goal was to write an app that allows you to interact with the lighting control system through this adapter.
We tried to implement the script "slow start" — a gradual increase in the brightness of light, this could, for example, contribute to a comfortable awakening in the morning. In article light Control via browser describes in detail how our web application. Source code is available on Google Code.
This article describes part of the system, which is responsible for interaction with the USB adapter. Namely, the identification of the device sending commands provides example code that implements the scenario of "smooth switching". Most of the time I'm working on .NET code examples are in C#.
USB devices are very common today. Unfortunately .NET not available out of the box component to work with USB. You can work with USB using the FileStream class. There are third-party libraries (including open source) that provide convenient wrappers over FileStream. We used the library HidLibrary. PC118 adapter connects via USB and can work with it as with HID device. To identify the device and obtain access to it, you need to specify two ID — ID vendor-specific (Vendor ID, VID) and product ID (Product ID, PID). These values can be read from documentation the device.
Class HidDevices from the library HidLibrary has a static method Enumerate. This method returns the list of HID devices with the ability to filter the list by PID and VID. Select the device you need (I took the first from the filtered list) and set up a connection by calling the OpenDevice.
the
The device is ready for operation. Now we can send a team.
The command for the device is an array of bytes.
the
Their values must be set in accordance with parameters sent to the command: command type (e.g., on/off/set brightness level), the channel (PC118 adapter can send commands to 8 channels numbered from 0 to 7), the value of the brightness level (if you select brightness setting). More about this is written in the documentation.
In our app we use several commands:
— enable load (On)
— off load (Off)
— switch the load status (Switch)
— set the brightness (SetLevel).
To send a command to the device, call the WriteFeatureData and give him the generated array:
the
In the documentation there is a note: "depending on your library, before you send 8 bytes, you may want to send the first byte with the value 0".
During the experiments it turned out that this is exactly our case. To correctly send the command to the beginning of the array you want to add an additional zero byte (to send to the adapter arrays by 9 bytes, not 8 as written in the instructions). Apparently, it is a feature of the library HidLibrary.
After we learned how to send commands to the adapter, everything was wrapped in a class that implements the IDisposable interface, and describes the enum for the types of commands. You can download the compiled DLL and use it to control the light from your own applications .NET. For example, the code for smooth turning on the light can look like this:
the
So we wrote a command-line utility that allows you to send commands to the device. This application can be useful as an example. In addition, it can be used for testing the device.
It was interesting to observe the result. I hope the article was useful to someone. Maybe it will help to appear interesting scenarios for the management of lights and appliances at home.
Thank you for your attention!
Article based on information from habrahabr.ru
I turned my attention to the system Noolite. It enables wireless control of electrical appliances. In his series it has different power blocks, different types of control panels. Among the products Noolite is there an adapter Pc118.
It allows you to manage the power series units via USB. Noolite provides a detailed and accessible documentation for the device. Our goal was to write an app that allows you to interact with the lighting control system through this adapter.
wanted to do
We tried to implement the script "slow start" — a gradual increase in the brightness of light, this could, for example, contribute to a comfortable awakening in the morning. In article light Control via browser describes in detail how our web application. Source code is available on Google Code.
This article describes part of the system, which is responsible for interaction with the USB adapter. Namely, the identification of the device sending commands provides example code that implements the scenario of "smooth switching". Most of the time I'm working on .NET code examples are in C#.
how to work with USB .NET
USB devices are very common today. Unfortunately .NET not available out of the box component to work with USB. You can work with USB using the FileStream class. There are third-party libraries (including open source) that provide convenient wrappers over FileStream. We used the library HidLibrary. PC118 adapter connects via USB and can work with it as with HID device. To identify the device and obtain access to it, you need to specify two ID — ID vendor-specific (Vendor ID, VID) and product ID (Product ID, PID). These values can be read from documentation the device.
Class HidDevices from the library HidLibrary has a static method Enumerate. This method returns the list of HID devices with the ability to filter the list by PID and VID. Select the device you need (I took the first from the filtered list) and set up a connection by calling the OpenDevice.
the
var device = HidDevices.Enumerate(VENDOR_ID, PRODUCT_ID).FirstOrDefault();
if (device != null)
{
device.OpenDevice();
}
The device is ready for operation. Now we can send a team.
with adapter
The command for the device is an array of bytes.
the
var data = new byte[] { ... };
Their values must be set in accordance with parameters sent to the command: command type (e.g., on/off/set brightness level), the channel (PC118 adapter can send commands to 8 channels numbered from 0 to 7), the value of the brightness level (if you select brightness setting). More about this is written in the documentation.
In our app we use several commands:
— enable load (On)
— off load (Off)
— switch the load status (Switch)
— set the brightness (SetLevel).
To send a command to the device, call the WriteFeatureData and give him the generated array:
the
device.WriteFeatureData(data);
In the documentation there is a note: "depending on your library, before you send 8 bytes, you may want to send the first byte with the value 0".
During the experiments it turned out that this is exactly our case. To correctly send the command to the beginning of the array you want to add an additional zero byte (to send to the adapter arrays by 9 bytes, not 8 as written in the instructions). Apparently, it is a feature of the library HidLibrary.
What happened
After we learned how to send commands to the adapter, everything was wrapped in a class that implements the IDisposable interface, and describes the enum for the types of commands. You can download the compiled DLL and use it to control the light from your own applications .NET. For example, the code for smooth turning on the light can look like this:
the
using (var adapter = new Pc118Adapter())
{
if (adapter.OpenDevice())
{
// 40 is the minimum level at which the lamp begins to glow
for (var level = 40; level < 100; level++)
{
adapter.SendCommand(
Pc118Command.SetLevel, // command
2, // channel
level // level of brightness
);
Thread.Sleep(60000); // stop for a minute
}
}
}
So we wrote a command-line utility that allows you to send commands to the device. This application can be useful as an example. In addition, it can be used for testing the device.
Opinion
It was interesting to observe the result. I hope the article was useful to someone. Maybe it will help to appear interesting scenarios for the management of lights and appliances at home.
Thank you for your attention!