-1

I am receiving live data on PC(7-10 instances per second), I am processing the data and want to send this data to a Raspberry pi 4, and based on the data received on RB_Pi I trigger signals. Can anyone suggest to me, which communication can be used to send the data live from PC to RB_Pi using Python?

Let me know If any additional info is required.

The live data is below:

One is String and its respective float value

Ashwath
  • 65
  • 6
  • What is a *"live data"* please? A byte? A 4-byte integer? A 6MB 1080p video frame? – Mark Setchell Jan 15 '22 at 17:51
  • I have added the live data now – Ashwath Jan 16 '22 at 12:24
  • Are they connected to the internet? Then just use a socket? I don't understand what you're asking here. – seven_seas Jan 16 '22 at 12:32
  • Is that one *"Live Data"* or two or the live data for a second, or for a minute? Surely you don't want to transmit something as bulky as this with strings and classes and `push`? Surely you would send 12 floats packed in network byte order for just 48 bytes rather than all that? Please try to be clearer about what you actually want to send and how big it is and how frequently you want to send it and how the PC and Raspberry Pi are connected - Wifi, Ethernet, Bluetooth, serial? – Mark Setchell Jan 16 '22 at 12:33
  • I want to send a small value of 1 byte continuously every second to RB_pi. – Ashwath Jan 17 '22 at 10:38

2 Answers2

1

Probably the easiest way of doing this is using socket. It's pretty low-level. You could probably do something like this:

# Server #
import socket

class Server:
    HOST = '0.0.0.0'
    PORT = 12345      
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.bind((self.HOST, self.PORT))    

    def accept(self):
        self.sock.listen()
        c, a = self.sock.accept()
        self.rpi = c
        self.send()

    def send(self):
        self.rpi.send(YOUR_DATA.encode())

s = Server()

And the client should look something like this:

#!/usr/bin/env python3
# Client #
import socket

class Client:
    HOST = "192.168.x.x" # Your IP of course
    PORT = 12345
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.HOST, self.PORT))
        self.recv()        

    def recv(self):
        while True:
            data = self.sock.recv(1024).decode()
            print(data)

c = Client()

Please note, that this is a very primitive script. You should also handle exceptions and the class structures could be better.

Wrench56
  • 290
  • 2
  • 14
0

I don't actually have experience with Raspberry pi, just with Arduino. But, I usually use serial communication for that.

import serial
ser = serial.Serial(SERIAL_PORT, BAUDRATE, timeout=0.1)
ser.write(data)

SERIAL_PORT as String

BAUDRATE as Int

example: serial.Serial("COM4", 9600, timeout=0.1)

jps
  • 20,041
  • 15
  • 75
  • 79
  • Yes this will help for wired connection in Arduino. Which I have implemented. Now I want to send the data in wireless communication to RB_Pi. – Ashwath Jan 16 '22 at 12:23
  • @Ashwath you could also use the serial communication for the Raspberry Pi as well. – Wrench56 Jan 16 '22 at 18:54