diff --git a/README.md b/README.md index 6f2e5a4..5041cbf 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ The mode can be activated in the ```WebSockets.h``` (see WEBSOCKETS_NETWORK_TYPE [ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required. +### Support for Particle devices ### +- ESP.getFreeHeap() replaced by macro GET_FREE_HEAP, defined by the type of device (currently only for ESP and STM32-based/Particle devices). +- Use Particle's TCPClient and TCPServer classes instead of Arduino's. + ### Issues ### Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues diff --git a/examples/ParticleWebSocketClient/application.cpp b/examples/ParticleWebSocketClient/application.cpp new file mode 100644 index 0000000..461228f --- /dev/null +++ b/examples/ParticleWebSocketClient/application.cpp @@ -0,0 +1,46 @@ +/* To compile using make CLI, create a folder under \firmware\user\applications and copy application.cpp there. +* Then, copy src files under particleWebSocket folder. +*/ + +#include "application.h" +#include "particleWebSocket/WebSocketsClient.h" + +WebSocketsClient webSocket; + +void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) +{ + switch (type) + { + case WStype_DISCONNECTED: + Serial.printlnf("[WSc] Disconnected!"); + break; + case WStype_CONNECTED: + Serial.printlnf("[WSc] Connected to URL: %s", payload); + webSocket.sendTXT("Connected\r\n"); + break; + case WStype_TEXT: + Serial.printlnf("[WSc] get text: %s", payload); + break; + case WStype_BIN: + Serial.printlnf("[WSc] get binary length: %u", length); + break; + } +} + +void setup() +{ + Serial.begin(9600); + + WiFi.setCredentials("[SSID]", "[PASSWORD]", WPA2, WLAN_CIPHER_AES_TKIP); + WiFi.connect(); + + webSocket.begin("192.168.1.153", 85, "/ClientService/?variable=Test1212"); + webSocket.onEvent(webSocketEvent); +} + +void loop() +{ + webSocket.sendTXT("Hello world!"); + delay(500); + webSocket.loop(); +} diff --git a/src/WebSockets.cpp b/src/WebSockets.cpp index 18eb019..3c3d69d 100644 --- a/src/WebSockets.cpp +++ b/src/WebSockets.cpp @@ -123,7 +123,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay #ifdef WEBSOCKETS_USE_BIG_MEM // only for ESP since AVR has less HEAP // try to send data in one TCP package (only if some free Heap is there) - if(!headerToPayload && ((length > 0) && (length < 1400)) && (ESP.getFreeHeap() > 6000)) { + if(!headerToPayload && ((length > 0) && (length < 1400)) && (GET_FREE_HEAP > 6000)) { DEBUG_WEBSOCKETS("[WS][%d][sendFrame] pack to one TCP package...\n", client->num); uint8_t * dataPtr = (uint8_t *) malloc(length + WEBSOCKETS_MAX_HEADER_SIZE); if(dataPtr) { diff --git a/src/WebSockets.h b/src/WebSockets.h index c43f4d9..272eeca 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -25,7 +25,12 @@ #ifndef WEBSOCKETS_H_ #define WEBSOCKETS_H_ +#ifdef STM32_DEVICE +#include +#define bit(b) (1UL << (b)) // Taken directly from Arduino.h +#else #include +#endif //#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ ) @@ -37,10 +42,17 @@ #ifdef ESP8266 #define WEBSOCKETS_MAX_DATA_SIZE (15*1024) #define WEBSOCKETS_USE_BIG_MEM +#define GET_FREE_HEAP ESP.getFreeHeap() +#else +#ifdef STM32_DEVICE +#define WEBSOCKETS_MAX_DATA_SIZE (15*1024) +#define WEBSOCKETS_USE_BIG_MEM +#define GET_FREE_HEAP System.freeMemory() #else //atmega328p has only 2KB ram! #define WEBSOCKETS_MAX_DATA_SIZE (1024) #endif +#endif #define WEBSOCKETS_TCP_TIMEOUT (2000) @@ -98,10 +110,15 @@ #elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100) +#ifdef STM32_DEVICE +#define WEBSOCKETS_NETWORK_CLASS TCPClient +#define WEBSOCKETS_NETWORK_SERVER_CLASS TCPServer +#else #include #include #define WEBSOCKETS_NETWORK_CLASS EthernetClient #define WEBSOCKETS_NETWORK_SERVER_CLASS EthernetServer +#endif #elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60) diff --git a/src/WebSocketsClient.cpp b/src/WebSocketsClient.cpp index c526d96..18c9f6c 100644 --- a/src/WebSocketsClient.cpp +++ b/src/WebSocketsClient.cpp @@ -460,8 +460,9 @@ void WebSocketsClient::sendHeader(WSclient_t * client) { } handshake += "\r\n"; - DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", handshake.c_str()); - client->tcp->write(handshake.c_str(), handshake.length()); + + DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t*)handshake.c_str()); + client->tcp->write((uint8_t*)handshake.c_str(), handshake.length()); #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine))); diff --git a/src/WebSocketsClient.h b/src/WebSocketsClient.h index bae1777..bdbbb3b 100644 --- a/src/WebSocketsClient.h +++ b/src/WebSocketsClient.h @@ -25,7 +25,6 @@ #ifndef WEBSOCKETSCLIENT_H_ #define WEBSOCKETSCLIENT_H_ -#include #include "WebSockets.h" class WebSocketsClient: private WebSockets { diff --git a/src/WebSocketsServer.cpp b/src/WebSocketsServer.cpp index c2262f6..9e6037e 100644 --- a/src/WebSocketsServer.cpp +++ b/src/WebSocketsServer.cpp @@ -790,20 +790,20 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) { "Connection: Upgrade\r\n" "Sec-WebSocket-Version: 13\r\n" "Sec-WebSocket-Accept: "); - client->tcp->write(sKey.c_str(), sKey.length()); + client->tcp->write((uint8_t*)sKey.c_str(), sKey.length()); if(_origin.length() > 0) { String origin = "\r\nAccess-Control-Allow-Origin: "; origin += _origin; origin += "\r\n"; - client->tcp->write(origin.c_str(), origin.length()); + client->tcp->write((uint8_t*)origin.c_str(), origin.length()); } if(client->cProtocol.length() > 0) { String protocol = "\r\nSec-WebSocket-Protocol: "; protocol += _protocol; protocol += "\r\n"; - client->tcp->write(protocol.c_str(), protocol.length()); + client->tcp->write((uint8_t*)protocol.c_str(), protocol.length()); } else { client->tcp->write("\r\n"); } diff --git a/src/WebSocketsServer.h b/src/WebSocketsServer.h index b50effa..33037ed 100644 --- a/src/WebSocketsServer.h +++ b/src/WebSocketsServer.h @@ -25,7 +25,6 @@ #ifndef WEBSOCKETSSERVER_H_ #define WEBSOCKETSSERVER_H_ -#include #include "WebSockets.h" #define WEBSOCKETS_SERVER_CLIENT_MAX (5)