32
© 2016 InterSystems Corporation. All rights reserved. Connecting IOT Devices Pierre-Yves Duquesnoy

Connecting IOT Devices

Embed Size (px)

Citation preview

© 2016 InterSystems Corporation. All rights reserved.

Connecting IOT Devices Pierre-Yves Duquesnoy

1 | © 2016 InterSystems Corporation. All Rights Reserved.

¿What are IOTs? • Devices connected to the Internet

• “Amazon Dash” button

• Heating systems

• Alarms

• Power plugs, Cameras, Sensors

• wearables

• Closed EcoSystems

Cannot interact with Caché/Ensemble

• Open Platforms/Protocols

• Json/rest

• MQTT

• Websockets

2 | © 2016 InterSystems Corporation. All Rights Reserved.

• Micro controller

• Good platform to get started

• Rapid Prototyping (Uno)

• Cost effective

• Low energy needs

• Lack of processing power

(SSL ?)

• Analog and Digital I/O

• Many sensors, relays

• Internet “shield” as an

extension

• C language (!)

IOT Platforms: Micro controllers, the Arduino

3 | © 2016 InterSystems Corporation. All Rights Reserved.

• Raspberry PI 3:

• Very powerful

• 4Core, 64Bits CPU

• Instalación OS (linux)

• Higher energy requirements

• Used with other smaller IOTs

• Eg: Alarm central

IOT Platforms: CPUs, Raspberry PI

4 | © 2016 InterSystems Corporation. All Rights Reserved.

IOT Plataforms: other examples

ESP8266: Wifi Espruino: Javascript

5 | © 2016 InterSystems Corporation. All Rights Reserved.

• Less powerful platforms

• Need a “shield” to connect to internet

• Power Supply issues

• Bluetooth, etc…

• Internet Connectivity

• Wifi / Cable Ethernet

• Firewall, connectivity

• Security

• SSL / TLS

• Intrusions

• Protocols

• Http, Rest, JSON

• websockets

• MQTT

• Propietarios

Interner Communication: challenges

6 | © 2016 InterSystems Corporation. All Rights Reserved.

Connecting sensors to Caché

ESP8266, Wifi, REST and JSON

7 | © 2016 InterSystems Corporation. All Rights Reserved.

Sending sensor data: Wifi, HTTP, JSON, REST

ESP8266

Caché

8 | © 2016 InterSystems Corporation. All Rights Reserved.

Class PYD.IOT.RestServer Extends %CSP.REST

