Local Websocket Server

Simple “local” (cloud disabled) websocket server implementation ported from https://github.com/brandenhall/Arduino-Websocket.git.
The server by default handles up-to 4 simultaneous clients.
I had to change the TCPServer (spark_wiring_tcpserver.cpp). For some reason the TCPServer had a references to a TCPClient, beside not making sense to me it was preventing me of having more than one client attached, hence the changes.
NOTE, I have only tested this with local builds, so try it with sparkulator on your own risk :).

I did not have time to create new make files, so for now, you have to copy files (backup original the files first).
Copy files from src|inc|build to core-firmware/src|inc|build. My plan is to learn how to create a proper sparkcore lib from this…

The applicaiton.cpp is a simplified version of the tinker firmware that I used for my testing and currently can handle: digitalread, digitalwrite, analogread and analogwrite. When I get a chance I will add something like Spark.publish…
applicaiton.cpp requires Serial connection, the core will halt the execution in the setup (the RGB led will flash blue, green and pink) until the serial communication is established.

example syntax to how use the tinker firmware


If you develop on Linux and if you have nodejs installed read on.
After flashing the core for the first time, you can use “doit” in the build folder. “doit” will put the core in the dfu mode, compile, and flash the core again.

tinker.js in the example folder can be used for demo and or testing, currently it has a loop that will turn on/off the first four digital outputs on the core.
Note you have to change the <IP>:< PORT> in the tinker.js before using it.

get it from git:

