Accessing Your YUN Worldwide

Accessing Your YUN from anywhere in the World with your Web Browser or Your Android Phone

Caveat: This is a Windows based tutorial- a lot of the data may apply to the users of other OS’s.
I know a lot of us don’t want to open our systems up to outside services whether free or paid for.
I want my IOT to be controlled by me as much as possible. With that in mind I wanted to be able to
access my Arduino YUN from anywhere in the world whenever I wanted to.

After much searching and anguish, reading lots of differing tutorials and watching a lot of Youtube
videos, I figured it was time to write something useful for us. Hopefully the Mods will sticky this
in the future or something like it and we can use it as a reference once its been approved, and even modified to suit our needs here.

I don’t feel like any of this is original by me. It is more or less a compilation set forth to make
life easier for the YUN users by having this data all in one place.

In order for you to successfully access your YUN from anywhere in the world with your Web Browser or
your Android phone. Some things need to happen with the YUN itself and some things have to happen
inside your sketch and on a web page you build.

  1. The YUN will be set to allow outside ports other than 80.
  2. You will need port forwarding to happen on your wireless router.
  3. Your local assigned IP by your router should be made static and kept in reserve by your router.
  4. Your Arduino sketch has to contain the proper code in order for it to connect to the YUN
    from outside your local network. The web code must match with the Arduino sketch.

Once 3 out of 4 of these parameters are met, it should be smooth sailing for you. The mandatory
parameters are 1,2,and 4 above. Number 3 is more for facility on your part so you don’t have to
keep trying to figure out what your local IP is for the YUN when things (global IP) get reset by your ISP,
which does happen periodically for us home users.

Okay let’s dive in.

  1. The YUN will be set to allow outside ports other than 80. The logical question to ask
    is “How do we do that?”
    Answer: For Windows users, by using a Terminal Program like Putty we can get inside the inner
    workings of our YUN.
    a. Download and install Putty.
    b. Open Putty. The first screen you see is the Putty Configuration. In the middle top there is a name
    "Host Name (or IP address). Enter your local IP address like 192.168.1.10, for example. The actual
    IP will already be known to you by one of 2 ways–1, you’ve already looked inside your router settings,
    or 2, when your YUN is connected wirelessly and going to load a sketch your will see your local IP
    under the PORT settings in the Arduino IDE.
    c. Leave the “Port” setting as 22. There is no need to change that now.
    d. Click “Open.” If you have used the correct parameters Putty will open to a Login screen.
    e. At that point type in “root” and hit “Enter.”. Putty will then ask for the password.
    f. Input your password and hit “Enter” and you will be at “root” inside the YUN at a command line.
    The password you use is either the standard “ardunio” or whatever you changed it to during your
    initial setup of the YUN when you first got it or have updated the image.
    g. Next, change directory to file location: cd /etc/config
    h. Edit with the included unix editor vi: vi uhttpd

vi can be daunting for us Windows users who are used to different editors. For all that we need,
go to: http://www.howtogeek.com/102468/a-beginners-guide-to-editing-text-files-with-vi/
Everything to know is at the top of the page.
What you need to do is modify the /etc/config file by adding at least 2 lines of code.
Here is the beginning portion:

# Server configuration
config uhttpd main

	# HTTP listen addresses, multiple allowed
	list listen_http	0.0.0.0:80
#	list listen_http	[::]:80

	# HTTPS listen addresses, multiple allowed
	list listen_https	0.0.0.0:443
#	list listen_https	[::]:443

	# Server document root
	option home		/www

	# Reject requests from RFC1918 IP addresses
	# directed to the servers public IP(s).
	# This is a DNS rebinding countermeasure.
	option rfc1918_filter 0

	# Maximum number of concurrent requests.
	# If this number is exceeded, further requests are
	# queued until the number of running requests drops
	# below the limit again.
	option max_requests 2

///////////////////////
This is the section you need to be at, which is at the top of the file:

# HTTP listen addresses, multiple allowed
	list listen_http	0.0.0.0:80
#	list listen_http	[::]:80

while in vi type i–this puts you in insert mode and you can go right under the

#	list listen_http	[::]:80

and create a new entry for yourself. For instance:

list listen_http	0.0.0.0:1680
#	list listen_http	[::]:1680

After you have finished typing this command above then type :wq to save the file and close the vi editor.

