AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 10:53 am

Hello friends,
I am making two systems using pi which require transfer of .txt from one pi to other, based on the received .txt i am controlling several motors. How to transfer the .txt? i cannot use internet, nor through a pc, just two Rpi. What is the easiest method? is it possible by any cable and some setup?
Basically i am looking at model where there are two Rpi and a cable between them to transfer .txt, is it possible?
(i tried reading through posts here but due to my insufficient knowledge, i am unable to follow them, please help me in simple terms how to do this, thanks)

Ajit

hippy
Posts: 7762
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 1:36 pm

Connect the two with a suitable RJ45 cable and you have a TCP/IP network allowing both to communicate with each other.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 4:13 pm

Thank you for ur reply, what are the setting or commands i need to perform to transfer file after connecting the cable to Rpis

klricks
Posts: 7155
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 5:22 pm

AjitPi wrote:Thank you for ur reply, what are the setting or commands i need to perform to transfer file after connecting the cable to Rpis
This may help viewtopic.php?t=98652&p=685943
Or Google [connect 2 raspberry pi with Ethernet]

Does your system need to automatically transfer and process the file without user intervention?
-or-
Is it a one time or infrequent manual file transfer process?

What is the distance between the 2 RPi?
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 6:01 pm

AjitPi wrote:Hello friends,
I am making two systems using pi which require transfer of .txt from one pi to other, based on the received .txt i am controlling several motors. How to transfer the .txt? i cannot use internet, nor through a pc, just two Rpi. What is the easiest method? is it possible by any cable and some setup?
If you want to quickly get it running, look into using the ssh suite of programs. There are simple ways to copy files from one machine to another. Then the other machine can load that file and decide what to do. So there is sftp, scp, ssh, and there is also the fuse using file system sshfs. You should be able to get a basic setup going in a few minutes even just using bash and a few simple command line programs.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 6:20 pm

klricks wrote:
AjitPi wrote:Thank you for ur reply, what are the setting or commands i need to perform to transfer file after connecting the cable to Rpis
This may help viewtopic.php?t=98652&p=685943
Or Google [connect 2 raspberry pi with Ethernet]

Does your system need to automatically transfer and process the file without user intervention?
-or-
Is it a one time or infrequent manual file transfer process?

What is the distance between the 2 RPi?
I have a button connected to one of the gpio pin , when the user presses the button a python script needs to run and send the .txt file to the second RPi.

It is not a one time activity, it has to transfer file as and when the user presses the button.

Both the RPi are in a single room, i am thinking of cable now, but once i understand and transfer files, i would venture into transferring wireless (for which i would need your help again :) )

User avatar
DougieLawson
Posts: 39169
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 7:46 pm

hippy wrote:Connect the two with a suitable RJ45 cable and you have a TCP/IP network allowing both to communicate with each other.
It's marginally more complex than that. If the eth0 interface comes up active without being able to connect to a DHCP server it will get an address from 169.254.xxx.xxx (random assignment) at both ends.

You need to know the address of the remote machine to connect to it.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
rpdom
Posts: 17189
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 8:30 pm

DougieLawson wrote:
hippy wrote:Connect the two with a suitable RJ45 cable and you have a TCP/IP network allowing both to communicate with each other.
It's marginally more complex than that. If the eth0 interface comes up active without being able to connect to a DHCP server it will get an address from 169.254.xxx.xxx (random assignment) at both ends.

You need to know the address of the remote machine to connect to it.
If they're isolated from any other network you might as well set static addresses of something 192.168.0.10 and 192.168.0.11 and then you don't have to guess.

User avatar
DougieLawson
Posts: 39169
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Transfer .txt between two RPi (no internet,no pc)

Sat Oct 31, 2015 8:34 pm

But that's a problem if you don't have a keyboard/screen attached to each system. And when you have that it's easy to discover the pre-assigned address.

Setting a new address from a command line is trivial
sudo ifconfig eth0 192.168.1.10
it doesn't even need a netmask because the default mask is OK.

Setting a known IP address for a headless system needs an update to /boot/cmdline.txt.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Sun Nov 01, 2015 5:50 am

stderr wrote:
AjitPi wrote:Hello friends,
I am making two systems using pi which require transfer of .txt from one pi to other, based on the received .txt i am controlling several motors. How to transfer the .txt? i cannot use internet, nor through a pc, just two Rpi. What is the easiest method? is it possible by any cable and some setup?
If you want to quickly get it running, look into using the ssh suite of programs. There are simple ways to copy files from one machine to another. Then the other machine can load that file and decide what to do. So there is sftp, scp, ssh, and there is also the fuse using file system sshfs. You should be able to get a basic setup going in a few minutes even just using bash and a few simple command line programs.
Hello thank for taking time to reply,

