Can two Arduinos communicate over two different networks?

I am wondering if there is a way to connect two Arduinos over WIFI, but have them be on two different networks (i.e. one be in my house connected to WIFI (Arduino A), and the other one be two blocks away connected to WIFI (Arduino B)).

I want to be able to have a computer send data to Arduino A via USB . Then have Arduino A send that data over the internet to Arduino B which is also hooked up to a separate WIFI connection, and then execute those commands. I would also like Arduino B to be able to send data back to Arduino A.

I've looked for things like that, but the things I found had to have both Arduinos on the same network.

I want it so I can be able to control my Arduino from about 1 mile away, by having them transmitting and receiving data via the "cloud" so to speak.

Any ideas?

Thanks ahead of time!

In other words you have 2 Arduinos, each one in a different house (and local network) and respectively connected to the internet via a WiFi connection, and you want them to be able to talk to each other via internet.

This is surely possible, but involves configuring both internet routers to make your Arduinos visible to the internet (which, depending on the application, may also involve security issues). This requires in general IP/port forwarding from your intranet and a static IP address toward the internet.

Please have a look at this article and see if it gets you closer to what you want to achieve (especially the final part on how to setup the router): http://www.instructables.com/id/Control-an-LED-over-the-internet-using-the-Arduino/

Be aware that many articles out there about how to control an Arduino via the "Web" or "internet" actually refer to controlling it with a web browser on your local network (intranet), which is a much different (and somehow simpler) case than controlling them via internet.

How far away is

two blocks

Sounds like its beyond the normal range that you'd get from a wifi unit of any kind.

You may be able to do it using directional antennas, but if there is anything in the way, e.g. trees or buildings,.

I just did a quick search on Wifi range and found this

https://en.wikipedia.org/wiki/Wi-Fi#Range

I see 100m quoted, but this will be line of sight, and not with anything in the way.

