Communication Protocol
Communication Protocol
Communication Protocol
INTERNET OF THINGS
Session 8 Communication Protocol
• GET
• POST
• HEAD
• OPTIONS
• PUT
• DELETE
• TRACE
• CONNECT
Request Message
Request Header
• Accept
• Accept-Charset
• Accept-Encoding
• Accept-Language
• Host
• User-Agent
• Cookie
• Content-Length
• Referer
Response Message
Response Header
• Date
• Server
• Last-Modified
• Expires
• Refresh
• Content-Length
• Content-Type
• Accept-Ranges
Status Code
• 1xx - informational
• 2xx - success
– 200 - OK
• 3xx - redirection
– 301 - move permanently
– 304 - not modified
• 4xx - client error
– 403 - forbidden
• 5xx - server error
HTTPS
WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "hello from esp!");
}
Making webserver ESP32
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
Making webserver ESP32
void setup(void) {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
Making webserver ESP32
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}
MQTT
Overview MQTT
• Short for Message Queuing Telemetry Transport
• Open standard
• Simple & lightweight publish-subscribe messaging
protocol
• Runs on embedded devices and mobile platforms
• Low latency
• Low power
• Low bandwidth
• Efficient distribution process to multiple recipients
• Easy
• Minimal overhead
Publish/Subscribe
Publisher and Subscriber
Publisher and Subscriber
• Publisher:
– client yang melakukan publish terhadap suatu topic
– client ini mengirimkan message dengan topic tertentu
• Subscriber:
– client yang melakukan subscribe terhadap suatu topic
– client ini akan menerima semua message dalam topic
tersebut
• Eclipse Paho
• Mosquitto
• HiveMQ
• RabbitMQ
Topic Best Practice
• Generality
• Familiarity
• Scalability
• Segmentation
• Speed
• Security
• Encapsulation
REST API
This request is used to get a resource from a server. If you perform a GET request, the server looks for the
GET data you requested and sends it back to you. In other words, a GET request performs a READ operation.
This is the default request method.
This request is used to create a new resource on a server. If you perform a POST request, the server creates a
POST new entry in the database and tells you whether the creation is successful. In other words, a POST request
performs an CREATE operation.
These two requests are used to update a resource on a server. If you perform a PUT request, the server
PUT updates an entry in the database and tells you whether the update is successful. In other words, a PUT
request performs an UPDATE operation.
This request is used to delete a resource from a server. If you perform a DELETE request, the server deletes
DELETE an entry in the database and tells you whether the deletion is successful. In other words, a DELETE request
performs a DELETE operation.
Header
{ 'sensorId': 'temp1',
'Value': 25
}
XML (eXtensible Markup
Language)
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Authentication
• curl https://api.github.com
Mengakses REST API Github
• curl https://api.github.com/users/zellwk/repos
Mengakses REST API Github
• curl
https://api.github.com/users/zellwk/repos\?
sort\=pushed
Mengakses REST API Github
• curl -X POST
https://api.github.com/user/repos
Mengakses REST API Github