• Keine Ergebnisse gefunden

MQTT & CoAP K E I N E N E U E C O M P A N Y

N/A
N/A
Protected

Academic year: 2022

Aktie "MQTT & CoAP K E I N E N E U E C O M P A N Y"

Copied!
27
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

MQTT

& CoAP

K E I N E N E U E C O M P A N Y

(2)

Für alle, die das Promovideo

gesehen haben ...

(3)

Kleine Werbeeinblendung

DE R RE F E RE NT

• Gelernter Informatiker

• Software Paläontologe

• Software Architekt

• Seit 2018 Arbeit am IoTHub

E L C O I N D U S T R I E A U T O M A T I O N G M B H

• Automation Produkte

• (Maschinen-) Daten sammeln

• Geräte steuern

• Visualisieren

(4)

OSI 7 Schichtmodell

Physical Layer Data Link Layer

Network Layer Transport Layer Presentation Layer

Application Layer

(5)

TCP/IP Referenzmodell

• OSI 7 Schichtmodell

Physical Layer Data Link Layer

Network Layer Transport Layer Presentation Layer

Session Layer

Network Access Layer

Internet Layer Transport Layer Application Layer

Application Layer

(6)

Web Stack Protokolle

• OSI 7 Schichtmodell

Physical Layer Data Link Layer

Network Layer Transport Layer Presentation Layer

Session Layer

• TCP/IP

Referenzmodell

Network Access Layer

Internet Layer Transport Layer Application Layer

Application Layer

Web Application

Ethernet, WiFi, WLan, DSL, ISDN

IPv6, IPv4, IPsec TCP, UDP

HTML, JSON, XML

HTTP(S), DNS, TLS, DHCP

(7)

IoT Protokolle

• OSI 7 Schichtmodell

Physical Layer Data Link Layer

Network Layer Transport Layer Presentation Layer

Session Layer

• TCP/IP

Referenzmodell

Network Access Layer

Internet Layer Transport Layer Application Layer

Application Layer

Web Application

Ethernet, WiFi, WLan, DSL, ISDN

IPv6, IPv4, IPsec TCP, UDP

HTML, JSON, XML

HTTP(S), DNS, TLS, DHCP

• Web Stack Protokolle

Application

IEEE 802.15.4e, Z-

Wave, Bluetooth, Zigbee, Dash7, HomePlug, G.9959, LTE-A, LoRa

6LoWPAN, 6TiSCH, 6Lo, IP..., RPL, CORPL, CARP

TCP, UDP, DTLS

Binary, JSON, CBOR MQTT, SMQTT, AMQP,

CoAP, XMPP, DDS

(8)

MQTT

Sensor

IoT Device

Controller

MQTT Broker

Apps

Data Center

Devices

• Für kleine Geräte

• Verwendet einen Broker

• Austausch via Topics

• Kommunication via TCP

• Sicherheit via TLS, Username/Password

• DoS Level 0-2

• Last Will Nachricht

(9)

MQTT Message

(10)

Code Beispiel github.com/eclipse/paho.mqtt.golang

var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { fmt.Printf("Received message: %s from topic: %s \n", msg.Payload(), msg.Topic())

}

var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) { fmt.Println("Connected")

}

var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) { fmt.Printf("Connect lost: %v", err)

}

(11)

Code Beispiel github.com/eclipse/paho.mqtt.golang

func main() {

var broker = "broker.emqx.io"

var port = 1883

opts := mqtt.NewClientOptions()

opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port))

opts.SetClientID("go_mqtt_client") opts.SetUsername("emqx")

opts.SetPassword("public")

opts.SetDefaultPublishHandler(messagePubHandler) opts.OnConnect = connectHandler

opts.OnConnectionLost = connectLostHandler client := mqtt.NewClient(opts)

if token := client.Connect(); token.Wait()

&& token.Error() != nil { panic(token.Error()) }

sub(client) publish(client)

client.Disconnect(250) }

func publish(client mqtt.Client) { num := 10

for i := 0; i < num; i++ {

text := fmt.Sprintf("Message %d", i)

token := client.Publish("topic/test", 0, false, text)

token.Wait()

time.Sleep(time.Second) }

}

func sub(client mqtt.Client) { topic := "topic/test"

token := client.Subscribe(topic, 1, nil) token.Wait()

fmt.Printf("Subscribed to topic: %s", topic) }

(12)

Code Beispiel org.eclipse.paho.client.mqttv3

public class SimpleMqttCallBack implements MqttCallback { public void connectionLost(Throwable throwable) {

System.out.println("Connection to MQTT broker lost!");

}

public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { System.out.println("Message received: \t"+ new String(mqttMessage.getPayload()) );

}

public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { }

}

(13)

Code Beispiel org.eclipse.paho.client.mqttv3

