How do I control an optocoupler with esp easy?
Moderators: grovkillen, Stuntteam, TD-er
How do I control an optocoupler with esp easy?
Hello,
I want to use espEasy to activate an optocoupler with a MQTT signal. Looking at the available plugins I think I need the "Internal GPIO handling" plugin, so I flashed the "climate" version and looked through the devices. I couldn't find anything related to GPIO handling and tried "Switch input -Switch" but couldn't get it to work.
All I need is for the esp to give 3.3V to a selected pin when it gets a MQTT message. It would be nice if the message could control the length of the pulse, which should be possible when controlling a servo, right? My question basically boils down to this: How do I control the GPIO handling and can it be triggered by an MQTT message?
I want to use espEasy to activate an optocoupler with a MQTT signal. Looking at the available plugins I think I need the "Internal GPIO handling" plugin, so I flashed the "climate" version and looked through the devices. I couldn't find anything related to GPIO handling and tried "Switch input -Switch" but couldn't get it to work.
All I need is for the esp to give 3.3V to a selected pin when it gets a MQTT message. It would be nice if the message could control the length of the pulse, which should be possible when controlling a servo, right? My question basically boils down to this: How do I control the GPIO handling and can it be triggered by an MQTT message?
Re: How do I control an optocoupler with esp easy?
The command you're looking for is probably either longpulse or longpulse_ms (units in msec instead of seconds)
See: https://espeasy.readthedocs.io/en/lates ... ernal-gpio
Aside of this, you need to use the MQTT import plugin, or formulate a command to send via MQTT.
The HomeAssistant/OpenHAB MQTT controller is the most generic MQTT controller we have and it allows to receive commands via MQTT.
See: https://espeasy.readthedocs.io/en/lates ... #c005-page
In those examples, the "GPIO" command is used, but you could of course use longpulse for this.
Personally I would rather construct sending an event and use some self chosen event values as parameters for this event.
This way you can be sure what's being called and don't have to jump through hoops to get the right parameter at the right position.
For example this command:
And then in the rules:
See: https://espeasy.readthedocs.io/en/lates ... ernal-gpio
Aside of this, you need to use the MQTT import plugin, or formulate a command to send via MQTT.
The HomeAssistant/OpenHAB MQTT controller is the most generic MQTT controller we have and it allows to receive commands via MQTT.
See: https://espeasy.readthedocs.io/en/lates ... #c005-page
In those examples, the "GPIO" command is used, but you could of course use longpulse for this.
Personally I would rather construct sending an event and use some self chosen event values as parameters for this event.
This way you can be sure what's being called and don't have to jump through hoops to get the right parameter at the right position.
For example this command:
Code: Select all
event,MyEvent=2,500 // GPIO 2, 500 msec duration
Code: Select all
on MyEvent do
LongPulse_mS,%eventvalue1%,1,%eventvalue2%
endon
Re: How do I control an optocoupler with esp easy?
Just to clarify, I am using the MQTT import plugin so that the ESP can receive MQTT messages. Looking at the plugin, it seems I can only specify the topics it will listen to, but where do I save the rules? Would it also be possible not to create a rule on the ESP but to send a MQTT message to the ESP containing: the action (LongPulse), the time (in msec) and the GPIO pin?
Sorry if I'm a bit obtuse, but I've only ever used easyEsp for sensors, which was always pretty straightforward.
Sorry if I'm a bit obtuse, but I've only ever used easyEsp for sensors, which was always pretty straightforward.
Re: How do I control an optocoupler with esp easy?
Rules are not enabled by default, and have to be switched on via the Tools/Advanced settings page. Don't forget to submit the page, an after that, switching tabs will reveal the Rules tab in the UI
/Ton (PayPal.me)
Re: How do I control an optocoupler with esp easy?
Just as a very basic "introduction" of the concept of events and rules in ESPEasy...
ESPEasy does send events for various actions.
For example WiFi#connected
But also when a task succesfully read some value from a device, or with MQTT import, from a MQTT message.
These events can then be acted on in the rules.
See: https://espeasy.readthedocs.io/en/lates ... Rules.html
An event can also have some event values, which can be used in handling the rules.
For example the event bme#temperature=22.0 has as eventvalue "22.0" and can be referred to in the rules via %eventvalue1%
Hopefully this info will help you in moving through the elaborate documentation a bit more quickly.
ESPEasy does send events for various actions.
For example WiFi#connected
But also when a task succesfully read some value from a device, or with MQTT import, from a MQTT message.
These events can then be acted on in the rules.
See: https://espeasy.readthedocs.io/en/lates ... Rules.html
An event can also have some event values, which can be used in handling the rules.
For example the event bme#temperature=22.0 has as eventvalue "22.0" and can be referred to in the rules via %eventvalue1%
Hopefully this info will help you in moving through the elaborate documentation a bit more quickly.

Re: How do I control an optocoupler with esp easy?
Alright, I got it working with this code:
While this works for what I'm doing at the moment, I still don't quite understand what you wrote in the first answer, but it seems pretty interesting, so I'd ask you to please explain it again. My current understanding is that I can put this in the rules and then trigger it somehow. I've tried many ways but couldn't figure out how to trigger the event/rule.
I've put in a MQTT message to the ESP but nothing happened. I've also tried putting it in the MQTT Import - Formula tab, but still nothing. Next I tried renaming the values to match the rule but it still failed.
Code: Select all
On opto#pcPower=1 Do
LongPulse_mS,12,1,1500
Endon
Code: Select all
on MyEvent
LongPulse_mS,%eventvalue1%,1,%eventvalue2%
endon
I've put
Code: Select all
event,MyEvent=2,500
Re: How do I control an optocoupler with esp easy?
If you enter the "event,MyEvent=2,500" command on the Tools page (without the quotes) and press the Submit button, the event should be triggered.
You have to send the command as the payload in a MQTT message to a topic that ends in /cmd
You have to send the command as the payload in a MQTT message to a topic that ends in /cmd
/Ton (PayPal.me)
Re: How do I control an optocoupler with esp easy?
Code: Select all
on MyEvent do
LongPulse_mS,%eventvalue1%,1,%eventvalue2%
endon
Re: How do I control an optocoupler with esp easy?
Thank you guys! It works with the /cmd and the added "Do" at the end.
Who is online
Users browsing this forum: No registered users and 12 guests