I have used Arduino IDE to program a IOT gateway using ESP32 and W5500 ethernet module to use them as a ethernet webserver.
I wanted to know for educational purpose, I know that sensor belong to physical layer of the OSI model. If I have to feed sensor data to webserver how would the sensor data flow in the OSI model? Would it have to go through network layer and other intermediate layers between physical layer and application layer where webserver software resides or is it like a direct interface between physical layer(sensor) and application layer(webserver) from OSI model point of view.
2 Answers 2
Let's say you have an ESP32 fitted with an Ethernet module, and you use it as a gateway between an I2C sensor and a Web browser running on a computer. The network topology would look like this:
┌────────┐ ┌───────┐ ┌──────────┐
│ sensor │ │ ESP32 │ │ computer │
└─────┬──┘ └─┬───┬─┘ └───┬──────┘
└──── I2C ───┘ └─── Ethernet ─────┘
Here you have two networks: the I2C link and the Ethernet link. The OSI model can only be applied to one network at a time, so you have to choose which of these two links you are modeling:
You may be able to map the I2C communication to the OSI model. However, as I said in a comment, the I2C "protocol stack" is so thin, this exercise would feel pretty contrived, and probably not be very useful.
On the Ethernet link you would be using HTTP/TCP/IP. This is a way richer protocol stack, and more amenable to be mapped to the OSI model. However, notice that the sensor is not connected to this network, so it doesn't belong to any layer in this model.
On the ESP32, the code sending the sensor data to the computer might look like this:
// Get the sensor data.
float temperature = theSensor.readTemperature();
float humidity = theSensor.readHumidity();
// Serialize it in JSON.
client.print("{\"temperature\":");
client.print(temperature);
client.print(",\"humidity\":");
client.print(humidity);
client.print("}");
Here, the sensor data (temperature
and humidity
) is the application
data: the actual stuff you care about. The code that serializes it in
JSON might be thought of as the presentation layer. The code that parses
and generates the HTTP headers (which may be your own or come from a
library) is the application layer. Below that you have the usual TCP/IP
stack. Keep in mind though that the layers of the TCP/IP model do not
map one-to-one to the layers of the OSI model.
Reading a sensor or controlling an actuator is implemented in some kind of hardware layer, commonly called "hardware abstraction layer", HAL. This is not the same thing as layer 1 of the OSI model.
However, data flow from or to these devices is commonly not viewed via the OSI layer model of communication. The intermediate layers would be simply no-operation, because they do not wrap data in protocol frames and use no protocol. If you insist, you could try to map the data flow onto the OSI model, from the hardware device to the application that works with the data, for example in communication with other systems. I doubt it will add any value to the understanding of the system.
Therefore, the data flow from the sensor to the communication is commonly just part of the application layer, because it is the application that uses this data. In the implementation of the application you would use the HAL to access the devices.
The OSI model is meant for and applicable for complex interconnections of systems, which use layers of protocols, for example HTTP over Ethernet.
So if you add network communication to a system with sensors and actuators, you have multiple separate data flows:
- Between the sensors and the application;
- Between the application and the actuators;
- Between the network hardware and the application;
- Between the network hardware and the other system(s).
Because of the respective complexity, you would only apply the OSI model for the latter two.
sensor belong to physical layer of the OSI model
... no, it does not ... the sensor data is a numeric value that is added directly to the HTML code ... the web server does not communicate with the sensor