public class Subscriber {

public static void main(String[] args) throws MqttException {

System.out.println("== START SUBSCRIBER ==");

MqttClient client=new

MqttClient("tcp://localhost:1883", MqttClient.generateClientId());

client.setCallback( new SimpleMqttCallBack() );

client.connect();

client.subscribe("iot_data");

} }

public class Publisher {

public static void main(String[] args) throws MqttException {

String messageString = "Hello World from Java!";

MqttClient client = new

MqttClient("tcp://localhost:1883", MqttClient.generateClientId());

client.connect();

MqttMessage message = new MqttMessage();

message.setPayload(messageString.getBytes());

client.publish("iot_data", message);

client.disconnect();

} }

}

(14)

MQTT SN

Sensor

Sensor

Sensor

MQTT SN Gateway

Sensor

Sensor

Sensor

MQTT SN Gateway

Apps

MQTT SN Gateway

MQTT Broker

• Sensoren verteilt über eine große Fläche

• Verbindung zum Broker via Gateways

• UDP und ein reduziert es Protokoll

• 3 Arten von Gateways:

• Integrated

• Transparent

• Aggregating

(15)

CoAP

• Für kleine Geräte mit instabiler Verbindung

• Verwendet UDP und ein

reduziertes Protokoll ähnlich wie HTTP(s)

• Subscriptions (Abonnements)

• Auffinden von Diensten und Ressourcen

CoAP Gerät

CoAP Client

CoAP

Gerät

(16)

CoAP Nachricht

0 1 2 3

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|Ver| T | TKL | Code | Message ID |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

| Token (if any, TKL bytes) ...

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

| Options (if any) ...

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|1 1 1 1 1 1 1 1| Payload (if any) ...

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+

(17)

Code Beispiel github.com/plgd-dev/go-coap/v2

func communicate() context.CancelFunc{

co, err := udp.Dial("192.168.188.50:5684") if err != nil {

log.Fatalf("Error dialing: %v", err) }

getPaths := []string{"/", "/config"}

observePaths := []string{"/module_1/channel_1"}

ctx, cancel := context.WithCancel(context.Background()) for _, path := range getPaths {

resp, _ := co.Get(ctx, path) printMessage(resp, path) }

for _, path := range observePaths { go observe(path, co, ctx)

}

return cancel }

func observe(path string, co *client.ClientConn, ctx context.Context) {

_, err := co.Observe(ctx, path, func(req *pool.Message) { printMessage(req, path)

})

if err != nil {

log.Fatalf("Unexpected error '%v'", err) }

}

(18)

Code Beispiel de.uzl.itm.ncoap

private void sendCoapRequest() throws URISyntaxException, UnknownHostException {

String host = arguments.getUriHost();

int port = arguments.getUriPort();

String path = arguments.getUriPath();

String query = arguments.getUriQuery();

URI resourceURI = new URI ("coap", null, host, port, path, query, null);

boolean useProxy = arguments.getProxyAddress() != null;

int messageType = arguments.isNon() ? MessageType.NON : MessageType.CON;

CoapRequest coapRequest = new CoapRequest(messageType, MessageCode.GET, resourceURI, useProxy);

if (arguments.isObserve()) { coapRequest.setObserve(0);

}

InetSocketAddress remoteSocket;

if (useProxy) {

InetAddress proxyAddress =

InetAddress.getByName(arguments.getProxyAddress());

int proxyPort = arguments.getProxyPort();

remoteSocket = new InetSocketAddress(proxyAddress, proxyPort);

} else {

InetAddress serverAddress

= InetAddress.getByName(arguments.getUriHost());

int serverPort = arguments.getUriPort();

remoteSocket = new InetSocketAddress(serverAddress, serverPort);

}

if (arguments.isObserve()) { callback =

new SimpleObservationCallback(arguments.getMaxUpdates());

} else {

callback = new SimpleCallback();

}

this.sendCoapRequest(coapRequest, remoteSocket, callback);

}

(19)

Zigbee

ZigZag Dance of Bees

(20)

ZigBee

• Netzwerk mit Koordinator, Routern, Endgeräten

• Basierend auf IEEE-802.15.4- Standard (WPAN)

• Mehrer Profile

• Home Automation

• Light Link

• Reichweiter bis zu 100m

• Sicherheit via AES-128 Netzwermschlüssel

Fernbedie nung

Steckdose

Funktaster

ZigBee Koordinator

Lampe

Garagen Öffner

Sensor

Zigbee Router

Zigbee Router Zigbee

Router

Dimming Schalter

Thermo- stat Rauch-

melder Türschloß

(21)

Code Beispiel

func toggleIkeaBulb(stewie *steward.Steward, message *model.DeviceIncomingMessage) { if isXiaomiButtonSingleClick(message) {

if ikeaBulb, registered := devices["TRADFRI bulb E27 W opal 1000lm"]; registered { toggleTarget(stewie, ikeaBulb.NetworkAddress)

} else {

fmt.Println("IKEA bulb is not available") }

} }

