So i have a pi3b+ and a zero hooked up to a tft ili9341 display. I can run pygame on boot and it works fine. I have also started using python sockets and successfully sent data (my wlan0 ip) from my 3b+ to my pi0 and display it in console. Lately I have been struggling to get that ip variable sent to my pygame to display it. All the scripts work fine alone, its only on the boot when pygame fails, when i have imported my socket file. Also tried making one big pygame script with the socket reciever but still had no success. Ill post my scripts down below maby someone could suggest something. So the sender script is on my 3b+ and the other two are on my pi0, both in the same directory.

First Script the sender:

import socket
import os
HOST = ''
PORT = 65432

A = os.popen("ifconfig wlan0 | grep inet | cut -d: -f2 | awk '{print $2}'").read()
B = bytes(A, encoding='utf-8')

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        data = s.recv(1024)
#print('Recieved', repr(data))

Second script that Recieves:

import os
import socket 

HOST = ''
PORT = 65432

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
conn, addr = s.accept()

data = conn.recv(1024)
#print('Connected by', addr)
A = repr(data)

And pygame:

import pygame
import os 
import socket
import sock

green = (0,255,0)
white = (255,255,255)
blue = (0,0,255)
red = (255,0,0)
black = (0,0,0)

#A = ([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('', 53)), s.getsockname()[0],$


B = sock.A

screen = pygame.display.set_mode((680,420))
done = False
font = pygame.font.Font('freesansbold.ttf', 25)
text = font.render("LOCAL IP:", True, white, black)
ip = font.render(B,True,white,black)
textip = text.get_rect()
textip.center = (250,50)
textRect = text.get_rect()
textRect.center = (150,50)

while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True

        screen.blit(ip, textip)
        screen.blit(text, textRect)
        pygame.draw.rect(screen, (255,0,255), pygame.Rect(30,30,60,60))

And rc.local:

fbcp &
#python3 /home/pi/sock.py &
python3 /home/pi/pygametest.py

exit 0
  • Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. – Ingo May 31 '19 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.