I have an idea for a special project, but in order for it to work I need two raspberry pi's to talk to each other over the internet, not wifi. I use python3 as my programming language, and I want one of the pi's to send the other commands from like a hundred miles away. Is there some free python3 software I can get for this? I also need to send gpio status

  • 1
    Welcome. You should detail where the Pi's are and how they can connect to the internet.
    – goldilocks
    May 3 '20 at 14:02
  • 2
    You want 2 computers to "talk" - just because they are Raspberrys makes no difference.
    – Milliways
    May 4 '20 at 0:21

What you want to do won't require any additional steps on your raspberry pi devices, and won't need anything proprietary or the gpio pins. The code can be the same as you would use to have two pi's talk over wifi. The only difference will be in what address the two devices use to talk to eachother.

When you have two devices talk over wifi, they are using your local network, and can use local IP addresses. Those typically look something like, or maybe However, these addresses only mean something on your wifi network, and can't be used to talk to a different wifi network. Think of them like room numbers in a building, and what you will need is a way to address a different building entirely.

Everybody's home wifi network is given its own address when it is connected to the internet by your internet service provider (eg, Comcast/Verizon/etc). This is your public IP address, and will be the same format as the local IP, but different numbers. You will need to know the public IP of the network you want to reach. You can find this pretty easily by simply googling "Whats my IP" from a device on that network.

Now that you have this address, you can use that in your code in place of the local address. For example, here's a snippet that sends a remote ssh command to a raspberry pi running on a local network. (Substitute with your own python script, or any command you want)

# example local IP address, on default ssh port 22
ssh pi@ /usr/bin/python3 --version

And now you can change it to

# example public IP address, on custom port 11122
ssh pi@ /usr/bin/python3 --version

So like I said, minimal changes in code. However! Communication across networks like this requires your local wifi network's permission, and you will have to configure your routers firewall to allow the traffic to pass through. This can be done through your routers administration page, and the specific steps will depend on your router model. What you want to find is called 'port forwarding', so I would recommend searching for "<router model> port forwarding" to find how to accomplish that on your specific device.

When configuring the port forwarding, you will need to know the local IP of the raspberry pi (which you want to be static, so look for "assign static private IP" instructions), as well as the port that your communication is using. Different applications will use different ports to avoid conflicting, in the example above I used port 11122.

Please be aware, setting up port forwarding provides external access to your internal network. There is a reason it's disabled by default, and requires administrative access to your router. You may intend only for your two raspberry pi devices to talk to eachother, but this inherently allows the world to see those new open ports, and to prod them at will. I strongly suggest you do not provide any sensitive information over these channels, and use only secure methods of communication such as ssh and https. These will require more setup than just making http requests, but will mean that while outsiders can still see the door, they wont have a key.

To summarize, since I ended up typing out a lot more than I thought...

  1. What you want to do is not a Rasberry Pi specific problem
  2. You need to configure your networks routers to forward the port you are using
  3. Communication will be open, so you should use a secure protocol like passwordless SSH using private key files.
  4. Definitely test locally first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.