Skip to content

Commit afb78e3

Browse files
authored
Merge change from branch imp (#10)
* new update * implement function microgear.writeFeed() * Update README.md * Update README.th.md * Update README.md * Update README.md
1 parent 717bdd4 commit afb78e3

File tree

5 files changed

+206
-85
lines changed

5 files changed

+206
-85
lines changed

MicroGear.cpp

Lines changed: 164 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ void (* cb_message)(char*, uint8_t*,unsigned int);
77
void (* cb_present)(char*, uint8_t*,unsigned int);
88
void (* cb_absent)(char*, uint8_t*,unsigned int);
99
void (* cb_connected)(char*, uint8_t*,unsigned int);
10+
void (* cb_error)(char*, uint8_t*,unsigned int);
11+
void (* cb_info)(char*, uint8_t*,unsigned int);
1012

1113
void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
1214
/* remove /appid/ */
@@ -31,6 +33,18 @@ void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
3133
if (mg) mg->resetEndpoint();
3234
}
3335
}
36+
else if (*topic == '@') {
37+
if (strcmp(topic,"@error")==0) {
38+
if (cb_error) {
39+
cb_error("error",payload,length);
40+
}
41+
}
42+
else if (strcmp(topic,"@info")==0) {
43+
if (cb_info) {
44+
cb_info("info",payload,length);
45+
}
46+
}
47+
}
3448
else if (cb_message) {
3549
cb_message(topic,payload,length);
3650
}
@@ -100,18 +114,19 @@ void MicroGear::initEndpoint(Client *client, char* endpoint) {
100114
char pstr[100];
101115
int port = this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT;
102116

103-
client->connect(GEARAUTHHOST,port);
104-
sprintf(pstr,"GET /api/endpoint/%s HTTP/1.1\r\n\r\n",this->gearkey);
105-
client->write((const uint8_t *)pstr,strlen(pstr));
117+
if(client->connect(GEARAUTHHOST,port)){
118+
sprintf(pstr,"GET /api/endpoint/%s HTTP/1.1\r\n\r\n",this->gearkey);
119+
client->write((const uint8_t *)pstr,strlen(pstr));
106120

107-
delay(1000);
108-
getHTTPReply(client,pstr,200);
121+
delay(1000);
122+
getHTTPReply(client,pstr,200);
109123

110-
if (strlen(pstr)>6) {
111-
strcpy(endpoint,pstr+6);
112-
writeEEPROM(endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
113-
}
114-
client->stop();
124+
if (strlen(pstr)>6) {
125+
strcpy(endpoint,pstr+6);
126+
writeEEPROM(endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
127+
}
128+
client->stop();
129+
}
115130
}
116131
}
117132

@@ -122,62 +137,63 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
122137
int port = (this->securemode)?GEARAUTHSECUREPORT:GEARAUTHPORT;
123138

124139
*bts = 0;
125-
client->connect(GEARAUTHHOST,port);
126-
127-
if (this->securemode) {
128-
WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
129-
130-
// verify a certificate fingerprint against a fingerprint saved in eeprom
131-
readEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
132-
#ifdef DEBUG_H
133-
Serial.print("fingerprint loaded from eeprom : ");
134-
Serial.println(tstr);
135-
#endif
136-
if (clientsecure->verify(tstr, GEARAUTHHOST)) {
137-
#ifdef DEBUG_H
138-
Serial.println("fingerprint matched");
139-
#endif
140-
}
141-
else {
142-
#ifdef DEBUG_H
143-
Serial.println("fingerprint mismatched, going to update");
144-
#endif
145-
AuthClient::randomString(nonce,8);
146-
sprintf(tstr,"GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n",this->gearkey,nonce);
147-
clientsecure->write((const uint8_t *)tstr,strlen(tstr));
148-
delay(800);
149-
getHTTPReply(clientsecure,tstr,200);
150-
tstr[FINGERPRINTSIZE-1] = '\0'; // split fingerprint and signature
151-
sprintf(hashkey,"%s&%s&%s",this->gearkey,this->gearsecret,nonce);
152-
Sha1.initHmac((uint8_t*)hashkey,strlen(hashkey));
153-
Sha1.HmacBase64(hash, tstr);
154-
for (int i=0;i<HMACSIZE;i++)
155-
if (hash[i]=='/') hash[i] = '_';
156-
157-
if(strcmp(hash,tstr+FINGERPRINTSIZE)==0) {
158-
#ifdef DEBUG_H
159-
Serial.println("new fingerprint updated");
160-
#endif
161-
writeEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
162-
}
163-
else {
164-
#ifdef DEBUG_H
165-
Serial.println("fingerprint verification failed, abort");
166-
#endif
167-
clientsecure->stop();
168-
delay(5000);
169-
return;
170-
}
171-
}
172-
}
173-
174-
strcpy(tstr,"GET /api/time HTTP/1.1\r\n\r\n");
175-
client->write((const uint8_t *)tstr,strlen(tstr));
176-
177-
delay(1000);
178-
getHTTPReply(client,tstr,200);
179-
*bts = atol(tstr) - millis()/1000;
180-
client->stop();
140+
if(client->connect(GEARAUTHHOST,port)){
141+
142+
if (this->securemode) {
143+
WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
144+
145+
// verify a certificate fingerprint against a fingerprint saved in eeprom
146+
readEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
147+
#ifdef DEBUG_H
148+
Serial.print("fingerprint loaded from eeprom : ");
149+
Serial.println(tstr);
150+
#endif
151+
if (clientsecure->verify(tstr, GEARAUTHHOST)) {
152+
#ifdef DEBUG_H
153+
Serial.println("fingerprint matched");
154+
#endif
155+
}
156+
else {
157+
#ifdef DEBUG_H
158+
Serial.println("fingerprint mismatched, going to update");
159+
#endif
160+
AuthClient::randomString(nonce,8);
161+
sprintf(tstr,"GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n",this->gearkey,nonce);
162+
clientsecure->write((const uint8_t *)tstr,strlen(tstr));
163+
delay(800);
164+
getHTTPReply(clientsecure,tstr,200);
165+
tstr[FINGERPRINTSIZE-1] = '\0'; // split fingerprint and signature
166+
sprintf(hashkey,"%s&%s&%s",this->gearkey,this->gearsecret,nonce);
167+
Sha1.initHmac((uint8_t*)hashkey,strlen(hashkey));
168+
Sha1.HmacBase64(hash, tstr);
169+
for (int i=0;i<HMACSIZE;i++)
170+
if (hash[i]=='/') hash[i] = '_';
171+
172+
if(strcmp(hash,tstr+FINGERPRINTSIZE)==0) {
173+
#ifdef DEBUG_H
174+
Serial.println("new fingerprint updated");
175+
#endif
176+
writeEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
177+
}
178+
else {
179+
#ifdef DEBUG_H
180+
Serial.println("fingerprint verification failed, abort");
181+
#endif
182+
clientsecure->stop();
183+
delay(5000);
184+
return;
185+
}
186+
}
187+
}
188+
189+
strcpy(tstr,"GET /api/time HTTP/1.1\r\n\r\n");
190+
client->write((const uint8_t *)tstr,strlen(tstr));
191+
192+
delay(1000);
193+
getHTTPReply(client,tstr,200);
194+
*bts = atol(tstr) - millis()/1000;
195+
client->stop();
196+
}
181197
}
182198

