Skip to content

Commit 2706c5b

Browse files
authored
Merge pull request #29 from grandeurtech/v1.0.3
Issue connecting on new ESP32 because of outdated websockets library is resolved
2 parents e0120cd + 8c9c255 commit 2706c5b

File tree

16 files changed

+698
-239
lines changed

16 files changed

+698
-239
lines changed

examples/CrossListening/CrossListening-esp32/CrossListening-esp32.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void setStatePinToNewValue(const char* path, bool state);
5454
void afterVoltageIsUpdated(const char* code, int voltage);
5555

5656
void setup() {
57-
Serial.begin(9600);
57+
.begin(9600);
5858
startWiFi();
5959
// This initializes the SDK's configurations and returns reference to your project.
6060
project = grandeur.init(apiKey, token);

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Grandeur
2-
version=1.0.2
2+
version=1.0.3
33
author=Grandeur Technologies
44
maintainer=Grandeur Technologies <[email protected]>
55
sentence=Let your arduinos and ESPs communicate with Grandeur in realtime.

src/Data.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ void Grandeur::Project::Device::Data::set(const char* path, Var data) {
7474
oPayload["path"] = path;
7575
oPayload["data"] = data;
7676

77-
// Sending the packet and scheduling callback.
78-
_duplex->send("/device/data/set", oPayload, NULL);
77+
// Sending the packet without response.
78+
_duplex->send("/device/data/set", oPayload);
7979
}
8080

8181
Grandeur::Project::Device::Event Grandeur::Project::Device::Data::on(const char* path, Callback cb) {

src/DuplexHandler.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void DuplexHandler::loop(bool valve) {
4646
timeSinceLastMessage = millis();
4747

4848
DEBUG_GRANDEUR("Pinging Grandeur.");
49-
send("ping", NULL);
49+
send("ping");
5050
}
5151
// Running duplex loop
5252
_client.loop();
@@ -111,6 +111,22 @@ Message DuplexHandler::send(const char* task, Callback cb) {
111111
return {message.id, message.str};
112112
}
113113

114+
Message DuplexHandler::send(const char* task) {
115+
// Preparing a new message.
116+
Message message = prepareMessage(task);
117+
118+
// If channel isn't connected yet, buffer the message and return.
119+
if(_status != CONNECTED) {
120+
_buffer.push(message.id, message.str);
121+
return {message.id, message.str};
122+
}
123+
124+
// Sending message.
125+
sendMessage(message.str.c_str());
126+
127+
return {message.id, message.str};
128+
}
129+
114130
Message DuplexHandler::send(const char* task, Var payload, Callback cb) {
115131
// Preparing a new message.
116132
Message message = prepareMessage(task, payload);
@@ -130,6 +146,22 @@ Message DuplexHandler::send(const char* task, Var payload, Callback cb) {
130146
return {message.id, message.str};
131147
}
132148

149+
Message DuplexHandler::send(const char* task, Var payload) {
150+
// Preparing a new message.
151+
Message message = prepareMessage(task, payload);
152+
153+
// If channel isn't connected yet, buffer the message and return.
154+
if(_status != CONNECTED) {
155+
_buffer.push(message.id, message.str);
156+
return {message.id, message.str};
157+
}
158+
159+
// Sending message.
160+
sendMessage(message.str.c_str());
161+
162+
return {message.id, message.str};
163+
}
164+
133165
void DuplexHandler::receive(Var header, Var payload) {
134166
// Extracting task and id from header and code.
135167
const char* task = header["task"];
@@ -254,8 +286,7 @@ void DuplexHandler::duplexEventHandler(WStype_t eventType, uint8_t* message, siz
254286
// We do not need to handle the unpair event in Device SDKs.
255287
if(strcmp(task, "unpair") == 0);
256288
// Ping has no data so we simply emit.
257-
else if(strcmp(task, "ping") == 0)
258-
_tasks.emit((gId) header["id"], "", undefined);
289+
else if(strcmp(task, "ping") == 0);
259290
// If it is an update event rather than a task (response message).
260291
else if(strcmp(task, "update") == 0)
261292
publish(payload["event"], payload["path"], payload["update"]);

src/DuplexHandler.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ class DuplexHandler {
7272
// Constructor
7373
DuplexHandler();
7474
void init(Config config);
75-
// Sends a message to duplex channel.
75+
// Sends a message to duplex channel:
76+
// without payload.
7677
Message send(const char* task, Callback cb);
78+
// without payload, without response.
79+
Message send(const char* task);
80+
// with payload.
7781
Message send(const char* task, Var payload, Callback cb);
82+
// with payload, without response.
83+
Message send(const char* task, Var payload);
7884

7985
// Subscribes to a topic.
8086
gId subscribe(const char* topic, Var payload, Callback updateHandler);

src/arduinoWebSockets/SocketIOclient.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,59 @@ SocketIOclient::~SocketIOclient() {
1818
void SocketIOclient::begin(const char * host, uint16_t port, const char * url, const char * protocol) {
1919
WebSocketsClient::beginSocketIO(host, port, url, protocol);
2020
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
21+
initClient();
2122
}
2223

2324
void SocketIOclient::begin(String host, uint16_t port, String url, String protocol) {
2425
WebSocketsClient::beginSocketIO(host, port, url, protocol);
2526
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
27+
initClient();
28+
}
29+
#if defined(HAS_SSL)
30+
void SocketIOclient::beginSSL(const char * host, uint16_t port, const char * url, const char * protocol) {
31+
WebSocketsClient::beginSocketIOSSL(host, port, url, protocol);
32+
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
33+
initClient();
34+
}
35+
36+
void SocketIOclient::beginSSL(String host, uint16_t port, String url, String protocol) {
37+
WebSocketsClient::beginSocketIOSSL(host, port, url, protocol);
38+
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
39+
initClient();
40+
}
41+
#if defined(SSL_BARESSL)
42+
void SocketIOclient::beginSSLWithCA(const char * host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
43+
WebSocketsClient::beginSocketIOSSLWithCA(host, port, url, CA_cert, protocol);
44+
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
45+
initClient();
46+
}
47+
48+
void SocketIOclient::beginSSLWithCA(const char * host, uint16_t port, const char * url, BearSSL::X509List * CA_cert, const char * protocol) {
49+
WebSocketsClient::beginSocketIOSSLWithCA(host, port, url, CA_cert, protocol);
50+
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
51+
initClient();
52+
}
53+
54+
void SocketIOclient::setSSLClientCertKey(const char * clientCert, const char * clientPrivateKey) {
55+
WebSocketsClient::setSSLClientCertKey(clientCert, clientPrivateKey);
56+
}
57+
58+
void SocketIOclient::setSSLClientCertKey(BearSSL::X509List * clientCert, BearSSL::PrivateKey * clientPrivateKey) {
59+
WebSocketsClient::setSSLClientCertKey(clientCert, clientPrivateKey);
60+
}
61+
62+
#endif
63+
#endif
64+
65+
void SocketIOclient::configureEIOping(bool disableHeartbeat) {
66+
_disableHeartbeat = disableHeartbeat;
67+
}
68+
69+
void SocketIOclient::initClient(void) {
70+
if(_client.cUrl.indexOf("EIO=4") != -1) {
71+
DEBUG_WEBSOCKETS("[wsIOc] found EIO=4 disable EIO ping on client\n");
72+
configureEIOping(true);
73+
}
2674
}
2775

2876
/**
@@ -51,7 +99,7 @@ bool SocketIOclient::send(socketIOmessageType_t type, uint8_t * payload, size_t
5199
if(length == 0) {
52100
length = strlen((const char *)payload);
53101
}
54-
if(clientIsConnected(&_client)) {
102+
if(clientIsConnected(&_client) && _client.status == WSC_CONNECTED) {
55103
if(!headerToPayload) {
56104
// webSocket Header
57105
ret = WebSocketsClient::sendFrameHeader(&_client, WSop_text, length + 2, true);
@@ -118,8 +166,8 @@ bool SocketIOclient::sendEVENT(String & payload) {
118166
void SocketIOclient::loop(void) {
119167
WebSocketsClient::loop();
120168
unsigned long t = millis();
121-
if((t - _lastConnectionFail) > EIO_HEARTBEAT_INTERVAL) {
122-
_lastConnectionFail = t;
169+
if(!_disableHeartbeat && (t - _lastHeartbeat) > EIO_HEARTBEAT_INTERVAL) {
170+
_lastHeartbeat = t;
123171
DEBUG_WEBSOCKETS("[wsIOc] send ping\n");
124172
WebSocketsClient::sendTXT(eIOtype_PING);
125173
}
@@ -135,6 +183,7 @@ void SocketIOclient::handleCbEvent(WStype_t type, uint8_t * payload, size_t leng
135183
DEBUG_WEBSOCKETS("[wsIOc] Connected to url: %s\n", payload);
136184
// send message to server when Connected
137185
// Engine.io upgrade confirmation message (required)
186+
WebSocketsClient::sendTXT("2probe");
138187
WebSocketsClient::sendTXT(eIOtype_UPGRADE);
139188
runIOCbEvent(sIOtype_CONNECT, payload, length);
140189
} break;
@@ -165,6 +214,8 @@ void SocketIOclient::handleCbEvent(WStype_t type, uint8_t * payload, size_t leng
165214
DEBUG_WEBSOCKETS("[wsIOc] get event (%d): %s\n", lData, data);
166215
break;
167216
case sIOtype_CONNECT:
217+
DEBUG_WEBSOCKETS("[wsIOc] connected (%d): %s\n", lData, data);
218+
return;
168219
case sIOtype_DISCONNECT:
169220
case sIOtype_ACK:
170221
case sIOtype_ERROR:

src/arduinoWebSockets/SocketIOclient.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ class SocketIOclient : protected WebSocketsClient {
4949
void begin(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
5050
void begin(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
5151

52+
#ifdef HAS_SSL
53+
void beginSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
54+
void beginSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
55+
#ifndef SSL_AXTLS
56+
void beginSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
57+
void beginSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", BearSSL::X509List * CA_cert = NULL, const char * protocol = "arduino");
58+
void setSSLClientCertKey(const char * clientCert = NULL, const char * clientPrivateKey = NULL);
59+
void setSSLClientCertKey(BearSSL::X509List * clientCert = NULL, BearSSL::PrivateKey * clientPrivateKey = NULL);
60+
#endif
61+
#endif
5262
bool isConnected(void);
5363

5464
void onEvent(SocketIOclientEvent cbEvent);
@@ -67,7 +77,10 @@ class SocketIOclient : protected WebSocketsClient {
6777

6878
void loop(void);
6979

80+
void configureEIOping(bool disableHeartbeat = false);
81+
7082
protected:
83+
bool _disableHeartbeat = false;
7184
uint64_t _lastHeartbeat = 0;
7285
SocketIOclientEvent _cbEvent;
7386
virtual void runIOCbEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
@@ -76,6 +89,8 @@ class SocketIOclient : protected WebSocketsClient {
7689
}
7790
}
7891

92+
void initClient(void);
93+
7994
// Handeling events from websocket layer
8095
virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
8196
handleCbEvent(type, payload, length);

src/arduinoWebSockets/WebSockets.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ extern "C" {
3939
#ifdef ESP8266
4040
#include <Hash.h>
4141
#elif defined(ESP32)
42+
#include <esp_system.h>
43+
44+
#if ESP_IDF_VERSION_MAJOR >= 4
45+
#include <esp32/sha.h>
46+
#else
4247
#include <hwcrypto/sha.h>
48+
#endif
49+
4350
#else
4451

4552
extern "C" {
@@ -494,7 +501,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
494501
reasonCode = payload[0] << 8 | payload[1];
495502
}
496503
#endif
497-
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get ask for close. Code: %d", client->num, reasonCode);
504+
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get ask for close. Code: %d\n", client->num, reasonCode);
498505
if(header->payloadLen > 2) {
499506
DEBUG_WEBSOCKETS(" (%s)\n", (payload + 2));
500507
} else {
@@ -503,6 +510,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
503510
clientDisconnect(client, 1000);
504511
} break;
505512
default:
513+
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] got unknown opcode: %d\n", client->num, header->opCode);
506514
clientDisconnect(client, 1002);
507515
break;
508516
}
@@ -623,7 +631,7 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
623631
}
624632

625633
if(!client->tcp->available()) {
626-
WEBSOCKETS_YIELD();
634+
WEBSOCKETS_YIELD_MORE();
627635
continue;
628636
}
629637

@@ -636,7 +644,9 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
636644
} else {
637645
//DEBUG_WEBSOCKETS("Receive %d left %d!\n", len, n);
638646
}
639-
WEBSOCKETS_YIELD();
647+
if(n > 0) {
648+
WEBSOCKETS_YIELD();
649+
}
640650
}
641651
if(cb) {
642652
cb(client, true);
@@ -686,9 +696,11 @@ size_t WebSockets::write(WSclient_t * client, uint8_t * out, size_t n) {
686696
total += len;
687697
//DEBUG_WEBSOCKETS("write %d left %d!\n", len, n);
688698
} else {
689-
//DEBUG_WEBSOCKETS("write %d failed left %d!\n", len, n);
699+
DEBUG_WEBSOCKETS("WS write %d failed left %d!\n", len, n);
700+
}
701+
if(n > 0) {
702+
WEBSOCKETS_YIELD();
690703
}
691-
WEBSOCKETS_YIELD();
692704
}
693705
WEBSOCKETS_YIELD();
694706
return total;

0 commit comments

Comments
 (0)