Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 77 additions & 13 deletions MicroGear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

/*
Expand Down Expand Up @@ -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) {
Expand Down
28 changes: 21 additions & 7 deletions MicroGear.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 .
Expand All @@ -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)**<br />
**bool chat(char* target, int message);**<br />
**bool chat(char* target, double message);**<br />
**bool chat(char* target, double, int decimal);**<br />
**bool chat(char* target, String message);**<br />

**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])**<br />
**bool MicroGear::publish(char* topic, double message [, bool retained]);**<br />
**bool MicroGear::publish(char* topic, double message, int decimal [, bool retained]);**<br />
**bool MicroGear::publish(char* topic, int message [, bool retained]);**<br />
**bool MicroGear::publish(char* topic, String message [, bool retained]);**<br />

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))

---
Expand Down