วันอังคารที่ 12 มีนาคม พ.ศ. 2562

ทดลองการใช้ระบบ IOT ผ่าน esp 32

1.เข้าไปที่ https://netpie.io จากนั้นกดไปที่ Application




2.สร้าง Application ตามลูกศร



3.ตั้งชื่อโปรเจค และกด Create



3.สร้าง Application Key


4.สร้าง device key และ session key

  
 
5.ติดตั้ง library microgear โดยเข้าไปที่ Sketch ->  include library -> Manage Libraries
ค้นหาคำว่า microgear


6.แก้ไขโค้ดของโปรแกรม โดยนำรหัสจาก device key มาใส่ในโค้ด ตั้งชื่อ board และตั้งเป้าหมาย



#include <WiFi.h>
#include <MicroGear.h>
const char* ssid= "wifi_name";
const char* password = "wifi_password";

#define APPID   "tutorial"
#define KEY     "KnBSvq3QiWjJ9uE"
#define SECRET  "g8TmL3ZSeCBMNKyzJQ6coFrIv"
#define ALIAS   "Board1"
#define TARGET   "LivingRoom"
#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);
}

7.จัดการกับ FreeBoard เริ่มโดยการเข้าไปในเมนู FreeBoard


8.กดปุ่มบวกเพื่อเพิ่ม Dash Board จากนั้นตั้งชื่อ โดยไม่ให้ซ้ำ และกดปุ่ม Create

 

9.กดปุ่มประแจ และ กดปุ่ม Add

 

10. ใส่ชื่อ Data Source และ Key ที่เราได้มา( Session Key)

 

11.เมื่อเสร็จ เราจะเห็นเวลาเดินอยู่ แปลว่า Freeboard ได้เชื่อมกับตัว ESP32 แล้ว


12.จากนั้นกดที่ Add pane และเลือก Data Sorce  ตาม Data Source ไปตามปัจุบัน

 

13. ซึ่งเลือกให้เป็น datasources["FreeBoard1"]["/SmartMirror/gearname/LivingRoom"] และกดเข้าไปที่ .js editor


14. เพื่อตัดคำเพื่อตรวจสอบต่อท้ายเป็น .split(",")[0] == 'CH1ON' เนื่องจากบอร์ดส่งข้อความ เมื่อ LED สว่าง CH1ON คือข้อความที่ส่งไป NETPIE 


15. ใส่คำสั่งเมื่ออยู่ในสถานะ ON และ OFF ดังนี้
microgear["FreeBoard1"].chat("Board1","CH1ON")
microgear["FreeBoard1"].chat("Board1","CH1OFF")


16.คลิ้กเข้าไปที่ add pane และเลือก Indicator light


17.เลือก Data source 


18. ตัดคำสั่งตอ่ท้าย .split(",")[0] == 'CH1ON'


19*

ไม่มีความคิดเห็น:

แสดงความคิดเห็น