[SOLVED] ESP-32 with RGB-LED

Hello Everyone,
I’ve got an Node-MCU ESP-32 with an RGB-LED.
Now my question: How can I control this LED using openHAB? I’m experienced in programming, but I use openHAB since 2 days and I haven’t got any idea to do this.

It would be great if anyone could post the .items, .rules, .sitemaps and .ino file.

Thanks for your reply!:smiley:

The easiest would be to flash the ESP with Tasmota or ESPEasy. Then configure an MQTT broker and use the MQTT binding. There are TONS of examples and docs available on how to use MQTT. Look around and try it. If you run into trouble come back and we can try to help with specific problems.

I have self written a simple rest API for my ESP.
With simple url command (http Binding) I can control en read every pin of my ESP.

For me this was the easiest way. Since you have to install an MQTT broker and stuff for using MQTT messages.

Also, if openhab stops working for some reason I still can control the lights with http commands.

The big problem with HTTP for sensors is you have to poll them and for many sensors (PIR, door sensors, etc) you can’t poll fast enough to get timely notification.

And Tasmota and ESP Easy both support HTTP so the custom firmware is still a good idea even if you did want to go down that route.

And if OH stops, you can still control the lights with MQTT.

I know http isn’t fast enough for polling many sensors. But the API I have made was mainly written for controlling outputs such as RGB Leds.

I know custom software isn’t always the best way to go, but it will always work the way you want it. Only the support needs to come from yourself :sweat_smile:

I haven’t used MQTT yet. So I don’t know much about it. But I believe that it is possible to control it outside from openhab. But http can be used with every web browser on every device, that’s why I also thought it would be good to use.

Thanks! I’ll try it with ESPEasy and post my results.

I use an ESP8266-12 with a Philips Living Color light.

For the ESP I use sonoff tasmota set up as a generic module.

I have mosquitto configured as my mqtt broker and OH is still running MQTT 1.

Items file:

