📜 ⬆️ ⬇️

Smart alarm clock on .NET

Recently, I began to be interested in the idea of ​​"smart home". I think it would be very convenient to control the lighting from your own applications. To be able to set the time of switching on / off the light or describe any scenario of controlling electrical appliances.
I turned my attention to the Noolite system. It enables wireless control of electrical appliances. In its series, it has various power units, different types of control panels. Among Noolite products, there is an Pc118 adapter.

image

It allows you to control the power units of the series via USB. Noolite provides detailed and accessible documentation for the device. Our goal was to write an application that allows us to interact with the lighting control system through this adapter.

What they wanted to do


We tried to implement the “soft start” scenario - a gradual increase in the brightness of the light, this could, for example, contribute to a comfortable wake up in the morning. The article Light Control via Browser describes in detail how our web application works. The source code of the application is available on Google Code .
')
This article describes the part of the system that is responsible for interacting with the USB adapter. Namely, the identification of the device, sending commands, also shows an example of a code that implements the “smooth on” scenario. Most of the time I am developing on .NET, so code samples will be in C #.

Principles of working with USB on .NET


USB devices are very common today. Unfortunately, in .NET there is no USB component available out of the box. You can work with USB using the FileStream class. There are third-party libraries (including open source) that provide convenient wrappers over the FileStream. We used the HidLibrary library. The PC118 adapter is connected via USB and can be operated as with a HID device . To identify the device and gain access to it, you need to specify two of its identifiers - vendor ID (Vendor ID, VID) and product ID (PID). These values ​​can be found in the documentation for the device.
The HidDevices class from the HidLibrary library has a static Enumerate method. This method returns a list of found HID devices with the ability to filter the list by PID and VID. Select the desired device (I took the first one from the filtered list) and establish a connection with it by calling the OpenDevice method.

var device = HidDevices.Enumerate(VENDOR_ID, PRODUCT_ID).FirstOrDefault(); if (device != null) { device.OpenDevice(); } 

The device is ready for operation. Now we can send commands to it.

Work with adapter


The command for the device is an array of bytes.

 var data = new byte[] { ... }; 

Their values ​​must be set in accordance with the parameters of the command being sent: command type (for example, on / off / set brightness level), channel (PC118 adapter can send commands to 8 channels with numbers from 0 to 7), brightness value (if the command set brightness). Details about this are written in the documentation.

Our application uses several commands:
- enable load (On)
- turn off the load (Off)
- switch the load status (Switch)
- Set the brightness (SetLevel).

To send a command to a device, call the WriteFeatureData method and pass the generated array to it:

 device.WriteFeatureData(data); 

There is a note in the documentation: “Depending on the library you are using, you may need to send the first byte with a value of 0 before sending 8 bytes.”
During the experiments, it turned out that this is our case. To correctly send a command to the beginning of the array, you need to add an additional zero byte (send arrays of 9 bytes to the adapter, and not 8, as written in the instructions). Apparently, this is a feature of the HidLibrary library.
It also turned out that between sending the commands it is necessary to pause 200 ms., Otherwise the adapter executes only the first command. The duration of the delay was determined experimentally.

What happened


After we learned how to send commands to the adapter, everything was wrapped in a class that implements the IDisposable interface, and the enum for command types was described. You can download the compiled DLL and use it to control the light from your own .NET applications. For example, the code for the smooth inclusion of light might look like this:

 using (var adapter = new Pc118Adapter()) { if (adapter.OpenDevice()) { // 40 -   ,      for (var level = 40; level < 100; level++) { adapter.SendCommand( Pc118Command.SetLevel, //  2, //  level //   ); Thread.Sleep(60000); //    } } } 

We also wrote a console utility that allows you to send commands to the device. This application can be useful as an example. In addition, it can be used to test the device.

Conclusion


We were interested to observe the result. I hope the article was useful to someone. Maybe it will help to create new interesting scenarios for controlling lights and appliances in the house.

Thanks for attention!

Source: https://habr.com/ru/post/176141/


All Articles