Python Forum
Send data BMP180 between client and server trought module socket
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Send data BMP180 between client and server trought module socket
#1
I would like to send data of BMP180 sensor between a client and a server with two raspberry pi 3 being that the client when executing the command get the server will give the temperature value to the client. However with the code I build when I execute the get command, the server goes down.
I need some help for this topic.
My code is as follows:

Server:
Code: Select all

import socket
import Adafruit_BMP.BMP085 as BMP085
host = '10 .6.4.198 '# Specifies the IP address of our Server
port = 4840 # Port through which to go to carry out the communication, replace 12345 by the port that we want above 1024

sensor = BMP085

# Function setting of our server so we can call it
def setupServer ():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM, 0)
    # s = socket (domain, type, protocol)
    # AF_INET specifies that the desired domain is the Internet domain
    # SOCK_STREAM is the type you want and supported by the AF_INET domain
    print ("Socket Created.")

    # Print statement of socket not working
    try:
        s.bind ((host, port)) # binds our host and port
    except socket.error as msg:
        print (msg)
    print ("Socket bind complete.")
    print ("Waiting for data ......")
    returns

# Function setting of our server connection
def setupConnection ():
    s.listen (10) # Allows one connection at a time. You can change the value for the desired connections at the same time.
    conn, address = s.accept () #Configuration of connection and address and accepting whatever is listening
    print ("Connected to:" + address [0] + ":" + str (address [1])) # Str (Address [1]) converts the ip address to string
## print ("Connected to:" + address [1] + ":" + str (address [2]))
    return conn # returns connection value

def GET (): # retrieves this stored value that was previously specified
    reply = sensor # variable data storage
    return reply

def REPEAT (dataMessage): # repeats the message entered in the command
    reply = dataMessage [1]
    return reply

def dataTransfer (conn):
    #A big loop that sendsreceives data until told not to.
    while True:

        #Receive the data
        data = conn.recv (1024) # receive the data
        data = data.decode ('utf-8') # decoding data received in utf-8

        #Split the data such that you separate the command from the rest of the data.
        dataMessage = data.split ('', 1)
        command = dataMessage [0]
        if command == 'get':
            reply = GET ()
            print ("Temperature = {0: 0.2f * C" .format (sensor.read_temperature ()))
        elif command == 'repeat':
            reply = REPEAT (dataMessage)
        elif command == 'quit':
            print ("Our client has left us :(")
            break
        elif command == 'kill':
            print ("Our server is shutting down.")
            s.close ()
            break
        else:
            reply = 'Unknow Command'
        conn.sendall (str.encode (reply))
        print ("Data has been sent !!!!")
    conn.close ()

s = setupServer () # calls the setupServer

while True:
    try:
        conn = setupConnection () # get the connection from the setupServer function
        dataTransfer (conn)
    except:
        break
Client:

import socket
import time

host = '10 .6.4.198 '# Server IP Address
port = 4840 # Port of communication, must be above 1024
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM, 0) # s = socket.socket (addr_family, type)
#################### GRADES ############################# ##############################
#### TCP #####
# Communication in IPV4 protocol with TCP flow-based connection
# Computers establish a connection to each other and read / write data
# in a continuous stream of bytes --- as a file, most common case.
#### UDP ####
# If we had AF_INET6 would be communication with IPV6 protocol
# Had we SOCK_DGRAM we had communication based on UDP datagrams
#Computers send discrete packets (or messages) to each other.
# Each packet contains a collection of bytes, but each packet is separate and independent.
################################################## ####################################

s.connect ((host, port))

print ("<======== Connected to Server =========> \ n")
while True:  
    command = input ("Enter your command:")
    if command == 'quit':
        print ("Disconnected of Server")
        #Send EXIT request to other end
        s.send (str.encode (command))
        break
    elif command == 'kill':
        print ("Server shutdown")
        #Send KILL command
        s.send (str.encode (command))
        break
    s.send (str.encode (command))
    reply = s.recv (1024)
    print (reply.decode ('utf-8'))
s.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,280 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Listen TCP and send data to websockets neoboby 3 2,898 Feb-13-2023, 09:47 PM
Last Post: Vadanane
  trying to stream data from a websocket to client tomtom 2 1,981 Jan-12-2023, 03:01 PM
Last Post: Vadanane
  Socket to stream data and tranmit and revieve data kiyoshi7 0 2,340 Aug-11-2022, 10:52 PM
Last Post: kiyoshi7
  Client/Server proper finalizing transfer wolfman5874 1 1,422 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
  Socket server problem H84Gabor 0 1,207 Jun-21-2022, 12:14 AM
Last Post: H84Gabor
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,361 Jun-12-2022, 05:57 PM
Last Post: gpropf
  how to split socket data onto multiple clients for separate processing ConcealedFox70 0 1,905 Jan-11-2022, 08:26 PM
Last Post: ConcealedFox70
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Help with Websocket-Client Module - Infinite Loop CluelessChris 0 3,886 Apr-25-2021, 01:53 PM
Last Post: CluelessChris

Forum Jump:

User Panel Messages

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