short video:
Just noticed that the video does not have any audio :(, here is a short description on what you see in the video:
On the right, the browser is connected to the core’s WebSocket server. The serial output from the core is on the left. After connection is established to the core, I change the state of the digital pins by clicking on the pins, When I click on the analog pins the core replies with analogue values for the pin in question.


Nice! Thanks for sharing!


I believe your websocket code is “Exactly” what I need but, I am out of town for next few days. I Will compile locally next week but, I do have a question for you; if you would:

? Do I call your tinker.js file from a HTML page then use my Core’s IP address + Your Contriol Syntax

For example:

Is that how to do it ? If so, I would only have to put the above link in an < a Href="…" > and use my jquery interface here:


Just can’t wait to get back and try it as this will finish the software part of my project allowing me to move back to my spark core shield of privacy.

Thanks a bunch - Really Appreciate this.


Hi @spydrop,
I just uploaded the html (still work in progress) page, you use it as an example. you would need to update the ws:// with the local address of your sparkcore.
I also uploaded a demo: http://youtu.be/B886_m16s6s

1 Like

Your websocket creates a persistant connection witht your core to send commands / receive data; got it ! All without the Cloud - this I like very much.

I only need DigitalWrite for my project and I know how to scale down the code for tinker.

Do you think it is possible to use both a websocket server and a webserver to control the spark core.

Where all the HTML can reside on the Core ? and the *.js files can be included by the Core ? Is this possible ?

Thanks for sharing all that you have done as I would never have reached this point; a websocket server.

Hi, thank you for sharing the code!
I would like to have the spark run a websocket server that I would like to connect directly from a web browser. Unfortunately I couldn’t get your websocket -code working on my spark device. I tried to make the program with standard www.spark.io/build/ -tool. Included all files from your https://github.com/Na2l/spark-websocket-server example (Base64, spark_wiring_tcpclient, spark_wiring_tcpserver, SparkWebSocketServer - both .h and .cpp files from each). tried to build the software and gor the error:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from SparkWebSocketServer.h:47,
from SparkWebSocketServer.cpp:43:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
SparkWebSocketServer.cpp: In member function 'bool SparkWebSocketServer::handshake(TCPClient&)':
SparkWebSocketServer.cpp:74:39: error: 'class TCPClient' has no member named 'equals'
if(clients[pos] != NULL && client.equals(*clients[pos])) { 
SparkWebSocketServer.cpp: In member function 'void SparkWebSocketServer::disconnectClient(TCPClient&)':
SparkWebSocketServer.cpp:160:35: error: 'class TCPClient' has no member named 'equals'
if(clients[i]!=NULL && client.equals(*clients[i])) { 
SparkWebSocketServer.cpp: In member function 'void SparkWebSocketServer::doIt()':
SparkWebSocketServer.cpp:333:39: error: cannot convert 'TCPClient' to 'TCPClient*' in initialization
TCPClient *client=server->available(); 
make: *** [SparkWebSocketServer.o] Error 1

Can you already from this what did I do wrong?
I know that this sounds like a stupid newbie question but could it be possible that you would make a super simple example that would be possible to build in the www.spark.io/build/ -tool - like the bare minimum that needs to be there to get the server up and running and a very simple sample application that accepts connections and writes some data to the clients. Thanks already in advance!


yes. you can use the TCPServer (port 80) and when a client connects you can write the html to the client.

yes that can be done too, e.g. you can use something like SD SPI Filesystem library and SD Card as NVRAM library - Libraries - Particle.

Hi @anttir
I have tested this only with local builds. Also, note that the source code I used to compile this was based on spark-code source code that I downloaded around Apr 10th (pretty outdated)
To compile and use my code for local builds, you have to copy the files in inc, src and build from https://github.com/Na2l/spark-websocket-server.git to the spark-core’s directory.

Hope this helps.

Thanks @NaAl for a quick reply. I’ll try to simplify your example so that I would get it working and maybe I’ll also start building the software locally.

Trying to get the websocket server to work … but no real luck (local build, windows)

Building file: …/src/application.cpp
Invoking: ARM GCC CPP Compiler
mkdir -p obj/src/
arm-none-eabi-gcc -g3 -gdwarf-2 -Os -mcpu=cortex-m3 -mthumb -I…/inc -I…/…/co
re-common-lib/CMSIS/Include -I…/…/core-common-lib/CMSIS/Device/ST/STM32F10x/In
clude -I…/…/core-common-lib/STM32F10x_StdPeriph_Driver/inc -I…/…/core-common
-lib/STM32_USB-FS-Device_Driver/inc -I…/…/core-common-lib/CC3000_Host_Driver -
I…/…/core-common-lib/SPARK_Firmware_Driver/inc -I…/…/core-communication-lib/
lib/tropicssl/include -I…/…/core-communication-lib/src -I…/…/web-socket/src
-I. -ffunction-sections -Wall -fmessage-length=0 -MD -MP -MF obj/src/application
no-exceptions -fno-rtti -c -o obj/src/application.o …/src/application.cpp
In file included from …/inc/main.h:37:0,
from …/inc/spark_utilities.h:30,
from …/inc/spark_wiring.h:33,
from …/inc/application.h:29,
from …/src/application.cpp:27:
…/…/core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:37:20: fatal error:
rgbled.h: No such file or directory
#include “rgbled.h”
compilation terminated.

I have been sick (the flu, bronchitis and some other infections). I will take a look at this when I feel better.

Sorry to hear, get well soon

I’ve adapted the make files to secure correct include dirs (the problem above)
I’ve adapted client and server with WiFi.ready

Now it works, not stable but works…now the real work starts, getting it stable and in manual mode

Hey @NaAl,

Hope you are feeling better. Was wondering - have you done any tests to see the max amount of simultaneous clients?

I’m trying to get an idea of what the Spark core is able to handle. I’d imagine the Photon will perform better too.

Hi @zachwilliams
Nope, I just made it work, and left the hard work for someone else :smile:
by the way, the number of clients are hard coded to 4 so if you want to run some tests, make sure you remove the hard coded value.

Haha all good - thanks @NaAl! I’ll pull it down and run some tests once the Photon is in. Will try to remember to post back in here for anyone wondering about max clients.

anyone know if this works with the photon yet?

Haven’t tried it yet. I probably will test it this coming week.

Did anyone have success with web sockets and photon?