wayner
Posts: 84
Joined: Thu Oct 25, 2012 1:40 am
Location: Toronto, Ontario, Canada

What is the best way to send data from one Pi to another?

Fri May 22, 2015 1:20 pm

Let's say I have two RPis, Apple and Banana. Both Apple and Banana are running Apache (if that helps). Banana has a DS18B20 sensor and I want Apple to be able to get that data. What is the easiest way in Python or a shell script form Apple to get that data reading from Banana?

henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 1:25 pm

If it were me......I am a novice mind you, I would send it via serial data either through the GPIO or a USB serial convertor.
This assumes that they are relatively close to each other < 100 ft.
Henry

wayner
Posts: 84
Joined: Thu Oct 25, 2012 1:40 am
Location: Toronto, Ontario, Canada

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 1:29 pm

Surely there are better ways than that which stay in the digital realm. Like Banana posting the info to a file that is accessible via Apache. Then Apple could just read the contents of that file into a variable using Python. But there have to be better ways using stuff like TCP sockets.

User avatar
PeterO
Posts: 5717
Joined: Sun Jul 22, 2012 4:14 pm

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 1:38 pm

There are lots of better ways, especially ones that work ! (You can't use USB as PI can only act as the host end not the device end)

To me the simplest way is to use network sockets:
https://docs.python.org/2/library/socketserver.html
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

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

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 1:41 pm

You could use OWFS and one of the OWFS server options.
Last edited by ame on Sat May 23, 2015 9:53 am, edited 1 time in total.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 2:13 pm

just to add an option, MQTT :)

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

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 2:13 pm

One IoT way of doing this stuff is MQTT using Mosquitto as the MQTT broker.

http://MQTT.org
http://mosquitto.org

That's easier than Peter's socket programming (because you're working to an API not designing your own protocol) and it's quite versatile (MQTT supports a one-to-one, one-to-many, many-to-one and many-to-many publisher and subscriber model).

The latest version of Mosquitto has support for websockets so driving it from a browser/web server got even easier.
Last edited by DougieLawson on Fri May 22, 2015 2:15 pm, edited 1 time in total.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

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

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 2:14 pm

I want to underline that my post is BEFORE dougie's one

:lol:

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

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 2:15 pm

Massi wrote:I want to underline that my post is BEFORE dougie's one

:lol:
We just need to get Peter O. converted to our cause.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

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

User avatar
PeterO
Posts: 5717
Joined: Sun Jul 22, 2012 4:14 pm

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 2:30 pm

DougieLawson wrote:
Massi wrote:I want to underline that my post is BEFORE dougie's one

:lol:
We just need to get Peter O. converted to our cause.
Never, because as normal everyone is proposing hugley overkill solutions.

Dougie, did you actually look at the page I linked to ? A TCP client and server are trivial, and your comment "designing your own protocol" shows you have not actually though about what is required to use a TCP socket to meet the OP's requirements.

There is no protocol. Simply making the TCP connection to banana is sufficient, then all it has to do is write the data to the socket and reap the socket when it is closed by the client. It really is trivial. No need to fart about with messaging protocol APIs for somethign this simple.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

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

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 2:38 pm

OK, use nc (aka netcat) and do it with zero programming. There is a protocol whatever you might think because the sender and receiver have to agree between them what their splurge of bytes coming down the line mean.

But for an extensible solution at the application layer rather than the transport layer then MQTT is a good choice.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

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

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 2:41 pm

DougieLawson wrote:The latest version of Mosquitto has support for websockets so driving it from a browser/web server got even easier.
tell me more, master!

User avatar
PeterO
Posts: 5717
Joined: Sun Jul 22, 2012 4:14 pm

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 2:51 pm

DougieLawson wrote:OK, use nc (aka netcat) and do it with zero programming. There is a protocol whatever you might think because the sender and receiver have to agree between them what their splurge of bytes coming down the line mean.
That pretty much summs up what is wrong with your suggestion. You can't think of a solution that is so simple that it sends the temperature as a string. "Splurge of bytes" suggests you think huge ammounts of data need to be exchanged to make this work.

But for an extensible solution at the application layer rather than the transport layer then MQTT is a good choice.
Yeah, lets confuse the OP by talking about "layers"... For goodness sake, all they want to transfer a single number between two hosts and you're worrying about using an "extensible application layer protocols". Take off your MQTT evangalist goggles that make every thing look like a MQTT problem, read the OP's question (where they already asked about using TCP sockets) and remember what it was like to learn the fundamental stuff before moving on to more complex solutions.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 3:00 pm

since we are just adding possibilities, even a web service would be an option. Maybe also easier than creating a socket with python.

wayner
Posts: 84
Joined: Thu Oct 25, 2012 1:40 am
Location: Toronto, Ontario, Canada

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 3:02 pm

Can anyone point me to an example of some code that shows an example of Pi to Pi communication using sockets or whatever solution.

The other specifications that I have to think about are a solution that is robust to the sender of the data being not available and low power usage (of CPU intensity) as the sender will be solar/battery powered.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 3:11 pm

wayner wrote:Can anyone point me to an example of some code that shows an example of Pi to Pi communication using sockets or whatever solution.

The other specifications that I have to think about are a solution that is robust to the sender of the data being not available and low power usage (of CPU intensity) as the sender will be solar/battery powered.
did you look at this?
https://docs.python.org/2/library/socketserver.html

there's also a "example" section..

You should also decide which pi will be the server..

andybill
Posts: 7
Joined: Tue Apr 07, 2015 1:03 pm

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 3:18 pm

probably a far too simplistic approach, but why not just have the sensor log text into the docroot and use wget?

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 3:20 pm

andybill wrote:probably a far too simplistic approach, but why not just have the sensor log text into the docroot and use wget?
viewtopic.php?f=63&t=111220&view=unread#p763225

andybill
Posts: 7
Joined: Tue Apr 07, 2015 1:03 pm

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 3:32 pm

Massi wrote:
andybill wrote:probably a far too simplistic approach, but why not just have the sensor log text into the docroot and use wget?
viewtopic.php?f=63&t=111220&view=unread#p763225
OP mentions shell as well as Python :)
More seriously: if the requirement is simply to read a single value, why would MQTT etc be a better approach for that task? Obviously possibilities for extending it and so on, but for now? Is the scope likely to extend, or is it only ever just going to be "read this value"?