func toggleTarget(stewie *steward.Steward, networkAddress string) { go func() {

stewie.Functions().Cluster().Local().OnOff().Toggle(networkAddress, 0xFF) }()

}

(22)

Code Example

X Bee xb ee = n ew XBe e() ; x bee .op en( "CO M5" , 9 600 );

/ / t his is th e S eri al Hig h ( SH) + Ser ial Lo w ( SL) of th e r emo te XBe e

X Bee Add res s64 ad dr6 4 = ne w X Bee Add res s64 (0x a, 0xb , 0 xc, 0x d, 0xe , 0 xf, 0, 1) ;

/ / T urn on DI O0 (Pi n 2 0)

R emo teA tRe que st req ues t = ne w R emo teA tRe que st( add r64 , " D0" , n ew int [{X Bee Pin .Ca pab ili ty. DIG ITA L_O UTP UT_ HIG H.g etV alu e() });

x bee .se ndA syn chr ono us( req ues t);

R emo teA tRe spo nse re spo nse = (Re mot eAt Res pon se) xb ee. get Res pon se( );

i f ( res pon se. isO k() ) {

Sys tem .ou t.p rin tln ("S ucc ess ful ly tur ned on DI O0" );

} el se {

Sys tem .ou t.p rin tln ("A tte mpt to tu rn on DIO 0 f ail ed. S tat us: " + r esp ons e.g etS tat us( ));

}

/ / s hut dow n t he ser ial po rt and as soc iat ed thr ead s x bee .cl ose ();

(23)

Fazit

• MQTT

• Pro: Einfaches Handling

• Pro: Topics

• Pro: Unidirektional

• Con: Broker

• Con: externe Metaprotokolle

• Con: Dedizierte Rollen

• ZigBee

• Pro: Höhere

Ausfallsicherheit

• Pro: Vernetzung

• Pro: Resourcennutzung

• Con: Komplexes Handling

• Con: Viel Hardware nötig

• Con: Reduzierter Funktionsumfang

• CoAP

• Pro: Spezifiziertes Protokoll

• Pro: Discovery und Multicast

• Pro: Bidirektional

• Con: Komplexeres Handling

• Con: Gerät ist Server

• Con: Strikterer

Funktionsumfang

(24)

Fragen?

(25)

Quellen

• OSI Modell und TCP/IP Referenz:

• https://de.wikipedia.org/wiki/OSI-Modell

• IoT Protokolle:

• http://revolutionofthings.com/de/iot-protokolle/

• https://nicolaswindpassinger.com/osi-reference-model

• https://www.industry-of-things.de/http-xmpp-coapp-und-mqtt-iot-protokolle-fuer-die-

kommunikation-a-813079/

(26)

Quellen

• MQTT

• http://www.steves-internet-guide.com/mqtt-protocol-messages-overview/

• CoAP:

• https://datatracker.ietf.org/doc/html/rfc7252

• ZigBee

• https://de.wikipedia.org/wiki/Tanzsprache

• https://de.wikipedia.org/wiki/ZigBee

• https://www.homeandsmart.de/zigbee-funkprotokoll-hausautomation

(27)

Quellen

• Code Examples:

• https://www.emqx.com/en/blog/how-to-use-mqtt-in-golang

• https://github.com/tgrall/mqtt-sample-java

• https://github.com/okleine/nCoAP

• https://github.com/dyrkin/zigbee-steward

• https://github.com/andrewrapp/xbee-api

Referenzen

ÄHNLICHE DOKUMENTE

$: N.. in other words, if we disregard the time-stamps, 0 and the induced 0' generate the same results. Another relationship that we have to consider is the one between

Das Gesetz verbietet den Verkauf von Wein, Bier und Apfelwein an unter 16-Jährige; Spirituosen, Aperitifs und Alcopos an unter 18-Jährige.. Alle Preise in

Sammelstelle beim Feuerwehrmagazin Montag bis Samstag 07:00 bis 20:00 Uhr oder. SpezielleSammeltage

Die für die Stationen Boknis Eck in der Kieler Bucht und Bojen in der Gotlandsee berechneten Zooplanktonexkretionen ergaben jedoch, daß nur 10 - 13 % des

oder den Postkartenkalender „Ostpreußen im Bild&#34; erhalten, und wenn Sie mehr Bezieher werben, dann haben Sie die Auswahl unter zahlreichen guten Heimatbüchern. Wir baten

[r]

This work has been digitalized and published in 2013 by Verlag Zeitschrift für Naturforschung in cooperation with the Max Planck Society for the Advancement of Science under

b) A second criterion for optimal regional decision-levels in economic policy concentrates on the cost advantages of larger economic units, the &#34;economies of