diff --git a/MicroGear.cpp b/MicroGear.cpp index 34fbe91..30ee2bc 100644 --- a/MicroGear.cpp +++ b/MicroGear.cpp @@ -26,7 +26,7 @@ void msgCallback(char* topic, uint8_t* payload, unsigned int length) { } else if (strcmp(rtopic,"&resetendpoint") == 0) { #ifdef DEBUG_H - Serial.println("RESETTTT-EP"); + Serial.println("to reset endpoint"); #endif if (mg) mg->resetEndpoint(); } @@ -89,7 +89,7 @@ int MicroGear::getHTTPReply(Client *client, char *buff, size_t buffsize) { } void MicroGear::resetEndpoint() { - writeEEPROM("",EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH); + writeEEPROM("",EEPROM_ENDPOINTSOFFSET,1); } void MicroGear::initEndpoint(Client *client, char* endpoint) { @@ -562,9 +562,9 @@ bool MicroGear::connect(char* appid) { } bool MicroGear::connected() { - if (constate == CLIENT_NOTCONNECT) return CLIENT_NOTCONNECT; - else return this->mqttclient->connected(); - //return this->sockclient->connected(); + //if (constate == CLIENT_NOTCONNECT) return CLIENT_NOTCONNECT; + //else return this->mqttclient->connected(); + return this->sockclient->connected(); } void MicroGear::subscribe(char* topic) { @@ -583,16 +583,52 @@ void MicroGear::unsubscribe(char* topic) { mqttclient->unsubscribe(top); } -void MicroGear::publish(char* topic, char* message) { - publish(topic, message, false); -} - -void MicroGear::publish(char* topic, char* message, bool retained) { +bool MicroGear::publish(char* topic, char* message, bool retained) { char top[MAXTOPICSIZE] = "/"; strcat(top,appid); strcat(top,topic); - mqttclient->publish(top, message, retained); + return mqttclient->publish(top, message, retained); +} + +bool MicroGear::publish(char* topic, char* message) { + return publish(topic, message, false); +} + +bool MicroGear::publish(char* topic, double message, int n) { + return publish(topic, message, n, false); +} + +bool MicroGear::publish(char* topic, double message, int n, bool retained) { + char mstr[16]; + dtostrf(message,0,n,mstr); + return publish(topic, mstr, retained); +} + +bool MicroGear::publish(char* topic, double message) { + return publish(topic, message, 8, false); +} + +bool MicroGear::publish(char* topic, double message, bool retained) { + return publish(topic, message, 8, retained); +} + +bool MicroGear::publish(char* topic, int message) { + return publish(topic, message, 0, false); +} + +bool MicroGear::publish(char* topic, int message, bool retained) { + return publish(topic, message, 0, retained); +} + +bool MicroGear::publish(char* topic, String message) { + return publish(topic, message, false); +} + +bool MicroGear::publish(char* topic, String message, bool retained) { + char buff[MAXBUFFSIZE]; + message.toCharArray(buff,MAXBUFFSIZE); + return publish(topic, buff, retained); } /* @@ -620,10 +656,38 @@ void MicroGear::setAlias(char* gearalias) { publish(top,""); } -void MicroGear::chat(char* targetgear, char* message) { +bool MicroGear::chat(char* targetgear, char* message) { + bool result; char top[MAXTOPICSIZE]; + sprintf(top,"/%s/gearname/%s",appid,targetgear); - mqttclient->publish(top, message); + result = mqttclient->publish(top, message); + mqttclient->loop(); + return result; +} + +bool MicroGear::chat(char* topic, double message, int n) { + bool result; + char mstr[16]; + + dtostrf(message,0,n,mstr); + result = chat(topic, mstr); + mqttclient->loop(); + return result; +} + +bool MicroGear::chat(char* topic, double message) { + return chat(topic, message, 8); +} + +bool MicroGear::chat(char* topic, int message) { + return chat(topic, message, 0); +} + +bool MicroGear::chat(char* topic, String message) { + char buff[MAXBUFFSIZE]; + message.toCharArray(buff,MAXBUFFSIZE); + return chat(topic, buff); } int MicroGear::init(char* gearkey,char* gearsecret) { diff --git a/MicroGear.h b/MicroGear.h index b42b1e4..c28148c 100644 --- a/MicroGear.h +++ b/MicroGear.h @@ -19,15 +19,13 @@ #define GBPORT 1883 #define GBSECUREPORT 8883 -//#define CAPORT 8079 -//#define CAFINGERPRINT "57 BA 02 8A 81 CB 6D D3 26 CE B7 21 7E A8 6C B5 DA D8 5A D0" - #define DEFAULTSECUREMODE false #define MINBACKOFFTIME 10 #define MAXBACKOFFTIME 10000 #define MAXENDPOINTLENGTH 200 #define MAXTOPICSIZE 128 +#define MAXBUFFSIZE 128 #define KEYSIZE 16 #define TOKENSIZE 16 @@ -93,7 +91,7 @@ class MicroGear { bool clientReadln(Client*, char*, size_t); void syncTime(Client*, unsigned long*); void initEndpoint(Client*, char*); - void getToken(char*, char*, char*, char*, char*); + void getToken(char*, char*, char*, char*, char*); public: int constate; @@ -105,11 +103,27 @@ class MicroGear { void useTLS(bool); bool connect(char*); bool connected(); - void publish(char*, char*); - void publish(char*, char*, bool); + + bool publish(char*, char*); + bool publish(char*, char*, bool); + + bool publish(char*, double); + bool publish(char*, double, bool); + bool publish(char*, double, int); + bool publish(char*, double, int, bool); + bool publish(char*, int); + bool publish(char*, int, bool); + bool publish(char*, String); + bool publish(char*, String, bool); + + bool chat(char*, char*); + bool chat(char*, int); + bool chat(char*, double); + bool chat(char*, double, int); + bool chat(char*, String); + void subscribe(char*); void unsubscribe(char*); - void chat(char*, char*); int state(); void loop(); void resetToken(); diff --git a/README.md b/README.md index ec97add..b9e4831 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,18 @@ 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 . ## Compatibility + We have tested this library and found it compatible with (but not limited to) the following hardware - ESP8266-01, 07, 12E, 12F - NodeMCU v1, v2, V3 - Espresso Lite v2.0 +## Outgoing Network Port + +Make sure ther following ports are allowed to connect from your network. +- Non-TLS mode : 8080 and 1883 (the library uses this mode by default) +- TLS mode : 8081 and 8883 (still under testing) + ## Installation * Download Arduino IDE 1.6.9 or later from https://www.arduino.cc/en/Main/Software * After installation, open Preferences @@ -182,6 +189,15 @@ Check the connection status, return true if it is connected. --- +**void MicroGear::useTLS(bool* enabled)** + +Switch between uncrypted and TLS mode (by default the library does not use TLS). This function must be called before the connection is made. + +**arguments** +* *enabled* - set to TRUE to use TLS. + +--- + **void MicroGear::setAlias(char* alias)** 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 --- -**void MicroGear::chat(char* target, char* message)** - +**bool MicroGear::chat(char* target, char* message)**
+**bool chat(char* target, int message);**
+**bool chat(char* target, double message);**
+**bool chat(char* target, double, int decimal);**
+**bool chat(char* target, String message);**
+ **arguments** * *target* - the alias of the microgear(s) that a message will be sent to. * *message* - message to be sent. +* *decimal* - number of digits after the deimal point. --- -**void MicroGear::publish(char* topic, char* message [, bool retained])** +**bool MicroGear::publish(char* topic, char* message [, bool retained])**
+**bool MicroGear::publish(char* topic, double message [, bool retained]);**
+**bool MicroGear::publish(char* topic, double message, int decimal [, bool retained]);**
+**bool MicroGear::publish(char* topic, int message [, bool retained]);**
+**bool MicroGear::publish(char* topic, String message [, bool retained]);**
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. **arguments** * *topic* - name of topic to be send a message to. * *message* - message to be sent. +* *decimal* - number of digits after the deimal point. * *retained* - retain a message or not, the default is false (optional)) ---