8000 Make library compatible with Particle devices · robokoding/arduinoWebSockets@7810d0d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7810d0d

Browse files
committed
Make library compatible with Particle devices
1 parent 1defe6d commit 7810d0d

File tree

8 files changed

+72
-7
lines changed

8 files changed

+72
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ The mode can be aktivated in the ```WebSockets.h``` (see WEBSOCKETS_NETWORK_TYPE
5252

5353
[ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required.
5454

55+
### Support for Particle devices ###
56+
- ESP.getFreeHeap() replaced by macro GET_FREE_HEAP, defined by the type of device (currently only for ESP and STM32-based/Particle devices).
57+
- Use Particle's TCPClient and TCPServer classes instead of Arduino's.
58+
5559
### Issues ###
5660
Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues
5761

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* To compile using make CLI, create a folder under \firmware\user\applications and copy application.cpp there.
2+
* Then, copy src files under particleWebSocket folder.
3+
*/
4+
5+
#include "application.h"
6+
#include "particleWebSocket/WebSocketsClient.h"
7+
8+
WebSocketsClient webSocket;
9+
10+
void webSocketEvent(WStype_t type, uint8_t* payload, size_t length)
11+
{
12+
switch (type)
13+
{
14+
case WStype_DISCONNECTED:
15+
Serial.printlnf("[WSc] Disconnected!");
16+
break;
17+
case WStype_CONNECTED:
18+
Serial.printlnf("[WSc] Connected to URL: %s", payload);
19+
webSocket.sendTXT("Connected\r\n");
20+
break;
21+
case WStype_TEXT:
22+
Serial.printlnf("[WSc] get text: %s", payload);
23+
break;
24+
case WStype_BIN:
25+
Serial.printlnf("[WSc] get binary length: %u", length);
26+
break;
27+
}
28+
}
29+
30+
void setup()
31+
{
32+
Serial.begin(9600);
33+
34+
WiFi.setCredentials("[SSID]", "[PASSWORD]", WPA2, WLAN_CIPHER_AES_TKIP);
35+
WiFi.connect();
36+
37+
webSocket.begin("192.168.1.153", 85, "/ClientService/?variable=Test1212");
38+
webSocket.onEvent(webSocketEvent);
39+
}
40+
41+
void loop()
42+
{
43+
webSocket.sendTXT("Hello world!");
44+
delay(500);
45+
webSocket.loop();
46+
}

src/WebSockets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
123123
#ifdef WEBSOCKETS_USE_BIG_MEM
124124
// only for ESP since AVR has less HEAP
125125
// try to send data in one TCP package (only if some free Heap is there)
126-
if(!headerToPayload && ((length > 0) && (length < 1400)) && (ESP.getFreeHeap() > 6000)) {
126+
if(!headerToPayload && ((length > 0) && (length < 1400)) && (GET_FREE_HEAP > 6000)) {
127127
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] pack to one TCP package...\n", client->num);
128128
uint8_t * dataPtr = (uint8_t *) malloc(length + WEBSOCKETS_MAX_HEADER_SIZE);
129129
if(dataPtr) {

src/WebSockets.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
#ifndef WEBSOCKETS_H_
2626
#define WEBSOCKETS_H_
2727

28+
#ifdef STM32_DEVICE
29+
#include <application.h>
30+
#define bit(b) (1UL << (b)) // Taken directly from Arduino.h
31+
#else
2832
#include <Arduino.h>
33+
#endif
2934

3035
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
3136

@@ -37,10 +42,17 @@
3742
#ifdef ESP8266
3843
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
3944
#define WEBSOCKETS_USE_BIG_MEM
45+
#define GET_FREE_HEAP ESP.getFreeHeap()
46+
#else
47+
#ifdef STM32_DEVICE
48+
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
49+
#define WEBSOCKETS_USE_BIG_MEM
50+
#define GET_FREE_HEAP System.freeMemory()
4051
#else
4152
//atmega328p has only 2KB ram!
4253
#define WEBSOCKETS_MAX_DATA_SIZE (1024)
4354
#endif
55+
#endif
4456

4557
#define WEBSOCKETS_TCP_TIMEOUT (2000)
4658

@@ -98,10 +110,15 @@
98110

99111
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100)
100112

113+
#ifdef STM32_DEVICE
114+
#define WEBSOCKETS_NETWORK_CLASS TCPClient
115+
#define WEBSOCKETS_NETWORK_SERVER_CLASS TCPServer
116+
#else
101117
#include <Ethernet.h>
102118
#include <SPI.h>
103119
#define WEBSOCKETS_NETWORK_CLASS EthernetClient
104120
#define WEBSOCKETS_NETWORK_SERVER_CLASS EthernetServer
121+
#endif
105122

106123
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60)
107124

src/WebSocketsClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
443443

444444
handshake += "\r\n";
445445

446-
client->tcp->write(handshake.c_str(), handshake.length());
446+
client->tcp->write((uint8_t*)handshake.c_str(), handshake.length());
447447

448448
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
449449
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));

src/WebSocketsClient.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#ifndef WEBSOCKETSCLIENT_H_
2626
#define WEBSOCKETSCLIENT_H_
2727

28-
#include <Arduino.h>
2928
#include "WebSockets.h"
3029

3130
class WebSocketsClient: private WebSockets {

src/WebSocketsServer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,20 +739,20 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
739739
"Connection: Upgrade\r\n"
740740
"Sec-WebSocket-Version: 13\r\n"
741741
"Sec-WebSocket-Accept: ");
742-
client->tcp->write(sKey.c_str(), sKey.length());
742+
client->tcp->write((uint8_t*)sKey.c_str(), sKey.length());
743743

744744
if(_origin.length() > 0) {
745745
String origin = "\r\nAccess-Control-Allow-Origin: ";
746746
origin += _origin;
747747
origin += "\r\n";
748-
client->tcp->write(origin.c_str(), origin.length());
748+
client->tcp->write((uint8_t*)origin.c_str(), origin.length());
749749
}
750750

751751
if(client->cProtocol.length() > 0) {
752752
String protocol = "\r\nSec-WebSocket-Protocol: ";
753753
protocol += _protocol;
754754
protocol += "\r\n";
755-
client->tcp->write(protocol.c_str(), protocol.length());
755+
client->tcp->write((uint8_t*)protocol.c_str(), protocol.length());
756756
} else {
757757
client->tcp->write("\r\n");
758758
}

src/WebSocketsServer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#ifndef WEBSOCKETSSERVER_H_
2626
#define WEBSOCKETSSERVER_H_
2727

28-
#include <Arduino.h>
2928
#include "WebSockets.h"
3029

3130
#define WEBSOCKETS_SERVER_CLIENT_MAX (5)

0 commit comments

Comments
 (0)
0