Can you please elaborate on what u are saying? i mean what do i exactly do?

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Sun Nov 01, 2015 5:54 am

DougieLawson wrote:But that's a problem if you don't have a keyboard/screen attached to each system. And when you have that it's easy to discover the pre-assigned address.

Setting a new address from a command line is trivial
sudo ifconfig eth0 192.168.1.10
it doesn't even need a netmask because the default mask is OK.

Setting a known IP address for a headless system needs an update to /boot/cmdline.txt.

So in summary ,
1. I have to connect them using the RJ45 cable
2. Then on each of them set a static IP using
sudo ifconfig eth0 192.168.1.10
3. Then?

(in this way will i be able to transfer even when i disconnect the cable then reconnect it after reboot?)

User avatar
rpdom
Posts: 17189
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Transfer .txt between two RPi (no internet,no pc)

Sun Nov 01, 2015 6:27 am

AjitPi wrote:So in summary ,
1. I have to connect them using the RJ45 cable
2. Then on each of them set a static IP using
sudo ifconfig eth0 192.168.1.10
Yes, but you need to give each Pi a different address. Use 192.168.1.10 for one Pi and 192.168.1.11 for the other.
3. Then?
Then you can use commands like scp to copy files from one Pi to the other. For example, to copy a file from your current folder on 192.168.1.10 to the other Pi, you would do something like

Code: Select all

scp myfile 192.168.1.11:/home/pi
It will ask for the password for your Pi user. You can get around that fairly easily. We'll cover that later.
(in this way will i be able to transfer even when i disconnect the cable then reconnect it after reboot?)
No, you would have to type the ifconfig commands into the Pi after rebooting. Putting the static addresses in the /etc/dhcpcd.conf files of each Pi will make them fixed.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Sun Nov 01, 2015 9:37 am

rpdom wrote:
AjitPi wrote:So in summary ,
1. I have to connect them using the RJ45 cable
2. Then on each of them set a static IP using
sudo ifconfig eth0 192.168.1.10
Yes, but you need to give each Pi a different address. Use 192.168.1.10 for one Pi and 192.168.1.11 for the other.
3. Then?
Then you can use commands like scp to copy files from one Pi to the other. For example, to copy a file from your current folder on 192.168.1.10 to the other Pi, you would do something like

Code: Select all

scp myfile 192.168.1.11:/home/pi
It will ask for the password for your Pi user. You can get around that fairly easily. We'll cover that later.
(in this way will i be able to transfer even when i disconnect the cable then reconnect it after reboot?)
No, you would have to type the ifconfig commands into the Pi after rebooting. Putting the static addresses in the /etc/dhcpcd.conf files of each Pi will make them fixed.

Hey, thank you very very much.. i am able to transfer the file now :) Can this same thing done throught Python script, like i give the path of the file and it is transferred to destination RPi?
(Such a helpful community! Now i will be venturing into further operations confidently :) )

User avatar
DougieLawson
Posts: 39169
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Transfer .txt between two RPi (no internet,no pc)

Sun Nov 01, 2015 9:41 am

AjitPi wrote:

So in summary ,
1. I have to connect them using the RJ45 cable
2. Then on each of them set a static IP using
sudo ifconfig eth0 192.168.1.10
3. Then?

(in this way will i be able to transfer even when i disconnect the cable then reconnect it after reboot?)
On rpi #1
sudo ifconfig eth0 192.168.1.10
On rpi #2
sudo ifconfig eth0 192.168.1.20
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 02, 2015 3:31 am

Thank you very much for the solution, please let me know what is the simplest setup for wireless transfer for the the same problem?

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 02, 2015 3:33 am

AjitPi wrote:Thank you very much for the solution, please let me know what is the simplest setup for wireless transfer for the the same problem?
Install USB wifi dongles in each Pi.
Set up one Pi as an access point.
Set up the other Pi to connect to it.

Your other software is the same, since an IP network with wifi is the same as a wired IP network.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 02, 2015 8:26 am

Thank you Ame! I will try that wifi thing.

Before that i am stuck in one thing, i have a wired ethernet now and using scp to copy files from one to another. But it is asking for password everytime i transfer
1.I want to disable password, how to disable it?

2. How do i do all this activity through python script? i want to just give the path and press a button to transfer file.

