523372 Embedded
Systems
เรื่อง IoT platform : IBM
Watson
รหัสนักศึกษา B5711826 ชื่อ นางสาว ไอลดา
พลพฤกษ์
IoT platform: IBM Watson
source:ibminternetofthingsfi.tumblr.com
We can never expect
the Big Blue to miss on the
opportunity to making a mark in the internet of things segment. IBM Watson is
an IoT platform which is pretty much taken among developers already. Backed by
IBM’s hybrid cloud PaaS (platform as a service) development platform, the
Bluemix, Watson IoT enables developers to easily deploy IoT applications.
Users of IBM Watson get:
·
Device Management
·
Secure Communications
·
Real Time Data Exchange
·
Data Storage
·
Recently added data sensor and weather data service
Demo Video
Ø ข้อดี ข้อจำกัดของ IoT platform : Watson IoT
แพลตฟอร์ม Watson IoT ได้รับการออกแบบเพื่อลดความซับซ้อนในการพัฒนา
IoT ด้านความรู้ความเข้าใจเพื่อให้คุณสามารถใช้ประโยชน์อินเทอร์เน็ตได้อย่างเต็มที่
การสมัครใช้งาน IoT platform
Ø การสมัครใช้งาน IoT platform : IBM Watson
IoT Platfrom
ขั้นตอนการสมัคร
IBM Watson IoT Platform
1. เข้าไปที่ลิงค์ https://www.ibm.com/internet-of-things/
1.1เข้าไปที่ แล้วคลิกเลือก My
IBM
1.2
คลิกเลือก
1.3 กรอกข้อมูลดังรูปด่านล่าง
1.4.หลังจากนั้น
คลิกที่
1.5 หลังจากนั้น
ระบบจะส่งรหัสไปที่เมลเราที่เราได้กรอกไว้
1.6.
เข้าไปที่เมลเพื่อเอารหัส
1.7.
นำรหัสที่ได้ในเมลมากรอกดังรูป
1.8.
หลังจากกรอกรหัสยืนยันเสร็จแล้ว คลิกเลือก
เสร็จสิ้นการสมัคร
Ø เรียกใช้ ESP8266
/ Arduino เป็นอุปกรณ์ที่ได้รับการจัดการ IBM Watson IoT
Platform
ส่วนผสม ESP8266 เป็นขั้นเป็นตอน
1. 1. Intermediate
การเป็นอุปกรณ์ที่ได้รับการจัดการในแพลตฟอร์ม
IBM Watson IoT ช่วยเพิ่มประสิทธิภาพในการจัดการอุปกรณ์จากระยะไกลและทำสิ่งต่างๆเช่นการอัปเดตระยะไกลบูตเครื่องใหม่และดำเนินการวินิจฉัย สูตรนี้จะแสดงวิธีเริ่มต้นใช้งานการกำหนดค่า
ESP8266 / Arduino เป็นอุปกรณ์ที่มีการจัดการ
2. 2.ก่อนอื่นคุณต้องมี ESP8266
มีหลายประเภทให้เลือกทั้งหมดควรจะทำงานที่นี่ฉันใช้กระดานสไตล์
NodeMCU ซึ่งง่ายต่อการใช้ต้นแบบเนื่องจากมีการสนับสนุน
USB สำหรับเขียนโปรแกรมและเปิดเครื่อง ESP8266
3. 3.จากนั้นติดตั้ง Arduino IDE
ขอรับ Arduino
IDE ที่นี่ การใช้ IDE เวอร์ชัน 1.6.5 หรือที่ใหม่กว่าทำให้ง่ายต่อการเพิ่มการสนับสนุน
ESP8266 ไปยัง IDE ด้วย Arduino IDE ติดตั้งใช้ฟังก์ชั่น Board
Manager เพื่อเพิ่มการสนับสนุนสำหรับ ESP8266 ดูที่นี่สำหรับวิธีการทำเช่นนั้น https://github.com/esp8266/Arduino#installing-with-boards-manager
4. 4.เพิ่มไลบรารี MQTT PubSubClient
IBM Watson IoT
Platform ใช้โปรโตคอล MQTT เพื่อเพิ่มไลบรารี MQTT
PubSubClient ลงใน Arduino IDE นี้จะต้องเป็นรุ่นล่าสุดของห้องสมุดที่จะทำงานกับ
ESP8266 ผมใช้รุ่น 2.3 จากที่นี่
(คุณสามารถนำเข้า zip library ไปยัง
Arduino IDE จากตัวเลือกเมนู IDE Sketch -> รวม Library -> Add .ZIP Library)
5. 5. เพิ่มห้องสมุด ArduinoJson
IBM Watson IoT
Platform ใช้ข้อความที่จัดรูปแบบ JSON เพื่อให้รหัส
Arduino ง่ายกว่าการใช้ไลบรารีการแยกวิเคราะห์ Json ฉันใช้ ArduinoJson แล้วจึงเพิ่ม Arduino IDE
ลงไป ฉันใช้เวอร์ชัน 5.0.7 แต่เวอร์ชันล่าสุดของไลบรารีควรใช้งานได้ ค้นหาได้ที่นี่: https://github.com/bblanchon/ArduinoJson/releases/tag/v5.0.7
(คุณสามารถนำเข้า zip library ไปยัง
Arduino IDE จากตัวเลือกเมนู IDE Sketch -> รวม Library -> Add .ZIP Library)
6. 6. ลงทะเบียนเพื่อ Bluemix และแพลตฟอร์ม IBM Iats Watson
ถัดไปคุณต้องลงชื่อสมัครเข้าใช้
IBM Watson IoT Platform และสร้าง "องค์กร"
และ "อุปกรณ์" รายละเอียดนี้ได้อธิบายไว้ที่เว็บไซต์ IBM และเกี่ยวข้องกับการลงชื่อสมัครใช้
Bluemix และ Internet of Things Platform และใน Bluemix Catalog เพิ่ม Internet of
Things Service การทำเช่นนี้จะสร้าง
Organization ของคุณจากนั้นในแดชบอร์ด IoT คุณสามารถเพิ่มอุปกรณ์ได้ มีหลายทางเลือกในการตั้งค่าเกือบทั้งหมดสามารถเว้นว่างได้คุณเพียงต้องการอุปกรณ์ประเภทหนึ่งฉันใช้
"ESP8266" และ ID อุปกรณ์ฉันใช้
"Test1"
เมื่อสร้างอุปกรณ์แล้วคุณจะเห็นหน้าข้อมูลประจำตัวผู้ใช้อุปกรณ์และคุณต้องบันทึกรายละเอียดเหล่านั้นซึ่งใช้เพื่อปรับแต่งร่าง
Arduino ของคุณในขั้นตอนถัดไป
7. The Arduino Sketch
Copy the following sketch to the Arduino IDE:
/**
IBM IoT Foundation managed Device
Author: Ant Elder
License: Apache License v2
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // https://github.com/knolleary/pubsubclient/releases/tag/v2.3
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson/releases/tag/v5.0.7
//-------- Customise these values -----------
const char* ssid = "<yourWiFiSSID>";
const char* password = "<yourWiFiPassword>";
#define ORG "<yourOrg>"
#define DEVICE_TYPE "yourDeviceType"
#define DEVICE_ID "yourDevice"
#define TOKEN "yourDeviceToken"
//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
const char publishTopic[] = "iot-2/evt/status/fmt/json";
const char responseTopic[] = "iotdm-1/response";
const char manageTopic[] = "iotdevice-1/mgmt/manage";
const char updateTopic[] = "iotdm-1/device/update";
const char rebootTopic[] = "iotdm-1/mgmt/initiate/device/reboot";
void callback(char* topic, byte* payload, unsigned int payloadLength);
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
int publishInterval = 30000; // 30 seconds
long lastPublishMillis;
void setup() {
Serial.begin(115200); Serial.println();
wifiConnect();
mqttConnect();
initManagedDevice();
}
void loop() {
if (millis() - lastPublishMillis > publishInterval) {
publishData();
lastPublishMillis = millis();
}
if (!client.loop()) {
mqttConnect();
initManagedDevice();
}
}
void wifiConnect() {
Serial.print("Connecting to "); Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("nWiFi connected, IP address: "); Serial.println(WiFi.localIP());
}
void mqttConnect() {
if (!!!client.connected()) {
Serial.print("Reconnecting MQTT client to "); Serial.println(server);
while (!!!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
Serial.println();
}
}
void initManagedDevice() {
if (client.subscribe("iotdm-1/response")) {
Serial.println("subscribe to responses OK");
} else {
Serial.println("subscribe to responses FAILED");
}
if (client.subscribe(rebootTopic)) {
Serial.println("subscribe to reboot OK");
} else {
Serial.println("subscribe to reboot FAILED");
}
if (client.subscribe("iotdm-1/device/update")) {
Serial.println("subscribe to update OK");
} else {
Serial.println("subscribe to update FAILED");
}
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& d = root.createNestedObject("d");
JsonObject& metadata = d.createNestedObject("metadata");
metadata["publishInterval"] = publishInterval;
JsonObject& supports = d.createNestedObject("supports");
supports["deviceActions"] = true;
char buff[300];
root.printTo(buff, sizeof(buff));
Serial.println("publishing device metadata:"); Serial.println(buff);
if (client.publish(manageTopic, buff)) {
Serial.println("device Publish ok");
} else {
Serial.print("device Publish failed:");
}
}
void publishData() {
String payload = "{\"d\":{\"counter\":";
payload += millis() / 1000;
payload += "}}";
Serial.print("Sending payload: "); Serial.println(payload);
if (client.publish(publishTopic, (char*) payload.c_str())) {
Serial.println("Publish OK");
} else {
Serial.println("Publish FAILED");
}
}
void callback(char* topic, byte* payload, unsigned int payloadLength) {
Serial.print("callback invoked for topic: "); Serial.println(topic);
if (strcmp (responseTopic, topic) == 0) {
return; // just print of response for now
}
if (strcmp (rebootTopic, topic) == 0) {
Serial.println("Rebooting...");
ESP.restart();
}
if (strcmp (updateTopic, topic) == 0) {
handleUpdate(payload);
}
}
void handleUpdate(byte* payload) {
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((char*)payload);
if (!root.success()) {
Serial.println("handleUpdate: payload parse FAILED");
return;
}
Serial.println("handleUpdate payload:"); root.prettyPrintTo(Serial); Serial.println();
JsonObject& d = root["d"];
JsonArray& fields = d["fields"];
for (JsonArray::iterator it = fields.begin(); it != fields.end(); ++it) {
JsonObject& field = *it;
const char* fieldName = field["field"];
if (strcmp (fieldName, "metadata") == 0) {
JsonObject& fieldValue = field["value"];
if (fieldValue.containsKey("publishInterval")) {
publishInterval = fieldValue["publishInterval"];
Serial.print("publishInterval:"); Serial.println(publishInterval);
}
}
}
}
Customise the lines after “//——– Customise these values ———–”
with your WiFi network SSID and password and with the IoT Device Credentials
from the previous step.
กำหนดเส้นหลังจาก "// --- ปรับแต่งค่าเหล่านี้ ----" ด้วย SSID
และรหัสผ่านเครือข่าย Wi-Fi และ IoT
Device Credentials จากขั้นตอนก่อนหน้า
8. อัปโหลดและเรียกใช้ sketch
อัพโหลดสเก็ตช์ไปที่ ESP8266 แล้วเปิด Serial Monitor และคุณจะเห็นมันลงทะเบียนเป็นอุปกรณ์ที่มีการเก็บรวบรวมและเริ่มเผยแพร่ข้อมูลทุกๆ
30 วินาที
9.ดูข้อมูลที่เผยแพร่ใน IBM Dashboard แพลตฟอร์ม IoT ของ IBM Watson
คุณสามารถดูกิจกรรมและข้อมูลที่เผยแพร่จากอุปกรณ์ของคุณบนแผงควบคุม
IoT โดยไปที่อุปกรณ์ของคุณและคลิกที่อุปกรณ์:
10. กำหนดค่าอุปกรณ์ใหม่จากแผงควบคุมแพลตฟอร์ม IBM Watson IoT Platform Dashboard
เลื่อนลงไปที่ส่วนข้อมูลเมตาและคุณจะเห็นข้อมูลเมตา
JSON ที่ส่งจาก Arduino:
พิมพ์ค่า publishInterval เพื่อเปลี่ยนเป็น
10000 และคลิกที่ปุ่ม Confirm Changes และคุณจะเห็น
Arduino Serial Monitor ในคำขออัพเดตและ Arduino จะเริ่มเผยแพร่ข้อมูลทุกๆ 10 วินาที:
กลับไปที่ IoT
และเลื่อนไปทางขวาล่างสุดของมุมมองอุปกรณ์ไปยังส่วน Action และควรจะแสดงปุ่ม Reboot,
คลิกที่ Reboot
และคุณจะเห็นใน Arduino Serial Monitor การเริ่มต้นใหม่
ESP8266:
1. แค่นั้นแหละ!
หวังเป็นอย่างยิ่งว่าคุณจะได้รับคำแนะนำเบื้องต้นเกี่ยวกับการใช้
ESP8266 / Arduino เป็นอุปกรณ์ที่ได้รับการจัดการของ IBM
Watson IoT Platform
คุณสามารถดูข้อมูลเพิ่มเติมเกี่ยวกับ API การจัดการอุปกรณ์ในเอกสาร
IBM Watson IoT Platform ได้ที่: https://docs.internetofthings.ibmcloud.com/#/device-management-operations#device-management-operations