Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to combine data taken from server between client and GUI?
#1
I'm reaching a server which takes distance data from ultrasonic sensor. (Server was created at another side with Arduino.) I have to show this data on Raspberry Pi, so I want to do GUI. I have two .py file.

1) Client module

2) GUI

I want to take data on client over GUI code. Data will be taken from URL WebPage (TCP/IP)
Where/How I should add data taken from server to GUI code? (Embedding client. py to GUI.py)
Any suggestions would be greatly appreciated

Client.py

import socket
HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE 

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)
GUI.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 30, 600, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.labelDistance = QtWidgets.QLabel(self.centralwidget)
        self.labelDistance.setGeometry(QtCore.QRect(150, 150, 400, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelDistance.setFont(font)
        self.labelDistance.setAlignment(QtCore.Qt.AlignCenter)
        self.labelDistance.setObjectName("labelDistance")
        self.labelcm = QtWidgets.QLabel(self.centralwidget)
        self.labelcm.setGeometry(QtCore.QRect(150, 150, 700, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelcm.setFont(font)
        self.labelcm.setAlignment(QtCore.Qt.AlignCenter)
        self.labelcm.setObjectName("labelcm")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Distance Measurement"))
        self.label.setText(_translate("MainWindow", "Obsctacle Distance"))
        self.labelDistance.setText(_translate("MainWindow", "0"))
        self.labelcm.setText(_translate("MainWindow", "cm"))
        
When GUI code runs: (0 is default value)

İmage
Quote
#2
If you want to keep client.py in a separate module, you need to provide some sort of interface to the data.
You can do this by wrapping your code in a function, and returning data, or by making client.py a class and exposing data
for outside use in the __init__ method, by defining data as self.data.

as a class:
Client.py
import socket

class Client:
    def __init__(self):
        self.HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
	    self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE

    def transfer_data(self):
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
        self.data = s.recv(1024)

    
# To try run stand-alone
if __name__ == '__main__':
    cl = Client()
    cl.transfer_data()
    print(cl.data)
as a function:
import socket
HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE 
 
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    return s.recv(1024)
Now in the GUI class:
import Client

# and add to GUI.py
class Ui_MainWindow(object):
    def __init__(self):
        self.client = Client.Client()
        self.cdata = self.client.transfer_data()
# then when needed:
        data = self.client.data()
A better way would to have a main module that imports both GUI and Client
and interfaces externally.
Quote
#3
(Apr-01-2019, 09:10 AM)Larz60+ Wrote: A better way would to have a main module that imports both GUI and Client
and interfaces externally.

Yes i want to create a main module that includes GUI and Client. And also, data should be taken dynamically and GUI should show dynamically, you estimate that distance is always changing.
Quote
#4
Quote:as a class:
Client.py
import socket

class Client:
    def __init__(self):
        self.HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
	    self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE

    def transfer_data(self):
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
        self.data = s.recv(1024)

    
# To try run stand-alone
if __name__ == '__main__':
    cl = Client()
    cl.transfer_data()
    print(cl.data)

İmage


I tried. What should i do ?
Quote
#5
Quote:I tried. What should i do ?
show what you've tried
Quote
#6
Quote:show what you've tried

I attached a photo to previous reply. Don't view it ? I'm adding code now.

import socket
 
class client:
    def __init__(self):
        self.HOST = '192.168.1.55'  # The server's hostname or IP address, NodeMCU IP Address
        self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE
 
    def transfer_data(self):
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
        self.data = s.recv(1024)
 
# To try run stand-alone
if __name__ == '__main__':
    cl = client()
    cl.transfer_data()
    print(cl.data)
    
Error:
> & "C:/Program Files/Python37/python.exe" c:/Users/User/Desktop/PYQT/client.py Traceback (most recent call last): File "c:/Users/User/Desktop/PYQT/client.py", line 16, in <module> cl.transfer_data() File "c:/Users/User/Desktop/PYQT/client.py", line 9, in transfer_data s.connect((HOST, PORT)) NameError: name 's' is not defined
Quote
#7
My mistake:
import socket
 
class Client:
    def __init__(self):
        self.HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
        self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE
 
    def transfer_data(self):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((HOST, PORT))
            s.sendall(b'Hello, world')
            self.data = s.recv(1024)
 
     
# To try run stand-alone
if __name__ == '__main__':
    cl = Client()
    cl.transfer_data()
    print(cl.data)
bescf likes this post
Quote
#8
I added HOST and PORT definition, because when there isnt them, it gave an error.
import socket
HOST = '192.168.1.62'
PORT = 80 

class client:
    def __init__(self):
        self.HOST = '192.168.1.62'  # The server's hostname or IP address, NodeMCU IP Address
        self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE
 
    def transfer_data(self):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((HOST, PORT))
            s.sendall(b'Hello, world')
            self.data = s.recv(1024)
 
# To try run stand-alone
if __name__ == '__main__':
    cl = client()
    cl.transfer_data()
    print(cl.data)
I could take output, thanks your solution. Can you share with me cause of "b" ? When i delete it, running was unsuccesful.
Output:
& "C:/Program Files/Python37/python.exe" c:/Users/User/Desktop/PYQT/client.py b'0'

(Apr-01-2019, 09:10 AM)Larz60+ Wrote: Now in the GUI class:
import Client

# and add to GUI.py
class Ui_MainWindow(object):
    def __init__(self):
        self.client = Client.Client()
        self.cdata = self.client.transfer_data()
# then when needed:
        data = self.client.data()
A better way would to have a main module that imports both GUI and Client
and interfaces externally.

Actually, i want to create as a function. But i tried as a class.
from PyQt5 import QtCore, QtGui, QtWidgets
import client 

class Ui_MainWindow(object):

    def __init__(self):
        self.client = client.client()
        self.cdata = self.client.transfer_data()
# then when needed:
        data = self.client.data()
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 30, 600, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.labelDistance = QtWidgets.QLabel(self.centralwidget)
        self.labelDistance.setGeometry(QtCore.QRect(150, 150, 400, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelDistance.setFont(font)
        self.labelDistance.setAlignment(QtCore.Qt.AlignCenter)
        self.labelDistance.setObjectName("labelDistance")
        self.labelcm = QtWidgets.QLabel(self.centralwidget)
        self.labelcm.setGeometry(QtCore.QRect(150, 150, 700, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelcm.setFont(font)
        self.labelcm.setAlignment(QtCore.Qt.AlignCenter)
        self.labelcm.setObjectName("labelcm")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Distance Measurement"))
        self.label.setText(_translate("MainWindow", "Obstacle Distance"))
        self.labelDistance.setText(_translate("MainWindow", "0"))
        self.labelcm.setText(_translate("MainWindow", "cm"))
        

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Error:
PS C:\Users\User> & "C:/Program Files/Python37/python.exe" c:/Users/User/Desktop/PYQT/GUI.py Traceback (most recent call last): File "c:/Users/User/Desktop/PYQT/GUI.py", line 64, in <module> ui = Ui_MainWindow() File "c:/Users/User/Desktop/PYQT/GUI.py", line 10, in __init__ data = self.client.data() TypeError: 'bytes' object is not callable


When i try as a function.

from PyQt5 import QtCore, QtGui, QtWidgets
import socket
HOST = '192.168.1.62'  # The server's hostname or IP address
PORT = 80        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(259, 124)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 10, 211, 41))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.labelDistance = QtWidgets.QLabel(self.centralwidget)
        self.labelDistance.setGeometry(QtCore.QRect(60, 50, 71, 41))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelDistance.setFont(font)
        self.labelDistance.setAlignment(QtCore.Qt.AlignCenter)
        self.labelDistance.setObjectName("labelDistance")
        self.labelcm = QtWidgets.QLabel(self.centralwidget)
        self.labelcm.setGeometry(QtCore.QRect(120, 50, 71, 41))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelcm.setFont(font)
        self.labelcm.setAlignment(QtCore.Qt.AlignCenter)
        self.labelcm.setObjectName("labelcm")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Distance Measurement"))
        self.label.setText(_translate("MainWindow", "Obsctacle Distance"))
        self.labelDistance.setText(_translate("MainWindow", "0"))
        self.labelcm.setText(_translate("MainWindow", "cm"))
        # User Code
        self.check_event()

    def check_event(self):
                self.labelDistance.setText(data)
            

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Error:
PS C:\Users\User> & "C:/Program Files/Python37/python.exe" c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py Traceback (most recent call last): File "c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py", line 79, in <module> ui.setupUi(MainWindow) File "c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py", line 58, in setupUi self.retranslateUi(MainWindow) File "c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py", line 68, in retranslateUi self.check_event() File "c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py", line 71, in check_event self.labelDistance.setText(data) TypeError: setText(self, str): argument 1 has unexpected type 'bytes'
As you see, I dont sure what I do.
Quote
#9
line 10:
data = self.client.data()
should not contain ()
the () at the end tells python to execute a method or function.
Here you just want to fetch the data, so:
data = self.client.data
Looks like there are other variables this is done with also
bescf likes this post
Quote
#10
Thanks for your help, i found another solution :) Now, i can take data from IP and show it. But, this is not dynamic. I see constant value. When distance measurement on IP change, display widget should show new data result. On my program, you can next distance result, only when run python code again. My aim is that GUI should have refresh screen. How it is possible ? (I mean that it should be updating like a clock.)

İmage
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Slow websockets server -> client pattagghiu 0 159 Sep-17-2019, 09:28 AM
Last Post: pattagghiu
  Chat (Client-Server) andresdrr 3 284 Aug-26-2019, 02:01 PM
Last Post: ThomasL
  Python server(Django web site)/client(Python app) connection Junior_Pythoneer 5 379 Jul-05-2019, 05:41 PM
Last Post: noisefloor
  tcp server/client port connection issue valshev 2 2,006 Sep-01-2018, 02:50 PM
Last Post: valshev
  Send data BMP180 between client and server trought module socket smalhao 0 733 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Sending/Receiving Multiple Message from Server or Client Lyperion 0 1,164 Jul-30-2018, 07:52 AM
Last Post: Lyperion
  Need help to make a server/client for DNS Eddie05 1 921 Jun-12-2018, 10:04 AM
Last Post: j.crater
  Simple send and recive string Server/Client Epilepsy 1 1,059 May-01-2018, 08:17 PM
Last Post: ThiefOfTime
  python socketio-client-2 and nod js socketio server communication is not success vmyadhu2 2 3,499 Oct-02-2017, 04:22 AM
Last Post: vmyadhu2
  socket server with SSL accepting unencrypted data kopite 0 1,241 Apr-20-2017, 08:31 AM
Last Post: kopite

Forum Jump:


Users browsing this thread: 1 Guest(s)