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 = '[Link]' # Replace with your server address
SERVER_PORT = 8888 # Replace with your server port
# Create a socket object
sock = [Link](socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the server address and port
[Link]((SERVER_ADDRESS, SERVER_PORT))
# Listen for incoming connections
[Link]()
print(f'Server listening on {SERVER_ADDRESS}:{SERVER_PORT}...')
# Accept incoming connections
conn, addr = [Link]()
print(f'Client connected from {addr}...')
# Serve temperature data
while True:
# Wait for client request
data = [Link](1024)
# If client sends 'get_temp', send back a random temperature value
if data == b'get_temp':
temp = round([Link](20, 30),2)
[Link](str(temp).encode('utf-8'))
# If client sends anything else, close the connection
else:
[Link]()
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
[Link])
import serial
import time
# Define server address and port
SERVER_ADDRESS = '[Link]' # 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
[Link](b'get_temp')
# Read temperature value from server
temp = [Link]().decode('utf-8').strip()
# Print temperature value
print(f'Temperature: {temp} °C')
# Wait for 1 second before reading temperature again
[Link](1)
▪ Also identify the difference with and without [Link] while
reading the temperature in the client program.
Client program with [Link]
import socket
import io
import time
# Define server address and port
SERVER_ADDRESS = '[Link]' # Replace with your server address
SERVER_PORT = 8888 # Replace with your server port
# Create a socket object and connect to the server
sock = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link]((SERVER_ADDRESS, SERVER_PORT))
# Create a text stream object using the socket connection
sock_file = [Link]('rwb')
text_stream = [Link](sock_file)
# Read temperature every second from server
while True:
# Send request to server to get temperature
[Link](b'get_temp')
# Read temperature value from server
temp_data = [Link](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
[Link](1)
In the client code we have written using [Link], 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 [Link], 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 [Link] 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