samuraihack_
Posts: 1
Joined: Wed Jun 20, 2018 5:22 pm

How to simultaneously send and receive data between a laptop and raspberry pi?

Wed Jun 20, 2018 5:29 pm

Hi everyone.

I am having a lot of trouble figuring out how to simultaneously send and receive data between a laptop and raspberry pi. In particular, I want to send thruster commands from the laptop to the raspberry pi WHILE CONCURRENTLY send sensor data from the raspberry pi to the laptop. This is all done via an Ethernet cable. Here is a photo to show you what I mean https://i.imgur.com/wQsmsUh.png.

I understand that this needs to be "asynchronous" and to use "asyncio" or something of the like, but I do not understand how to do this. Since I have two devices (laptop and pi) talking to each other, would I want to use the client-server model? That doesn't seem to work with asynchronous communication. Would I want to have two threads on each device, one for receiving and one for sending? Wouldn't that just be having a client and a server on each device?
If anyone has any guides or examples for this, that would be great. I am really lost and most of the help I have gotten has been pretty vague (e.g. use asyncio). Thanks, and have a great day!


User avatar
DougieLawson
Posts: 34166
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to simultaneously send and receive data between a laptop and raspberry pi?

Wed Jun 20, 2018 9:32 pm

MQTT with Mosquitto or RabbitMQ is what you're looking for.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

ghans
Posts: 7818
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: How to simultaneously send and receive data between a laptop and raspberry pi?

Wed Jun 20, 2018 9:33 pm

I think you should just use two threads, one sends data and other only recieves.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

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

Re: How to simultaneously send and receive data between a laptop and raspberry pi?

Thu Jun 21, 2018 8:02 am

I think having to use threads for some simple communications is overkill and unnecessary complexity.

Now a days I like to do these kind of things with NATS:

Code: Select all

c = pynats.Connection(verbose=True)
c.connect()

# Simple Publisher
c.publish('foo', 'Hello World!')

# Simple Subscriber
def callback(msg):
    print 'Received a message: %s' % msg.data

c.subscribe('foo', callback)
Short, sweet, sending and receiving. No threads.

https://nats.io/
https://github.com/mcuadros/pynats
https://github.com/nats-io/gnatsd

One can do much the same with MQTT. However NATS has some nice extra facilities if you need them.

Return to “Python”