Having just spent a week reading 1980s-era serial protocol specs, all i can say is simple = good, flexible = very very bad when it comes to reverse engineering things 30 years later :-) :-)

MarkTF
Posts: 314
Joined: Tue Mar 03, 2015 4:59 pm

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 6:40 pm

For what it's worth, I did essentially this in a bash script that periodically reads the time and temperature, logs that data to a file, and uses "scp" to transfer the file to a remote computer. It required setting up ssh keys on both machines, but it's only a few lines of code. If one needs the server side to promptly do something when the data is sent this probably isn't the most elegant approach, but I was just interested in logging to a computer that was visible outside of my home network.

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

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 7:31 pm

For my setup (which does a load of other things as well as temperature), I use a MySQL database. The units with the sensors store the readings into the database with timestamp, sensor_id, reading, and the web page reads the required data from the database.

I have considered a few other ways of doing it, the most likely candidate was the sockets option as suggested above.

User avatar
B.Goode
Posts: 9831
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: What is the best way to send data from one Pi to another

Fri May 22, 2015 8:23 pm

wayner wrote:Can anyone point me to an example of some code that shows an example of Pi to Pi communication using sockets or whatever solution.
https://www.raspberrypi.org/learning/ne ... g-lessons/

Heater
Posts: 15378
Joined: Tue Jul 17, 2012 3:02 pm

Re: What is the best way to send data from one Pi to another

Sat May 23, 2015 9:20 am

wayner,

Here is a way to send your sensor data from one Pi to another over a network connection and have the received sensor data served up as a web page. All in about 40 lines of code. No Apache or other nonsense required.

Just install node.js as described here: http://weworkweplay.com/play/raspberry-pi-nodejs/
Save this code as someFile.js
Run it with "node someFile.js"
In your web browser visit http://localhost:9000

Code: Select all

"use strict";

var HOST = '127.0.0.1';

var HTTP_PORT = 9000;
var TCP_PORT = 9001;

//----------------------- The Web server ---------------------------------------
var http = require('http');

var pageStart = '<html><head> <meta http-equiv="refresh" content="1">  </head><body>';
var pageText  = 'Sensor reading is: ';
var pageEnd   = '</body></html>';

var receivedSensorData = 'No reading yet';

http.createServer(function (req, res) {
    res.write(pageStart);
    res.write(pageText);
    res.write(receivedSensorData);
    res.write(pageEnd);
    res.end();
}).listen(HTTP_PORT);


//----------------------- The TCP/IP server-------------------------------------
var net = require('net');
var server = net.createServer();

server.listen(TCP_PORT, HOST);
console.log('Server listening on ', HOST, TCP_PORT);
server.on('connection', function (socket) {
    console.log('CONNECTED: ' + socket.remoteAddress + ':' + socket.remotePort);

    socket.on('data', function (data) {
        console.log('DATA: ' + data);
        receivedSensorData = data;
        socket.destroy();
    });
});


//----------------------- The TCP/IP client ------------------------------------
var client = new net.Socket();

function send(data) {
    client.connect(TCP_PORT, HOST, function () {
        console.log('CONNECTED TO: ' + HOST + ':' + TCP_PORT);
        client.write(data);
        client.destroy();
    });
}


//----------------------- The sensor reading and sending -----------------------
// Some reading from some sensor.
var sensorData = 42;

// Every second, read sensor data and send it to a remote server.
setInterval(function () {
    send(sensorData.toString());
    sensorData += 1;
}, 1000);
Memory in C++ is a leaky abstraction .

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

Re: What is the best way to send data from one Pi to another

Sat May 23, 2015 9:56 am

Heater wrote:wayner,

Here is a way to send your sensor data from one Pi to another over a network connection and have the received sensor data served up as a web page. All in about 40 lines of code. No Apache or other nonsense required.
OWFS server will do this. No code required.

Heater
Posts: 15378
Joined: Tue Jul 17, 2012 3:02 pm

Re: What is the best way to send data from one Pi to another

Sat May 23, 2015 11:21 am

OWFS might be cool. Never heard of it before.

A quick google tells me its a way to get one-wire connected devices to show up as a file system and/or get that data out over the net. http://owfs.org/

My first impression is that it would take longer for me to install and configure OWFS than create some simple code like the above.

I like playing with code :)
Memory in C++ is a leaky abstraction .

Return to “General discussion”