You are on page 1of 4

CS2013

Machine to Machine Communication


Lab Assignment 7

Name: Tejaswi Samavedula Date: 02-03-2023


Roll Number: ME21B2003
1. Write python programs for Temperature monitoring system simulation.
(Packages requires: socket, random, serial, and io)
▪ Create a server program using TCP sockets to generate continuous
temperature values (generated using a random package).
import socket
import random

# Define server address and port


SERVER_ADDRESS = '127.0.0.1' # Replace with your server address
SERVER_PORT = 8888 # Replace with your server port

# Create a socket object


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the server address and port


sock.bind((SERVER_ADDRESS, SERVER_PORT))

# Listen for incoming connections


sock.listen()

print(f'Server listening on {SERVER_ADDRESS}:{SERVER_PORT}...')

# Accept incoming connections


conn, addr = sock.accept()
print(f'Client connected from {addr}...')

# Serve temperature data


while True:
# Wait for client request
data = conn.recv(1024)

# If client sends 'get_temp', send back a random temperature value


if data == b'get_temp':
temp = round(random.uniform(20, 30),2)
conn.sendall(str(temp).encode('utf-8'))

# If client sends anything else, close the connection


else:
conn.close()
print(f'Client disconnected from {addr}...')
break

▪ Create a client program using serial communication. The program needs to


read the temperature every second from the server. (without
io.TextIOWrapper)

import serial
import time

# Define server address and port


SERVER_ADDRESS = '127.0.0.1' # Replace with your server address
SERVER_PORT = 8888 # Replace with your server port

# Create a serial object


ser = serial.serial_for_url(f'socket://{SERVER_ADDRESS}:{SERVER_PORT}')

# Read temperature every second from server


while True:
# Send request to server to get temperature
ser.write(b'get_temp')

# Read temperature value from server


temp = ser.readline().decode('utf-8').strip()

# Print temperature value


print(f'Temperature: {temp} °C')

# Wait for 1 second before reading temperature again


time.sleep(1)

▪ Also identify the difference with and without io.TextIOWrapper while


reading the temperature in the client program.
Client program with io.TextIOWrapper
import socket
import io
import time

# Define server address and port


SERVER_ADDRESS = '127.0.0.1' # Replace with your server address
SERVER_PORT = 8888 # Replace with your server port

# Create a socket object and connect to the server


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER_ADDRESS, SERVER_PORT))

# Create a text stream object using the socket connection


sock_file = sock.makefile('rwb')
text_stream = io.TextIOWrapper(sock_file)

# Read temperature every second from server


while True:
# Send request to server to get temperature
sock.sendall(b'get_temp')

# Read temperature value from server


temp_data = sock.recv(1024).decode('utf-8').rstrip()
if temp_data:
temp = float(temp_data)
print(f'Temperature: {temp} °C')
else:
print('No response from server.')

# Wait for 1 second before reading temperature again


time.sleep(1)

In the client code we have written using io.TextIOWrapper, the difference in reading
temperature is that the bytes received from the server are automatically converted
to Unicode text data using the specified character encoding (UTF-8 in this case)
before being read by the client.

Without using io.TextIOWrapper, the bytes were manually converted to Unicode


text data using the decode() method of the bytes object. This can be error-prone if
the wrong character encoding is used or if the data is not in a valid format.

Using io.TextIOWrapper simplifies the process of reading text data over a network
socket by handling character encoding automatically. It also provides additional
functionality, such as buffering and line-ending conversion, that can make reading
and writing text data more efficient and convenient.

Output

Server
Client

Both Server and Client output

Server Client

You might also like