บทความประกอบการเรียนรู้ => IOT : Internet of Thing (ESP8266, NodeMCU, WeMos D1 mini) => ข้อความที่เริ่มโดย: admin ที่ พฤษภาคม 14, 2018, 11:28:34 AM

หัวข้อ: งานครั้งที่ 47 [iot#22 NETPIE] วิธีส่งข้อมูลจำนวนมากขึ้น NETPIE แบบไม่ติดลิมิต
เริ่มหัวข้อโดย: admin ที่ พฤษภาคม 14, 2018, 11:28:34 AM
งานครั้งที่ 47 [iot#22 NETPIE] วิธีส่งข้อมูลจำนวนมากขึ้น NETPIE แบบไม่ติดลิมิต
NETPIE ยอมให้มีการส่งค่าได้สูงสุด 4 ครั้งใน 1 วินาที (เพื่อป้องกันการโจมตีเซิร์ฟเวอร์) ดังนั้นการส่งข้อมูลที่เกิน 4 ครั้งในครั้งที่เกิน NETPIE จะไม่รับค่านั้น วิธีการแก้ไขสามารถทำได้โดยการรวบรวมข้อมูลหลาย ๆ อย่างที่ต้องการส่งเป็นข้อความเดียวแล้วส่งไปในครั้งเดียวแทนการส่งข้อมูลหลายครั้ง

ลักษณะการส่งข้อมูล
1. กรณีที่ส่งข้อมูลมากกว่า 4 ครั้งใน 1 วินาที ข้อมูลในครั้งที่เกิน NETPIE จะไม่รับ (ข้อมูลนั้นจะสูญหาย)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/1.png)

2. วิธีการแก้ไขทำได้โดยการรวมข้อมูลให้เป็นข้อความเดียวแล้วส่งไปในครั้งเดียวแทน
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/2.png)

คอนเซ็ปต์หลักของงาน
บอร์ดตรวจสอบสถานะของขาพอร์ตแล้วรวบรวมสถานะเป็นข้อความเดียวกันโดยแยกข้อมูลดัวยเครื่องหมาย , ก่อนส่งให้ NETPIE

[ขั้นตอนการดำเนินการ]
-สร้าง Application ID
-สร้าง Device Key สำหรับใช้กับบอร์ดทดลอง
-สร้าง Session Key สำหรับใช้กับ FreeBoard
-เขียนโค้ดเพื่อใช้งาน
-ออกแบบ FreeBoard

ซึ่งรายละเอียดเป็นดังนี้
วงจรที่ใช้ทดลอง ทั้งที่เป็นบอร์ด NodeMCU และ WeMOS D1 mini ที่ใช้งานเชื่อมต่อเดิมในงานนั้น ๆ ตัวอย่างเช่น
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/3.png)

ส่วนของการดำเนินการ NETPIE
1. เริ่มต้นการใช้งานโดยเข้าไปที่ https://netpie.io/ (หากยังไม่สมัครให้ดำเนินการ)
   (1) ล็อกอิน
   (2) คลิกที่ APPLICATIONS
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/4.png)

2. คลิกสร้าง Application คลิกที่ตำแหน่งดังรูป (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/5.png)

3. ใส่รายละเอียดของ Application  (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
   (1) ตั้งชื่อ Application (ชื่อที่ไม่ซ้ำกับใคร ๆ)
   (2) คลิก CREATE
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/6.png)

4. สร้าง Application Key  คลิกที่ตำแหน่งดังรูป  (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/7.png)

5. สร้าง Application Key (Device Key)  (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
   (1) ตั้งชื่อ
   (2) เลือกชนิด (เลือกเป็น Device Key)
   (3) คลิก CREATE
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/8.png)

6. สร้าง Application Key (Session Key)  (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
   (1) ตั้งชื่อ
   (2) เลือกชนิด (เลือกเป็น Session Key)
   (3) คลิก CREATE
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/9.png)

7. คลิกที่ชื่อ Application Device   (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/10.png)

8. คลิกที่ (1) เพื่อดูรหัส นำรหัสที่ปรากฏไปใช้ในโค้ดโปรแกรม
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/11.png)

ส่วนจัดการโค้ด
9. แก้ไขรายละเอียดของโปรแกรม
   (1) นำค่าจากขั้นตอน 3,8 มาใส่ในโค้ด
   (2) กำหนดชื่อของบอร์ด
   (3) กำหนดชื่อเป้าหมายที่ต้องการสื่อสาร
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/12.png)

10. โค้ดในส่วนที่ใช้ตรวจสอบข้อความที่ได้รับมาจาก NETPIE เพื่อควบคุมการติดดับของ LED
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/13.png)

