Pages: [1] 2 3   Go Down
Author Topic: Setting up Arduino Webserver for remote access  (Read 10742 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been googling all day for a comprehensive set of instructions that will allow an Arduino with Ethernet Shield to be visible to the whole world versus just being visible to my local area network.  I did this example:

http://bildr.org/2011/06/arduino-ethernet-pin-control/

but of course it only works with a local connection.  I would like to be able to access my arduino remotely from anywhere but there doesn't seem to be a lot on the web about it.  I have a cable internet modem connected to a linksys E1200 wireless router.  I have found a few things that I don't understand very well such as dynamic dns and port forwarding and I am sure that I have found some of the pieces, but I just don't know how to put them together.  Can someone help me out with this?
Logged

Arduino Uno;
Mega328

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50902
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to be able to access my arduino remotely from anywhere but there doesn't seem to be a lot on the web about it.
Perhaps because there is nothing Arduino-specific in what you want to do.

Your Arduino is connected via a cable to some physical device - usually a router. The router allows the Arduino exclusive use of an IP address that is local to the network that the router manages. The router may, or may not, perform port-forwarding of data it gets to the Arduino.

If the router is set up to forward all data that arrives on port 60, for instance, to the Arduino, then the Arduino will be accessible from computers not on the network using "http://router-ip:60".

Of course, this means that you need to know your routers IP address, as assigned (generally temporarily) by your internet service provider.

You can get a static IP address, so that the router's IP address never changes, but be prepared to pay for that. Sometimes a lot.

Alternatively, you can have code on a PC connected to the router that periodically checks the IP address assigned to the router, and updates DNS data (worldwide) mapping the router's IP address to a domain name (that you host/pay for). Then, the world will access your Arduino (via your router) by name, rather than IP address.

As your internet service provider changes your router's IP address (typically weekly), DNS will be updated to know where your router went, and the Arduino will remain visible, except for the few minutes just after the router's IP address is changed.
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fantastic explanation PaulS!  I appreciate that!  So it sounds like I need to do two things.  First I need to take advantage of a service like dynDNS and second would be to enable port forwarding on my router.  I think my router should take care of updating dynDNS when the IP Address changes (I hope).
Logged

Arduino Uno;
Mega328

Dublin, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

This is a useful thread. Can I just ask is the router-ip the same as IPv4 address, which is found in cmd->ipconfig?

Is it true to say that the computers IP, (got by Googling 'what is my ip') is different from IPv4 which is the routers IP?

Thanks,

Shane
Logged

Those who say it can't be done are usually interrupted by others doing it.

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a useful thread. Can I just ask is the router-ip the same as IPv4 address, which is found in cmd->ipconfig?

There are a few different systems but generally no. The cable modem gets a real IP address from the cable system. It then hands out "fake net" addresses to the computers and other devices using a protocol called DHCP (dynamic host control protocol). So the cable modem / FiOS router / whatever might have an IP address like 72.249.12.17 and the computers on your network would have an IP address like 192.168.1.5, 192.168.1.6 and so on.

Quote
Is it true to say that the computers IP, (got by Googling 'what is my ip') is different from IPv4 which is the routers IP?

Well, yes and no.

First, IPv4 is a system of IP addresses, which has 4 octets (so xxx.xxx.xxx.xxx). IPv4 is not a reference to the IP on our cable modem.

Yes the computer has its own IP, but the cable modem / router / whatever device translates packets from your PC that go to the Internet so they look like they come from the real IP address. It keeps track, so packets that are supposed to go to your PC get sent to your PC and so on. This is called NAT (Network Address Translation).

As a result of NAT, everything that comes from any PC or device on your network looks like it comes from the one IP address on the cable modem.

Port forwarding is a mechanism where by you tell the cable modem that if a packet comes to your real IP address for port 8080, it should be forwarded to fake IP address 192.168.1.5 (or whatever). That way, an Arduino web server can answer requests.
 
Logged

Dublin, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Skyjumper,

Thanks for your clear explanation. I understand much better now.

I am trying to manually fill in the snip of code below from a tutorial on the Instructables website.

Code:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

  • The first thing is the mac[] address which is on a sticker on my Ethernet shield. In some tutorials I'm told this rarely needs to be changed in the code, I've read that any mac address will work. But can it still work without the specific MAC address from the Ethernet shield being put into the code?
  • The next thing is the ip[] which must be the ip of my Ethernet shield. This I can get from running the example sketch "DHCP-based IP printer" and the Ethernet shield's ip pops up in the serial monitor.
  • The next thing then to be filled in is the gateway[], which say from your example would be 72.249.12.17, in other words the router ip or the ip I get when I Google "what is my ip". (If this is true it is a pity the example ip given in the code is misleading, it looks like the ip of a computer inside the network, not like a router's ip.)
  • The subnet mask is 255.255.255.0 this is what it says for the subnet mask in cmd->ipconfig

Thanks,

Shane
« Last Edit: August 07, 2012, 06:01:25 pm by ofey » Logged

Those who say it can't be done are usually interrupted by others doing it.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

using a protocol called DHCP (dynamic host control protocol).

(The 'C' stands for configuration.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 148
Posts: 9750
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Be aware that using DHCP may not work well if your arduino server is behind a router. Normally one would assign a static lan IP and port in the arduino code, then set the router to foward the incomming request to the arduino. In my setup I would assign the arduino an IP/port like 192.168.1.105 and port 85. then using a dynamic IP address service  the outside  address for the arduino would look like zoomkat.dyndns.com:85 to connect to the arduino behind the router. Note that the dynamic IP service usually requires an application running on another computer behind the router, or some routers have provisions to keep the IP service updated on the current assigned IP provided to the router by the internet connection provider.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, exactly what Zoomkat said, especially the part about manually assigning a static IP instead of letting the Arduino (or any other device) get a DHCP IP. The IP assigned by DHCP is temporary and may change, although usually not. Still, it can happen. In your router / cable modem / whatever, you can control the range of IPs that can be assigned to other devices using DHCP. So, you could tell it that addresses for use by DHCP are from 192.168.1.2 to 192.168.1.31. Then, you can use 192.168.1.40 or any other IP address outside this range on your Arduino. Just make sure its a 192.168.1.x IP address.

The gateway address is not the "real" IP address on the router. Usually, in examples like this, its the .1 address for the class C you're using. The class C is everything like this: 192.168.1.x. A different class C is 192.168.2.x. So the gateway for 192.168.1.20 is probably (but not required to be) 192.168.1.1. This gets just a little complex, and outside the scope of this thread, but if you set the gateway as 192.168.1.1 you're probably safe. Again, the router configuration will tell you this.

Its best to set the MAC address to whatever is on the sticker of your shield.

Hm, I think that covers your questions. Let us know if you have more and/or how you make out.

Logged

Dublin, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In my router under DHCP setup it says start address is 192.168.1.1 and the pool size in 200 so I presume that means the ip could be 192.168.1.201 or with anything greater than 200 on the end. In the  router the under TCP/IP the lan ip is 192.168.1.254 so i think that's what I set ip[] to.

My shield's mac address is 90-A2-....... I'm sure that's filled in as 0x90, 0xA2, .... etc.

I have the gateway[] as 192.168.1.1 and the

I've filled it all in like this: (hope I'm not putting too much information on the net!)

Code:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFB, 0x94 }; //physical mac address
byte ip[] = { 192, 168, 1, 254 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

When it comes to connecting in a browser from outside my home network, 192.168.1.177:80 won't work because 192.168.1.177 is only for inside my network. So should i use my router's ip followed by :80?

Thanks for all your help and patience.

Shane
Logged

Those who say it can't be done are usually interrupted by others doing it.

Dublin, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The above isn't even working locally. So I had to change the ip[] line to

byte ip[] = { 192, 168, 1, 177 };

and now that works inside my network.

Also is there any easy way to test if it's working outside my home network. I have to keep calling a friend and asking him to see if it's working. But as I said in the post before I am not even sure what should be typed into a browser exactly.

Thanks,

Shane
« Last Edit: August 08, 2012, 12:15:19 pm by ofey » Logged

Those who say it can't be done are usually interrupted by others doing it.

Offline Offline
Newbie
*
Karma: 1
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What are the security issues with forwarding the arduino.  Could someone crash(bog down) the arduino with denial of service attacks.
  What I'm working on is a home automation system.  I'm using my pc for a gui using vb.net program to talk to the arduino.

 If I use arduino webserver and forward that ip, so I can control my home automation system from the internet.  What issues could I have.  I might use a service like cosm.com and have the arduino post the data to that site, so I don't have to forward the arduino ip and leave it hidden.

 Any input would be helpful.  Thanks
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

192.168.1.1 is your routers internal address. you need to find out what you routers external address is. It can tell you in the router, but you can also find out by going to something like ipchicken.com.

once you have you external address, you need to forward a port through your routers firewall. if your worried about security, pick something obscure like 61230 and forward it to port 80 at the arduino's ip address. i think you set that to 192.168.1.254 in you previous comment.

*generally* you cannot access you external address form you internal network. your router just gets confused. to access it you will need to be "outside" your network. i usually connect my laptop to the internet via my smartphone. do that if you can, because calling your friend 1000 times to test might make them less of a friend smiley-wink
access it by connecting to you external address in a browser and specify a port number by using a colon.
eg   58.7.1.3:61230

Logged

Dublin, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

grOp3R, 

Your post is very helpful. So the router has an external and an internal ip. I understand the external ip. But is the internal ip different from the ip of each computer inside the network which are usually 192.168.1.1 or 192.168.1.2 etc.

I'm just a little confused still when you say:

Quote
once you have you external address, you need to forward a port through your routers firewall. if your worried about security, pick something obscure like 61230 and forward it to port 80 at the arduino's ip address. i think you set that to 192.168.1.254 in you previous comment.

I didn't realise that one port forwards to another, I thought it was just the external ip ports to the arduino's ip. That's really stupid of me considering it is called 'port' forwarding!

In my router the port configuring has an start port and an end port. So i suppose they would be the 61230 port and the 80 port. Is that correct?

Thanks,

Shane
Logged

Those who say it can't be done are usually interrupted by others doing it.

Dublin, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My router tells me that the end port has to be greater than the start port. So I put 80 as the start and 610 as the end. But does that mean someone outside my network should be trying

router's external ip:80

and in the Arduino code the port should be 610 ? Or is it just the other way around? I guess I'll just have to test it!

thanks,

Shane
Logged

Those who say it can't be done are usually interrupted by others doing it.

Pages: [1] 2 3   Go Up
Jump to: