mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-15 00:06:30 +02:00
Make library compatible with Particle devices
This commit is contained in:
@ -52,6 +52,10 @@ The mode can be aktivated in the ```WebSockets.h``` (see WEBSOCKETS_NETWORK_TYPE
|
|||||||
|
|
||||||
[ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required.
|
[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 ###
|
### Issues ###
|
||||||
Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues
|
Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues
|
||||||
|
|
||||||
|
46
examples/ParticleWebSocketClient/application.cpp
Normal file
46
examples/ParticleWebSocketClient/application.cpp
Normal file
@ -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();
|
||||||
|
}
|
@ -123,7 +123,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
|
|||||||
#ifdef WEBSOCKETS_USE_BIG_MEM
|
#ifdef WEBSOCKETS_USE_BIG_MEM
|
||||||
// only for ESP since AVR has less HEAP
|
// only for ESP since AVR has less HEAP
|
||||||
// try to send data in one TCP package (only if some free Heap is there)
|
// 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);
|
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] pack to one TCP package...\n", client->num);
|
||||||
uint8_t * dataPtr = (uint8_t *) malloc(length + WEBSOCKETS_MAX_HEADER_SIZE);
|
uint8_t * dataPtr = (uint8_t *) malloc(length + WEBSOCKETS_MAX_HEADER_SIZE);
|
||||||
if(dataPtr) {
|
if(dataPtr) {
|
||||||
|
@ -25,7 +25,12 @@
|
|||||||
#ifndef WEBSOCKETS_H_
|
#ifndef WEBSOCKETS_H_
|
||||||
#define WEBSOCKETS_H_
|
#define WEBSOCKETS_H_
|
||||||
|
|
||||||
|
#ifdef STM32_DEVICE
|
||||||
|
#include <application.h>
|
||||||
|
#define bit(b) (1UL << (b)) // Taken directly from Arduino.h
|
||||||
|
#else
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
|
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
|
||||||
|
|
||||||
@ -37,10 +42,17 @@
|
|||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
|
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
|
||||||
#define WEBSOCKETS_USE_BIG_MEM
|
#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
|
#else
|
||||||
//atmega328p has only 2KB ram!
|
//atmega328p has only 2KB ram!
|
||||||
#define WEBSOCKETS_MAX_DATA_SIZE (1024)
|
#define WEBSOCKETS_MAX_DATA_SIZE (1024)
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
#define WEBSOCKETS_TCP_TIMEOUT (2000)
|
#define WEBSOCKETS_TCP_TIMEOUT (2000)
|
||||||
|
|
||||||
@ -98,10 +110,15 @@
|
|||||||
|
|
||||||
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100)
|
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100)
|
||||||
|
|
||||||
|
#ifdef STM32_DEVICE
|
||||||
|
#define WEBSOCKETS_NETWORK_CLASS TCPClient
|
||||||
|
#define WEBSOCKETS_NETWORK_SERVER_CLASS TCPServer
|
||||||
|
#else
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#define WEBSOCKETS_NETWORK_CLASS EthernetClient
|
#define WEBSOCKETS_NETWORK_CLASS EthernetClient
|
||||||
#define WEBSOCKETS_NETWORK_SERVER_CLASS EthernetServer
|
#define WEBSOCKETS_NETWORK_SERVER_CLASS EthernetServer
|
||||||
|
#endif
|
||||||
|
|
||||||
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60)
|
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60)
|
||||||
|
|
||||||
|
@ -443,7 +443,7 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
|
|||||||
|
|
||||||
handshake += "\r\n";
|
handshake += "\r\n";
|
||||||
|
|
||||||
client->tcp->write(handshake.c_str(), handshake.length());
|
client->tcp->write((uint8_t*)handshake.c_str(), handshake.length());
|
||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
||||||
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
|
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
#ifndef WEBSOCKETSCLIENT_H_
|
#ifndef WEBSOCKETSCLIENT_H_
|
||||||
#define WEBSOCKETSCLIENT_H_
|
#define WEBSOCKETSCLIENT_H_
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include "WebSockets.h"
|
#include "WebSockets.h"
|
||||||
|
|
||||||
class WebSocketsClient: private WebSockets {
|
class WebSocketsClient: private WebSockets {
|
||||||
|
@ -739,20 +739,20 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
"Connection: Upgrade\r\n"
|
"Connection: Upgrade\r\n"
|
||||||
"Sec-WebSocket-Version: 13\r\n"
|
"Sec-WebSocket-Version: 13\r\n"
|
||||||
"Sec-WebSocket-Accept: ");
|
"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) {
|
if(_origin.length() > 0) {
|
||||||
String origin = "\r\nAccess-Control-Allow-Origin: ";
|
String origin = "\r\nAccess-Control-Allow-Origin: ";
|
||||||
origin += _origin;
|
origin += _origin;
|
||||||
origin += "\r\n";
|
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) {
|
if(client->cProtocol.length() > 0) {
|
||||||
String protocol = "\r\nSec-WebSocket-Protocol: ";
|
String protocol = "\r\nSec-WebSocket-Protocol: ";
|
||||||
protocol += _protocol;
|
protocol += _protocol;
|
||||||
protocol += "\r\n";
|
protocol += "\r\n";
|
||||||
client->tcp->write(protocol.c_str(), protocol.length());
|
client->tcp->write((uint8_t*)protocol.c_str(), protocol.length());
|
||||||
} else {
|
} else {
|
||||||
client->tcp->write("\r\n");
|
client->tcp->write("\r\n");
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
#ifndef WEBSOCKETSSERVER_H_
|
#ifndef WEBSOCKETSSERVER_H_
|
||||||
#define WEBSOCKETSSERVER_H_
|
#define WEBSOCKETSSERVER_H_
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include "WebSockets.h"
|
#include "WebSockets.h"
|
||||||
|
|
||||||
#define WEBSOCKETS_SERVER_CLIENT_MAX (5)
|
#define WEBSOCKETS_SERVER_CLIENT_MAX (5)
|
||||||
|
Reference in New Issue
Block a user