Skip to content

Commit dc426c9

Browse files
authored
Merge pull request #170 from CAOU123/master
Make library compatible with Particle devices
2 parents 26fc61f + 60e3d10 commit dc426c9

File tree

8 files changed

+74
-8
lines changed

8 files changed

+74
-8
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ The mode can be activated 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

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

+1-1
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

+17
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

+3-2
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,9 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
460460
}
461461

462462
handshake += "\r\n";
463-
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", handshake.c_str());
464-
client->tcp->write(handshake.c_str(), handshake.length());
463+
464+
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t*)handshake.c_str());
465+
client->tcp->write((uint8_t*)handshake.c_str(), handshake.length());
465466

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

src/WebSocketsClient.h

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -790,20 +790,20 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
790790
"Connection: Upgrade\r\n"
791791
"Sec-WebSocket-Version: 13\r\n"
792792
"Sec-WebSocket-Accept: ");
793-
client->tcp->write(sKey.c_str(), sKey.length());
793+
client->tcp->write((uint8_t*)sKey.c_str(), sKey.length());
794794

795795
if(_origin.length() > 0) {
796796
String origin = "\r\nAccess-Control-Allow-Origin: ";
797797
origin += _origin;
798798
origin += "\r\n";
799-
client->tcp->write(origin.c_str(), origin.length());
799+
client->tcp->write((uint8_t*)origin.c_str(), origin.length());
800800
}
801801

802802
if(client->cProtocol.length() > 0) {
803803
String protocol = "\r\nSec-WebSocket-Protocol: ";
804804
protocol += _protocol;
805805
protocol += "\r\n";
806-
client->tcp->write(protocol.c_str(), protocol.length());
806+
client->tcp->write((uint8_t*)protocol.c_str(), protocol.length());
807807
} else {
808808
client->tcp->write("\r\n");
809809
}

src/WebSocketsServer.h

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