Making home lighting a little smarter.







About the benefit of f.lux, to one degree or another, they have written several times on both the habr and on the giktimes. Having tried this program some time ago, I had a question - what about the rest of the lighting in the room? Too much at night is knocked out of the general, warm light of incandescent lamps, the orange screen of the monitor.



We will try to correct this unfortunate misunderstanding.



Disclaimer



The author of the text does not fully understand how human vision is arranged, and whether the operations described below will be useful or harmful to him. Aside, the aspects of testing indicators of LED lamps for their quality are overboard. It was decided to take them for "satisfactory", thanks to AlexeyNadezhin and Tiberius for the relevant research.



Buying lamps



A cursory analysis of the market showed that there is something to choose from. Starting with the super bullet nouneym of Chinese lamps, ending with the very immodest money Belkin and Philips. As a result, it was decided to use the Yeelight Smart Led Bulb, manufactured under the Xiaomi brand, as a basis. Their advantage is the ability to operate without any additional equipment from the “smart home” category and the ability to work autonomously, without being tied to the company's management servers (after some tweaks).



At the time of this writing, the cost of one lamp is about $ 20.



Software part



We will write code to control the lamps in python, for it there is a ready-made module that allows you to transfer control commands to these lamps. There is also a document detailing api. Thus, it remains only to add a small web server in order to receive control commands from f.lux.



However, you first need to initialize the Smart Bulb. We carry out the initialization of the lamps through the proprietary MiHome application and achieve the appearance of the Yeelight Color Bulb icon in the application interface. Connection problems may occur due to dns and the selection of the default server. They are solved by writing 8.8.8.8 to dns and choosing China (Mainland).







It is also required to unlock the ability to directly access the devices by activating Developer Mode on them through the second application, Yeelight . After all these operations, the lamps appear on the network and they can already be accessed.







Due to their reluctance to tell Wi-Fi passwords to smart lights, they move to a separate subnet with a separate SSID and without access to the Internet.



So, the lamps hang in the network, in the application they went offline and you can try to transfer control commands to them.



F.lux integration



f.lux can send data about the change in color temperature and brightness to a given URL. Data arrives in the form of a post-request of the form:

POST / room_1?ct=6500&bri=0.800000 HTTP / 1.1






Let's write a small web server on the bottle that processes these parameters:



from bottle import run, post, request from yeelight import Bulb @post('/room_1') def room_handler(): post_dict = request.query.decode() if 'ct' in post_dict and 'bri' in post_dict: ct = int(post_dict['ct']) bri = int(float(post_dict['bri']) * 100) bulb.set_color_temp(ct) bulb.set_brightness(bri) bulb = Bulb("%lamp_ip%") run(host='0.0.0.0', port=8080)
      
      







Final result



Having worked for a week in this mode, it is difficult for me to say if there are any positive changes and if my eyes got tired. Dynamically adjusted light looks interesting and the difference between the monitor and the surrounding lighting is not so striking. On the other hand, the very fact of constant change in ambient light is unusual.


















All Articles