WiFi TCP/IP Model for IoT Devices

Created:  
Updated:   15Aug2024   05:54:25   UTC 2024-08-15T05:54:25Z
Rating:   (0 reviewsThis article has not been rated yet)

The TCP/IPTCP/IP is a suite of internet communication protocols. TCP (Transmission Control Protocol) is responsible for establishing reliable, ordered data delivery, and handling congestion control, while IP (Internet Protocol) focuses on routing packets of data to their destination. model is a fundamental framework for computer networking, defining how data is transmitted over networks to ensure reliable communication between IoTInternet Of Things devices. When data is sent over the internet, the TCP/IP model breaks down the data into packets and sends them through different layers (Physical, Link, Internet, Transport, and Application Layers), where each layer is responsible for a specific function. This article will cover the role each of these layers have in WiFi communication.

Physical Layer (PHY)

The WiFi Physical Layer (PHY) is responsible for transmitting and receiving data between devices over the airwaves. The PHY layer is the first and lowest layer that converts data into bits for transmission and converts received bits into usable data for the layers above it. Key components of the WiFi Physical Layer include the following.

Physical Medium-Dependent (PMD) Layer:
This layer defines the physical characteristics of the wireless medium, such as frequency, bandwidth, and transmission power.
Physical Layer Convergence Procedure (PLCP) Layer:
This layer adds a preamble and PHY header to the data frame, which helps synchronize radios and determine the transmission rate.
Modulation and Coding:
The WiFi physical layer uses various modulation schemes, such as BPSKBinary Phase Shift Keying, QPSKQuadrature Phase Shift Keying, QAMQuadrature Amplitude Modulation, and OFDMOrthogonal Frequency-Division Multiplexing, to encode data onto the carrier wave. Error correction codes, like Turbo encoding, are also used to ensure reliable data transmission.

Link Layer

The communication channel that connects the adjacent nodes (devices) in a network is known as a link. The responsibility of the link layer is to move data across an individual link (also referred to as a single hop). For WiFi radio signals, the link must agree on which radio frequency channels are to be used to transmit data and how the digital data is to be encoded in the radio signal.

The link layer also determines how data is formatted for transmission in (Data Frames), who should be sending data across a link (Line Discipline), how much data should be sent (Flow Control), and error detection and correction (Error Control).

Data Frames

The data link layer handles the process of dividing the data into frames and reassembling it. Framing allows the data to be broken up into recoverable chunks that can be more easily processed and checked, and is also used to wrap the data payload with additional information such as addressing information and a checksum. This allows the transmission of data to be organized, controlled, and ensures the data is delivered accurately and efficiently. The structure of the data link layer frame is shown below.

Link Layer Data Frame Structure

The frame structure consists of a header, body, and trailer. The header contains the control field, duration ID field, address fields, and sequence control field. The frame body consists of the data payload. The frame trailer includes the Frame Check Sequence (FCSFrame Check Sequence) containing the CRCCyclic Redundancy Check for error detection information.

Frame Control:
The frame control field is 2 bytes long and defines the form of the frame, type of frame, function of the frame, and control information.
Duration ID:
The duration ID field contains the period of time (in µs) indicating how long the field's transmission will take so other devices know when the channel will be available again.
Addresses:
The address fields have up to four up to four MACMedium Access Control, Medium Access Controller, Media Access Control, or Medium Access Controller addresses. The definition of each of the four MAC address fields will change depending on whether the traffic is upstream or downstream, determined by the To DS bit and From DS bit in the Frame Control field. The five definitions are as follows:
  • Source Address (SA) of the original sending station.
  • Destination Address (DA) of the final destination station.
  • Transmitter Address (TA) of the station that is transmitting the frame.
  • Receiver Address (RA) of the station that is intended to receive the incoming transmission from the transmitting station.
  • Basic Service Set Identifier (BSSID) address of the AP's station.
Sequence Control:
The sequence control field is 16-bits containing the sequence number and fragment number of the frame for rearrangement of out-of-sequence frames and sending acknowledgments by the receiver.
Payload:
The data payload or frame body field is variable in size, from 0 to 2304 bytes.
Frame Check Sequence (FCS):
The Frame Check Sequence (FCS) field is the last four bytes containing a 32-bit CRC that allows for integrity checks of received frames. When a frame is sent, the FCS is calculated over all the fields of the header and the frame body field and then appended to the frame. When the frame is received, it can calculate the FCS of the frame and compare it to the FCS value received. If they match, it is assumed that the frame was not distorted during transmission.

