1

I am trying to access my Yun externally.

My router recognises traffic on Port XXXXX and forwards it to my Arduino Yun.

I have the below code working internally and now trying to adapt this to work for external (adapted from http://arduino.cc/en/Tutorial/TemperatureWebPanel).

To access the Arduino Yun over the network I type:

http://XXX.XXX.XXX.XXX:XXXXX/arduino/on

When using this script locally, i.e. myArduinoName.local/arduino/on, the onboard light turns on (and off if URL changed accordingly).

Adapting this working code, I have changed to "noListenOnLocalhost" and put in my port number (XXXXX).

The code is reached (see below for the response).

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h> 

// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server(XXXXX);
String startString;
long hits = 0;

void setup() {
  //Serial.begin(9600);

  // Bridge startup
  pinMode(13,OUTPUT);
  digitalWrite(13, LOW);
  digitalWrite(13, HIGH);


  Bridge.begin();
  Console.begin();


  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  //server.listenOnLocalhost();
  server.noListenOnLocalhost();
  server.begin();

}

void loop() {
  // Get clients coming from server
  YunClient client = server.accept();

  client.println("hello");

  // There is a new client?
  if (client) {
    // read the command
    String command = client.readString();

    command.trim();        //kill whitespace

    client.println(command); // why does this return a full GET request instead of "on"?

    if (command == "on") {
      digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);   
    }

    if (command == "off") {
      digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);              // wait for a second
    }


    // Close connection and free resources.
    client.stop();
    hits++;
  }

  delay(50); // Poll every 50ms
}

The response I get is (I have replaced my IP and port with X's):

hello

GET /arduino/off HTTP/1.1 Host: XXX.XXX.XXX.XXX:XXXXX Connection: keep-alive Pragma: no-cache Cache-Control: no-cache Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

I expected

hello

on

So it would seem that because I'm using a different port (other than 80 or 5555) the response is no longer being interpreted as an HTTP request.

I have also tried to edit the uhttpd file via SSH to provide a specific address and port for both my external IP address and my fixed internal IP address.

Then tried to restart the device and also a uhttpd restart i.e.:

/etc/init.d/uhttpd restart

I have attempted to access the URL using my mobile phone (i.e. not on the same network).

Any ideas why my request is not being treated as HTTP?

per1234
  • 4,278
  • 2
  • 24
  • 43
DanAbdn
  • 161
  • 5

1 Answers1

1

It works!

The changes to the uhttpd must have done the trick to listen for http traffic on a given IP/port (other than 0.0.0.0:80).

Instead of supplying a port number for the server in the code, I would surmise that the change to the config takes care of it so this, so the port number in code is no longer needed:

YunServer server(XXXXX);

should be changed back to

YunServer server;

DanAbdn
  • 161
  • 5