Philips Hue Dimmable Light Remote Control via ESP8266 two push buttons & OLED (openHAB,B4R,MQTT)

Exploring openHAB & openHABian.
Site Admin
Posts: 217
Joined: 07 Oct 2017, 12:16

Philips Hue Dimmable Light Remote Control via ESP8266 two push buttons & OLED (openHAB,B4R,MQTT)

Postby rwblinn » 27 Feb 2018, 13:43

openHAB2 Experiment - Philips Hue Dimmable Light Remote Control via ESP8266 two push buttons & OLED (openHAB,B4R,MQTT)
Version 20180217

To control a Philips Hue dimmable light, via WiFi & MQTT, by using two push buttons UP & DOWN and an OLED display connected to an ESP8266 NodeMCU v1.0.

As a proof-of-concept, build the solution into a LEGO "case" using some spare LEGO stones. Considering to build a proper LEGO "case".
The OLED shows brightness of 10%, right from the OLED is the control LED (turned off) and the two push buttons UP & DOWN.

  • 1x ESP8266 NodeMCU v1.0
  • 2x Push Buttons
  • 2x 10kOhm Resistors
  • 1x OLED 128x32 I2C
  • 1x LED RED
  • 1x 220Ohm Resistor
  • 1x Hue Dimmable Light (White Lamp E27)


The openHAB server is a Raspberry Pi 3 with openHABian, openHAB 2.2.0 and Mosquitto MQTT Broker.


No download - the openHAB configuration and the B4R source are listed below.

The Hue light brightness is remote controlled via MQTT.
When pressing a push button, a MQTT topic with payload, containing the brightness value between 0 - 100, is send to the openHAB server.
An openHAB number item listens to the MQTT topic and if the value (=state) changes, an openHAB rules triggers setting the value of an openHAB item Dimmer assigned to the Hue Light Thing.
The Hue Light Thing is updated via its Hue Bridge Channel.
The other way around is also implemented, means if the Hue Light brightness is changed via the openHAB Basic-UI, the new value is displayed on the OLED display.

openHAB Configuration
For the Hue Dimmable Light, the items, sitemap and rules are extracted from a full openHAB installation.
The Things are defined using the openHAB Basic-UI.

The thing defined is named "Hue white lamp 1" with status ONLINE.
It is a Dimmable Light connected to Hue Bridge hue:0100:00178863da1d and has Light ID 1.

The Dimmer Item uses the brightness channel of the Thing "Hue white lamp 1".

Code: Select all

Dimmer aHA_MakeLab_HueLight1bsl "Brightness" <light> (gHA_MakeLab) { channel="hue:0100:00178863da1d:1:brightness" }

The Number Item contains the brightness by listening to the MQTT topic "homeautomation/makelab/huelight1/set" with payload between 0 - 100.

Code: Select all

Number nHA_MakeLab_HueLight1 { mqtt="<[openhabprod:homeautomation/makelab/huelight1/set:state:default:.*]" }

The sitemap snippets shows the Dimmer Item as a Slider.

Code: Select all