11. โค้ดในส่วนที่ส่งข้อความเข้า NETPIE
   (1) ตรวจสอบสถานะของลอจิกที่ขาพอร์ตแล้วกำหนดข้อความเป็น 1, และ 0, แทนสถานะ
   (2) รวบรวมสถานะของขาพอร์ตทุกขาไว้ในตัวแปรเดียว
   (3) ส่งข้อความขึ้น NETPIE
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/14.png)

โค้ดโปรแกรม
โค๊ด: [Select]
#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   "Board1"
#define TARGET   "LivingRoom"
#define LED1     D1
#define LED2     D2
#define LED3     D3
#define LED4     D4
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]);
  }
  String stateStr = String(strState).substring(0, msglen);
  Serial.println(stateStr);
  if (stateStr == "LED1ON") digitalWrite(LED1, HIGH);
  else if (stateStr == "LED1OFF") digitalWrite(LED1, LOW);
  else if (stateStr == "LED2ON") digitalWrite(LED2, HIGH);
  else if (stateStr == "LED2OFF") digitalWrite(LED2, LOW);
  else if (stateStr == "LED3ON") digitalWrite(LED3, HIGH);
  else if (stateStr == "LED3OFF") digitalWrite(LED3, LOW);
  else if (stateStr == "LED4ON") digitalWrite(LED4, HIGH);
  else if (stateStr == "LED4OFF") digitalWrite(LED4, LOW);
}
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(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);     
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, 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();
    String status_LED1,status_LED2,status_LED3,status_LED4,data2freeboard;
    status_LED1= digitalRead(LED1)? "1,":"0,";
    status_LED2= digitalRead(LED2)? "1,":"0,";
    status_LED3= digitalRead(LED3)? "1,":"0,";
    status_LED4= digitalRead(LED4)? "1,":"0,";           
    data2freeboard = status_LED1+status_LED2+status_LED3+status_LED4 ;
    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);
}

12. อัพโหลดโปรแกรม แล้วเปิด Serial monitor
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/15.png)

ส่วนจัดการ FreeBoard
13. คลิกสร้าง FreeBoard ดังรูป (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 และต้องการใช้ตัวเดิมให้ข้ามขั้นตอนนี้ไป)
   -คลิก RESOURCES เลือก FREEBOARDS
 (http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/16.png)
   -คลิกที่ +
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/17.png)

12. ตั้งชื่อ FreeBoard (ชื่อต้องไม่ซ้ำใคร) (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/18.png)

13. คลิกที่ชื่อ FreeBoard
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/19.png)

14. คลิกที่รูปประแจ
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/20.png)

15. คลิก ADD (Data Source)   (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 47 และต้องการใช้ตัวเดิมจะมีข้อมูลอยู่แล้วให้ข้ามขั้นตอนนี้ไป)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/21.png)

16. เลือก NETPIE Microgear   (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/22.png)

17. เปิดหน้า Application ทำการคลิกที่ Session key เพื่อดูรหัส   (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/23.png)

18. ใส่รายละเอียด   (หากผู้ใช้งานได้ทำแล้วในงานครั้งที่ 46 ข้ามขั้นตอนนี้ไป)
   (1) ตั้งชื่อ Data Source
   (2) ใส่ Key ต่าง ๆ จากข้อ 17
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/24.png)

สร้างกระดานแสดงข้อความ Text
19. เมื่อบอร์ดเชื่อมต่อตัวเลขเวลาจะเดิน ทำการคลิก ADD PANE
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/25.png)

20. คลิกที่รูปบวก
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/26.png)

21. เลือก Text
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/27.png)

22. คลิกที่ Data Source จะปรากฏรายการชื่อให้เลือกชื่อที่ตรงกับ Data Source ที่ใช้งานปัจจุบันหน้านี้
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/28.png)

23. คลิกต่อ ให้สังเกตุรายการ Target ที่ปรากฏ เลือกชื่อ Target ให้ตรงกับโค้ดที่เขียนบนบอร์ด โดยชื่อ Target จะขึ้นได้เมื่อบอร์ดเชื่อมต่อแล้ว(ตัวเลขนาฬิกาเดิน)
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/29.png)

สร้างปุ่มกดแบบ Toggle
24. ข้อความที่ส่งมาจากบอร์ดจะปรากฏ ทำการเพิ่มวิตเจ็ตโดยกดที่ ADD PANE
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/30.png)

25. คลิกที่ +
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/31.png)

26. เลือก Toggle
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/32.png)

