Skip to content

Commit a6363da

Browse files
committed
add comments in example codes
1 parent 0f3223b commit a6363da

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ We have tested this library and found it compatible with (but not limited to) th
2323

2424
**Usage Example**
2525
```c++
26+
/* NETPIE ESP8266 basic sample */
27+
/* More information visit : https://netpie.io */
28+
2629
#include <ESP8266WiFi.h>
2730
#include <MicroGear.h>
2831

@@ -40,6 +43,7 @@ AuthClient *authclient;
4043
int timer = 0;
4144
MicroGear microgear(client);
4245

46+
/* If a new message arrives, do this */
4347
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
4448
Serial.print("Incoming message --> ");
4549
msg[msglen] = '\0';
@@ -60,24 +64,34 @@ void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) {
6064
Serial.println();
6165
}
6266

67+
/* When a microgear is connected, do this */
6368
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
6469
Serial.println("Connected to NETPIE...");
70+
/* Set the alias of this microgear ALIAS */
6571
microgear.setName(ALIAS);
6672
}
6773

6874

6975
void setup() {
70-
/* Event listener */
76+
/* Add Event listeners */
77+
/* Call onMsghandler() when new message arraives */
7178
microgear.on(MESSAGE,onMsghandler);
79+
80+
/* Call onFoundgear() when new gear appear */
7281
microgear.on(PRESENT,onFoundgear);
82+
83+
/* Call onLostgear() when some gear goes offline */
7384
microgear.on(ABSENT,onLostgear);
85+
86+
/* Call onConnected() when NETPIE connection is established */
7487
microgear.on(CONNECTED,onConnected);
7588

7689
Serial.begin(115200);
7790
Serial.println("Starting...");
7891

92+
/* Initial WIFI, this is just a basic method to configure WIFI on ESP8266. */
93+
/* You may want to use other method that is more complicated, but provide better user experience */
7994
if (WiFi.begin(ssid, password)) {
80-
8195
while (WiFi.status() != WL_CONNECTED) {
8296
delay(500);
8397
Serial.print(".");
@@ -88,19 +102,28 @@ void setup() {
88102
Serial.println("IP address: ");
89103
Serial.println(WiFi.localIP());
90104

105+
/* Initial with KEY, SECRET and also set the ALIAS here */
91106
microgear.init(KEY,SECRET,ALIAS);
107+
108+
/* connect to NETPIE to a specific APPID */
92109
microgear.connect(APPID);
93110
}
94111

95112
void loop() {
113+
/* To check if the microgear is still connected */
96114
if (microgear.connected()) {
97115
Serial.println("connected");
116+
117+
/* Call this method regularly otherwise the connection may be lost */
98118
microgear.loop();
119+
99120
if (timer >= 1000) {
100121
Serial.println("Publish...");
122+
123+
/* Chat with the microgear named ALIAS which is myself */
101124
microgear.chat(ALIAS,"Hello");
102125
timer = 0;
103-
}
126+
}
104127
else timer += 100;
105128
}
106129
else {

README.th.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ microgear-esp8266-arduino คือ client library ที่ทำหน้า
2323

2424
**ตัวอย่างการเรียกใช้**
2525
```c++
26+
/* NETPIE ESP8266 basic sample */
27+
/* More information visit : https://netpie.io */
28+
2629
#include <ESP8266WiFi.h>
2730
#include <MicroGear.h>
2831

@@ -40,6 +43,7 @@ AuthClient *authclient;
4043
int timer = 0;
4144
MicroGear microgear(client);
4245

46+
/* If a new message arrives, do this */
4347
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
4448
Serial.print("Incoming message --> ");
4549
msg[msglen] = '\0';
@@ -60,24 +64,34 @@ void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) {
6064
Serial.println();
6165
}
6266

67+
/* When a microgear is connected, do this */
6368
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
6469
Serial.println("Connected to NETPIE...");
70+
/* Set the alias of this microgear ALIAS */
6571
microgear.setName(ALIAS);
6672
}
6773

6874