These four numbers, in this case, will represent the port you are forwarding in the router soon, but also is
telling the YUN it should be listening in on this port for incoming traffic. Create your own port numbers.
Once this is done you can exit Putty.

Moving on:
2. You will need port forwarding to happen on your wireless router.
I am using the older Netgear wndr3400v2 wireless router. I imagine yours may be similar.
Once you login to the router, you have to go to the “Advanced” tab then “Advanced Setup” then
“Port Forwarding/Port Triggering.” Once there check the radio button for “Port Forwarding” and create
a new Service by choosing “HTTP” from the dropdown box. At this point you must have your local IP
address for the YUN, as you will need to enter this into the IP address box. Assign your chosen port
from above in the first section to the “Internal Start Port”, “Internal End Port” and
“External Start Port”, “External End Port.” The port number you chose should populate these areas.

Now for Static IP
3. Your assigned local IP by your router should be made static and kept in reserve by your router.
Again, using the Netgear wndr3400v2 wireless router you have to go to the “Advanced” tab then “LAN Setup” under
“Address Reservation”, choose “Add.” This popups a new screen which allows you to choose your local IP, the
192.168.x.x, which is assigned to your YUN. Your MAC address is there, and you now have a chance to name
your device and save these settings.

Final Part
4. Your Arduino sketch has to contain the proper code in order to connect to the YUN
from outside your local network. The web code must match with the Arduino sketch.

I actually just modified the “Bridge” sketch as follows:

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

YunServer server;
YunClient client;



void setup() {
  // Bridge startup
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  digitalWrite(13, HIGH);
  Bridge.begin();
  Console.begin();//for debugging purposes
  

  // Listen for incoming connection from anywhere  
  server.noListenOnLocalhost();
  server.begin();  
}

void loop() {
  // Get clients coming from server
  YunClient client = server.accept();  
   
  //Is there a new client?  
  
  if (client) {    
    // Process request
    process(client);
    // Close connection and free resources.
    client.stop();
    }

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


void process(YunClient client) {
  // read the command  
  String command = client.readStringUntil('/');
  command.trim();
 
 if (command == "led13on") {
       digitalWrite(13, HIGH);              
    }
    else if (command == "led13off") {
       digitalWrite(13, LOW);
    }

}

///////////////////////////
b. Here is the simple web code for the index.html

<!DOCTYPE html>
<html>
<head>
	<script type="text/javascript" src="zepto.min.js"></script>
	<script type="text/javascript">
	function led13on(){
    $('#content').load('/arduino/led13on');
    }

    function led13off(){
    $('#content').load('/arduino/led13off');
    </script>

</head>
	<body>
		<span id="content"></span>
		<button onclick="led13on()">LED 13 ON</button> 

        <button onclick="led13off()">LED 13 OFF</button>
	</body>
</html>

////////////////////////

Be sure to include the zepto.min.js in the www folder

I have setup my YUN based on similar settings and I am able to access it anywhere by typing in my global IP with the forwarded port with the sketch name in this fashion:

56.23.40.12:1689/sd/sketch_name

I hope this little tutorial will get more YUN users to be able to do their IOT projects without paid for services or using someone else’s web servers.

houdinihar

Turn on firewall, obtain public ip address for Yun

You could access Yun any where at world whether Yun at home router or Yun at behind corp firewall.
Key issue is from any where to any where.

Thanks, @houdinihar, I have been interested in this. I will read it carefully later today.

...R

@houdinihar, I have added your tutorial to A List of Tutorials Contributed by Forum Members. The list will be published later this week. --Jesse

Thanks for that Jesse. Very thoughtful. I appreciate that. :)

houdinihar

Hello houdinihar,

Very Nice Tutorial,

1) In this tutorial you taken 1680 port, why we taken 1680 port only.Is there any reason for this one.

2) and iam accessing data from 3g donge to arduino yun. when i am given 1680 port,web page opened in mobile phone(mobile data) only. when i try to open web page in my router network(my laptop) its showing following error:

Connection refused: xx.xxx.xxx.160:1680

how to solve this one.

Thanks in advance.

Thank you houdinihar, Is all of this required if I am only trying to have access to my Yun via the web interface(LuCi) ?

Is it possible to simply use port forwarding to allow access to the web interface from an external network?

Answering my own question: port forwarding alone does work if you are just trying to have access to the web interface- we set up a DMZ host, but you could accomplish the same thing with port forwarding.