Python Forum
how to split socket data onto multiple clients for separate processing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to split socket data onto multiple clients for separate processing
#1
i am controlling 4 stepper motors as a mecanum robot in python socket to esp8266 arduino with a limited amount of pins. i would like to add a pair of servos for a camera gimbal but it will have to be controlled by my raspberry pi because i have no spare pins on the esp8266.

i have the server running pygame joystick and will need to split the controller data to a second client. i see two issues here. firstly how do i run a pair of clients on the same server with each their own dedicated tasks? and second how do i differentiate the two clients so they receive the right data?

below is my current server script that only controls the 4 stepper motors. how do i start adding the two servo controls for my raspberry pi? currently using left joystick and both triggers on my gamepad and would like to use the right joystick for the camera gimbal

import socket
import pygame
from pygame.locals import *

m1x = 0.0
m2x = 0.0
m3x = 0.0
m4x = 0.0
m1y = 0.0
m2y = 0.0
m3y = 0.0
m4y = 0.0
m1rx = 0.0
m2rx = 0.0
m3rx = 0.0
m4rx = 0.0
value_tl = 0.0
value_tr = 0.0
pause_state = False
connected = False
m1 = 15000
m2 = 15000
m3 = 15000
m4 = 15000
startline = '<'
finishline = '>'

pygame.init()
clock = pygame.time.Clock()
pygame.joystick.init()
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joystick in joysticks:
    print(joystick.get_name())

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostbyname(socket.gethostname()), 1324))
s.listen()
print('listening')
client, address = s.accept()
print(f'Connected from {address}')
connected = True

if 'FrSky' in joystick.get_name():
    while True:
        pass
else:
    while True:
        joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
        for event in pygame.event.get():
            if event.type == JOYAXISMOTION:
                if event.axis == 0:
                    value_x = event.value
                    m1x = value_x
                    m2x = value_x * (-1)
                    m3x = value_x * (-1)
                    m4x = value_x
                if event.axis == 1:
                    value_y = event.value * (-1)
                    m1y = value_y
                    m2y = value_y
                    m3y = value_y
                    m4y = value_y
                if event.axis == 2:
                    value_rx = event.value
                    m1rx = value_rx * (-1)
                    m2rx = value_rx * (-1)
                    m3rx = value_rx
                    m4rx = value_rx
                if event.axis == 4:
                    value_tl = round(event.value + 1, 4)
                if event.axis == 5:
                    value_tr = round(event.value + 1, 4)
            if event.type == JOYBUTTONDOWN:
                if event.button == 7:
                    pause_state = not pause_state
            value_t = ((value_tr) - (value_tl)) / 2
            m1t = value_t * (-1)
            m2t = value_t * (-1)
            m3t = value_t
            m4t = value_t
            m1 = round((m1x + m1y + m1t + 2) * 15000, 0)
            m2 = round((m2x + m2y + m2t + 2) * 15000, 0)
            m3 = round((m3x + m3y + m3t + 2) * 15000, 0)
            m4 = round((m4x + m4y + m4t + 2) * 15000, 0)
        if pause_state == False:
            try:
                print(f'm 1 {m1} 2 {m2} 3 {m3} 4 {m4}')
                data = (f'{startline}{m1},{m2},{m3},{m4}{finishline}')
                data = bytes(data, 'utf-8')
                client.send(data)
            except ConnectionAbortedError:
                print('client aborted')
                connected = False
                while not connected:
                    try: 
                        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        s.bind((socket.gethostbyname(socket.gethostname()), 1324))
                        s.listen()
                        print('listening')
                        client, address = s.accept()
                        print(f'Connected from {address}')
                        connected = True
                    except socket.error:
                        clock.tick(0.5)
            except ConnectionResetError:
                print('client lost')
                connected = False
                while not connected:
                    try: 
                        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        s.bind((socket.gethostbyname(socket.gethostname()), 1324))
                        s.listen()
                        print('listening')
                        client, address = s.accept()
                        print(f'Connected from {address}')
                        connected = True
                    except socket.error:
                        clock.tick(0.5)
                    
        else:
            print('pause')
            pause_data = (f'{startline}{15000},{15000},{15000},{15000}{finishline}')
            pause_data = bytes(pause_data, 'utf-8')
            client.send(pause_data)
            while pause_state:
                for event in pygame.event.get():
                            if event.type == JOYBUTTONDOWN:
                                if event.button == 7:
                                    pause_state = not pause_state
                                
        clock.tick(60)
client.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Socket to stream data and tranmit and revieve data kiyoshi7 0 2,340 Aug-11-2022, 10:52 PM
Last Post: kiyoshi7
  socket without blocking loop and automatically retrieve data from the port RubenP 3 3,487 Jun-21-2020, 10:59 PM
Last Post: Gribouillis
  TCP socket, data transnission Simba 4 3,225 Dec-07-2019, 03:15 PM
Last Post: Simba
  Chat box locking up with multiple clients running Clunk_Head 1 1,954 Nov-27-2019, 04:13 PM
Last Post: Clunk_Head
  Soft Access Point & Socket Data Streaming groger57 1 2,473 Aug-01-2019, 02:53 PM
Last Post: groger57
  Python Regex multiple search into separate variable? searching1 11 7,305 Jan-04-2019, 07:37 PM
Last Post: nilamo
  Multiple network socket servers? MuntyScruntfundle 1 2,470 Nov-13-2018, 03:46 PM
Last Post: wavic
  Send data BMP180 between client and server trought module socket smalhao 0 2,805 Jul-30-2018, 12:56 PM
Last Post: smalhao
  How to support multiple users with heavy data processing Gingmeister 3 2,993 Jun-19-2018, 05:00 PM
Last Post: DeaD_EyE
  Python socket : Error receive data quanglnh1993 1 12,952 Mar-14-2018, 11:59 AM
Last Post: avorane

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020