{

XData UrlMap { <Routes> <Route Url="/ESP8266/sensorData" Method="POST" Call="sensorData" /> </Routes>

ClassMethod sensorData() As %Status {

… //Procesar JSON del Request

//Grabar a Tabla o Global

} }

JSON and REST in Caché

9 | © 2016 InterSystems Corporation. All Rights Reserved.

#include "ESP8266WiFi.h“

WiFiClient client;

String json = "{\"temperature\":\""+String(temperature)+"\"}";

WiFi.begin(ssid, password); //---Conectar a la Wifi

client.connect(host,port); //---Verificar Conexión a Servidor Caché

client.println(

"POST http://52.17.73.170:57772/csp/IOTRest/ESP8266/sensorData HTTP/1.1");

client.println("Host: 52.17.73.170:57772");

client.println("Content-Type: application/json");

client.print("Content-Length: " client.println(json.length());

client.println();

client.println(json);

JSON & REST on ESP8266

10 | © 2016 InterSystems Corporation. All Rights Reserved.

Sending sensor data

11 | © 2016 InterSystems Corporation. All Rights Reserved.

Connected vending machine

Arduino AT Mega + Ethernet Shield, MQTT, Mosquitto Broker, Ensemble

12 | © 2016 InterSystems Corporation. All Rights Reserved.

Benefits

• Remote control

• Out of Stock products, price changes..

• Remote maintenance

• Temperatrue sensors, vibration sensors, etc.

• RFID Keys for payment

• Autentication, Server-side balance management (prevents key copies and theft)

• Purchasing data (Time and Date, consumer) for data mining

• Special offers

• happy hour, 3x2,

• Offer on products (like sandwiches) before they expire

Connected vending machine

13 | © 2016 InterSystems Corporation. All Rights Reserved.

• Publish/Subscribe protocol

• On top of TCP/IP

• Light messaging

• Designed for communication for remote sites

• Tiny footprint for client library

• Limited bandwidth usage

• MQTT Brokers

• HiveMQ (propietary)

• Mosquitto (Open Source)

• Client Libraries

• Eclipse PAHO (java)

• C Library for Arduino

14 | © 2016 InterSystems Corporation. All Rights Reserved.

Venduino, MQTT, Ensemble Ensemble

MQTT Broker

Mgmt Interface

15 | © 2016 InterSystems Corporation. All Rights Reserved.

Publishes

• Venduino/Ens/CARD

• Asks Details for an RFID Card

• Venduino/Ens/BUY

• Notifies a purchase

• Venduino/Ens/STATUS -> “ONLINE”

Suscribes

• Venduino/Dev/A#

• Venduino/Dev/A/CARD

• Details for an RFID Card

• ID | CustomerName | Balance

• Venduino/Dev/A/PRICE

• Price change Notification

• Venduino/Dev/A/OFFER

• Offer Notification

MQTT Venduino Topics

16 | © 2016 InterSystems Corporation. All Rights Reserved.

Publishes (Business Operation)

• Venduino/Dev/A/CARD

• Details reply for an RFID card

• Venduino/Dev/A/PRICE

• Price change notification

• Venduino/Dev/A/OFFER

• Special Offer notification

Suscribes (Business Service)

• Venduino/Ens/CARD

• Details request for an RFID Card

• Venduino/Ens/BUY

• Notification of a purchase

• Venduino/Ens/STATUS -> “ONLINE”

MQTT Ensemble Topics

Ensemble

17 | © 2016 InterSystems Corporation. All Rights Reserved.

Venduino: MQTT messages

Ensemble

Venduino/Ens/CARD ID=XX-XX-XX-XX

Venduino/A/CARD Jordi | $30

Venduino/Ens/BUY CardID | TRAY 2

Venduino/A/OFFER $1

Venduino/A/PRICE $3

18 | © 2016 InterSystems Corporation. All Rights Reserved.

• JavaInitiator: Allows to define Ensemble Components in Java (BS y BO)

• MqttSub: Business Service (Java) that subscribes to MQTT Topics

• MqttPub: Business Operation (Java) that sends messages to the MQTT Broker

• BPCard: Busines Process that processes messages from RFID Card

Ensemble Production

19 | © 2016 InterSystems Corporation. All Rights Reserved.

Ensemble Messages: Venduino/Ens/CARD

20 | © 2016 InterSystems Corporation. All Rights Reserved.

#include <PubSubClient.h>

PubSubClient client(ethClient);

//---Initialize

client.setServer(MqttServer, MqttServerPort);

client.connect(MQTTClientName);

//---Subscribe

void callback(char* topic, byte* payload, unsigned int length) { … }

client.setCallback(callback);

client.subscribe(topic);

//---Publish

client.publish(topic,content);

Arduino: MQTT Library

21 | © 2016 InterSystems Corporation. All Rights Reserved.

Venduino: Casing and Idea by Ryan Bates:

http://www.retrobuiltgames.com/the-build-page/arduino-vending-machine/

Modified with:

- Arduino Uno replaced with AT MEGA 2560

- Ethernet Shield added

- RFID reader added

- RGB led added

Venduino Connected to Ensemble through MQTT

22 | © 2016 InterSystems Corporation. All Rights Reserved.

Initialization

23 | © 2016 InterSystems Corporation. All Rights Reserved.

RFID Card Data from Server

24 | © 2016 InterSystems Corporation. All Rights Reserved.

Special Offer message

25 | © 2016 InterSystems Corporation. All Rights Reserved.

Connect a light through websockets

Amazon Alexa, REST, Caché, WebSockets, ESP8266

26 | © 2016 InterSystems Corporation. All Rights Reserved.

Connect a voice service to Caché

27 | © 2016 InterSystems Corporation. All Rights Reserved.

Connect a voice service to Caché

Caché AWS Lambda

Skill “Intersystems”

Amazon Echo, Echo Dot, or Raspberry

REST & JSON Text reply Alexa

voice service

WebSockets

ESP8266

HTTPS +Rest&Json

28 | © 2016 InterSystems Corporation. All Rights Reserved.

Class PYD.IOTDemo.WebSocketServer Extends %CSP.WebSocket {

Method Server() As %Status {

//---Read data if there is any. set len=32000, timeout=0.5 //500 ms timeout set data=..Read(.len,.tSC,.timeout)

if (ledStatusChanged=1) {

//---Send Status

set tSC=..Write(##class(PYD.IOTDemo.Data.LedStatus).getStatusAsString())

}

quit tSC

}

Caché WebSockets

29 | © 2016 InterSystems Corporation. All Rights Reserved.

#include "ESP8266WiFi.h"

#include "WebSocketClient.h“

WiFiClient client;

WebSocketClient webSocketClient;

char* socketURL="/csp/IOTSocket/PYD.IOTDemo.WebSocketServer.cls";

//--- Connect to server and establish WebSocket

if (client.connect(host, port)) { ... }

webSocketClient.path = socketURL;

webSocketClient.host = host;

if (webSocketClient.handshake(client)) { ... }

/---Write & Read Data

webSocketClient.getData(data);

webSocketClient.sendData(json);

ESP8266 WebSockets

30 | © 2016 InterSystems Corporation. All Rights Reserved.

• Alexa

• https://developer.amazon.com/

• Tutorials available…

• Alexa Skills Kit

• Invocation name “Intersystems”

• Utterances: <Text of the commands to recognize> + sinonims

• Intent Schema: <action to execute>

• Target: HTTPS o AWS Lambda

• AWS Lambda

• aws.amazon.com

• GitHub examples: amzn/alexa-skills-kit-js

Modified the javaScript server code to to add a REST call to Caché.

Amazon Alexa Skills & AWS Lambda

31 | © 2016 InterSystems Corporation. All Rights Reserved.

Thank you!

Code, sketches, and further questions: [email protected]