Color AH_Slaapkamer_LivingColor "Living Color" <slider> (gLights_AH_Slaapkamer)
Number sonoff_FE4D45_BLUE  "PWM Blauw" 	 {mqtt=">[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/cmnd/Pwm3:command:*:default],
	<[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/Pwm:state:default]"}
Number sonoff_FE4D45_GREEN  "PWM Groen" 	 {mqtt=">[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/cmnd/Pwm2:command:*:default],
	<[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/Pwm:state:default]"}
Number sonoff_FE4D45_RED  "PWM Rood" 	 {mqtt=">[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/cmnd/Pwm1:command:*:default],
	<[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/Pwm:state:default]"}
Switch sonoff_FE4D45_Reachable "FE4D45: LivingColor SK" <network> (gOnlineStatus)
    { mqtt="<[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/tele/LWT:state:MAP(reachable.map)]" }
Number sonoff_FE4D45_RSSI "FE4D45: RSSI [%d %%]" <qualityofservice> (gRSSI)
    { mqtt="<[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/tele/STATE:state:JSONPATH($.Wifi.RSSI)]" }	
String sonoff_FE4D45_LWT "FE4D45: LWT [%s]" <shield> (gLastWill) 
	{mqtt="<[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/tele/LWT:state:default]"}
String sonoff_FE4D45_FW "FE4D45: FW [%s]" 									<text> 				(gSoftwareLevel)
	{mqtt="<[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/stat/STATUS2:state:JSONPATH($.StatusFWR.Version)]"} 
	String sonoff_FE4D45_AP "FE4D45: AP [%s]" 									<qualityofservice>	(gConnectedtoAP)
	{mqtt="<[mosquitto:home/esp8266/tasmota/sonoff_FE4D45/tele/STATE:state:JSONPATH($.Wifi.SSId)]"} 

Sitemap:

Group item=gLights_AH_Slaapkamer

Rule file:

rule "Set Slaapkamer Living Color RGB value"
when
 	Item AH_Slaapkamer_LivingColor changed
then
 	hsbValue = AH_Slaapkamer_LivingColor.state as HSBType

 	redValue = hsbValue.red.intValue
 	greenValue = hsbValue.green.intValue
 	blueValue = hsbValue.blue.intValue

 	RGBvalues= redValue.toString + ";" + greenValue.toString + ";" + blueValue.toString
 	sendCommand( sonoff_FE4D45_RED, redValue*10)
 	sendCommand( sonoff_FE4D45_GREEN, greenValue*10)
 	sendCommand( sonoff_FE4D45_BLUE, blueValue*10)
 	logInfo( "AH_Slaapkamer_LivingColor", RGBvalues )
end

kr Han

2 Likes

Thanks for that!
But now I have got an other problem: I can’t flash ESPEasy to the ESP32…
What am I doing wrong?
Here is the log of the flash.cmd file:

Comport (example 3, 4, ..)           :3
Flash Size (example 512, 1024, 4096) :4096
Build (example 71, 72, ..)           :147
Using com port: 3
Using bin file: ESPEasy_R147_4096.bin
esptool v0.4.6 - (c) 2014 Ch. Klippel <ck@atelier-klippel.de>
    setting board to nodemcu
    setting baudrate from 115200 to 115200
    setting port from COM1 to COM3
    setting address from 0x00000000 to 0x00000000
    espcomm_upload_file
    stat ESPEasy_R147_4096.bin success
    setting serial port timeouts to 1000 ms
opening bootloader
resetting board
trying to connect
    flush start
    setting serial port timeouts to 1 ms
    setting serial port timeouts to 1000 ms
    flush complete
    espcomm_send_command: sending command header
    espcomm_send_command: sending command payload
    read 0, requested 1
trying to connect
    flush start
    setting serial port timeouts to 1 ms
    setting serial port timeouts to 1000 ms
    flush complete
    espcomm_send_command: sending command header
    espcomm_send_command: sending command payload
    espcomm_send_command: receiving 4 bytes of data
trying to connect
    flush start
    setting serial port timeouts to 1 ms
    setting serial port timeouts to 1000 ms
    flush complete
    espcomm_send_command: sending command header
    espcomm_send_command: sending command payload
    espcomm_send_command: receiving 4 bytes of data
    espcomm_send_command: receiving 4 bytes of data
    espcomm_send_command: receiving 4 bytes of data
    espcomm_send_command: receiving 4 bytes of data
    espcomm_send_command: receiving 4 bytes of data
    espcomm_send_command: receiving 4 bytes of data
    espcomm_send_command: receiving 4 bytes of data
    espcomm_send_command: receiving 4 bytes of data
    espcomm_open
Uploading 431376 bytes from ESPEasy_R147_4096.bin to flash at 0x00000000
    erasing flash
    size: 069510 address: 000000
    first_sector_index: 0
    total_sector_count: 106
    head_sector_count: 16
    adjusted_sector_count: 90
    adjusted_size: 05a000
    espcomm_send_command: sending command header
    espcomm_send_command: sending command payload
    setting serial port timeouts to 10000 ms
    read 0, requested 1
    setting serial port timeouts to 1000 ms
warning: espcomm_send_command: didn't receive command response
warning: espcomm_send_command(FLASH_DOWNLOAD_BEGIN) failed
closing bootloader
    flush start
    setting serial port timeouts to 1 ms
    setting serial port timeouts to 1000 ms
    flush complete

I can restart it but the ESP32 only runs the old program…

Thanks for your Reply! :grinning:

Try using esptool to erase the info on the chip then flash your new firmware.

Command used:

esptool.py erase_flash

Now i have flashed the ESP, but now I get the following Serial Monitor-Output every 25ms:

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x10
clk_drv:0x00,q_drv:0x01,d_drv:0x00,cs0_drv:0x02,hd_drv:0x00,wp_drv:0x04
mode:DIO, clock div:2
load:0x09a40c01,len:20563313
ets Jun  8 2016 00:22:57

And if I try to erase it again and flash it, I get the following output:

C:\WINDOWS\system32>esptool.py --port COM3 erase_flash
esptool.py v2.5.1
Serial port COM3
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\Scripts\esptool.py-script.py", line 11, in <module>
    load_entry_point('esptool==2.5.1', 'console_scripts', 'esptool.py')()
  File "c:\program files (x86)\python36-32\lib\site-packages\esptool.py", line 2846, in _main
    main()
  File "c:\program files (x86)\python36-32\lib\site-packages\esptool.py", line 2547, in main
    esp = ESPLoader.detect_chip(each_port, initial_baud, args.before, args.trace)
  File "c:\program files (x86)\python36-32\lib\site-packages\esptool.py", line 244, in detect_chip
    detect_port = ESPLoader(port, baud, trace_enabled=trace_enabled)
  File "c:\program files (x86)\python36-32\lib\site-packages\esptool.py", line 213, in __init__
    self._port = serial.serial_for_url(port)
  File "c:\program files (x86)\python36-32\lib\site-packages\serial\__init__.py", line 88, in serial_for_url
    instance.open()
  File "c:\program files (x86)\python36-32\lib\site-packages\serial\serialwin32.py", line 62, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Zugriff verweigert', None, 5)

Sorry for all those messages, but I am very confused :grimacing:
Thanks for your reply!

I’m not a Windows user but the last line in the message above mentions not being able to open port COM3. Do a google search and find what’s needed to give that port the correct permissions.

I only use esptool to erase my Esp devices. For flashing the firmware I use Atom with platformio.

Thanks, it’s now working. I’ll try to install ESPEasy again now and post my results after it.

Permissions granted.:grin: The logs are sometimes hard to read with several error messages but if you look thru all of then usually you’ll find the answer.:wink:

If you need help with items after getting EspEasy running let me know I have several of them on my OH setup.

1 Like

Now I am running ESPEasy on those ESP32’s but I don’t know how to configure the ESPEasy-System to run an RGB-LED over the ESP’s GPIO’s. And after that I think I need help in configuring the openHAB stuff :wink:

Thank you for investing so much time in my questions :+1:

I don’t have an RGB-LED to test with but I’ll check the doc’s. You might want to connect a simple LED to one of the GPIO pin and have OH control it. Just to get the connection to Esp and OH configured. After you have one thing working the rest will be easy.:wink:

Here is some info on RGB-LED from the EspEasy forum that might help.
http://www.letscontrolit.com/forum/search.php?keywords=RGB-LED&sid=a924df6134bd75f36ed4838610e4fe6d

Ok. Now I have one LED on the ESP and I can send a state to it via MQTT with this command:
mosquitto_pub -d -t /ESPLed/GPIO/12 -m 1
I can’t controll my ESP Led with openHAB :frowning:
esps.items:
Switch ESPLed {mqtt=">[localbroker:/ESPLed/GPIO/12:command:ON:1],>[localbroker:/ESPLed/gpio/12:command:OFF:0]"}
default.sitemap:
sitemap default label="Philipp's Zimmer" { Frame label="Beleuchtung" { Switch item=ESPLed label="ESP-LED" } }
I see the Switch and I can turn it on and off, but the LED stays off :face_with_raised_eyebrow:
Thanks for help :heart_eyes:

What mqtt binding version did you install?

binding-mqtt - 2.4.0

For that binding version you will need to change the way you have the items/things file. What’s posted above is for the 1.x binding. If you want to use the 1.x mqtt binding then enable legacy bindings via PaperUI and select the 1.x mqtt.

How could I use MQTT with the 2.x Binding?