Frame label="Make Lab" {
   Slider  item=aHA_MakeLab_HueLight1bsl

MQTT Broker
The broker is defined in the file mqtt.cfg located on the Raspberry Pi openHAB server /etc/openhabs2/services.

Code: Select all

# Local Mosquitto Broker running on the Raspberry Pi

There are two rules handling the change if the Number Item and the Dimmer Item.

State Change Number Item nHA_MakeLab_HueLight1
As defined in the item, the state will change if the payload of the MQTT message with topic "homeautomation/makelab/huelight1/set" changes.
If a change is made, the Dimmer Item aHA_MakeLab_HueLight1bsl is updated with the value of the Number Item nHA_MakeLab_HueLight1.

Code: Select all

rule "HueLight1 Brightness Set"
   Item nHA_MakeLab_HueLight1 changed
   logInfo("HUELIGHT1", "*** Brightness set to " + nHA_MakeLab_HueLight1.state.toString)

State Change Dimmer Item aHA_MakeLab_HueLight1bsl
If the state of the Dimmer Item changes, by changing the position of the slider in the openHAB Basic-UI, a MQTT message is published with
Broker: openhabprod
Topic: homeautomation/makelab/huelight1
Payload: Value of the Dimmer Item aHA_MakeLab_HueLight1bsl between 0 - 100

Code: Select all

rule "HueLight1 Brightness Changed"
   Item aHA_MakeLab_HueLight1bsl changed
   // Publish the message to topic using the specified MQTT broker.
   publish("openhabprod", "homeautomation/makelab/huelight1", aHA_MakeLab_HueLight1bsl.state.toString)

B4R Solution
The ESP8266 connects to the WLAN followed by a connection to the MQTT broker.
MQTT Outbound:
When pressing one of the push buttons, a MQTT message with the new brightness value is published to the MQTT broker running on the openHAB server:
Topic: "homeautomation/makelab/huelight1/set", Payload: 0 - 100. The OLED display is updated with the new brightness value.
MQTT Inbound:
If a new incoming MQTT message is received, the OLED display is updated with the new brightness value.

For more information, see the comments in the B4R code below.
Libraries used: rcore, resp8266wifi, rmqtt, rrandomaccessfile, radafruitssd1306, radafruitgfx

Code: Select all

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
   Private wificlient As WiFiSocket

   Private PushButtonUp As Pin      ' D6 - GPIO12
   Private PushButtonDown As Pin   ' D5 - GPIO14
   Private PushButtonTimer As Timer
   Private PushButtonTimerInterval As ULong = 120
   Private PushButtonUpPressed As Boolean = False

   'Control LED
   Private ControlLED As Pin       ' D7 - GPIO13

   'OLED Display
   Private ssd As AdafruitSSD1306
   'Reset Pin to init the display. Important: Use any free pin - Ensure not to use the pins for SDA D2 (GPIO4) & SCL PINS D1 (GPIO5)
   Private ResetPin As Int = 2      ' D4 - GPIO2
   'Hue Light Brightness set by MQTT message arrived or pressing a button
   Private HueLight1Brightness As Byte = 0

   'Mosquitto MQTT Broker running on a Raspberry Pi openHAB Home Automation
   Private serverIP() As Byte = Array As Byte(192, 168, N, NN)
   Private serverPort As UInt = 1883

   Private mqtt As MqttClient
   Private mqttCO As MqttConnectOptions
   'Ensure to set a unique clientid in the network
   Private clientID As String = "HAHueRemoteControl"
   'MQTT Topics subscribed (NOT USED)
   Private mqttTopicHueLight1 As String = "homeautomation/makelab/huelight1"
   'MQTT Topics published
   Private mqttTopicHueLight1Set As String = "homeautomation/makelab/huelight1/set"
End Sub

Private Sub AppStart
   Log("AppStart v20180226")
   'Control LED - Init and turn on to indicate something wiill happen.
   ControlLED.Initialize(13, ControlLED.MODE_OUTPUT)
   'Init the pushbuttons and add a listener for each of the buttons
   PushButtonUp.Initialize(12, PushButtonUp.MODE_INPUT_PULLUP)
   PushButtonDown.Initialize(14, PushButtonDown.MODE_INPUT_PULLUP)
   PushButtonTimer.Initialize("PushButtonTimer_Tick", PushButtonTimerInterval)
   PushButtonTimer.Enabled = False
   'Init OLED Display and set a welcome text
   ssd.InitializeI2C(ResetPin, 0x3c)
   SSDSetText("Welcome", "HomeAutomation", "MakeLab", "HueLight1 Brightness")
   'Connect with wifi by setting SSID and password
   If wifi.Connect2("SSID", "*****") Then
      Log("[INFO] Connected to wireless network with local IP ", wifi.LocalIp)
      Log("[ERROR] Failed to connect to wireless network. Check the network parameter.")
   End If
   'Init MQTT
   mqtt.Initialize(wificlient.Stream, serverIP, serverPort, clientID, "Mqtt_MessageArrived", "Mqtt_Disconnected")
   'Make a connection to the MQTT Broker - ensure its up and running
End Sub

#Region MQTT
' Check if connected to mqtt. if not, then try again after 1 sec
Sub Mqtt_Connect(unused As Byte)
   mqttCO.Initialize("", "")
   If mqtt.Connect2(mqttCO) = False Then
      Log("[ERROR] Can not connect the MQTT broker. Trying again ...")
      CallSubPlus("Mqtt_Connect", 1000, 0)
   End If
   Log("[INFO] Connected to the MQTT broker.")
   ' Subscribe to topics
   mqtt.Subscribe(mqttTopicHueLight1, 0)
End Sub

'MQTT New Message arrived
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
   Dim bc As ByteConverter   'Requires library rRandomAccessFile
'   Log("MQTT: Message arrived. Topic=", Topic, ", Payload=", Payload)
   Dim PayLoadStr As String = bc.StringFromBytes(Payload)
   If IsNumber(PayLoadStr) Then
      HueLight1Brightness = PayLoadStr
      Log("HueLight1 New Brightness ", HueLight1Brightness)
      SSDSetText("MakeLab", "HueLight1", "Brightness:", PayLoadStr)
   End If
End Sub

'MQTT disconnected. If the server is nor reachable, then MQTT is disconnected. To connect again, reset the ESP.
Sub Mqtt_Disconnected
End Sub
#End Region

'Handle setting the brightness as long as one of the buttons is pressed (holding down)
'The brightness is checked if in range 0 - 100 and published accordingly to the openHAB MQTT broker
Private Sub PushButtonTimer_Tick
   If PushButtonUpPressed Then
      If HueLight1Brightness < 100 Then HueLight1Brightness = HueLight1Brightness + 1
      If HueLight1Brightness > 0 Then HueLight1Brightness = HueLight1Brightness - 1
   End If
   'Publish the new value
   Dim pmvpayload As String = HueLight1Brightness
   mqtt.Publish(mqttTopicHueLight1Set, pmvpayload.GetBytes)
End Sub

'Handlebutton pressed state change for the UP and DOWN buttons
'Button is pressed if state is false. Use a short delay for debounce.

Private Sub PushbuttonUp_StateChanged (State As Boolean)
   PushButtonUpPressed = True
   PushButtonTimer.Enabled = State
End Sub

Private Sub PushbuttonDown_StateChanged (State As Boolean)
   PushButtonUpPressed = False
   PushButtonTimer.Enabled = State
End Sub

#Region Helpers
'Flash the control LED n Times with Wait (ms) between flashing
Private Sub FlashLED(Times As Int, Wait As Int)   'ignore
   For i = 1 To Times
      If (i + 1 < Times) Then
      End If
End Sub

'Set the OLED display text covering 4 lines top down.
Private Sub SSDSetText(Line1 As String, Line2 As String, Line3 As String, Line4 As String)
   ssd.GFX.SetCursor(0, 0)
   ssd.GFX.ConfigureText(1, ssd.WHITE, True)
End Sub
#End Region

  • Experiment completed.
  • Additional ideas: Add a push button to select a Hue Light; Build a proper LEGO "case".

  • 20180217

Return to “Experiments”

Who is online

Users browsing this forum: No registered users and 1 guest