After buying myself a google home mini a couple weeks ago I started looking into Smart Home devices, and got put off by the pricing of a lot of the devices and that most devices need a dedicated hub. I then discovered Openhab, https://www.openhab.org/, which is a software for integrating different home automation systems and technologies into one single solution that allows over-arching automation rules and that offers uniform user interfaces.
I started playing about with Openhab installing Openhabian on a Raspberry Pi 3 and used videos by MK Smart Home on Youtube https://www.mksmarthouse.com/ who covers home automation using Openhab and mostly ESP8266. Whilst plating about with openhab, I saw that it has a binding, a way of integrating different technologies into Openhab, for the multimedia software Kodi. I have been using Kodi on a Raspberry pi for a media centre for several years and have always wanted to implement a movie theatre style lighting system for use with it, my original plan was to write a script on the Raspi to control the lighting directly from the Pi's GPIO, but i could never manage to get anywhere with that, but with Openhab and using MQTT to send commands to an ESP8266, i have managed to complete this dream.
What i want the Light to do is
- If nothing is playing, Light fuly on
- When media starts playing Turn light off
- When media paused set light to dim, Currently 50% but may change to lower
- When media stop turn Light fully on
To make it more like the movie theatres, i wanted the lights to fade up and fade down. Taking longer to fade down than to fade up.
I bought some cheap LED Lights from a bargain store, about £3 each which was reasonable as i didn't have to wait for delivery, and are also very bright
I started by binding my Kodi Instance into Openhab in a file things/kodi.things;
Thing kodi:kodi:myKodi "Kodi"@"Living Room" [ipAddress ="192.168.0.2", port=9090, httpPort=8080]
Then I declared the items in items/home.items i would need for using this;
//======================================== //Kodi Instances //======================================== //Living Room Switch myKodi_mute "Mute" ["Switchable"] { channel="kodi:kodi:myKodi:mute"} Dimmer myKodi_volume "Volume [%d]" ["Lighting"] {channel="kodi:kodi:myKodi:volume"} Player myKodi_control "Control" {channel="kodi:kodi:myKodi:control"} Switch myKodi_stop "Stop" {channel="kodi:kodi:myKodi:stop"}
The only two items for the Kodi Instance required for the system is the myKodi_control and myKodi_stop, which react on the play and stop states of kodi changing.
Here is a copy of the Log for playing a TV Show
2018-03-22 22:49:19.035 [vent.ItemStateChangedEvent] - myKodi_control changed from PAUSE to PLAY 2018-03-22 22:49:19.041 [vent.ItemStateChangedEvent] - myKodi_stop changed from ON to OFF 2018-03-23 02:24:26.110 [vent.ItemStateChangedEvent] - myKodi_control changed from PLAY to PAUSE 2018-03-23 02:24:26.124 [vent.ItemStateChangedEvent] - myKodi_stop changed from OFF to ON
The Cycle of events for playing media
- Media Play ; Control Changes to PLAY and Stop Changes to OFF
- Media Stop ; Control Changes to PAUSE and Stop Changes to ON
- Media Pause (not shown in log); Control Changes to PAUSE and Stop stays OFF
The last items to add to the Items file is the dimmer for the lighting and a switch for turning on the movie mode. The movie mode allows the system stop being controlled by kodi so as to not have the lights running if I don't want them automated.
Switch Movie_Time "Movie Mode" ["Switchable"] Dimmer Movie_Light "Movie Light"["Lighting"] {mqtt=">[broker:DF/lights/LEDMovie:command:*:default"}
The dimmer control also has the declaration for the MQTT topic which the Lightning uses.
The last thing to be done in openhab is to declare Rules for controlling everything
//=========================================== //Movie Lighting //=========================================== rule "Movie Time Turned On" when Item Movie_Time changed to ON then if(myKodi_stop.state == ON) { Movie_Light.sendCommand("100") } if(myKodi_control.state == PLAY && myKodi_stop.state == OFF) { Movie_Light.sendCommand("0") } if(myKodi_control.state == PAUSE && myKodi_stop.state == OFF) { Movie_Light.sendCommand("50") } end //Kodi Pause rule "Kodi Pause Playing" when Item myKodi_control changed to PAUSE then if(Movie_Time.state == ON) { if(myKodi_stop.state == OFF) { Movie_Light.sendCommand("50") } else { Move_Light.sendCommand("100") } } end //Kodi Play rule "Kodi Start Playing" when Item myKodi_control changed to PLAY then if(Movie_Time.state == ON) { Movie_Light.sendCommand("0") } end //Kodi Stop rule "Kodi Stop" when Item myKodi_stop changed to ON then if(Movie_Time.state == ON) { Movie_Light.sendCommand("100") } end
The last three rules are triggered with a change in the kodi instances, and it will send different values to the Movie Light MQTT. The first one is triggered when the Movie Mode Switch is turned on, this allows the system to be be started with any point in the cycle.
I'am using an ESP8266 NodeMCU for testing purposes, I am powering it through a chopped up USB cable through the Vin and the on-board power regulator. The Leds are controlled through a transistor. I have added headers for two LEDS, as thats how many i bought, though i may add more to put under the TV stand. The next step will be to design a PCB and use an ESP-12.
I am not going to upload the Arduino Code just now as i still need to do some work on it, the issue i am having is that is that when the media stops it send two commands to the ESP8266, both the play and stop rules, and the ESP8266 only responses to the first command Pause so only goes to 50%
Here is a short video of the completed system;
The lights and Movie Mode Switch can be controlled via the Openhab App
But the best part, in my opinion, is the fact that i can control the system through my Google Home, once i have linked google to my openhab account. It also requires me to include tags in the item declarations, see above, the ["Switchable"] and ["Lighting"] tag allow for a simple switch and a dimmer.
This means i can simple say "OK Google, Set Movie Light to 10%" and the lights react. I'am also wanting to add custom phasing to Google Home, so when I say OK Google, Im putting on the Popcorn" it will turn on the Movie Mode.
I am hoping this will be the first step in home automation, i hope to add some smart light switches so that the movie mode switch will also dim my main living room lights and Turn off my kitchen lights (My kitchen is attached onto my living room and doesn't have a door) and turn off Hallway lights (Again attached right onto my living room, but with a glass panel door).
I'am going to do some more work on the Arduino code and post an update with it, i am also considering once I have a PCB designed to add more of the Lights in the Living room to increase the effect.
Updated
I have managed to fix the issue with Openhab sending two simultaneous commands to the ESP8266. I have changed the rule for the Pause State by telling it that it should only run if the stop state is off.
//Kodi Pause rule "Kodi Pause Playing" when Item myKodi_control changed to PAUSE then if(Movie_Time.state == ON) { if(myKodi_stop.state == OFF) { Movie_Light.sendCommand("50") } } end
I have also added some more lines to the rule for movie mode changing to on or changing to off to display a notification on my kodi system.
I have also been running some tests with my arduino code for the timings, trying to figure out how long I want the fade to last, here is the serial output showing timings
Message Received = 100 > LED at Value = 100 Time Taken = 3.04s Message Received = 0 < LED at Value = 0 Time Taken = 5.06s
The < and > just indicate whether the new value is larger or smaller than the previous value. With the current delay set, it will take 3 seconds to fade up and 5 seconds to fade down. I will experiment with the delay values if i find that i am not satisfied with these delays.
I have also now attached the Arduino Code.
Top Comments