183199
MicroGear::MicroGear(Client& netclient ) {
@@ -198,6 +214,9 @@ MicroGear::MicroGear(Client& netclient ) {
198214
cb_connected = NULL;
199215
cb_absent = NULL;
200216
cb_present = NULL;
217+
cb_error = NULL;
218+
cb_info = NULL;
219+
201220
eepromready = false;
202221

203222
mg = (MicroGear *)this;
@@ -220,6 +239,13 @@ void MicroGear::on(unsigned char event, void (* callback)(char*, uint8_t*,unsign
220239
break;
221240
case CONNECTED :
222241
if (callback) cb_connected = callback;
242+
break;
243+
case ERROR :
244+
if (callback) cb_error = callback;
245+
break;
246+
case INFO :
247+
if (callback) cb_info = callback;
248+
break;
223249
}
224250
}
225251

@@ -257,20 +283,21 @@ void MicroGear::resetToken() {
257283
char revokecode[REVOKECODESIZE+1];
258284
int port = this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT;
259285

260-
sockclient->connect(GEARAUTHHOST,port);
261-
readEEPROM(token,EEPROM_TOKENOFFSET,TOKENSIZE);
262-
readEEPROM(revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE);
263-
sprintf(pstr,"GET /api/revoke/%s/%s HTTP/1.1\r\n\r\n",token,revokecode);
264-
sockclient->write((const uint8_t *)pstr,strlen(pstr));
265-
266-
delay(1000);
267-
getHTTPReply(sockclient,pstr,200);
268-
269-
if (strcmp(pstr,"FAILED")!=0) {
270-
*state = EEPROM_STATE_NUL;
271-
writeEEPROM(state,EEPROM_STATEOFFSET,1);
272-
}
273-
sockclient->stop();
286+
if(sockclient->connect(GEARAUTHHOST,port)){
287+
readEEPROM(token,EEPROM_TOKENOFFSET,TOKENSIZE);
288+
readEEPROM(revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE);
289+
sprintf(pstr,"GET /api/revoke/%s/%s HTTP/1.1\r\n\r\n",token,revokecode);
290+
sockclient->write((const uint8_t *)pstr,strlen(pstr));
291+
292+
delay(1000);
293+
getHTTPReply(sockclient,pstr,200);
294+
295+
if (strcmp(pstr,"FAILED")!=0) {
296+
*state = EEPROM_STATE_NUL;
297+
writeEEPROM(state,EEPROM_STATEOFFSET,1);
298+
}
299+
sockclient->stop();
300+
}
274301
}
275302
else {
276303
*state = EEPROM_STATE_NUL;
@@ -375,6 +402,7 @@ bool MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
375402
#endif
376403
authclient->stop();
377404
delay(1000);
405+
break;
378406
}
379407
}
380408
#ifdef DEBUG_H
@@ -466,9 +494,7 @@ int MicroGear::connectBroker(char* appid) {
466494
int gbport;
467495
bool tokenOK;
468496

469-
do {
470-
syncTime(sockclient, &bootts);
471-
} while (bootts == 0);
497+
syncTime(sockclient, &bootts);
472498

473499
#ifdef DEBUG_H
474500
Serial.print("Time stamp : ");
@@ -633,10 +659,64 @@ bool MicroGear::publish(char* topic, String message) {
633659

634660
bool MicroGear::publish(char* topic, String message, bool retained) {
635661
char buff[MAXBUFFSIZE];
636-
message.toCharArray(buff,MAXBUFFSIZE);
662+
message.toCharArray(buff,MAXBUFFSIZE-1);
637663
return publish(topic, buff, retained);
638664
}
639665

666+
bool MicroGear::publish(char* topic, String message, String apikey) {
667+
char buff[MAXBUFFSIZE];
668+
message.toCharArray(buff,MAXBUFFSIZE-1);
669+
670+
char top[MAXTOPICSIZE] = "";
671+
strcat(top,topic);
672+
if(apikey!=""){
673+
strcat(top,"/");
674+
char buffapikey[MAXBUFFSIZE];
675+
apikey.toCharArray(buffapikey,MAXBUFFSIZE);
676+
strcat(top,buffapikey);
677+
}
678+
return publish(top, buff);
679+
}
680+
681+
bool MicroGear::publish(char* topic, String message, char* apikey) {
682+
char buff[MAXBUFFSIZE];
683+
message.toCharArray(buff,MAXBUFFSIZE-1);
684+
685+
char top[MAXTOPICSIZE] = "";
686+
strcat(top,topic);
687+
if(apikey!=""){
688+
strcat(top,"/");
689+
strcat(top,apikey);
690+
}
691+
return publish(top, buff);
692+
}
693+
694+
bool MicroGear::writeFeed(char* feedname, char *data, char* apikey) {
695+
char buff[MAXBUFFSIZE] = "/@writefeed/";
696+
697+
strcat(buff,feedname);
698+
if(apikey!=NULL && strlen(apikey)>0){
699+
strcat(buff,"/");
700+
strcat(buff,apikey);
701+
}
702+
return publish(buff, data);
703+
}
704+
705+
bool MicroGear::writeFeed(char* feedname, char *data) {
706+
return writeFeed(feedname, data, NULL);
707+
}
708+
709+
bool MicroGear::writeFeed(char* feedname, String data, char* apikey) {
710+
char buff[MAXBUFFSIZE];
711+
data.toCharArray(buff,MAXBUFFSIZE-1);
712+
713+
return writeFeed(feedname, data,apikey);
714+
}
715+
716+
bool MicroGear::writeFeed(char* feedname, String data) {
717+
return writeFeed(feedname, data,NULL);
718+
}
719+
640720
/*
641721
setName() is deprecated
642722
*/

MicroGear.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
#define ABSENT 3
6868
#define CONNECTED 4
6969
#define CALLBACK 5
70+
#define ERROR 6
71+
#define INFO 7
7072

7173

7274
class MicroGear {
@@ -119,6 +121,13 @@ class MicroGear {
119121
bool publish(char*, int, bool);
120122
bool publish(char*, String);
121123
bool publish(char*, String, bool);
124+
bool publish(char*, String, String);
125+
bool publish(char*, String, char*);
126+
127+
bool writeFeed(char*, char*);
128+
bool writeFeed(char*, char*, char*);
129+
bool writeFeed(char*, String);
130+
bool writeFeed(char*, String, char*);
122131

123132
bool chat(char*, char*);
124133
bool chat(char*, int);

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ cancel subscription
251251
**arguments**
252252
* *topic* - name of topic to be send a message to.
253253

254+
---
255+
**void microgear.writeFeed (char* feedid, char *datajson)**<br/>
256+
**void microgear.writeFeed (char* feedid, char *datajson, char *apikey)**<br/>
257+
**void microgear.writeFeed (char* feedid, String datajson)**<br/>
258+
**void microgear.writeFeed (char* feedid, String datajson, char *apikey)**<br/>
259+
260+
write time series data to a feed storage
261+
262+
**arguments**
263+
* *feedid* - name of the feed
264+
* *datajson* - data string in json format
265+
* *apikey* - apikey for authorization. If apikey is not specified, you will need to allow the AppID to access feed and then the default apikey will be assigned automatically.
266+
267+
```js
268+
microgear.writeFeed("homesensor","{temp:25.7,humid:62.8,light:8.5}");
269+
```
254270
---
255271

256272
**void MicroGear::resetToken()**

README.th.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ microgear อาจจะมีความสนใจใน topic ใดเป
236236
* *topic* - ชื่อของ topic ที่ต้องการจะส่งข้อความไปถึง
237237

238238
---
239+
**void microgear.writeFeed (char* feedid, char *datajson)**<br/>
240+
**void microgear.writeFeed (char* feedid, char *datajson, char *apikey)**<br/>
241+
**void microgear.writeFeed (char* feedid, String datajson)**<br/>
242+
**void microgear.writeFeed (char* feedid, String datajson, char *apikey)**<br/>
243+
เขียนข้อมูลลง feed storage
244+
245+
**arguments**
246+
* *feedid* - ชื่อของ feed ที่ต้องการจะเขียนข้อมูล
247+
* *datajson* - ข้อมูลที่จะบันทึก ในรูปแบบ json
248+
* *apikey* - apikey สำหรับตรวจสอบสิทธิ์ หากไม่กำหนด จะใช้ default apikey ของ feed ที่ให้สิทธิ์ไว้กับ AppID
249+
250+
```js
251+
microgear.writeFeed("homesensor","{temp:25.7,humid:62.8,light:8.5}");
252+
```
253+
---
254+
239255

240256
**void MicroGear::resetToken()**
241257

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP8266 Microgear
2-
version=1.2.0
2+
version=1.2.1
33
author=Chavee Issariyapat <i@chavee.com>
44
maintainer=Chavee Issariyapat <i@chavee.com>
55
sentence=A client library for ESP8266 to connect to NETPIE IOT Platform.

0 commit comments

Comments
 (0)