From d8fb8df876c9aa22be30b4747fb2f9409c2b08cd Mon Sep 17 00:00:00 2001 From: NETPIE Date: Tue, 10 Apr 2018 16:06:52 +0700 Subject: [PATCH 1/5] add pushOwner --- MicroGear.cpp | 20 ++++- MicroGear.h | 5 ++ microgear-esp8266-arduino.ino | 156 ++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 microgear-esp8266-arduino.ino diff --git a/MicroGear.cpp b/MicroGear.cpp index ef4d98f..1a73975 100644 --- a/MicroGear.cpp +++ b/MicroGear.cpp @@ -712,7 +712,6 @@ bool MicroGear::writeFeed(char* feedname, char *data) { bool MicroGear::writeFeed(char* feedname, String data, char* apikey) { char buff[MAXBUFFSIZE]; data.toCharArray(buff,MAXBUFFSIZE-1); - return writeFeed(feedname, buff, apikey); } @@ -720,6 +719,25 @@ bool MicroGear::writeFeed(char* feedname, String data) { return writeFeed(feedname, data, NULL); } +bool MicroGear::pushOwner(char *message) { + char buff[MAXBUFFSIZE] = "/@push/owner"; + return publish(buff, message); +} + +bool MicroGear::pushOwner(double message) { + return pushOwner(message); +} + +bool MicroGear::pushOwner(int message) { + return pushOwner(message); +} + +bool MicroGear::pushOwner(String message) { + char buff[MAXBUFFSIZE]; + message.toCharArray(buff,MAXBUFFSIZE-1); + return pushOwner(buff); +} + /* setName() is deprecated */ diff --git a/MicroGear.h b/MicroGear.h index 366adb3..166cc60 100644 --- a/MicroGear.h +++ b/MicroGear.h @@ -130,6 +130,11 @@ class MicroGear { bool writeFeed(char*, String); bool writeFeed(char*, String, char*); + bool pushOwner(char*); + bool pushOwner(double); + bool pushOwner(int); + bool pushOwner(String); + bool chat(char*, char*); bool chat(char*, int); bool chat(char*, double); diff --git a/microgear-esp8266-arduino.ino b/microgear-esp8266-arduino.ino new file mode 100644 index 0000000..dbdc1c1 --- /dev/null +++ b/microgear-esp8266-arduino.ino @@ -0,0 +1,156 @@ +#include +#include "MicroGear.h" +#include "DHT.h" // library สำหรับอ่านค่า DHT Sensor ต้องติดตั้ง DHT sensor library by Adafruit v1.2.3 ก่อน + +// ----- แก้ค่า config 7 ค่าข้างล่างนี้ -------------------------------------------------------- +const char* ssid = "@A4MY_wifi"; // ชื่อ ssid +const char* password = "88888888"; // รหัสผ่าน wifi + +#define APPID "testnotinoti" // ให้แทนที่ด้วย AppID รวม +#define KEY "ko0sILWTNwfwHpL" // ให้แทนที่ด้วย Key รวม +#define SECRET "i5FRR1gfJwngKIpzNoK80DhIt" // ให้แทนที่ด้วย Secret รวม + +#define ALIAS "esp8266" // แทนที่ด้วยหมายเลขของท่าน เช่น "A01" +#define NEIGHBOR "freeboard" // ชื่ออุปกรณ์ของเพื่อน เช่น "A02" +// -------------------------------------------------------------------------------------- + +#define LEDSTATETOPIC "/ledstate/" ALIAS // topic ที่ต้องการ publish ส่งสถานะ led ในที่นี้จะเป็น /ledstate/{ชื่อ alias ตัวเอง} +#define DHTDATATOPIC "/dht/" ALIAS // topic ที่ต้องการ publish ส่งข้อมูล dht ในที่นี่จะเป็น /dht/{ชื่อ alias ตัวเอง} + +#define BUTTONPIN D5 // pin ที่ต่อกับปุ่ม Flash บนบอร์ด NodeMCU +#define LEDPIN LED_BUILTIN // pin ที่ต่อกับไฟ LED บนบอร์ด NodeMCU + +#define FEEDID "testfeed99" // ให้แทนที่ด้วย FeedID +#define FEEDAPI "R1Zq3FTqKaNR9dtFXbfvCrWnsqrZRtyE" // ให้แทนที่ด้วย FeedAPI + +int currentLEDState = 0; // ให้เริ่มต้นเป็น OFF +int lastLEDState = 1; +int currentButtonState = 1; // หมายเหตุ ปุ่ม flash ต่อเข้ากับ GPIO0 แบบ pull-up +int lastButtonState = 0; + +#define DHTPIN D4 // GPIO2 ขาที่ต่อเข้ากับขา DATA (บางโมดูลใช้คำว่า OUT) ของ DHT +#define DHTTYPE DHT22 // e.g. DHT11, DHT21, DHT22 +DHT dht(DHTPIN, DHTTYPE); + +float humid = 0; // ค่าความชื้น +float temp = 0; // ค่าอุณหภูมิ + +long lastDHTRead = 0; +long lastDHTPublish = 0; + +long lastTimeWriteFeed = 0; + +WiFiClient client; +MicroGear microgear(client); + +void updateLED(int state) { + currentLEDState = state; + + // ไฟ LED บน NodeMCU เป็น active-low จะติดก็ต่อเมื่อส่งค่า LOW ไปให้ LEDPIN + if (currentLEDState == 1) digitalWrite(LEDPIN, LOW); // LED ON + else digitalWrite(LEDPIN, HIGH); // LED OFF +} + +void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) { + Serial.print("Incoming message --> "); + msg[msglen] = '\0'; + Serial.println((char *)msg); + + if (*(char *)msg == '0') updateLED(0); + else if (*(char *)msg == '1') updateLED(1); +} + +void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { + Serial.println("Connected to NETPIE..."); + microgear.setAlias(ALIAS); +} + +void setup() { + microgear.on(MESSAGE,onMsghandler); + microgear.on(CONNECTED,onConnected); + + Serial.begin(115200); + Serial.println("Starting..."); + dht.begin(); // initialize โมดูล DHT + + // กำหนดชนิดของ PIN (ขาI/O) เช่น INPUT, OUTPUT เป็นต้น + pinMode(LEDPIN, OUTPUT); // LED pin mode กำหนดค่า + pinMode(BUTTONPIN, INPUT); // Button pin mode รับค่า + updateLED(currentLEDState); + + if (WiFi.begin(ssid, password)) { + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + } + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + microgear.resetToken(); + microgear.init(KEY,SECRET,ALIAS); // กำหนดค่าตันแปรเริ่มต้นให้กับ microgear + microgear.connect(APPID); // ฟังก์ชั่นสำหรับเชื่อมต่อ NETPIE +} + +void loop() { + if (microgear.connected()) { + microgear.loop(); + + if(currentLEDState != lastLEDState){ + microgear.publish(LEDSTATETOPIC, currentLEDState); // LEDSTATETOPIC ถูก define ไว้ข้างบน + lastLEDState = currentLEDState; + } + + if (digitalRead(BUTTONPIN)==HIGH) currentButtonState = 0; + else currentButtonState = 1; + + if(currentButtonState != lastButtonState){ + microgear.chat(NEIGHBOR, currentButtonState); + lastButtonState = currentButtonState; + } + + // เซนเซอร์​ DHT อ่านถี่เกินไปไม่ได้ จะให้ค่า error เลยต้องเช็คเวลาครั้งสุดท้ายที่อ่านค่า + // ว่าทิ้งช่วงนานพอหรือยัง ในที่นี้ตั้งไว้ 2 วินาที ก + if(millis() - lastDHTRead > 2000){ + humid = dht.readHumidity(); // อ่านค่าความชื้น + temp = dht.readTemperature(); // อ่านค่าอุณหภูมิ + lastDHTRead = millis(); + + Serial.print("Humid: "); Serial.print(humid); Serial.print(" %, "); + Serial.print("Temp: "); Serial.print(temp); Serial.println(" C "); + + // ตรวจสอบค่า humid และ temp เป็นตัวเลขหรือไม่ + if (isnan(humid) || isnan(temp)) { + Serial.println("Failed to read from DHT sensor!"); + } + else{ + // เตรียมสตริงในรูปแบบ "{humid},{temp}" + String datastring = (String)humid+","+(String)temp; + Serial.print("Sending --> "); + Serial.println(datastring); + microgear.publish(DHTDATATOPIC,datastring); // DHTDATATOPIC ถูก define ไว้ข้างบน + } + } + + if(millis()-lastTimeWriteFeed > 15000){ + lastTimeWriteFeed = millis(); + if(humid!=0 && temp!=0){ + String data = "{\"humid\":"; + data += humid ; + data += ", \"temp\":"; + data += temp ; + data += "}"; + Serial.print("Write Feed --> "); + Serial.println(data); + //microgear.writeFeed(FEEDID,data); + microgear.writeFeed(FEEDID,data,FEEDAPI); + } + microgear.pushOwner("test"); + } + } + else { + Serial.println("connection lost, reconnect..."); + microgear.connect(APPID); + } +} From ffa17a97582056c6518f72208b36e7f1d5acebcf Mon Sep 17 00:00:00 2001 From: NETPIE Date: Tue, 10 Apr 2018 16:21:51 +0700 Subject: [PATCH 2/5] add pushOwner --- MicroGear.cpp | 6 +- microgear-esp8266-arduino.ino | 156 ---------------------------------- 2 files changed, 4 insertions(+), 158 deletions(-) delete mode 100644 microgear-esp8266-arduino.ino diff --git a/MicroGear.cpp b/MicroGear.cpp index 1a73975..b4557ae 100644 --- a/MicroGear.cpp +++ b/MicroGear.cpp @@ -725,11 +725,13 @@ bool MicroGear::pushOwner(char *message) { } bool MicroGear::pushOwner(double message) { - return pushOwner(message); + char buff[MAXBUFFSIZE] = "/@push/owner"; + return publish(buff, message); } bool MicroGear::pushOwner(int message) { - return pushOwner(message); + char buff[MAXBUFFSIZE] = "/@push/owner"; + return publish(buff, message); } bool MicroGear::pushOwner(String message) { diff --git a/microgear-esp8266-arduino.ino b/microgear-esp8266-arduino.ino deleted file mode 100644 index dbdc1c1..0000000 --- a/microgear-esp8266-arduino.ino +++ /dev/null @@ -1,156 +0,0 @@ -#include -#include "MicroGear.h" -#include "DHT.h" // library สำหรับอ่านค่า DHT Sensor ต้องติดตั้ง DHT sensor library by Adafruit v1.2.3 ก่อน - -// ----- แก้ค่า config 7 ค่าข้างล่างนี้ -------------------------------------------------------- -const char* ssid = "@A4MY_wifi"; // ชื่อ ssid -const char* password = "88888888"; // รหัสผ่าน wifi - -#define APPID "testnotinoti" // ให้แทนที่ด้วย AppID รวม -#define KEY "ko0sILWTNwfwHpL" // ให้แทนที่ด้วย Key รวม -#define SECRET "i5FRR1gfJwngKIpzNoK80DhIt" // ให้แทนที่ด้วย Secret รวม - -#define ALIAS "esp8266" // แทนที่ด้วยหมายเลขของท่าน เช่น "A01" -#define NEIGHBOR "freeboard" // ชื่ออุปกรณ์ของเพื่อน เช่น "A02" -// -------------------------------------------------------------------------------------- - -#define LEDSTATETOPIC "/ledstate/" ALIAS // topic ที่ต้องการ publish ส่งสถานะ led ในที่นี้จะเป็น /ledstate/{ชื่อ alias ตัวเอง} -#define DHTDATATOPIC "/dht/" ALIAS // topic ที่ต้องการ publish ส่งข้อมูล dht ในที่นี่จะเป็น /dht/{ชื่อ alias ตัวเอง} - -#define BUTTONPIN D5 // pin ที่ต่อกับปุ่ม Flash บนบอร์ด NodeMCU -#define LEDPIN LED_BUILTIN // pin ที่ต่อกับไฟ LED บนบอร์ด NodeMCU - -#define FEEDID "testfeed99" // ให้แทนที่ด้วย FeedID -#define FEEDAPI "R1Zq3FTqKaNR9dtFXbfvCrWnsqrZRtyE" // ให้แทนที่ด้วย FeedAPI - -int currentLEDState = 0; // ให้เริ่มต้นเป็น OFF -int lastLEDState = 1; -int currentButtonState = 1; // หมายเหตุ ปุ่ม flash ต่อเข้ากับ GPIO0 แบบ pull-up -int lastButtonState = 0; - -#define DHTPIN D4 // GPIO2 ขาที่ต่อเข้ากับขา DATA (บางโมดูลใช้คำว่า OUT) ของ DHT -#define DHTTYPE DHT22 // e.g. DHT11, DHT21, DHT22 -DHT dht(DHTPIN, DHTTYPE); - -float humid = 0; // ค่าความชื้น -float temp = 0; // ค่าอุณหภูมิ - -long lastDHTRead = 0; -long lastDHTPublish = 0; - -long lastTimeWriteFeed = 0; - -WiFiClient client; -MicroGear microgear(client); - -void updateLED(int state) { - currentLEDState = state; - - // ไฟ LED บน NodeMCU เป็น active-low จะติดก็ต่อเมื่อส่งค่า LOW ไปให้ LEDPIN - if (currentLEDState == 1) digitalWrite(LEDPIN, LOW); // LED ON - else digitalWrite(LEDPIN, HIGH); // LED OFF -} - -void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) { - Serial.print("Incoming message --> "); - msg[msglen] = '\0'; - Serial.println((char *)msg); - - if (*(char *)msg == '0') updateLED(0); - else if (*(char *)msg == '1') updateLED(1); -} - -void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { - Serial.println("Connected to NETPIE..."); - microgear.setAlias(ALIAS); -} - -void setup() { - microgear.on(MESSAGE,onMsghandler); - microgear.on(CONNECTED,onConnected); - - Serial.begin(115200); - Serial.println("Starting..."); - dht.begin(); // initialize โมดูล DHT - - // กำหนดชนิดของ PIN (ขาI/O) เช่น INPUT, OUTPUT เป็นต้น - pinMode(LEDPIN, OUTPUT); // LED pin mode กำหนดค่า - pinMode(BUTTONPIN, INPUT); // Button pin mode รับค่า - updateLED(currentLEDState); - - if (WiFi.begin(ssid, password)) { - while (WiFi.status() != WL_CONNECTED) { - delay(1000); - Serial.print("."); - } - } - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - - microgear.resetToken(); - microgear.init(KEY,SECRET,ALIAS); // กำหนดค่าตันแปรเริ่มต้นให้กับ microgear - microgear.connect(APPID); // ฟังก์ชั่นสำหรับเชื่อมต่อ NETPIE -} - -void loop() { - if (microgear.connected()) { - microgear.loop(); - - if(currentLEDState != lastLEDState){ - microgear.publish(LEDSTATETOPIC, currentLEDState); // LEDSTATETOPIC ถูก define ไว้ข้างบน - lastLEDState = currentLEDState; - } - - if (digitalRead(BUTTONPIN)==HIGH) currentButtonState = 0; - else currentButtonState = 1; - - if(currentButtonState != lastButtonState){ - microgear.chat(NEIGHBOR, currentButtonState); - lastButtonState = currentButtonState; - } - - // เซนเซอร์​ DHT อ่านถี่เกินไปไม่ได้ จะให้ค่า error เลยต้องเช็คเวลาครั้งสุดท้ายที่อ่านค่า - // ว่าทิ้งช่วงนานพอหรือยัง ในที่นี้ตั้งไว้ 2 วินาที ก - if(millis() - lastDHTRead > 2000){ - humid = dht.readHumidity(); // อ่านค่าความชื้น - temp = dht.readTemperature(); // อ่านค่าอุณหภูมิ - lastDHTRead = millis(); - - Serial.print("Humid: "); Serial.print(humid); Serial.print(" %, "); - Serial.print("Temp: "); Serial.print(temp); Serial.println(" C "); - - // ตรวจสอบค่า humid และ temp เป็นตัวเลขหรือไม่ - if (isnan(humid) || isnan(temp)) { - Serial.println("Failed to read from DHT sensor!"); - } - else{ - // เตรียมสตริงในรูปแบบ "{humid},{temp}" - String datastring = (String)humid+","+(String)temp; - Serial.print("Sending --> "); - Serial.println(datastring); - microgear.publish(DHTDATATOPIC,datastring); // DHTDATATOPIC ถูก define ไว้ข้างบน - } - } - - if(millis()-lastTimeWriteFeed > 15000){ - lastTimeWriteFeed = millis(); - if(humid!=0 && temp!=0){ - String data = "{\"humid\":"; - data += humid ; - data += ", \"temp\":"; - data += temp ; - data += "}"; - Serial.print("Write Feed --> "); - Serial.println(data); - //microgear.writeFeed(FEEDID,data); - microgear.writeFeed(FEEDID,data,FEEDAPI); - } - microgear.pushOwner("test"); - } - } - else { - Serial.println("connection lost, reconnect..."); - microgear.connect(APPID); - } -} From 171036c51c1442143c4ac5fc1c81ebba7194eb3c Mon Sep 17 00:00:00 2001 From: NETPIE Date: Wed, 18 Apr 2018 10:42:40 +0700 Subject: [PATCH 3/5] add pushOwner --- README.md | 13 +++++++++++++ README.th.md | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/README.md b/README.md index e04c6cf..102ed3e 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,19 @@ microgear.writeFeed("homesensor","{temp:25.7,humid:62.8,light:8.5}"); To send a revoke token control message to NETPIE and delete the token from cache. As a result, the microgear will need to request a new token for the next connection. +--- +**void microgear.pushOwner (char *message)**
+**void microgear.pushOwner (double *message)**
+**void microgear.pushOwner (int *message)**
+**void microgear.pushOwner (String *message)**
+push notification to NETPIE mobile application + +**arguments** +* *message* - message to be sent + +```js +microgear.pushOwner("temp:25.7"); +``` --- **void MicroGear::loop()** diff --git a/README.th.md b/README.th.md index fc1e77f..a570034 100644 --- a/README.th.md +++ b/README.th.md @@ -269,7 +269,19 @@ microgear.writeFeed("homesensor","{temp:25.7,humid:62.8,light:8.5}"); ส่งคำสั่ง revoke token ไปยัง netpie และลบ token ออกจาก cache ส่งผลให้ microgear ต้องขอ token ใหม่ในการเชื่อมต่อครั้งต่อไป --- +**void microgear.pushOwner (char *message)**
+**void microgear.pushOwner (double *message)**
+**void microgear.pushOwner (int *message)**
+**void microgear.pushOwner (String *message)**
+ส่งการแจ้งเตือน ไปยัง NETPIE mobile application +**arguments** +* *message* - ข้อความ + +```js +microgear.pushOwner("temp:25.7"); +``` +--- **void MicroGear::loop()** method นี้ควรถูกเรียกใน arduino loop() เป็นระยะๆ เพื่อที่ microgear library จะได้ keep alive connection alive และจัดการกับ message ที่เข้ามา From 17ef0a0b1e7813c0418dd18a5ff9d16b1ef8dd43 Mon Sep 17 00:00:00 2001 From: NETPIE Date: Wed, 18 Apr 2018 10:45:44 +0700 Subject: [PATCH 4/5] add pushOwner --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 102ed3e..be93e11 100644 --- a/README.md +++ b/README.md @@ -285,6 +285,7 @@ microgear.writeFeed("homesensor","{temp:25.7,humid:62.8,light:8.5}"); To send a revoke token control message to NETPIE and delete the token from cache. As a result, the microgear will need to request a new token for the next connection. --- + **void microgear.pushOwner (char *message)**
**void microgear.pushOwner (double *message)**
**void microgear.pushOwner (int *message)**
From 8a7ab798faab93a0a727d629c8a192b80efc4b43 Mon Sep 17 00:00:00 2001 From: NETPIE Date: Wed, 18 Apr 2018 13:47:41 +0700 Subject: [PATCH 5/5] update pushOwner --- MicroGear.cpp | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/MicroGear.cpp b/MicroGear.cpp index b4557ae..10dba30 100644 --- a/MicroGear.cpp +++ b/MicroGear.cpp @@ -666,34 +666,6 @@ bool MicroGear::publish(char* topic, String message, bool retained) { return publish(topic, buff, retained); } -bool MicroGear::publish(char* topic, String message, String apikey) { - char buff[MAXBUFFSIZE]; - message.toCharArray(buff,MAXBUFFSIZE-1); - - char top[MAXTOPICSIZE] = ""; - strcat(top,topic); - if(apikey!=""){ - strcat(top,"/"); - char buffapikey[MAXBUFFSIZE]; - apikey.toCharArray(buffapikey,MAXBUFFSIZE); - strcat(top,buffapikey); - } - return publish(top, buff); -} - -bool MicroGear::publish(char* topic, String message, char* apikey) { - char buff[MAXBUFFSIZE]; - message.toCharArray(buff,MAXBUFFSIZE-1); - - char top[MAXTOPICSIZE] = ""; - strcat(top,topic); - if(apikey!=""){ - strcat(top,"/"); - strcat(top,apikey); - } - return publish(top, buff); -} - bool MicroGear::writeFeed(char* feedname, char *data, char* apikey) { char buff[MAXBUFFSIZE] = "/@writefeed/"; @@ -735,9 +707,10 @@ bool MicroGear::pushOwner(int message) { } bool MicroGear::pushOwner(String message) { + char top[MAXBUFFSIZE] = "/@push/owner"; char buff[MAXBUFFSIZE]; message.toCharArray(buff,MAXBUFFSIZE-1); - return pushOwner(buff); + return publish(top, message); } /*