Skip to content

Commit b921d07

Browse files
authored
Merge pull request #2 from netpieio/dev
Overload publish and chat functions
2 parents 34bde07 + 41050ec commit b921d07

File tree

3 files changed

+127
-23
lines changed

3 files changed

+127
-23
lines changed

MicroGear.cpp

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
2626
}
2727
else if (strcmp(rtopic,"&resetendpoint") == 0) {
2828
#ifdef DEBUG_H
29-
Serial.println("RESETTTT-EP");
29+
Serial.println("to reset endpoint");
3030
#endif
3131
if (mg) mg->resetEndpoint();
3232
}
@@ -89,7 +89,7 @@ int MicroGear::getHTTPReply(Client *client, char *buff, size_t buffsize) {
8989
}
9090

9191
void MicroGear::resetEndpoint() {
92-
writeEEPROM("",EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
92+
writeEEPROM("",EEPROM_ENDPOINTSOFFSET,1);
9393
}
9494

9595
void MicroGear::initEndpoint(Client *client, char* endpoint) {
@@ -562,9 +562,9 @@ bool MicroGear::connect(char* appid) {
562562
}
563563

564564
bool MicroGear::connected() {
565-
if (constate == CLIENT_NOTCONNECT) return CLIENT_NOTCONNECT;
566-
else return this->mqttclient->connected();
567-
//return this->sockclient->connected();
565+
//if (constate == CLIENT_NOTCONNECT) return CLIENT_NOTCONNECT;
566+
//else return this->mqttclient->connected();
567+
return this->sockclient->connected();
568568
}
569569

570570
void MicroGear::subscribe(char* topic) {
@@ -583,16 +583,52 @@ void MicroGear::unsubscribe(char* topic) {
583583
mqttclient->unsubscribe(top);
584584
}
585585

586-
void MicroGear::publish(char* topic, char* message) {
587-
publish(topic, message, false);
588-
}
589-
590-
void MicroGear::publish(char* topic, char* message, bool retained) {
586+
bool MicroGear::publish(char* topic, char* message, bool retained) {
591587
char top[MAXTOPICSIZE] = "/";
592588

593589
strcat(top,appid);
594590
strcat(top,topic);
595-
mqttclient->publish(top, message, retained);
591+
return mqttclient->publish(top, message, retained);
592+
}
593+
594+
bool MicroGear::publish(char* topic, char* message) {
595+
return publish(topic, message, false);
596+
}
597+
598+
bool MicroGear::publish(char* topic, double message, int n) {
599+
return publish(topic, message, n, false);
600+
}
601+
602+
bool MicroGear::publish(char* topic, double message, int n, bool retained) {
603+
char mstr[16];
604+
dtostrf(message,0,n,mstr);
605+
return publish(topic, mstr, retained);
606+
}
607+
608+
bool MicroGear::publish(char* topic, double message) {
609+
return publish(topic, message, 8, false);
610+
}
611+
612+
bool MicroGear::publish(char* topic, double message, bool retained) {
613+
return publish(topic, message, 8, retained);
614+
}
615+
616+
bool MicroGear::publish(char* topic, int message) {
617+
return publish(topic, message, 0, false);
618+
}
619+
620+
bool MicroGear::publish(char* topic, int message, bool retained) {
621+
return publish(topic, message, 0, retained);
622+
}
623+
624+
bool MicroGear::publish(char* topic, String message) {
625+
return publish(topic, message, false);
626+
}
627+
628+
bool MicroGear::publish(char* topic, String message, bool retained) {
629+
char buff[MAXBUFFSIZE];
630+
message.toCharArray(buff,MAXBUFFSIZE);
631+
return publish(topic, buff, retained);
596632
}
597633

598634
/*
@@ -620,10 +656,38 @@ void MicroGear::setAlias(char* gearalias) {
620656
publish(top,"");
621657
}
622658

623-
void MicroGear::chat(char* targetgear, char* message) {
659+
bool MicroGear::chat(char* targetgear, char* message) {
660+
bool result;
624661
char top[MAXTOPICSIZE];
662+
625663
sprintf(top,"/%s/gearname/%s",appid,targetgear);
626-
mqttclient->publish(top, message);
664+
result = mqttclient->publish(top, message);
665+
mqttclient->loop();
666+
return result;
667+
}
668+
669+
bool MicroGear::chat(char* topic, double message, int n) {
670+
bool result;
671+
char mstr[16];
672+
673+
dtostrf(message,0,n,mstr);
674+
result = chat(topic, mstr);
675+
mqttclient->loop();
676+
return result;
677+
}
678+
679+
bool MicroGear::chat(char* topic, double message) {
680+
return chat(topic, message, 8);
681+
}
682+
683+
bool MicroGear::chat(char* topic, int message) {
684+
return chat(topic, message, 0);
685+
}
686+
687+
bool MicroGear::chat(char* topic, String message) {
688+
char buff[MAXBUFFSIZE];
689+
message.toCharArray(buff,MAXBUFFSIZE);
690+
return chat(topic, buff);
627691
}
628692

629693
int MicroGear::init(char* gearkey,char* gearsecret) {

MicroGear.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919

2020
#define GBPORT 1883
2121
#define GBSECUREPORT 8883
22-
//#define CAPORT 8079
23-
//#define CAFINGERPRINT "57 BA 02 8A 81 CB 6D D3 26 CE B7 21 7E A8 6C B5 DA D8 5A D0"
24-
2522
#define DEFAULTSECUREMODE false
2623

2724
#define MINBACKOFFTIME 10
2825
#define MAXBACKOFFTIME 10000
2926
#define MAXENDPOINTLENGTH 200
3027
#define MAXTOPICSIZE 128
28+
#define MAXBUFFSIZE 128
3129

3230
#define KEYSIZE 16
3331
#define TOKENSIZE 16
@@ -93,7 +91,7 @@ class MicroGear {
9391
bool clientReadln(Client*, char*, size_t);
9492
void syncTime(Client*, unsigned long*);
9593
void initEndpoint(Client*, char*);
96-
void getToken(char*, char*, char*, char*, char*);
94+
void getToken(char*, char*, char*, char*, char*);
9795

9896
public:
9997
int constate;
@@ -105,11 +103,27 @@ class MicroGear {
105103
void useTLS(bool);
106104
bool connect(char*);
107105
bool connected();
108-
void publish(char*, char*);
109-
void publish(char*, char*, bool);
106+
107+
bool publish(char*, char*);
108+
bool publish(char*, char*, bool);
109+
110+
bool publish(char*, double);
111+
bool publish(char*, double, bool);
112+
bool publish(char*, double, int);
113+
bool publish(char*, double, int, bool);
114+
bool publish(char*, int);
115+
bool publish(char*, int, bool);
116+
bool publish(char*, String);
117+
bool publish(char*, String, bool);
118+
119+
bool chat(char*, char*);
120+
bool chat(char*, int);
121+
bool chat(char*, double);
122+
bool chat(char*, double, int);
123+
bool chat(char*, String);
124+
110125
void subscribe(char*);
111126
void unsubscribe(char*);
112-
void chat(char*, char*);
113127
int state();
114128
void loop();
115129
void resetToken();

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33
microgear-esp8266-arduino is a client library that is used to connect an ESP8266 chip to the NETPIE Platform's service for developing IoT applications. For more details on the NETPIE Platform, please visit https://netpie.io .
44

55
## Compatibility
6+
67
We have tested this library and found it compatible with (but not limited to) the following hardware
78
- ESP8266-01, 07, 12E, 12F
89
- NodeMCU v1, v2, V3
910
- Espresso Lite v2.0
1011

12+
## Outgoing Network Port
13+
14+
Make sure ther following ports are allowed to connect from your network.
15+
- Non-TLS mode : 8080 and 1883 (the library uses this mode by default)
16+
- TLS mode : 8081 and 8883 (still under testing)
17+
1118
## Installation
1219
* Download Arduino IDE 1.6.9 or later from https://www.arduino.cc/en/Main/Software
1320
* After installation, open Preferences
@@ -182,6 +189,15 @@ Check the connection status, return true if it is connected.
182189

183190
---
184191

192+
**void MicroGear::useTLS(bool* enabled)**
193+
194+
Switch between uncrypted and TLS mode (by default the library does not use TLS). This function must be called before the connection is made.
195+
196+
**arguments**
197+
* *enabled* - set to TRUE to use TLS.
198+
199+
---
200+
185201
**void MicroGear::setAlias(char* alias)**
186202

187203
microgear can set its own alias, which to be used for others make a function call chat(). The alias will appear on the key management portal of netpie.io .
@@ -191,21 +207,31 @@ microgear can set its own alias, which to be used for others make a function cal
191207

192208
---
193209

194-
**void MicroGear::chat(char* target, char* message)**
195-
210+
**bool MicroGear::chat(char* target, char* message)**<br />
211+
**bool chat(char* target, int message);**<br />
212+
**bool chat(char* target, double message);**<br />
213+
**bool chat(char* target, double, int decimal);**<br />
214+
**bool chat(char* target, String message);**<br />
215+
196216
**arguments**
197217
* *target* - the alias of the microgear(s) that a message will be sent to.
198218
* *message* - message to be sent.
219+
* *decimal* - number of digits after the deimal point.
199220

200221
---
201222

202-
**void MicroGear::publish(char* topic, char* message [, bool retained])**
223+
**bool MicroGear::publish(char* topic, char* message [, bool retained])**<br />
224+
**bool MicroGear::publish(char* topic, double message [, bool retained]);**<br />
225+
**bool MicroGear::publish(char* topic, double message, int decimal [, bool retained]);**<br />
226+
**bool MicroGear::publish(char* topic, int message [, bool retained]);**<br />
227+
**bool MicroGear::publish(char* topic, String message [, bool retained]);**<br />
203228

204229
In the case that the microgear want to send a message to an unspecified receiver, the developer can use the function publish to the desired topic, which all the microgears that subscribe such topic will receive a message.
205230

206231
**arguments**
207232
* *topic* - name of topic to be send a message to.
208233
* *message* - message to be sent.
234+
* *decimal* - number of digits after the deimal point.
209235
* *retained* - retain a message or not, the default is false (optional))
210236

211237
---

0 commit comments

Comments
 (0)