Line Discipline

WiFi is half-duplex, where each device can transmit or receive, but not at the same time. If both the devices at the end of the link transmit the data simultaneously, they will collide which leads to the loss of the information. Line Discipline is a functionality of the Data link layer that provides the coordination among the link systems by determining which device can send and when it can send the data.

Flow Control

A receiving node can receive the frames at a faster rate than it can process the frame. Without flow control, the receiver's buffer can overflow, and frames can get lost. The link layer regulates flow control so that a fast sender does not drown a slow receiver. Flow control restricts the number of frames the sender can send before it waits for an acknowledgment from the receiver.

Error Control

Error control consists of error detection and correction. Communication errors could arise from noise in the transmitting or receiving electronics, clock skew, or interference (EMIElectromagnetic Interference). It is important that any errors in transmission are detected to prevent miscommunication. If an error is detected on the receiver end, then it can request that the message be re-sent (known as Backward Error Correction) or perform some kind of auto-recover to correct some kind of errors in corrupted data (Forward Error Correction).

There are many different ways to detect transmission errors to verify data integrity. The general approach is to add some redundancy (some extra data) to the message to be sent that the receiver can use to check its consistency. A common method for verifying data is generating a checksum, a small-sized block of data derived from the message. A good checksum algorithm would output a unique value for any kind of change in the input message. The checksum algorithm used in WiFi communication is the Cyclic Redundancy Check (CRCCyclic Redundancy Check), which converts the message into a polynomial, divides it by a so-called generator polynomial, and takes the integer remainder of the division as the CRC.

WiFi has a Frame Check Sequence (FCSFrame Check Sequence) at the end of each data frame with a CRC for error detection and built-in Forward Error Correction (FECForward Error Correction), the process of adding redundant data such as Error Correction Code (ECCError Correction Code) to a message so that it can be recovered by a receiver when errors are introduced. This redundancy allows the receiver to detect a limited number of errors that may occur anywhere in the message, and often to correct these errors without retransmission.

Internet Layer

The role of the internet layer is to send information between and across networks through the use of routers. This allows data to be sent across the country or across the world in multiple hops over multiple networks. The internet layer assembles a data packet encapsulating a frame plus an IP address information. The header of each IP packet contains the IP address of the sending host and that of the destination host.

IP Address

IP addresses serve two main functions: network interface identification and location addressing. The public IP address of a website, server, or router contains information about where it is in order to provide the capability of establishing a route to its location.

Most of the world currently uses the IPv4 version of addressing, with a size of 32-bits that limits the address space to 4,294,967,296 (232) addresses. An IPv4 address is expressed as a set of four numbers called octets, each ranging from 0 to 255, separated by periods. The full IPv4 addressing range goes from 0.0.0.0 to 255.255.255.255. For example, Google's DNSDomain Name System (DNS) translates the human-readable names of internet sites to their underlying IP addresses server IPv4 address is 8.8.8.8.

The exhaustion of IPv4 address gave rise to a newer IPv6, where the address size was increased from 32-bits in IPv4 to 128-bits in IPv6, thus providing up to 2128 (approximately 3.403x1038) addresses that was deemed sufficient for the foreseeable future. An IPv6 address is expressed as a set of eight hexadecimal numbers called segments, each ranging from 0 to FFFF, separated by colons (:). For example, Google's DNSDomain Name System (DNS) translates the human-readable names of internet sites to their underlying IP addresses server IPv6 address is 2001:4860:4860:0:0:0:0:8888.

IP address are generated by an integrated algorithm by the Internet Assigned Numbers Authority (IANA). The IANA allocates IP address blocks to Regional Internet Registries (RIRs), who in turn geographically distribute these blocks to Internet Service Providers (ISPs). An ISP generates individual IP addresses for their customers automatically using the Dynamic Host Configuration Protocol (DHCP).

There are four types of IP address: public, private, static, and dynamic.

