How do I control an optocoupler with esp easy?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Boerny41
New user
Posts: 4
Joined: 18 Feb 2023, 00:10

How do I control an optocoupler with esp easy?

#1 Post by Boerny41 » 18 Feb 2023, 00:19

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?

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: How do I control an optocoupler with esp easy?

#2 Post by TD-er » 18 Feb 2023, 00:41

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:

Code: Select all

event,MyEvent=2,500 // GPIO 2, 500 msec duration
And then in the rules:

Code: Select all

on MyEvent do
  LongPulse_mS,%eventvalue1%,1,%eventvalue2%
endon

Boerny41
New user
Posts: 4
Joined: 18 Feb 2023, 00:10

Re: How do I control an optocoupler with esp easy?

#3 Post by Boerny41 » 18 Feb 2023, 02:21

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.

User avatar
Ath
Normal user
Posts: 3494
Joined: 10 Jun 2018, 12:06
Location: NL

Re: How do I control an optocoupler with esp easy?

#4 Post by Ath » 18 Feb 2023, 10:53

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)

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: How do I control an optocoupler with esp easy?

#5 Post by TD-er » 18 Feb 2023, 13:23

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. :)

Boerny41
New user
Posts: 4
Joined: 18 Feb 2023, 00:10

Re: How do I control an optocoupler with esp easy?

#6 Post by Boerny41 » 18 Feb 2023, 19:54

Alright, I got it working with this code:

Code: Select all

On opto#pcPower=1 Do
  LongPulse_mS,12,1,1500
  Endon
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

Code: Select all

 on MyEvent
  LongPulse_mS,%eventvalue1%,1,%eventvalue2%
endon
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

Code: Select all

event,MyEvent=2,500
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.

User avatar
Ath
Normal user
Posts: 3494
Joined: 10 Jun 2018, 12:06
Location: NL

Re: How do I control an optocoupler with esp easy?

#7 Post by Ath » 18 Feb 2023, 20:35

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
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: How do I control an optocoupler with esp easy?

#8 Post by TD-er » 18 Feb 2023, 21:10

Code: Select all

 on MyEvent do
  LongPulse_mS,%eventvalue1%,1,%eventvalue2%
endon
You missed a "do" in the "on ... do" line.

Boerny41
New user
Posts: 4
Joined: 18 Feb 2023, 00:10

Re: How do I control an optocoupler with esp easy?

#9 Post by Boerny41 » 19 Feb 2023, 10:50

Thank you guys! It works with the /cmd and the added "Do" at the end.

User avatar
Ath
Normal user
Posts: 3494
Joined: 10 Jun 2018, 12:06
Location: NL

Re: How do I control an optocoupler with esp easy?

#10 Post by Ath » 19 Feb 2023, 11:55

Great :D

thanks for the feedback ;)
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: No registered users and 102 guests