22
33microgear-esp8266-arduino คือ client library ที่ทำหน้าที่เป็นตัวกลางในการเชื่อมต่อ ESP8266 เข้ากับบริการของ netpie platform เพื่อการพัฒนา IOT application รายละเอียดเกี่ยวกับ netpie platform สามารถศึกษาได้จาก http://netpie.io
44
5+ ## ความเข้ากันได้
6+ ทางทีมพัฒนาได้ทำการทดสบพบว่า library สามารถใช้ได้กับอุปกรณ์ต่อไปนี้ (อาจมีมากกว่านี้)
7+ - ESP8266-01
8+ - ESP8266-12E
9+ - NodeMCU v1 และ v2
10+
511## การติดตั้ง
612* ดาวน์โหลด Arduino IDE 1.6.5 จาก https://www.arduino.cc/en/Main/Software
713* หลังจากติดตั้งเสร็จ เปิด Preferences
@@ -16,9 +22,12 @@ microgear-esp8266-arduino คือ client library ที่ทำหน้า
1622``` c++
1723#include < AuthClient.h>
1824#include < MicroGear.h>
25+ #include < MQTTClient.h>
26+ #include < SHA1.h>
1927#include < Arduino.h>
2028#include < ESP8266WiFi.h>
2129#include < EEPROM.h>
30+ #include < MicroGear.h>
2231
2332const char * ssid = <WIFI_SSID>;
2433const char * password = <WIFI_KEY>;
@@ -31,39 +40,43 @@ const char* password = <WIFI_KEY>;
3140WiFiClient client;
3241AuthClient *authclient;
3342
34- void msghandler (char * topic, uint8_t* msg, unsigned int msglen) {
43+ int timer = 0 ;
44+ MicroGear microgear (client);
45+
46+ void onMsghandler(char * topic, uint8_t* msg, unsigned int msglen) {
3547 Serial.print("Incoming message --> ");
36- Serial.print(topic);
37- for (int i=0; i<msglen; i++)
38- Serial.print((char)msg[ i] );
39- Serial.println();
48+ msg[ msglen] = '\0';
49+ Serial.println((char * )msg);
4050}
4151
42- void foundgear (char * attribute, uint8_t* msg, unsigned int msglen) {
52+ void onFoundgear (char * attribute, uint8_t* msg, unsigned int msglen) {
4353 Serial.print("Found new member --> ");
4454 for (int i=0; i<msglen; i++)
4555 Serial.print((char)msg[ i] );
4656 Serial.println();
4757}
4858
49- void lostgear (char * attribute, uint8_t* msg, unsigned int msglen) {
59+ void onLostgear (char * attribute, uint8_t* msg, unsigned int msglen) {
5060 Serial.print("Lost member --> ");
5161 for (int i=0; i<msglen; i++)
5262 Serial.print((char)msg[ i] );
53- Serial.println();
63+ Serial.println();
5464}
5565
56- int timer = 0;
57- MicroGear microgear(client);
66+ void onConnected(char * attribute, uint8_t* msg, unsigned int msglen) {
67+ Serial.println("Connected to NETPIE...");
68+ microgear.setName("mygear");
69+ }
5870
59- void setup() {
60- Serial.begin(115200);
6171
72+ void setup() {
6273 /* Event listener * /
63- microgear.on(MESSAGE,msghandler);
64- microgear.on(PRESENT,foundgear);
65- microgear.on(ABSENT,lostgear);
74+ microgear.on(MESSAGE,onMsghandler);
75+ microgear.on(PRESENT,onFoundgear);
76+ microgear.on(ABSENT,onLostgear);
77+ microgear.on(CONNECTED,onConnected);
6678
79+ Serial.begin(115200);
6780 Serial.println("Starting...");
6881
6982 if (WiFi.begin(ssid, password)) {
@@ -81,7 +94,6 @@ void setup() {
8194 //microgear.resetToken();
8295 microgear.init(GEARKEY,GEARSECRET,SCOPE);
8396 microgear.connect(APPID);
84- microgear.setName("mygear");
8597 }
8698}
8799
0 commit comments