Alternative/fix to HttpClient.get

Hi,

I have a php file placed on the SD card of the Yun which echoes an integer when rendered. I then use HttpClient.get to get that integer. Everything works fine with the code below where I use the IP-address, but when I use myyun.local I get some weird number.

HttpClient webClient;

webClient.get("http://192.168.1.50/sd/myproject/testfile.php");

while (webClient.available()) {
    int x = webClient.parseInt();
}

Not working:

webClient.get("myyun.local/sd/myproject/testfile.php");

Is there a quick fix to this problem so I don't have to edit the code when using a new wifi?

Do you see the correct number if you go to myyun.local/sd/myproject/testfile.php with your browser?

No, I can only access with the IP.

Do you have Bonjour (if you are on windows) or Avahi (if you are on Linux) installed?

No, I don't.

I read about Bonjour in another thread, but it is not a problem that I cannot access when not using the IP in my browser, but only that the Arduino can't either.

The current Yun image does not support avahi or bonjour

So you can't access to your Yun from another Yun using the host name.

The latest OpenWrt image adds support for that (work in progress), but it is not available for the Yun.

Okay, I see :slight_smile:

So what alternatives do I have? I can of course host the file remotely, but the idea was to gather everything on the SD card.

You can assign to your Yun a fixed address.

Angelo9999:
You can assign to your Yun a fixed address.

Or, you can leave the Yun using dynamic addresses (DHCP) and set up your DHCP server (usually in your router) to use Static DHCP to always hand out the same address to your Yun (based on the Yun's MAC address.) the advantage to this is that all of the network addressing details are all in one place (in the router) rather than scattered across multiple devices.

Maybe I did not explain myself well enough. My Yun always have the same IP on my wifi, so everything always works at home, but if I connect it to another network it of course gets a different IP, and that's where my problem is.

The Yun that runs the client can search for the IP of the server.

It can try to get a page from x.x.x.2 and if it does not work it can increment the address until it gets the correct value.

It is not very efficient but it should work.

Let's back up for a moment...

SomethingClever:
I have a php file placed on the SD card of the Yun which echoes an integer when rendered. I then use HttpClient.get to get that integer.

Is the PHP file, the SD card, and the sketch doing the get() call all on the same Yun? That's what it sounds like. If so:

  • try using the address 127.0.0.1, which is a local loopback address and will always reference the local machine, regardless of its current address
  • rather than incurring the web server and network overhead, try using the Bridge library's Process class to execute the PHP file directly

Maybe one of these will accomplish the goal?

@Angelo9999

That could maybe be a possibility. Thanks.

@ShapeShifter

Yes, everything is on the same Yun. I will try the loopback address and hopefully that will work.

I should use the Process class to execute the PHP file, or maybe install the php client class (which I tried but I could not make it work), but right now it seems unmanageable as I don't understand it, or Linux, at all. When I bought the Yun I thought it was only a bit harder to get a grip of than the ordinary Arduinos as the Leonardo :slight_smile:

ShapeShifter:

  • try using the address 127.0.0.1, which is a local loopback address and will always reference the local machine, regardless of its current address

This was the solution! :slight_smile: Thank you very much.

Glad to hear it helped! It's good that it was a simple fix.