Public IP Address:
Assigned by an ISP and can be accessed directly over the internet. It used to communicate outside a network to the internet. It is also used by websites and servers so they can be accessed anywhere around the world.
Private IP Address:
Assigned by a router to devices within a local network or by an ISP. This address is only visible within the local network, so it is unavailable on the Internet. When devices within the local network connect to the internet, the private IP address is mapped to the public IP address by the router.
Static IP Address:
A fixed address that does not change. They are typically used for hosting websites or running servers. Static IP addresses are manually created by the device itself, a network administrator, or provided by the ISP upon request.
Dynamic IP Address:
Assigned by a DHCP server and is subject to change. Dynamic IP addresses are only active for a certain amount of time, after which they expire and the computer will either automatically request a new lease, or the computer may receive a new IP address.

Transport Layer

The main role of the transport layer provide the communication services directly to the application processes running on different hosts. It is considered an end-to-end layer because it provides a point-to-point connection rather than hop-to-hop, between the source host and destination host.

Packets are segmented before being transported across the network. The transport layer requires a port number to deliver the segments of data to the correct process among the multiple processes running on a particular host. When the transport layer receives data, it reassembles the segmented data into fully-fledged messages for the application process.

The two most common protocols in the transport layer that can be used by an application are the connection-orientated Transmission Control Protocol (TCP) and connectionless User Datagram Protocol (UDP). Together, TCP and UDP comprise of most of the traffic on the Internet. Python has a standard built-in socket module (Docs) to establish TCPTransmission Control Protocol or UDPUser Datagram Protocol Socket connections and the socketserver module (Docs) that has classes that make using the low-level socket functions easier. A detailed article on Python sockets and its usage can be found at realpython.com.

Ports

Ports are 16-bit unsigned integers in the range of 0 to 65535 that specifically determines which program or service on a server is going to be used. Port numbers allow a client application to choose which server application it wants to interact with. Different server applications can be available on a single computer, where each application listens on a specific port for incoming connections. Some well known ports for different server applications are provided in the table below.