27. รายละเอียด
   (1) ใส่ชื่อสวิตช์เป็น LED1
   (2) คลิกที่ DATASOURCE
   (3) เลือกชื่อ DataSource ที่เราสร้างไว้
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/33.png)

28. ต่อ..
   (1) รายละเอียด Target จะปรากฏขึ้น ให้เลือก Target ตัวที่เขียนไว้ในโค้ด (จะขึ้นก็ต่อเมื่อบอร์ดส่งค่าขึ้น NETPIE สังเกตจากตัวเลขเวลา)
   (2) คลิก JS EDITOR
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/34.png)

29. เพิ่มคำสั่งเพื่อตัดข้อความที่รับเข้ามาโดยเอาเฉพาะตัวแรกที่ส่งมาตรวจสอบว่าเป็นค่า '1' ซึ่งเป็นค่าที่ส่งมาหรือไม่ ใช้คำสั่ง
   .split(",")[0]=='1'
   หมายถึงแยกข้อความด้วยเครื่องหมาย , และข้อมูลแรกเป็น '1' หรือไม่
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/35.png)

30. ใส่คำสั่งที่ต้องการให้ทำเมื่อคลิก ON และ OFF ในที่นี้ต้องการให้ DataSouce ส่งค่าไปยังบอร์ดที่ชื่อว่า Board1 ว่า LED1ON เมื่อคลิก ON และส่งคำว่า LED1OFF เมื่อคลิก OFF รูปแบบดังนี้
   microgear["FreeBoard1"].chat("Board1","LED1ON")
   microgear["FreeBoard1"].chat("Board1","LED1OFF")
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/36.png)

31. ทำการคลิกทดสอบแล้วดูผลที่บอร์ด  แล้วคลิก + เพื่อเพิ่มสวิตช์ตัวที่ 2,3,4 ตามลำดับ คำสั่งเพิ่มในแต่ละปุ่มเป็นดังนี้
   .split(",")[1]=='1'       สำหรับสวิตช์ตัวที่ 2
   .split(",")[2]=='1'       สำหรับสวิตช์ตัวที่ 3
   .split(",")[3]=='1'       สำหรับสวิตช์ตัวที่ 4
ส่วนคำสั่งเมื่อกดสวิตช์ ON และ OFF ใช้คำสั่งแบบสวิตช์ตัวที่ 1 เพียงแต่แก้เป็น LED2ON,LED2OFF,LED3ON,LED3OFF....
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/37.png)

สร้างหลอดแสดงผล
32. ทดลองคลิกสวิตช์แล้วดูผลที่บอร์ด คลิกที่ ADD PANE เพื่อเพิ่มวิตเจ็ต
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/38.png)

33. คลิก +
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/39.png)

34. คลิกเลือก Indicator Light
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/40.png)

35. รายละเอียด
   (1) ใส่ชื่อสวิตช์เป็น LED1
   (2) คลิกที่ DATASOURCE
   (3) เลือกชื่อ DataSource ที่เราสร้างไว้
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/41.png)

36. ต่อ..
   (1) รายละเอียด Target จะปรากฏขึ้น ให้เลือก Target ตัวที่เขียนไว้ในโค้ด (จะขึ้นก็ต่อเมื่อบอร์ดส่งค่าขึ้น NETPIE สังเกตจากตัวเลขเวลา)
   (2) คลิก JS EDITOR
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/42.png)

37. เพิ่มคำสั่งเพื่อตัดข้อความที่รับเข้ามาโดยเอาเฉพาะตัวแรกที่ส่งมาตรวจสอบว่าเป็นค่า '1' ซึ่งเป็นค่าที่ส่งมาหรือไม่ ใช้คำสั่ง
   .split(",")[0]=='1'
   หมายถึงแยกข้อความด้วยเครื่องหมาย , และข้อมูลแรกเป็น '1' หรือไม่
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/43.png)

38. ทดสอบโดยคลิกสวิตช์ LED1 แล้วดูผลที่บอร์ด  แล้วคลิก + เพื่อเพิ่มหลอด์ตัวที่ 2,3,4 ตามลำดับ คำสั่งเพิ่มในแต่ละปุ่มเป็นดังนี้
   .split(",")[1]=='1'       สำหรับหลอดไฟตัวที่ 2
   .split(",")[2]=='1'       สำหรับหลอดไฟตัวที่ 3
   .split(",")[3]=='1'       สำหรับหลอดไฟตัวที่ 4
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/44.png)

39. ทดสอบโดยคลิกสวิตชแต่ละตัว แล้วดูผลที่บอร์ดและหน้า FreeBoard
(http://www.praphas.com/PhotoForum/iot/Lab-47-Netpie4LED/45.png)