Skip to content

Commit 662bd62

Browse files
committed
add CONNECTED event and update a Basic example
1 parent 1fda3cb commit 662bd62

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

MicroGear.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ unsigned char topicprefixlen;
44
void (* cb_message)(char*, uint8_t*,unsigned int);
55
void (* cb_present)(char*, uint8_t*,unsigned int);
66
void (* cb_absent)(char*, uint8_t*,unsigned int);
7+
void (* cb_connected)(char*, uint8_t*,unsigned int);
78

89
void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
910
/* remove /appid/ */
@@ -107,6 +108,8 @@ void MicroGear::on(unsigned char event, void (* callback)(char*, uint8_t*,unsign
107108
if (connected())
108109
subscribe("/&absent");
109110
break;
111+
case CONNECTED :
112+
if (callback) cb_connected = callback;
110113
}
111114
}
112115

@@ -355,6 +358,9 @@ boolean MicroGear::connect(char* appid) {
355358
sprintf(buff,"/&id/%s/#",token);
356359
subscribe(buff);
357360

361+
cb_connected(NULL,NULL,0);
362+
363+
358364
break;
359365
case CLIENT_NOTCONNECT :
360366
if (backoff < MAXBACKOFFTIME) backoff = 2*backoff;

MicroGear.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
#define MESSAGE 1
5555
#define PRESENT 2
5656
#define ABSENT 3
57-
#define CALLBACK 4
57+
#define CONNECTED 4
58+
#define CALLBACK 5
5859

5960

6061
class MicroGear {

examples/Basic/Basic.ino

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,43 @@ const char* password = <WIFI_KEY>;
1818
WiFiClient client;
1919
AuthClient *authclient;
2020

21-
void msghandler(char *topic, uint8_t* msg, unsigned int msglen) {
21+
int timer = 0;
22+
MicroGear microgear(client);
23+
24+
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
2225
Serial.print("Incoming message --> ");
23-
Serial.print(topic);
24-
for (int i=0; i<msglen; i++)
25-
Serial.print((char)msg[i]);
26-
Serial.println();
26+
msg[msglen] = '\0';
27+
Serial.println((char *)msg);
2728
}
2829

29-
void foundgear(char *attribute, uint8_t* msg, unsigned int msglen) {
30+
void onFoundgear(char *attribute, uint8_t* msg, unsigned int msglen) {
3031
Serial.print("Found new member --> ");
3132
for (int i=0; i<msglen; i++)
3233
Serial.print((char)msg[i]);
3334
Serial.println();
3435
}
3536

36-
void lostgear(char *attribute, uint8_t* msg, unsigned int msglen) {
37+
void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) {
3738
Serial.print("Lost member --> ");
3839
for (int i=0; i<msglen; i++)
3940
Serial.print((char)msg[i]);
40-
Serial.println();
41+
Serial.println();
42+
}
43+
44+
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
45+
Serial.println("Connected to NETPIE...");
46+
microgear.setName("mygear");
4147
}
4248

43-
int timer = 0;
44-
MicroGear microgear(client);
4549

4650
void setup() {
4751
/* Event listener */
48-
microgear.on(MESSAGE,msghandler);
49-
microgear.on(PRESENT,foundgear);
50-
microgear.on(ABSENT,lostgear);
52+
microgear.on(MESSAGE,onMsghandler);
53+
microgear.on(PRESENT,onFoundgear);
54+
microgear.on(ABSENT,onLostgear);
55+
microgear.on(CONNECTED,onConnected);
5156

57+
Serial.begin(115200);
5258
Serial.println("Starting...");
5359

5460
if (WiFi.begin(ssid, password)) {
@@ -66,7 +72,6 @@ void setup() {
6672
//microgear.resetToken();
6773
microgear.init(GEARKEY,GEARSECRET,SCOPE);
6874
microgear.connect(APPID);
69-
microgear.setName("mygear");
7075
}
7176
}
7277

@@ -90,4 +95,4 @@ void loop() {
9095
else timer += 100;
9196
}
9297
delay(100);
93-
}
98+
}

0 commit comments

Comments
 (0)