Common Ports
Ports Protocol Function
23 Telnet Remote login to hosts
22 SSHThe Secure Shell Protocol (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Its most notable applications are remote login and command-line execution. Secure remote login to hosts
80 HTTPHypertext Transfer Protocol Web page hypertext documents
443 HTTPHypertext Transfer Protocol Secure web page hypertext documents
25 SMTPSimple Mail Transfer Protocol Sending email
143/220/993 IMAPInternet Message Access Protocol Access and manage email
109/110 POPPost Office Protocol Access to email inbox
21 FTPFile Transfer Protocol File transfer
53 DNSDomain Name System (DNS) translates the human-readable names of internet sites to their underlying IP addresses Domain Name Resolution

The port number is appended to the IP address separated by a colon. For example, 8.8.8.8:80 gives you Google's DNSDomain Name System (DNS) translates the human-readable names of internet sites to their underlying IP addresses server HTTP web page that has an IPv4 address of 8.8.8.8 on port 80.

TCP

Transmission Control Protocol (TCP) is connection-orientated where a connection between the receiver and sender is established so that data can be transmitted in two directions. The connection is maintained while transferring data and terminated once the communication is over. TCP is commonly used for webpages, emails, downloading a file, and database information because it guarantees that all sent packets will reach the destination in the correct order without errors. It accomplishes this with the use of acknowledgment packets sent back to the sender with automatic retransmission when there are errors or missing packets.

UDP

User Datagram Protocol (UDP) is a connectionless protocol where there is no overhead for opening a connection, maintaining a connection, or terminating a connection. Data is continuously sent to the recipient, whether or not they receive it. UDP has no form of flow control or error correction. This makes UDP less reliable than TCP, but with less overhead and hold ups. UDP is commonly used for live streaming audio and video so the stream does not get held up if a packet is corrupted or goes missing (which may just be a single frame of video or a small fraction of a second of audio that would go unnoticed).

Application Layer

The application layer allows users to interact with programs to access and send data over networks, such as Email clients (SMTPSimple Mail Transfer Protocol, IMAPInternet Message Access Protocol, POPPost Office Protocol), text messaging (SMSShort Message Service), web browsing (HTTPHypertext Transfer Protocol, HTTPSHypertext Transfer Protocol Secure), transferring files (FTPFile Transfer Protocol), audio and/or video streaming (RTSPReal-Time Streaming Protocol), remote login to hosts (Telnet, SSHThe Secure Shell Protocol (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Its most notable applications are remote login and command-line execution.), etc. The following subsections provide the most common application layer protocols used for IoTInternet Of Things devices. A more comprehensive list of application layer protocols can be found on Wikipedia.

HTTP

Hypertext Transfer Protocol (HTTPHypertext Transfer Protocol) is an application layer request/response client-server protocol for transmitting hypermedia documents, such as HTMLHyperText Markup Language, where requests are sent by the client and the server provides a response.

HTTP

HTTP has three main versions: HTTP/1, HTTP/2, and HTTP/3. HTTP/1 was published in 1996 and is text-based with an ordered and blocking format. HTTP/2 was published in 2015 and provides more efficient semantics to speed up browser-side and server-side transactions. HTTP/2 is binary rather than textual, has less resource usage, utilizes multiplexing to handle TCP requests instead of the ordered and blocking format used by HTTP/1, and implements server push and bi-directional communication. HTTP/3 was published in 2022 and has even lower latency for web pages and loads faster than HTTP/2 and HTTP/1. All three protocols are backwards compatible with the previous protocol.

Python has a standard built-in webserver library called http.server (Docs) for handling HTTP requests. This module creates a webserver to handle basic HTTP communication with GET and POST methods (note that SimpleHTTPServer has been migrated to Python's http.server module in Python 3). Although this is not a fully featured web server, it can parse text and static HTML files, give response code, and you can set your own URL address and port number of the server.

MQTT

MQ Telemetry Transport (MQTTMQ Telemetry Transport (MQTT) is a lightweight publish-subscribe messaging protocol for small sensors and mobile devices, optimized for high-latency or unreliable networks. Historically, the "MQ" in "MQTT" came from the IBM MQ (then 'MQSeries') product line, where it stands for "Message Queue". However, the protocol provides publish-and-subscribe messaging (no queues, in spite of the name).) is a lightweight protocol to facilitate machine-to-machine communication. This messaging protocol is based on the publish/subscribe model, which can provide real-time and reliable messaging service for IoTInternet Of Things devices.

MQTT

MQTT has a small footprint and minimal bandwidth consumption. MQTT messaging is between one broker and clients, where there is only a broker (server) and multiple clients (e.g., sensors) in a MQTT system. MQTT has some advantages that make it more convenient to use than other protocols. The broker in MQTT will deliver a message from a publisher to any (all) subscribers where interested parties can subscribe to messages that may interest them. MQTT handles all connection, disconnection, and reconnection events. The MQTT broker also has a "Last Will and Testament" (LWT) defined in the protocol that will generate an automatic message when a client fails or abnormally disconnects.

There are many different MQTT libraries and implementations. Eclipse Mosquitto is an open source message broker that can be locally installed on Windows and Linux or you can use a free online broker. Python has the Paho MQTT client library that you can install to setup Broker-Client messaging. The meaning of Paho is "to broadcast, make widely known, announce, disseminate, transmit." A comparison of MQTT implementations can be found on Wikipedia.

WebSocket

WebSocket is a protocol that provides bi-directional full-duplex communication in a persistent connection between client and server.

Websocket

WebSocket was developed from the limitation of HTTP/1 as a strictly unidirectional protocol where any data sent from server to the client must first be requested by the client. WebSockets operate over HTTP through a single TCP/IPTCP/IP is a suite of internet communication protocols. TCP (Transmission Control Protocol) is responsible for establishing reliable, ordered data delivery, and handling congestion control, while IP (Internet Protocol) focuses on routing packets of data to their destination. socket connection, using HTTP as the initial transport mechanism for the initial handshake, but then keeps the TCP/IP connection alive after the HTTP response is received in order for sending messages back and forth between client and server using a basic framed message protocol. The bi-directional capability of WebSockets is ideal for monitoring systems that require constantly fast updates.

SMTP

The Simple Mail Transfer Protocol (SMTPSimple Mail Transfer Protocol) is an Internet standard communication protocol for sending email. A client who wants to send an email opens a TCPTransmission Control Protocol connection to the SMTP server and then sends the email across the connection.

SMTP

Most computer email applications, such as Gmail, Outlook, Apple Mail, Yahoo Mail, Netscape, Mozilla Thunderbird, and many others use SMTP for sending email. SMTP dictates how the email messages should be formatted, encrypted, and relayed between mail servers. SMTP is a push protocol used to send the mail whereas Post Office Protocol (POPPost Office Protocol) and Internet Message Access Protocol (IMAPInternet Message Access Protocol) are used to retrieve those emails at the receiver's side.

When you send an email, the application opens a connection to an SMTP server at a specific address or domain name and port number to send the email data over TCPTransmission Control Protocol. The address identifies the machine on the IPInternet Protocol network, while ports identify particular applications or services on a system. Each email address, such as @gmail.com, has an underlying SMTP server that runs on the domain (e.g., smtp.gmail.com) and listens for incoming emails.

The domain name for the SMTP server will usually be the name of your provider's email domain name. For the port number, SMTP typically uses 25, 465, 587, or 2525. Ports 465 and 587 are the more common ports used to send emails to SMTP servers. Port 2525 is used as an alternative in circumstances where other ports (465 and 587) are blocked. Port 25 is used these days for relaying between mail servers and is often blocked by providers to curb the amount of spam. The table below lists some common email providers with their SMTP server domain name and port.

Email Providers & SMTP Servers
Provider SMTP Server Domain Name Port
Gmail smtp.gmail.com 587
Outlook / Hotmail smtp-mail.outlook.com 587
Yahoo Mail smtp.mail.yahoo.com 587
AT&T smpt.mail.att.net 465
Comcast smtp.comcast.net 587
Verizon smtp.verizon.net 465
Apple iCloud smtp.mail.me.com 587

When an email is sent by SMTP, it can be encrypted with protocols such as Secure Sockets Layer (SSLSecure Sockets Layer) or Transport Layer Security (TLSTransport Layer Security), where TLS is the more secure successor of SSL. The email application needs to setup with the encryption standard the SMTP server uses in order to connect and communicate with it. Some email providers have additional security settings that may need to be configured. For instance, Gmail has an additional security feature for Google accounts called application-specific passwords that needs to be setup before automating emails with your application.

You can create a program in Python to automatically send emails for you using the built-in smtplib module and the ssl module can provide TLSTransport Layer Security encryption. If you are using a Python web framework (Flask, FastAPI, or Django), there are extensions for these frameworks that can send the email for you. For example, Flask has the extension Flask-Mail to automatically send emails.

SMS

Short Message Service (SMSShort Message Service) is a text messaging service built into telephone, internet, or mobile devices. It uses standardized communication protocols to send short text messages containing all the necessary information (including sender phone number, recipient phone number, delivery report flag, message text, etc).

SMS

Text messages can be sent using services such as Twilio or Textbelt. They can also be sent by SMS Gateways using the same routines used for sending emails. Many mobile carriers have their own gateway as shown for a few examples in the table below where [number] is the phone number the text is to be sent to.

Carriers and SMS Gateways
Carrier SMS Gateway
AT&T [number]@txt.att.net
Sprint [number]@messaging.sprintpcs.com
or
[number]@pm.sprint.com
T-Mobile [number]@tmomail.net
Verizon [number]@vtext.com
Cricket [number]@sms.mycricket.com

To send a text message, your application would send an email to the SMS gateway through your email server (i.e., the SMS Gateway would be in the To: field of the email).

Related Content


WiFi Communication

Created:   15Jan2024   19:48:50   UTC 2024-01-15T19:48:50Z
Updated:   15Aug2024   03:54:20   UTC 2024-08-15T03:54:20Z
Rating:  (0 reviewsThis article has not been rated yet)

Overview of WiFi communication to explain the specs and features of IoT devices:

  • IEEE 802.11 Standards
  • Protocols
  • IP Addresses
  • Ports
  • Security

WiFi Module

Created:   18Jan2024   03:43:35   UTC 2024-01-18T03:43:35Z
Updated:   15Aug2024   23:21:16   UTC 2024-08-15T23:21:16Z
Rating:  (0 reviewsThis article has not been rated yet)

Overview of the different kinds of WiFi modules.

  • ESP8266 Modules
  • ESP32 Modules
  • ATWINC1500 Modules

WiFi Microcontroller Development Board

Created:   29Jan2024   04:07:40   UTC 2024-01-29T04:07:40Z
Updated:   16Aug2024   03:12:14   UTC 2024-08-16T03:12:14Z
Rating:  (0 reviewsThis article has not been rated yet)

Overview of the different kinds of WiFi Microcontroller Development Boards.

  • ESP8266
  • ESP32, ESP32-C, and ESP32-S
  • CYW43439
  • ATWINC1500

Products







Article Rating

Sign in to rate this article

Sign In


(0) Comments

Sign in to leave a comment

Sign In