Thank you, please reply i am in need of this kind of urgently :(

User avatar
rpdom
Posts: 17189
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 02, 2015 8:58 am

AjitPi wrote:Thank you Ame! I will try that wifi thing.

Before that i am stuck in one thing, i have a wired ethernet now and using scp to copy files from one to another. But it is asking for password everytime i transfer
1.I want to disable password, how to disable it?
First step, on both Pis make sure you have a .ssh directory

Code: Select all

pi@raspi5 ~ $ mkdir .ssh
(don't worry if it says "mkdir: cannot create directory `.ssh': File exists", that means it has already been done)
Then create some private and public key files on each Pi

Code: Select all

pi@raspi5 ~ $ sshkeygen -t dsa
Just press Enter for any questions it asks.
Add the public keyfiles to the other Pis list (do this for both Pis, changing the IP address.

Code: Select all

pi@raspi5 ~ $ ssh 192.168.1.11 'cat >>.ssh/authorized_keys'  < .ssh/id_dsa.pub
(pi@raspi5 is the account on one of my Pis. It will be 'pi@somethinglese' on yours)

After that it shouldn't ask you for a password again.
2. How do i do all this activity through python script? i want to just give the path and press a button to transfer file.
Good question. I don't use python. I think it has something called "system" that can be used to run commands. Probably some examples on the forum.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 02, 2015 9:29 am

2. How do i do all this activity through python script? i want to just give the path and press a button to transfer file.
Good question. I don't use python. I think it has something called "system" that can be used to run commands. Probably some examples on the forum.
Use the "subprocess" module (this is recommended in place of using os.system that you'll also see people using).

My experience of scp is light but, if I recall, the format for copying a local file to remote is

Code: Select all

scp /path/to/local/file user@address:/path/to/remote/file
If so, doing this in python would be:

Code: Select all

from subprocess import call
local_file = "/path/to/local/file"
remote_user = "user"
remote_address = "address"
remote_path = "/path/to/remote/file"

remote_destination = "{}@{}:{}".format(remote_user, remote_address, remote_path)

cmd = ["scp", local_file, remote_destination]

call(cmd)
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 02, 2015 12:12 pm

elParaguayo wrote:
2. How do i do all this activity through python script? i want to just give the path and press a button to transfer file.
Good question. I don't use python. I think it has something called "system" that can be used to run commands. Probably some examples on the forum.
Use the "subprocess" module (this is recommended in place of using os.system that you'll also see people using).

My experience of scp is light but, if I recall, the format for copying a local file to remote is

Code: Select all

scp /path/to/local/file user@address:/path/to/remote/file
If so, doing this in python would be:

Code: Select all

from subprocess import call
local_file = "/path/to/local/file"
remote_user = "user"
remote_address = "address"
remote_path = "/path/to/remote/file"

remote_destination = "{}@{}:{}".format(remote_user, remote_address, remote_path)

cmd = ["scp", local_file, remote_destination]

call(cmd)
Thank you very much rpdom!! that works when i manually type scp command!!

but when i run scp through python script , it asks for password again :(

@elParaguayo : can you please help too

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 02, 2015 12:35 pm

That seems odd.

Googling for python and scp, I've seen a few suggestions of using subprocess.Popen rather then subprocess.call.

I don't see any reason why this should make a difference but try:

Code: Select all

from subprocess import Popen

... rest of code ...

#call(cmd)
proc = Popen(cmd).wait()

# proc will have the return code from the command once it's finished.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 02, 2015 4:16 pm

I wil try this out tomorrow morning, its late night here :(
i will let you know after this

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Transfer .txt between two RPi (no internet,no pc)

Tue Nov 03, 2015 9:25 am

Actually, I wouldn't bother trying it!

Looking at the source code for the subprocess module, subprocess.call(cmd) is just a shortcut for subprocess.Popen(cmd).wait(). So my suggestion above won't make any difference.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

AjitPi
Posts: 18
Joined: Sat Oct 31, 2015 10:45 am

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 09, 2015 5:09 am

Ya the command still asking for password when i do it through python script :(

is there a way that i enter the password through the script when it asks for? i am exploring pexpect but cant find how to exactly write the code and use it

User avatar
rpdom
Posts: 17189
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Transfer .txt between two RPi (no internet,no pc)

Mon Nov 09, 2015 5:36 am

I don't understand why it would ask for the password when run from a python script instead of running from the command line. :?:

Oh... hang on... are you running the script with sudo? If so, the scp command will be run from the root user, not the pi user. It'll be using the credentials in /root/.ssh/ instead of /home/pi/.ssh/

Return to “Python”