6975
void setup() {
70-
/* Event listener */
76+
/* Add Event listeners */
77+
/* Call onMsghandler() when new message arraives */
7178
microgear.on(MESSAGE,onMsghandler);
79+
80+
/* Call onFoundgear() when new gear appear */
7281
microgear.on(PRESENT,onFoundgear);
82+
83+
/* Call onLostgear() when some gear goes offline */
7384
microgear.on(ABSENT,onLostgear);
85+
86+
/* Call onConnected() when NETPIE connection is established */
7487
microgear.on(CONNECTED,onConnected);
7588

7689
Serial.begin(115200);
7790
Serial.println("Starting...");
7891

92+
/* Initial WIFI, this is just a basic method to configure WIFI on ESP8266. */
93+
/* You may want to use other method that is more complicated, but provide better user experience */
7994
if (WiFi.begin(ssid, password)) {
80-
8195
while (WiFi.status() != WL_CONNECTED) {
8296
delay(500);
8397
Serial.print(".");
@@ -88,19 +102,28 @@ void setup() {
88102
Serial.println("IP address: ");
89103
Serial.println(WiFi.localIP());
90104

105+
/* Initial with KEY, SECRET and also set the ALIAS here */
91106
microgear.init(KEY,SECRET,ALIAS);
107+
108+
/* connect to NETPIE to a specific APPID */
92109
microgear.connect(APPID);
93110
}
94111

95112
void loop() {
113+
/* To check if the microgear is still connected */
96114
if (microgear.connected()) {
97115
Serial.println("connected");
116+
117+
/* Call this method regularly otherwise the connection may be lost */
98118
microgear.loop();
119+
99120
if (timer >= 1000) {
100121
Serial.println("Publish...");
122+
123+
/* Chat with the microgear named ALIAS which is myself */
101124
microgear.chat(ALIAS,"Hello");
102125
timer = 0;
103-
}
126+
}
104127
else timer += 100;
105128
}
106129
else {

examples/Basic/Basic.ino

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ AuthClient *authclient;
1818
int timer = 0;
1919
MicroGear microgear(client);
2020

21+
/* If a new message arrives, do this */
2122
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
2223
Serial.print("Incoming message --> ");
2324
msg[msglen] = '\0';
@@ -38,24 +39,35 @@ void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) {
3839
Serial.println();
3940
}
4041

42+
/* When a microgear is connected, do this */
4143
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
4244
Serial.println("Connected to NETPIE...");
45+
/* Set the alias of this microgear ALIAS */
4346
microgear.setName(ALIAS);
4447
}
4548

4649

4750
void setup() {
48-
/* Event listener */
51+
/* Add Event listeners */
52+
53+
/* Call onMsghandler() when new message arraives */
4954
microgear.on(MESSAGE,onMsghandler);
55+
56+
/* Call onFoundgear() when new gear appear */
5057
microgear.on(PRESENT,onFoundgear);
58+
59+
/* Call onLostgear() when some gear goes offline */
5160
microgear.on(ABSENT,onLostgear);
61+
62+
/* Call onConnected() when NETPIE connection is established */
5263
microgear.on(CONNECTED,onConnected);
5364

5465
Serial.begin(115200);
5566
Serial.println("Starting...");
5667

68+
/* Initial WIFI, this is just a basic method to configure WIFI on ESP8266. */
69+
/* You may want to use other method that is more complicated, but provide better user experience */
5770
if (WiFi.begin(ssid, password)) {
58-
5971
while (WiFi.status() != WL_CONNECTED) {
6072
delay(500);
6173
Serial.print(".");
@@ -66,16 +78,25 @@ void setup() {
6678
Serial.println("IP address: ");
6779
Serial.println(WiFi.localIP());
6880

81+
/* Initial with KEY, SECRET and also set the ALIAS here */
6982
microgear.init(KEY,SECRET,ALIAS);
83+
84+
/* connect to NETPIE to a specific APPID */
7085
microgear.connect(APPID);
7186
}
7287

7388
void loop() {
89+
/* To check if the microgear is still connected */
7490
if (microgear.connected()) {
7591
Serial.println("connected");
92+
93+
/* Call this method regularly otherwise the connection may be lost */
7694
microgear.loop();
95+
7796
if (timer >= 1000) {
7897
Serial.println("Publish...");
98+
99+
/* Chat with the microgear named ALIAS which is myself */
79100
microgear.chat(ALIAS,"Hello");
80101
timer = 0;
81102
}

examples/SecureConnect/SecureConnect.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* NETPIE ESP8266 secure microgear connection sample */
22
/* It differs from the normal connection by 2 stpes */
33
/* 1. Declare a client as WiFiClientSecure instead of WiFiClient. */
4-
/* 2. Call microgear.useTLS(true); */
4+
/* 2. Call microgear.useTLS(true) before initial */
55
/* More information visit : https://netpie.io */
66

77
#include <ESP8266WiFi.h>
@@ -15,6 +15,7 @@ const char* password = <WIFI_KEY>;
1515
#define SECRET <APPSECRET>
1616
#define ALIAS "esp8266tls"
1717

18+
/* 1. Declare a client as WiFiClientSecure instead of WiFiClient. */
1819
WiFiClientSecure client;
1920
AuthClient *authclient;
2021

@@ -68,6 +69,7 @@ void setup() {
6869
Serial.println("IP address: ");
6970
Serial.println(WiFi.localIP());
7071

72+
/* 2. Call microgear.useTLS(true) before initial */
7173
microgear.useTLS(true);
7274
microgear.init(KEY,SECRET,ALIAS);
7375
microgear.connect(APPID);

0 commit comments

Comments
 (0)