@@ -4,12 +4,12 @@ microgear-esp8266-arduino คือ client library ที่ทำหน้า
44
55## ความเข้ากันได้
66ทางทีมพัฒนาได้ทำการทดสบพบว่า library สามารถใช้ได้กับอุปกรณ์ต่อไปนี้ (อาจมีมากกว่านี้)
7- - ESP8266-01
8- - ESP8266-12E
9- - NodeMCU v1 และ v2
7+ - ESP8266-01, 07, 12E, 12F
8+ - NodeMCU v1, v2, V3
9+ - Espresso Lite v2.0
1010
1111## การติดตั้ง
12- * ดาวน์โหลด Arduino IDE 1.6.5 จาก https://www.arduino.cc/en/Main/Software
12+ * ดาวน์โหลด Arduino IDE 1.6.9 หรือใหม่กว่า จาก https://www.arduino.cc/en/Main/Software
1313* หลังจากติดตั้งเสร็จ เปิด Preferences
1414* ใส่ข้อความ ` http://arduino.esp8266.com/stable/package_esp8266com_index.json ` ลงในช่อง Additional Board Manager URLs
1515* เปิด Boards Manager เมนู Tools ค้นหาคำว่า ` esp8266 ` และคลิก install
@@ -18,24 +18,21 @@ microgear-esp8266-arduino คือ client library ที่ทำหน้า
1818* Unzip ไปวางใน folder Libraries ของ Arduino IDE
1919* รายละเอียดเพิ่มเติมเกี่ยวกับ ESP8266 Arduino IDE ศึกษาได้จาก https://github.com/esp8266/Arduino
2020
21- ตัวอย่างการเรียกใช้
21+ ** ข้อจำกัที่พบด**
22+ * ฟีเจอร์ TLS ทำงานได้บน ESP8266 SDK 2.1.0 ไม่ทำงานบนเวอร์ชั่น 2.2.0 แต่ทำงานได้บนเวอร์ชั่น 2.3.0-rc1
23+
24+ ** ตัวอย่างการเรียกใช้**
2225``` c++
23- #include < AuthClient.h>
24- #include < MicroGear.h>
25- #include < MQTTClient.h>
26- #include < SHA1.h>
27- #include < Arduino.h>
2826#include < ESP8266WiFi.h>
29- #include < EEPROM.h>
3027#include < MicroGear.h>
3128
3229const char * ssid = <WIFI_SSID>;
3330const char * password = <WIFI_KEY>;
3431
35- #define APPID <APPID>
32+ #define APPID <APPID >
3633#define KEY <APPKEY>
3734#define SECRET <APPSECRET >
38- #define ALIAS "myplant "
35+ #define ALIAS "esp8266 "
3936
4037WiFiClient client;
4138AuthClient *authclient;
@@ -44,28 +41,28 @@ int timer = 0;
4441MicroGear microgear (client);
4542
4643void onMsghandler(char * topic, uint8_t* msg, unsigned int msglen) {
47- Serial.print("Incoming message --> ");
48- msg[ msglen] = '\0';
49- Serial.println((char * )msg);
44+ Serial.print("Incoming message --> ");
45+ msg[ msglen] = '\0';
46+ Serial.println((char * )msg);
5047}
5148
5249void onFoundgear(char * attribute, uint8_t* msg, unsigned int msglen) {
53- Serial.print("Found new member --> ");
54- for (int i=0; i<msglen; i++)
55- Serial.print((char)msg[ i] );
56- Serial.println();
50+ Serial.print("Found new member --> ");
51+ for (int i=0; i<msglen; i++)
52+ Serial.print((char)msg[ i] );
53+ Serial.println();
5754}
5855
5956void onLostgear(char * attribute, uint8_t* msg, unsigned int msglen) {
60- Serial.print("Lost member --> ");
61- for (int i=0; i<msglen; i++)
62- Serial.print((char)msg[ i] );
63- Serial.println();
57+ Serial.print("Lost member --> ");
58+ for (int i=0; i<msglen; i++)
59+ Serial.print((char)msg[ i] );
60+ Serial.println();
6461}
6562
6663void onConnected(char * attribute, uint8_t* msg, unsigned int msglen) {
67- Serial.println("Connected to NETPIE...");
68- microgear.setName("mygear" );
64+ Serial.println("Connected to NETPIE...");
65+ microgear.setName(ALIAS );
6966}
7067
7168
@@ -81,53 +78,139 @@ void setup() {
8178
8279 if (WiFi.begin(ssid, password)) {
8380
84- while (WiFi.status() != WL_CONNECTED) {
85- delay(500);
86- Serial.print(".");
87- }
81+ while (WiFi.status() != WL_CONNECTED) {
82+ delay(500);
83+ Serial.print(".");
84+ }
85+ }
8886
89- Serial.println("WiFi connected");
90- Serial.println("IP address: ");
91- Serial.println(WiFi.localIP());
87+ Serial.println("WiFi connected");
88+ Serial.println("IP address: ");
89+ Serial.println(WiFi.localIP());
9290
93- microgear.init(KEY,SECRET,ALIAS);
94- microgear.connect(APPID);
95- }
91+ microgear.init(KEY,SECRET,ALIAS);
92+ microgear.connect(APPID);
9693}
9794
9895void loop() {
99- if (microgear.connected()) {
100- Serial.println("connected");
101- microgear.loop();
102- if (timer >= 1000) {
103- Serial.println("Publish...");
104- microgear.chat("mygear","Hello");
105- timer = 0;
106- }
107- else timer += 100;
108- }
109- else {
110- Serial.println("connection lost, reconnect...");
111- if (timer >= 5000) {
112- microgear.connect(APPID);
113- timer = 0;
96+ if (microgear.connected()) {
97+ Serial.println("connected");
98+ microgear.loop();
99+ if (timer >= 1000) {
100+ Serial.println("Publish...");
101+ microgear.chat(ALIAS,"Hello");
102+ timer = 0;
103+ }
104+ else timer += 100;
114105 }
115- else timer += 100;
116- }
117- delay(100);
106+ else {
107+ Serial.println("connection lost, reconnect...");
108+ if (timer >= 5000) {
109+ microgear.connect(APPID);
110+ timer = 0;
111+ }
112+ else timer += 100;
113+ }
114+ delay(100);
118115}
119116```
120117## การใช้งาน library
121118---
122- **microgear.init (*gearkey*, *gearsecret*, *alias*)**
119+ Initial library ด้วยคำสั่ง
120+
121+ **int MicroGear::init(char* *key*, char* *secret* [,char* *alias*])**
123122
124123**arguments**
125124* *key* `string` - เป็น key สำหรับ gear ที่จะรัน ใช้ในการอ้างอิงตัวตนของ gear
126125* *secret* `string` - เป็น secret ของ key ซึ่งจะใช้ประกอบในกระบวนการยืนยันตัวตน
127126* *alias* `string` - เป็นการระบุชื่อของ device
128127
129128```c++
130- microGear.init("sXfqDcXHzbFXiLk",
131- "DNonzg2ivwS8ceksykGntrfQjxbL98",
132- "myplant");
129+ microgear.init("sXfqDcXHzbFXiLk", "DNonzg2ivwS8ceksykGntrfQjxbL98", "myplant");
133130```
131+
132+ ---
133+
134+ ** void MicroGear::on(unsigned char event, void (* callback)(char* , uint8_t* ,unsigned int))**
135+
136+ เพิ่มฟังก์ชั่นที่ตอบสนองต่อ event
137+
138+ ** arguments**
139+ * * event* - ชืิ่อ event (MESSAGE|CONNECTED|PRESENT|ABSENT)
140+ * * callback* - ฟังก์ชั่น callback
141+
142+ ---
143+
144+ ** bool MicroGear::connect(char* appid)**
145+
146+ เชื่อต่อกับ NETPIE platform ถ้าเชื่อมต่อสำเร็จ จะมี event ชื่อ CONNECTED เกิดขึ้น
147+
148+ ** arguments**
149+ * * appidt* - App ID.
150+
151+ ---
152+
153+ ** bool MicroGear::connected(char* appid)**
154+
155+ ส่งค่าสถานะการเชื่อมต่อ เป็น true หากกำลังเชื่อมต่ออยู่
156+
157+ ** arguments**
158+ * * appidt* - App ID.
159+
160+ ---
161+
162+ ** void MicroGear::setAlias(char* alias)**
163+
164+ microgear สามารถตั้งนามแฝงของตัวเองได้ ซึ่งสามารถใช้เป็นชื่อให้คนอื่นเรียกในการใช้ฟังก์ชั่น chat() และชื่อที่ตั้งในโค้ด จะไปปรากฎบนหน้าจัดการ key บนเว็บ netpie.io อย่างอัตโนมัติ
165+
166+ ** arguments**
167+ * * alias* - ชื่อของ microgear นี้
168+
169+ ---
170+
171+ ** void MicroGear::chat(char* target, char* message)**
172+
173+ ** arguments**
174+ * * target* - ชื่อของ microgear ที่ต้องการจะส่งข้อความไปถึง
175+ * * message* - ข้อความ
176+
177+ ---
178+
179+ ** void MicroGear::publish(char* topic, char* message [ , bool retained] )**
180+
181+ ในกรณีที่ต้องการส่งข้อความแบบไม่เจาะจงผู้รับ สามารถใช้ฟังชั่น publish ไปยัง topic ที่กำหนดได้ ซึ่งจะมีแต่ microgear ที่ subscribe topoic นี้เท่านั้น ที่จะได้รับข้อความ
182+
183+ ** arguments**
184+ * * topic* - ชื่อของ topic ที่ต้องการจะส่งข้อความไปถึง
185+ * * message* - ข้อความ
186+ * * retained* - ให้ retain ข้อความไว้หรือไม่ default เป็น false (optional)
187+
188+ ---
189+
190+ ** void MicroGear::subscribe(char* topic)**
191+
192+ microgear อาจจะมีความสนใจใน topic ใดเป็นการเฉพาะ เราสามารถใช้ฟังก์ชั่น subscribe() ในการบอกรับ message ของ topic นั้นได้ และหาก topic นั้นเคยมีการ retain ข้อความไว้ microgear จะได้รับข้อความนั้นทุกครั้งที่ subscribe topic
193+
194+ ** arguments**
195+ * * topic* - ชื่อของ topic ที่ต้องการจะส่งข้อความไปถึง
196+
197+ ---
198+
199+ ** void MicroGear::unsubscribe(char* topic)**
200+
201+ ยกเลิกการ subscribe
202+
203+ ** arguments**
204+ * * topic* - ชื่อของ topic ที่ต้องการจะส่งข้อความไปถึง
205+
206+ ---
207+
208+ ** void MicroGear::resetToken()**
209+
210+ ส่งคำสั่ง revoke token ไปยัง netpie และลบ token ออกจาก cache ส่งผลให้ microgear ต้องขอ token ใหม่ในการเชื่อมต่อครั้งต่อไป
211+
212+ ---
213+
214+ ** void MicroGear::loop()**
215+
216+ method นี้ควรถูกเรียกใน arduino loop() เป็นระยะๆ เพื่อที่ microgear library จะได้ keep alive connection alive และจัดการกับ message ที่เข้ามา
0 commit comments