งานครั้งที่ 46 [iot#21 NETPIE] ควบคุมการติดดับ LED 1 ตัวด้วยวิธี Chat
การสื่อสารภายในกลุ่มเป็นการรับส่งข้อความไปมาหากันซึ่งเป็นข้อความอะไรก็ได้ ขึ้นอยู่กับผู้พัฒนาจะออกแบบ แต่จะต้องเป็นที่เข้าใจระหว่างกัน เช่น ข้อความว่าคำว่า ON ผู้พัฒนาเป็นผู้กำหนดให้หมายถึงสิ่งใดในอุปกรณ์ในกลุ่ม โดยลักษณะการส่งข้อความแบ่งตามจุดหมายปลายทางแบ่งได้สองแบบ คือ
1. ข้อความที่ส่งแบบเจาะจงผู้รับ การส่งลักษณะนี้จะใช้ฟังก์ชั่น microgear.chat()
2. ข้อความที่ส่งแบบไม่เจาะจง่ผู้รับ แต่ส่งเข้าหาสิ่งที่เรียกว่า topic การส่งลักษณะนี้จะใช้ฟังก์ชั่น microgear.publish()
งานครั้งนี้เป็นการทดลองการส่งข้อความแบบเจาะจงผู้รับคือการ chat
คุณสมบัติของการ Chat คือการส่งข้อความโดยระบุผู้รับมีลักษณะดังนี้
1. เมื่อผู้รับออนไลน์อยู่แล้วผู้ส่งได้ส่งข้อความไปหาผู้รับโดยระบุชื่อผู้รับ ผู้รับจะได้รับข้อความนั้น
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/a.png)
2. เมื่อผู้รับออฟไลน์ (ออกจากระบบ)ผู้ส่งได้ส่งข้อความไปหาผู้รับโดยระบุชื่อผู้รับ ผู้รับจะไม่ได้รับข้อความนั้น
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/b.png)
3. เมื่อผู้รับออนไลน์และใช้ชื่อเดียวกัน ผู้ส่งได้ส่งข้อความไปหาผู้รับโดยระบุชื่อผู้รับ ผู้รับที่ชื่อเดียวกันจะได้รับข้อความนั้น
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/c.png)
คำเตือน: ความถี่ในการส่งข้อความขึ้น NETPIE กำหนดส่งได้ไม่เกิน 4 ครั้งใน 1 วินาที หากผู้ใช้งานส่งเกินข้อความครั้งที่ส่งเกิน NETPIE จะไม่รับข้อความที่เกินนั้น ๆ (เรียกว่าติดลิมิต)
คอนเซ็ปต์หลักของงาน
บอร์ดรับข้อความจาก FreeBoard NETPIE มาควบคุมการติดดับของ LED และให้บอร์ดส่งสถานะของ LED ไปแสดงบน FreeBoard
[ขั้นตอนการดำเนินการ]
-สร้าง Application ID
-สร้าง Device Key สำหรับใช้กับบอร์ดทดลอง
-สร้าง Session Key สำหรับใช้กับ FreeBoard
-เขียนโค้ดเพื่อใช้งาน
-ออกแบบ FreeBoard
ซึ่งรายละเอียดเป็นดังนี้
วงจรที่ใช้ทดลอง ทั้งที่เป็นบอร์ด NodeMCU และ WeMOS D1 mini ที่ใช้งานเชื่อมต่อเดิมในงานนั้น ๆ ตัวอย่างเช่น
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/1.png)
ส่วนของการดำเนินการ NETPIE
1. เริ่มต้นการใช้งานโดยเข้าไปที่ https://netpie.io/ (หากยังไม่สมัครให้ดำเนินการ)
(1) ล็อกอิน
(2) คลิกที่ APPLICATIONS
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/2.png)
2. คลิกสร้าง Application คลิกที่ตำแหน่งดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/3.png)
3. ใส่รายละเอียดของ Application
(1) ตั้งชื่อ Application (ชื่อที่ไม่ซ้ำกับใคร ๆ)
(2) คลิก CREATE
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/4.png)
4. สร้าง Application Key คลิกที่ตำแหน่งดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/5.png)
5. สร้าง Application Key (Device Key)
(1) ตั้งชื่อ
(2) เลือกชนิด (เลือกเป็น Device Key)
(3) คลิก CREATE
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/6.png)
6. สร้าง Application Key (Session Key)
(1) ตั้งชื่อ
(2) เลือกชนิด (เลือกเป็น Session Key)
(3) คลิก CREATE
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/7.png)
7. คลิกที่ชื่อ Application Device
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/8.png)
8. คลิกที่ (1) เพื่อดูรหัส นำรหัสที่ปรากฏไปใช้ในโค้ดโปรแกรม
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/9.png)
ส่วนจัดการโค้ด
9. ติดตั้งไลบรารี่ใช้งาน NETPIE ดำเนินการดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/10a.png)
10. พิมพ์ในช่องค้นหาว่า microgear
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/10b.png)
11. แก้ไขรายละเอียดของโปรแกรม
(1) นำค่าจากขั้นตอน 3,8 มาใส่ในโค้ด
(2) กำหนดชื่อของบอร์ด
(3) กำหนดชื่อเป้าหมายที่ต้องการสื่อสาร
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/10c.png)
โค้ดโปรแกรม
#include <ESP8266WiFi.h>
#include <MicroGear.h>
const char* ssid= "wifi_name";
const char* password = "wifi_password";
#define APPID "Application_ID"
#define KEY "Application_ KEY"
#define SECRET "Application_SECRET"
#define ALIAS "Board_name"
#define TARGET "Target_name"
#define LED D1
WiFiClient client;
int timer = 0;
MicroGear microgear(client);
void onMsghandler(char* topic, uint8_t* msg, unsigned int msglen){
Serial.print("Incoming message --> ");
Serial.print(topic);
Serial.print(" : ");
char strState[msglen];
for(int i= 0; i< msglen; i++){
strState[i]=(char)msg[i];
Serial.print((char)msg[i]);
}
Serial.println();
String stateStr = String(strState).substring(0, msglen);
if(stateStr == "CH1ON")
{
digitalWrite(LED,HIGH);
Serial.println("ON LED");
}
else if(stateStr == "CH1OFF")
{
digitalWrite(LED,LOW);
Serial.println("OFF LED");
}
}
void onFoundgear(char* attribute, uint8_t* msg, unsigned int msglen){
Serial.print("Found new member --> ");
for(int i=0; i<msglen; i++)
Serial.print((char)msg[i]);
Serial.println();
}
void onLostgear(char* attribute, uint8_t* msg, unsigned int msglen){
Serial.print("Lost member --> ");
for(int i=0; i<msglen; i++)
Serial.print((char)msg[i]);
Serial.println();
}
/*When a microgear is connected, dothis*/
void onConnected(char* attribute, uint8_t* msg, unsigned int msglen){
Serial.println("Connected to NETPIE...");
/* Set the alias of this microgearALIAS */
microgear.setAlias(ALIAS);
}
void setup()
{
microgear.on(MESSAGE,onMsghandler);
microgear.on(PRESENT,onFoundgear);
microgear.on(ABSENT,onLostgear);
microgear.on(CONNECTED,onConnected);
Serial.begin(115200);
Serial.println("Starting...");
pinMode(LED,OUTPUT);
digitalWrite(LED,LOW);
if(WiFi.begin(ssid, password))
{
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
}
Serial.println("WiFiconnected");
microgear.init(KEY,SECRET,ALIAS);
microgear.connect(APPID);
}
void loop()
{
if(microgear.connected()){
microgear.loop();
bool led = digitalRead(LED);
String status_LED;
if(led == HIGH){
status_LED="CH1ON,";
}
else{
status_LED="CH1OFF,";
}
String data2freeboard = status_LED ;
microgear.chat(TARGET,data2freeboard);
Serial.print("Send message to NetPie: ");
Serial.println(data2freeboard);
}
else{
Serial.println("connection lost, reconnect...");
if(timer >= 5000){
microgear.connect(APPID);
timer=0;
}
else timer += 100;
}
delay(1000);
}
ส่วนจัดการ FreeBoard
12. คลิกเลือก FREEBOARDS ดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/11.png)
13. คลิกสร้าง FreeBoard ดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/12.png)
12. ตั้งชื่อ FreeBoard (ชื่อต้องไม่ซ้ำใคร)
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/13.png)
13. คลิกที่ชื่อ FreeBoard
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/14.png)
14. คลิกที่รูปประแจ
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/15.png)
15. คลิก ADD (Data Source)
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/16.png)
16. เลือก NETPIE Microgear
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/17.png)
17. เปิดหน้า Application ทำการคลิกที่ Session key เพื่อดูรหัส
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/18.png)
18. ใส่รายละเอียด
(1) ตั้งชื่อ Data Source
(2) ใส่ Key ต่าง ๆ จากข้อ 17
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/19.png)
สร้างปุ่มกดเปิด/ปิดแบบ Toggle
19. เมื่อบอร์ดเชื่อมต่อตัวเลขเวลาจะเดิน ทำการคลิก ADD PANE
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/20.png)
20. คลิกที่รูปบวก
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/21.png)
21. เลือก Toggle
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/22.png)
22. คลิกที่ Data Source จะปรากฏรายการชื่อให้เลือกชื่อที่ตรงกับ Data Source ที่ใช้งานปัจจุบันหน้านี้
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/23.png)
23. คลิกต่อ ให้สังเกตุรายการ Target ที่ปรากฏ โดยชื่อ Target จะขึ้นได้เมื่อบอร์ดเชื่อมต่อแล้ว(ตัวเลขนาฬิกาเดิน) แล้วคลิกที่ JS EDITOR
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/24.png)
24. เพื่อคำสั่งตัดคำเพื่อตรวจสอบต่อท้ายเป็น .split(",")[0]=='CH1ON' เนื่องจากบอร์ดส่งข้อความนี้ขึ้นมาเมื่อ LED ติดสว่าง
CH1ON คือข้อความที่บอร์ดส่งขึ้นมายัง NETPIE ซึ่งผู้ใช้งานสามารถใช้ข้อความอื่นได้ตามต้องการ
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/25.png)
25. รายละเอียดที่มาของค่าต่าง ๆ
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/26.png)
26. ใส่คำสั่งเมื่อสวิตช์อยู่ในสถานะ ON และ OFF โดย DataSource ที่ชื่อว่า FreeBoard1 ส่งข้อความว่า CH1ON ไปยัง Board1 รูปแบบ
microgear["FreeBoard1"].chat("Board1","CH1ON")
microgear["FreeBoard1"].chat("Board1","CH1OFF")
CH1ON,CH1OFF เป็นข้อความที่ FreeBoard ส่งไปยังบอร์ดโดยที่ตัวบอร์ดต้องเขียนโค้ดตรวจสอบคำนี้เพื่อใช้ควบคุมงาน ผู้ใช้งานสามารถใช้ข้อความอื่น ๆ ได้ตามความต้องการ
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/27.png)
27. รายละเอียดของปุ่ม ON
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/28.png)
28. ผลจะได้ ลองทดสอบโดยการคลิก สังเกตผลที่เกิดขึ้นที่บอร์ด
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/29.png)
29. เมื่อคลิก OFF
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/30.png)
สร้างหลอดแสดงผล
30. คลิกตามลำดับ
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/31.png)
31. เลือกดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/32.png)
32. คลิกที่ Data Source จะปรากฏรายการชื่อให้เลือกชื่อที่ตรงกับ Data Source ที่ใช้งานปัจจุบันหน้าน
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/33.png)
33. คลิกต่อ ให้สังเกตุรายการ Target ที่ปรากฏ โดยชื่อ Target จะขึ้นได้เมื่อบอร์ดเชื่อมต่อแล้ว(ตัวเลขนาฬิกาเดิน)
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/34.png)
34. คลิกที่ JS EDITOR
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/35.png)
35. เพื่อคำสั่งตัดคำเพื่อตรวจสอบต่อท้ายเป็น .split(",")[0]=='CH1ON' เนื่องจากบอร์ดส่งข้อความนี้ขึ้นมาเมื่อ LED ติดสว่าง
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/36.png)
36. ผลจะได้ ลองทดสอบโดยการคลิกสวิตช์สังเกตผลที่เกิดขึ้นที่บอร์ดและที่หลอดไฟที่สร้างขึ้น
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/37.png)
สร้างสวิตช์กด
37. คลิก ADD PANE
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/38.png)
38. เลือก Button
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/39.png)
39. ใส่รายละเอียด สวิตช์ ON
(1) ข้อความแสดงบนตัวสวิตช์
(2) เลือกสี
(3) ใส่คำสั่งเมื่อปุ่มถูกกด
microgear["FreeBoard1"].chat("Board1","CH1ON")
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/40.png)
40. เฟิ่มสวิตช์
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/41.png)
41. ใส่รายละเอียดเช่นเดียวกับข้อ 39 เพียงแต่เป็นสวิตช์ OFF
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/42.png)
42. เมื่อสร้างเสร็จ
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/43.png)
43. ทดสอบการทำงาน พร้อมดูผลที่บอร์ด
(http://www.praphas.com/PhotoForum/iot/Lab-46-Netpie1LED/44.gif)