One way to implement this that avoids the complexity and security issues related to running your Arduino as a publically-accessible server is to communicate by email. You would put an email client on each Arduino (I'm sure you can find examples showing how to send and receive email) and set up an email account for each Arduino. Communication wouldn't instantaneous (it will take a while for each email to be delivered) but if the accounts are all at the same provider delivery should be pretty quick.

Marco_Ghislanzoni: In other words you have 2 Arduinos, each one in a different house (and local network) and respectively connected to the internet via a WiFi connection, and you want them to be able to talk to each other via internet.

This is surely possible, but involves configuring both internet routers to make your Arduinos visible to the internet (which, depending on the application, may also involve security issues). This requires in general IP/port forwarding from your intranet and a static IP address toward the internet.

Correct.

Would there be a tutorial on how this would be coded?

Marco_Ghislanzoni: Please have a look at this article and see if it gets you closer to what you want to achieve (especially the final part on how to setup the router): http://www.instructables.com/id/Control-an-LED-over-the-internet-using-the-Arduino/

Not quite, this tutorial looks like it you control it via a browser. I want each Arduino to talk to each other, not a browser. Thanks though.

rogerClark: How far away is

two blocks

Sounds like its beyond the normal range that you'd get from a wifi unit of any kind.

I just used two blocks to show that the Arduinos were not in the same house, and that they would be on different networks.

PeterH: One way to implement this that avoids the complexity and security issues related to running your Arduino as a publically-accessible server is to communicate by email.

I didn't know that was possible. Thats good to know. Although that way wouldn't work for me because I would need data transferred as fast as possible. The data being sent back and forth will be used to send information to and from a quadcopter.

This is no different than have 2 computers talk to each other over the Internet. It's absolutely possible, but requires some steps.

First and foremost, do both house's internet connections have static external-facing IP addresses? Probably not. So you'll have to deal with that. You see, each home will likely have a dynamic IP address that changes periodically. This could be one a day, several times a day, once a week, whatever. In order for internet device A to initiate communication to internet device B, it must know the externally facing IP address of device B. But if the IP address changes, how do you deal with that?

The answer is to use a dynamic DNS service. Some are free, some cost money. I'm currently paying for one that costs me like $15 a year, and I use to access my home automation in my house from anywhere in the world without having to know my current external IP address for my home network. You get to choose a partial domain on one of their hosts, like x.domain.com, where "x" is the part you choose. Then you run a client on one of the computers on your home network (some routers even can do it for you without the need to run a client) that automatically updates domain.com with your externally facing IP address whenever your IP address changes. When a computer attempts to connect to "x.domain.com", the first thing it does is attempt resolve the domain name to an actual IP address. When you type in "www.google.com" in your web browser, your web browser actually using some services on your computer and something called a domain name service to get an IP address. When your browser actually connects to the web server to download the page, it does this over the IP address, not the domain name. But that's all magic under the covers that you don't realize is happening. If you only need to initiate the communication from one house to the other, and not the other way around, then you only need to setup one of these houses with one of these services. Once device A establishes communication to device B, both can talk to each other as long as the socket is open. If you need device B to open a socket back to device A, then you will need dynamic DNS services for both houses.

So anyway, let's say you setup a dynamic domain name service for house B so that you can just connect to "houseb.somedomain.com". You will resolve the domain name to the currently correct IP externally facing IP address for home b's network.

The next step is you need to setup port forwarding on home B's router. houseb.somedomain.com resolves to the IP address of essentially your router, but not your Arduino. How do the packets know they need to go to the Arduino? You setup port forwarding. Let's say your Arduino has a local (LAN) network address of 192.168.0.17. In order for packets to be sent to that IP address, you have to setup port forwarding to make the packets go there. Typically you would choose a particular port so that only packets sent to that port will get redirected to your device. Let's just for the sake of argument use port 80 (which is what HTTP normally is under).

Let's walk through how communication is done:

House A:

Arduino A has local IP of 192.168.0.300 (just making this up). House A has external IP of 200.203.156.177 (just making this up).

House B:

Arduino B has local IP of 192.168.1.17 (again, just making this up) House B has external IP of 197.231.56.18 House B has dynamic DNS setup on houseb.dnsalias.org

Now the sequence of events: 1. Arduino A wants to open a TCP socket to houseb.dnsalias.org on port 80. 2. You need to do a DNS lookup on houseb.dnsalias.org to get the current IP address on Arduino A. Not sure if there is already a library to do this for you, but this must be accomplished. Result should be 197.231.56.18 3. Arduino A attempts to open a TCP socket to 197.231.56.18:80 (port 80) 4. Router in house B sees the socket request from Arduino A. It has a port forwarding rule that tells it forward packets on port 80 to internal IP 192.168.1.17 5. Router forwards packets to 192.168.1.17 6. Arduino B accepts the socket and begins processing the packets. It knows the external IP address of House A and the socket number and can respond to Arduino A. Two way communication is established and both devices are able to send and receive to each other as long as the socket stays open.

This would work even if house A and house B were in opposite sides of the planet. Assuming Internet traffic can get between those 2 homes.

Xpendable: This is no different than have 2 computers talk to each other over the Internet. It's absolutely possible, but requires some steps.

First and foremost, do both house's internet connections have static external-facing IP addresses? Probably not. So you'll have to deal with that. You see, each home will likely have a dynamic IP address that changes periodically. This could be one a day, several times a day, once a week, whatever. In order for internet device A to initiate communication to internet device B, it must know the externally facing IP address of device B. But if the IP address changes, how do you deal with that?

The answer is to use a dynamic DNS service. Some are free, some cost money. I'm currently paying for one that costs me like $15 a year, and I use to access my home automation in my house from anywhere in the world without having to know my current external IP address for my home network. You get to choose a partial domain on one of their hosts, like x.domain.com, where "x" is the part you choose. Then you run a client on one of the computers on your home network (some routers even can do it for you without the need to run a client) that automatically updates domain.com with your externally facing IP address whenever your IP address changes. When a computer attempts to connect to "x.domain.com", the first thing it does is attempt resolve the domain name to an actual IP address. When you type in "www.google.com" in your web browser, your web browser actually using some services on your computer and something called a domain name service to get an IP address. When your browser actually connects to the web server to download the page, it does this over the IP address, not the domain name. But that's all magic under the covers that you don't realize is happening. If you only need to initiate the communication from one house to the other, and not the other way around, then you only need to setup one of these houses with one of these services. Once device A establishes communication to device B, both can talk to each other as long as the socket is open. If you need device B to open a socket back to device A, then you will need dynamic DNS services for both houses.

So anyway, let's say you setup a dynamic domain name service for house B so that you can just connect to "houseb.somedomain.com". You will resolve the domain name to the currently correct IP externally facing IP address for home b's network.

The next step is you need to setup port forwarding on home B's router. houseb.somedomain.com resolves to the IP address of essentially your router, but not your Arduino. How do the packets know they need to go to the Arduino? You setup port forwarding. Let's say your Arduino has a local (LAN) network address of 192.168.0.17. In order for packets to be sent to that IP address, you have to setup port forwarding to make the packets go there. Typically you would choose a particular port so that only packets sent to that port will get redirected to your device. Let's just for the sake of argument use port 80 (which is what HTTP normally is under).

Let's walk through how communication is done:

House A:

Arduino A has local IP of 192.168.0.300 (just making this up). House A has external IP of 200.203.156.177 (just making this up).

House B:

Arduino B has local IP of 192.168.1.17 (again, just making this up) House B has external IP of 197.231.56.18 House B has dynamic DNS setup on houseb.dnsalias.org

Now the sequence of events: 1. Arduino A wants to open a TCP socket to houseb.dnsalias.org on port 80. 2. You need to do a DNS lookup on houseb.dnsalias.org to get the current IP address on Arduino A. Not sure if there is already a library to do this for you, but this must be accomplished. Result should be 197.231.56.18 3. Arduino A attempts to open a TCP socket to 197.231.56.18:80 (port 80) 4. Router in house B sees the socket request from Arduino A. It has a port forwarding rule that tells it forward packets on port 80 to internal IP 192.168.1.17 5. Router forwards packets to 192.168.1.17 6. Arduino B accepts the socket and begins processing the packets. It knows the external IP address of House A and the socket number and can respond to Arduino A. Two way communication is established and both devices are able to send and receive to each other as long as the socket stays open.

This would work even if house A and house B were in opposite sides of the planet. Assuming Internet traffic can get between those 2 homes.

Thank you for your detailed reply! Now I know a little more on what I'm supposed to do. Anyone know where I can find information and code on how to do that?

I found this for a dynamic DNS service http://www.noip.com/free/ Its "free" for limited opptions. Would this work as one?

This...

HailStorm: I am wondering if there is a way to connect two Arduinos over WIFI, but have them be on two different networks (i.e. one be in my house connected to WIFI (Arduino A), and the other one be two blocks away connected to WIFI (Arduino B)).

And this

I want to be able to have a computer send data to Arduino A via USB . Then have Arduino A send that data over the internet to Arduino B which is also hooked up to a separate WIFI connection, and then execute those commands. I would also like Arduino B to be able to send data back to Arduino A.

are two different things. In the first case, you are communicating over WiFi, and in the second, you are saying you want to send the data over the internet. You and I could do the second method now. I use an Arduino to send data over WiFi and out over the internet to your place, and your Arduino receives it from the router via WiFi.

Somehow I don't think that's what you are talking about (though I could be wrong). It sounds more like you want to control a remote Arduino over WiFi, which is not "over the internet". You can do it with a WiFi shield or a WiFi module that accepts serial data, communicating with the other Arduino via another WiFi shield or module, and using high-gain, directional antennae. It is quite possible to communicate via WiFi out to 10 or 15 kilometres, though that does get expensive.

Here are a few things for you to research.

WiFi232 module made by USR IOT. Serial over WiFi. "cantenna". "Pringles antenna" Yagi

I've looked for things like that, but the things I found had to have both Arduinos on the same network.

I want it so I can be able to control my Arduino from about 1 mile away, by having them transmitting and receiving data via the "cloud" so to speak.

You need to figure out exactly what you want to do. Do you really need data to go over the internet? Do you want to send data from one PC, to another, through two Arduino's connected by WiF?

lar3ry: ...you are saying you want to send the data over the internet. You and I could do the second method now. I use an Arduino to send data over WiFi and out over the internet to your place, and your Arduino receives it from the router via WiFi.

Thats exactly what I want it to do, while also making me able to send data back to you.

lar3ry: You need to figure out exactly what you want to do. Do you really need data to go over the internet? Do you want to send data from one PC, to another, through two Arduino's connected by WiF?

Yes I need to send data over the Internet. This is what the layout would look like:

  1. A program that is running a user interface on a computer (in this case a Microsoft Visual Studio windows form application) will send data to Arduino A(with WIFI or ethernet capability) which is connected to the computer via USB.
  2. Arduino A will then send that data over the internet to an awaiting Arduino B in Germany(used that as an exaggeration).
  3. Arduino B (with it to being connected to the internet) will receive the data and execute the command.
  4. If necessary the process will go in reverse, with Arduino B sending, and Arduino A receiving.

Note that the computer is just giving the Arduino code to send like move, stop, etc. The arduino itself is doing all the sending and receiving.

That is pretty much what I want to do

Granted there will be server stuff in there to.

HailStorm: I didn't know that was possible. Thats good to know. Although that way wouldn't work for me because I would need data transferred as fast as possible. The data being sent back and forth will be used to send information to and from a quadcopter.

In that case I suggest that one of the end points needs to be set up as a publically-accessible server i.e. with port forwarding configured, and either a static IP address or a dynamic address registered with a dynamic DNS service. The other end point will need to initiate a TCP connection to the server, and recover the connection if it drops. (Once connected, you will have two-ways comms over the connection.)