@@ -18,37 +18,43 @@ const char* password = <WIFI_KEY>;
1818WiFiClient client;
1919AuthClient *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
4650void 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