Posts: 1
Joined: Sun Feb 11, 2018 10:07 pm

Python TCP/UDP Server-Client for controlling RPi

Sun Feb 11, 2018 10:15 pm

Hello, as first please note I am beginning with Python and so on. Its just my hobby after work and it's just a while when i started. I created RPi car (Raspberry Zero W and motor control via L298N Bridge). I am planning control 2 motors (moving like a tank). The server UTP or UDP running on Raspberry Pi and Client running for example on Laptop. I want to server recieve from client. I want to client "logging" keyboard and if the key (we can tell "W" is pressed it sends information to server and server turn on GPIO pins for moving forward). But i got stuck in client. Just for test client create console in which you can write number 1 or 3 to turn on or turn off GPIO pins (but it dont work - console shutdown after writing a number and nothing happen). So if somebody know how I want to help create Client and improve Server to work together. Client will accepts keys like W,A,S,D for moving and send it via TCP or UDP to server. Please help. Thank you for all your replies.

*I attached my try for test (dont work)
(770 Bytes) Downloaded 281 times

User avatar
Posts: 6720
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Python TCP/UDP Server-Client for controlling RPi

Mon Feb 12, 2018 11:31 am

The code is not bigger than you can post it using the correct tags:

Code: Select all

import socket
import RPi.GPIO as GPIO
# GPIO Setting Up
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)

# Create a Server Socket and wait for a client to connect
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('', 6666))
print ("UDPServer Waiting for client on port 6666")

# Define moving functions

def FW():
    print ("Forward")

def STOP():
    print ("Stop")

options = {    "0" : FW,
               "3" : STOP,

# Recive data from client and decide which function to call
while True:
    dataFromClient, address = server_socket.recvfrom(256)

Code: Select all

import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while 1:
     data = input("Enter Data :")
# IPADRESS = RPi IP address
# 6666 = Number Port
     client_socket.sendto(data, ("IPADRESS",6666))
     print ("Sending request")

except Exception as ex:
    print ex

There is no danger in writing the LAN address of your Pi in the code. Its probably in the private "192.168.x.y" range as 95% of our own networks.
Also I would add some output to the while loop in the server so you can see if you receive something or not. In general I'm a fan of adding print statements in my code so that I can verify that the flow is what I intended.

Posts: 1
Joined: Thu Nov 09, 2017 4:51 pm

Re: Python TCP/UDP Server-Client for controlling RPi

Mon Apr 09, 2018 3:07 pm

Works if you strip the newline character ...

Code: Select all

dataFromClient, address = server_socket.recvfrom(256)
dataFromClient = dataFromClient.rstrip()

Return to “Python”