HTTP communication

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
xzalagaq
Normal user
Posts: 22
Joined: 24 Jul 2024, 18:41

HTTP communication

#1 Post by xzalagaq » 11 Aug 2024, 14:14

I have 2 boards named Pir_1 and Oled_2, I want them to communicate through HTTP, when pir sensor state is 1 (detection) board no. 2 displays message on oled screen. But i am getting 404 error and I can't set it up.

Pir_1 rules:
on Pir#Switch do
if [PIR#Switch]=1
SendToHTTP 192.168.0.110,80,/update?pirstate=1
else
SendToHTTP 192.168.0.110,80,/update?pirstate=0
endif
endon

Pir device name: Pir
Pir variable state name: pirstate
controller: generic HTTP controller ip 192.168.0.110, publish: /update?pirstate=%val%

Oled_2 rules:
on System#Boot do
OLED,clear
OLED,font,1
OLED,text,1,Waiting for data...
endon

on HTTP#update do
if %eventvalue1% = 1
OLED,clear
OLED,text,1,Motion Detected!
else
OLED,clear
OLED,text,1,No Motion
endif
endon

oled device name: OLED_Display

Any help would be appreciated.

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

Re: HTTP communication

#2 Post by TD-er » 11 Aug 2024, 17:27

/update on ESPEasy is being used for OTA updates.

You should either give the command to generate an event on the other node, or use ESPEasy p2p.

To see what the URL will be like, you can enter the event command you like to give on the tools page in the command field.
When pressing enter, the page will reload and the complete command URL will be shown in the browser's address bar, including any required HTTP escape characters.

For example:

Code: Select all

event,myevent=12,34,56
Will be converted in the address-bar like this:

Code: Select all

http://192.168.11.10/tools?cmd=event%2Cmyevent%3D12%2C34%2C56
Or you can use the ESPEasy p2p protocol sending commands to other nodes using the "sendto" command.
Make sure all nodes use the same UDP port (default port 8266) and each have a unique unit ID (Config tab) and don't use 0 or 255.

xzalagaq
Normal user
Posts: 22
Joined: 24 Jul 2024, 18:41

Re: HTTP communication

#3 Post by xzalagaq » 11 Aug 2024, 17:51

Okay I've implemented changes sending rule looks like this:
On Pir#pirstate do
if [Pir#pirstate]=1
sendto,192.168.0.110,"event,motion=1"
else
sendto,192.168.0.110,"event,motion=0"
endif
endon

And now how do I recieve it? I have the same UDP ports

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

Re: HTTP communication

#4 Post by TD-er » 11 Aug 2024, 17:55

See how the sendto command is used: https://espeasy.readthedocs.io/en/lates ... nd-publish
You need to address the other node via its unit ID, not an IP-address.
This also makes it clear why you need an unique ID per node.


When you send an event via sendto, the event will be received on the other end, so you can act on it in rules, just like local generated events.

Just assuming the 'other' node is using ID 2, then the rule will be like this:

Code: Select all

On Pir#pirstate do
  sendto,2,"event,motion=%eventvalue1%"
endon

xzalagaq
Normal user
Posts: 22
Joined: 24 Jul 2024, 18:41

Re: HTTP communication

#5 Post by xzalagaq » 11 Aug 2024, 18:11

Okay but i don't see anything about recieving data sent this way on tutorial you sent

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

Re: HTTP communication

#6 Post by TD-er » 11 Aug 2024, 20:27

You just send some event to another node, and there it is handled the same way you handle local events.

So you send some event as mentioned and on the receiving node you act on it.

For example I name my event "example#event" (or whatever you want to call it....)

Code: Select all

sendto,2,"event,example#event=123"
Then on the receiving node (the node with node-id 2 in this example)

Code: Select all

on example#event do
  logentry,"received event with eventvalue: %eventvalue1%"
endon
You can then do anything you like to do with that value, like put it into a dummy task and send it to its connected controller(s):

Code: Select all

on example#event do
  //Assume task2 to be a dummy task
  taskvalueset,2,1,%eventvalue1%"
  taskrun,2 // Send all taskvalues of task 2 to the connected controllers
endon

xzalagaq
Normal user
Posts: 22
Joined: 24 Jul 2024, 18:41

Re: HTTP communication

#7 Post by xzalagaq » 12 Aug 2024, 11:37

Thanks so much for help i managed to display the value but i cant find how to check the value by if instruction in esp easy docs. I need to do that to display various communicates depending on the value

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

Re: HTTP communication

#8 Post by Ath » 12 Aug 2024, 12:03

xzalagaq wrote: 12 Aug 2024, 11:37 i cant find how to check the value by if instruction in esp easy docs. I need to do that to display various communicates depending on the value
The concept of Rules is explained in the Rules documentation.

2 often used ways:
- In the event for the task-value:

Code: Select all

on Pir#pirstate do
  if %eventvalue1%=1 // Use first argument of current event
    // When on...
    // Start an alarm
    // Alert the remote server
  else
    // When off...
    // Silence the alarm
    // Update remote server
  endif
endon
- From another event:

Code: Select all

on System#Clock=All,**:** do // Every minute
  if [Pir#pirstate]=1 // Use explicit value by name
    // When on...
    // Start an alarm
    // Alert the remote server
  else
    // When off...
    // Silence the alarm
    // Update remote server
  endif
endon
/Ton (PayPal.me)

xzalagaq
Normal user
Posts: 22
Joined: 24 Jul 2024, 18:41

Re: HTTP communication

#9 Post by xzalagaq » 12 Aug 2024, 12:12

Really appreciate your help, I'll try when I get home

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

Re: HTTP communication

#10 Post by TD-er » 12 Aug 2024, 13:37

And just some info on why we (strongly) suggest to use %eventvalue1%... in rules.

Consider these 2 rules which may seem the same at first sight...

Code: Select all

on Pir#pirstate do
  if %eventvalue1%=1 // Use first argument of current event
    // ...
  endif
endon

on Pir#pirstate do
  if [Pir#pirstate]=1 // Use first argument of current event
    // ...
  endif
endon
Keep in mind the event is something like this:
Pir#pirstate=1

The first block using %eventvalue1% is using the value as how it was when the event was generated.
The second block, using [Pir#pirstate] is using the value as it is when evaluating the event.

Most of the time, these will both result in the same behavior, but there might be situations where these might have changed in the mean time.
So it is best to evaluate %eventvalue1% where applicable, just to avoid timing-critical issues.

xzalagaq
Normal user
Posts: 22
Joined: 24 Jul 2024, 18:41

Re: HTTP communication

#11 Post by xzalagaq » 12 Aug 2024, 13:44

I think in my project there is what you describe, I will change it as well, thanks for the advice. And btw are you the only person responding on this forum? It's a pity that so few people are active.

xzalagaq
Normal user
Posts: 22
Joined: 24 Jul 2024, 18:41

Re: HTTP communication

#12 Post by xzalagaq » 12 Aug 2024, 14:33

And yup everything is working perfectly, thanks so much for help

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

Re: HTTP communication

#13 Post by TD-er » 12 Aug 2024, 14:42

Nope, not the only one.
Ton (Ath here on the forum) is also extremely regular :)
And there are some others who frequently reply, only less frequent than Ton and I do.

Me and Ton are also responsible for about 90+% of all the commits in the last few years.

xzalagaq
Normal user
Posts: 22
Joined: 24 Jul 2024, 18:41

Re: HTTP communication

#14 Post by xzalagaq » 12 Aug 2024, 14:52

Thanks for the explanation! I appreciate your work and commitment on the forum. It's nice to know that there are a few people who help others on a regular basis.

sartam
Normal user
Posts: 50
Joined: 13 Mar 2022, 21:25

Re: HTTP communication

#15 Post by sartam » 15 Aug 2024, 19:04

Hello colleagues. I have a problem connecting one espeasy to the Internet. There are 2 absolutely identically configured devices, but one device sends data to mqtt and narodmon, and also receives data from NTP. And the second device receives NTP data, but does not send data to mqtt and narodmon. How to check whether data is being sent from a device to the Internet?

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

Re: HTTP communication

#16 Post by TD-er » 15 Aug 2024, 19:37

Just a few pointers to check:

- Is the gateway configured correctly?
- Is DNS configured correctly?
- Is timeout of controller too low? (increase to 1000 msec for testing)
- Try saving the controller settings again (will perform a new DNS lookup on save)

sartam
Normal user
Posts: 50
Joined: 13 Mar 2022, 21:25

Re: HTTP communication

#17 Post by sartam » 15 Aug 2024, 20:52

The fact of the matter is that the settings of the two devices are exactly the same. Only one transmits data to the Internet, the other does not. :shock:

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

Re: HTTP communication

#18 Post by Ath » 15 Aug 2024, 21:50

sartam wrote: 15 Aug 2024, 20:52 The fact of the matter is that the settings of the two devices are exactly the same. Only one transmits data to the Internet, the other does not. :shock:
If the settings are exactly the same, does that imply that the name and unit number (Config tab) are the same? As the MQTT, and possibly narodmon, server may ignore data from the same device but using a different IP address.

For optimal operation, each ESPEasy unit should have a unique name and unitnr, where the unitnr should not be 0 or 255. For proper networking operations, they also should have a unique IP address.
/Ton (PayPal.me)

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

Re: HTTP communication

#19 Post by TD-er » 15 Aug 2024, 23:00

Can you still try to increase the timeout?
ESPEasy is internally adjusting the required timeout based on the average response time.
However if you're just on the edge of what might be usable, then it can fail and keep failing.

sartam
Normal user
Posts: 50
Joined: 13 Mar 2022, 21:25

Re: HTTP communication

#20 Post by sartam » 17 Aug 2024, 19:31

Hello again. I solved the problem of lack of Internet on the device! The issue was with the router. It is not clear for what reason, but after removing the IP from the router and then connecting the device, the Internet appeared on the device. Mikrotik router.

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

Re: HTTP communication

#21 Post by Ath » 17 Aug 2024, 20:22

Hmm, good you found the culprit, and thanks for the update ;)
/Ton (PayPal.me)

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

Re: HTTP communication

#22 Post by TD-er » 17 Aug 2024, 22:06

Maybe you should also check to see if there is >1 device running a DHCP service in your network.

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests