03 Protocols
Communication protocols allow devices to send and receive data/commands to/from other services (like web apis).
Internet Protocol
The internet protocol (IP) is a whole suite of different protocols. The different layers are loosely based on the OSI model.
Internet Protocol Addresses
IP addresses are used to identify devices:
- IPv4 (four octets separated by dots, e.g. 192.168.0.1)
- IPv6
- UDP
- TCP
- HTTP
Domain Name System
IP addresses are hard to remember for humans. We use domain names and the domain name system (DNS) to make it easier. A domain name will be resolved to an IP address.
User Datagram Protocol
UDP is unreliable communication. There is no way to check if a package made it to the remote.
Transmission Control Protocol
TCP is reliable. Packages are automatically sent again if they were lost or damaged on the way to the remote.
API
An API provides a web interface to control a device. A simple web api can be implemented by creating a server on the MCU which listens on port 80.
The following examples are different implementations of an “API” to control an LED via the network.
REST
Based on HTTP verbs (GET, POST, PUT, DELETE, …).
GET: retrieve information from the serverPOST: send information to the server for processing/storagePUT: update information on the serverDELETE: remove information
Example: Philips HUE
CoAP
Lightweight message protocol, where messages fit into exactly one UDP frame.
Example: IKEA Tradfri
Hands-On: Display API
The display API allows to control brightness, dots and digits of a 4-digit 7-segment display.
#include <TM1637Display.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServerSecure.h>
#include "WiFiCredentials.h"
#define CLK 5
#define DIO 4
const int port = 80;
TM1637Display display(CLK, DIO);
ESP8266WebServer server(port);
void handleRoot()
{
unsigned long seconds = millis() / 1000;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
char response[9];
snprintf(response, 17, "Uptime: %02lu:%02lu:%02lu", hours, minutes % 60, seconds % 60);
server.send(200, "text/plain", response);
}
void handleDisplay()
{
if (server.hasArg("value"))
{
String value = server.arg("value");
int length = value.length();
int digits = value.toInt();
int brightness = server.hasArg("brightness") ? server.arg("brightness").toInt() : 7;
int dots = server.hasArg("show_dots") ? 64 : 0;
bool zeros = server.hasArg("leading_zeros");
display.clear();
display.setBrightness(brightness);
display.showNumberDecEx(digits, dots, zeros, zeros ? 4 : length, zeros ? 0 : 4 - length);
Serial.println("Set display value");
server.send(200, "text/plain", "OK");
}
else if (server.hasArg("state"))
{
String state = server.arg("state");
if (state == "on")
{
Serial.println("Turn display on");
display.clear();
display.setBrightness(7, true);
display.showNumberDec(0, true);
server.send(200, "text/plain", "OK");
}
else if (state == "off")
{
Serial.println("Turn display off");
display.clear();
display.setBrightness(0, false);
display.showNumberDec(0, true);
server.send(200, "text/plain", "OK");
}
else
{
Serial.println("Invalid state");
server.send(400, "text/plain", "Bad request");
}
}
else
{
server.send(400, "text/plain", "Bad request");
}
}
void handleNotFound()
{
server.send(404, "text/plain", "Not found");
}
void setup()
{
Serial.begin(115200);
display.clear();
display.setBrightness(0, false);
display.showNumberDec(0, true);
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("\nConnected");
server.on("/", handleRoot);
server.on("/display", handleDisplay);
server.onNotFound(handleNotFound);
server.begin();
Serial.print("Server started on http://");
Serial.print(WiFi.localIP());
Serial.println("/ in your browser");
}
void loop()
{
server.handleClient();
}