Skip to content

Make library compatible with Particle devices #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 46 additions & 0 deletions examples/ParticleWebSocketClient/application.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion src/WebSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions src/WebSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
#ifndef 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>
#endif

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

Expand All @@ -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)

Expand Down Expand Up @@ -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 <Ethernet.h>
#include <SPI.h>
#define WEBSOCKETS_NETWORK_CLASS EthernetClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS EthernetServer
#endif

#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60)

Expand Down
5 changes: 3 additions & 2 deletions src/WebSocketsClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
1 change: 0 additions & 1 deletion src/WebSocketsClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#ifndef WEBSOCKETSCLIENT_H_
#define WEBSOCKETSCLIENT_H_

#include <Arduino.h>
#include "WebSockets.h"

class WebSocketsClient: private WebSockets {
Expand Down
6 changes: 3 additions & 3 deletions src/WebSocketsServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
1 change: 0 additions & 1 deletion src/WebSocketsServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#ifndef WEBSOCKETSSERVER_H_
#define WEBSOCKETSSERVER_H_

#include <Arduino.h>
#include "WebSockets.h"

#define WEBSOCKETS_SERVER_CLIENT_MAX (5)
Expand Down