custom http rules

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
tchi59
Normal user
Posts: 26
Joined: 24 Jun 2021, 15:04

custom http rules

#1 Post by tchi59 » 23 Aug 2022, 15:34

Hi

I would like to know if it is possible to create a custom rules in espeasy in order to trigger an action and do a http request

example:

http://ESPEASY/control?cmd=event,MycustomRule

on MycustomRule do
logentry,"I want to reply to a httprequest"

=> How to reply?

endon

Thanks in advance

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

Re: custom http rules

#2 Post by TD-er » 23 Aug 2022, 16:01

Yep this is for sure possible.
You could reply to the other ESPEasy node by calling SendToHTTP.
It is also possible to give a number of event values along with the event.

The normal syntax for an event with eventvalues is:
event,"MyCustomRule=1,2,3,4"

N.B. since the event values are separated by commas, which is also the separator for command arguments, you must wrap the argument for the event command in quotes, like I did in my example.

These can be accessed via %eventvalue1% ... %eventvalueN%
See: https://espeasy.readthedocs.io/en/lates ... eventvalue

One last remark.
In the last build, there seems to be an issue with the SendToHttp command, which I'm now working on.

Another way to interact among ESPEasy nodes is to send commands via the sendto command when using ESPEasy p2p.
See: https://espeasy.readthedocs.io/en/lates ... #c013-page

tchi59
Normal user
Posts: 26
Joined: 24 Jun 2021, 15:04

Re: custom http rules

#3 Post by tchi59 » 23 Aug 2022, 16:51

since i am not really sure to well understand what is eventvalue
i should be like this ?

example:

http://ESPEASY/control?cmd=event,MycustomRule

on MycustomRule do
logentry,"I want to reply to a httprequest"

event,"MycustomRule=actiondone"

endon

thx

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

Re: custom http rules

#4 Post by Ath » 23 Aug 2022, 17:09

tchi59 wrote: 23 Aug 2022, 16:51 since i am not really sure to well understand what is eventvalue
The script variables %eventvalue1%..%eventvalueN% (where N is a positive numeric value) are the arguments passed to your event after the = character
For backward compatibility, %eventvalue% represents the same value as %eventvalue1%
%eventvalue0% is the entire passed string of characters passed after the =
%eventname% is the name of the event, MycustomRule in this example
%eventpar% is the parameter for the event, between the # and the =, meaning that it is empty in your example

For passing non-numeric values as the first argument, it is required to add a * to the event trigger: ]]on MycustomRule* do[[ to enable it to parse string arguments, then you can use %eventvalue1% in your rule to pass on to the SendToHttp statement, for example
tchi59 wrote: 23 Aug 2022, 16:51 i should be like this ?

example:

http://ESPEASY/control?cmd=event,MycustomRule

Code: Select all

on MycustomRule do
  logentry,"I want to reply to a httprequest"
  event,"MycustomRule=actiondone"
endon
That will result in a reboot of the ESP, as you keep calling the same event from your event --> stack overflow!
You can call your rule externally, or from another rule, like that, f.e. from the Tools page: event,"MycustomRule=actiondone"

Example:

Code: Select all

on MycustomRule* do
  logentry,"I want to reply %eventvalue1% to a httprequest"
  SendToHTTP,10.0.0.1,80,"/path/with/%eventvalue1%"
endon
/Ton (PayPal.me)

tchi59
Normal user
Posts: 26
Joined: 24 Jun 2021, 15:04

Re: custom http rules

#5 Post by tchi59 » 23 Aug 2022, 17:17

i tested your code:

on EventName* do
logentry,"I want to reply %eventvalue1% to a httprequest"
SendToHTTP,10.0.0.1,80,"/path/with/%eventvalue1%"
endon

from my workstation in my browser, i always received "OK"
and i would like to receive a message like "actiondone" for instance

it is mandatory to mention the ip address in the SendToHTTP ? my workstation is in dhcp

regards

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

Re: custom http rules

#6 Post by Ath » 23 Aug 2022, 18:35

Ah, the OK response you see is the result of executing the operation, either OK or error, and has nothing to with the content of that operation. Don't know of a way to return another response there.

If your ESP is configured to use a DNS server, you can use dns-names instead of IP-addresses.
/Ton (PayPal.me)

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

Re: custom http rules

#7 Post by TD-er » 23 Aug 2022, 19:09

The builds not "size constraint" (the "minimal" and "collection" builds have excluded some features in a build to make it all fit) can also serve mDNS requests.
Meaning you can name your ESPEasy node and it will be available on the network via <name>.local
So when you name your node "bladiebla" it is accessible via "bladiebla.local"
When this feature is present in your build, you will see the ".local" hostname being showin on the root page of the ESPEasy interface.

But I think sending via the p2p layer is probably easier, as long as you have your nodes in the same network subnet.
Then you can give each node an unique "unit nr" (not 0 and not 255, but some value inbetween) and use the sendto command to send some command to another node, addressed via this unit nr.

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests