# Local Websocket Server

**URL:** <https://community.particle.io/t/local-websocket-server/3856>\
**Category:** Project Share\
**Created:** [April 10, 2014, 6:55am UTC](https://community.particle.io/t/local-websocket-server/3856 "2014-04-10T06:55:36Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![NaAl](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/naal/32/852_2.png) [@NaAl](https://community.particle.io/u/NaAl)\
**Post date:** [April 10, 2014, 6:55am UTC](https://community.particle.io/t/local-websocket-server/3856/1 "2014-04-10T06:55:36Z")

</div>

Simple “local” (cloud disabled) websocket server implementation ported from [https://github.com/brandenhall/Arduino-Websocket.git](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

/digitalwrite?D3=HIGH  
/digitalread?D3

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:

> **[Na2l/spark-websocket-server](https://github.com/Na2l/spark-websocket-server)**
>
> Contribute to spark-websocket-server development by creating an account on GitHub.

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.

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [April 10, 2014, 7:22pm UTC](https://community.particle.io/t/local-websocket-server/3856/2 "2014-04-10T19:22:08Z")

</div>

Nice! Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![spydrop](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@spydrop](https://community.particle.io/u/spydrop)\
**Post date:** [April 17, 2014, 11:00pm UTC](https://community.particle.io/t/local-websocket-server/3856/3 "2014-04-17T23:00:22Z")

</div>

@NaAl,

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: [http://192.168.1.101/digitalwrite?D3=HIGH](http://192.168.1.101/digitalwrite?D3=HIGH)

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:

[http://www.spydrop.com/mobile/](http://www.spydrop.com/mobile/)

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.

Bobby

---

<div class="post-metadata">

**Author:** ![NaAl](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/naal/32/852_2.png) [@NaAl](https://community.particle.io/u/NaAl)\
**Post date:** [April 18, 2014, 3:06am UTC](https://community.particle.io/t/local-websocket-server/3856/4 "2014-04-18T03:06:14Z")

</div>

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://192.168.2.8:2525 with the local address of your sparkcore.  
I also uploaded a demo: [http://youtu.be/B886\_m16s6s](http://youtu.be/B886_m16s6s)

---

<div class="post-metadata">

**Author:** ![spydrop](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@spydrop](https://community.particle.io/u/spydrop)\
**Post date:** [April 18, 2014, 3:43am UTC](https://community.particle.io/t/local-websocket-server/3856/5 "2014-04-18T03:43:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anttir](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/anttir/32/3231_2.png) [@anttir](https://community.particle.io/u/anttir)\
**Post date:** [September 28, 2014, 9:01am UTC](https://community.particle.io/t/local-websocket-server/3856/6 "2014-09-28T09:01:02Z")

</div>

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/](http://www.spark.io/build/) -tool. Included all files from your [https://github.com/Na2l/spark-websocket-server](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/](http://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!

---

<div class="post-metadata">

**Author:** ![NaAl](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/naal/32/852_2.png) [@NaAl](https://community.particle.io/u/NaAl)\
**Post date:** [September 28, 2014, 4:25pm UTC](https://community.particle.io/t/local-websocket-server/3856/7 "2014-09-28T16:25:06Z")

</div>

@spydrop

> [@spydrop](#):
>
> Do you think it is possible to use both a websocket server and a webserver to control the spark core.

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

> [@spydrop](#):
>
> Where all the HTML can reside on the Core ? and the \*.js files can be included by the Core ? Is this possible ?

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](https://community.spark.io/t/sd-spi-filesystem-library-and-sd-card-as-nvram-library/3927).

---

<div class="post-metadata">

**Author:** ![NaAl](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/naal/32/852_2.png) [@NaAl](https://community.particle.io/u/NaAl)\
**Post date:** [September 28, 2014, 4:48pm UTC](https://community.particle.io/t/local-websocket-server/3856/8 "2014-09-28T16:48:26Z")

</div>

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](https://github.com/Na2l/spark-websocket-server.git) to the spark-core’s directory.

Hope this helps.  
/N

---

<div class="post-metadata">

**Author:** ![anttir](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/anttir/32/3231_2.png) [@anttir](https://community.particle.io/u/anttir)\
**Post date:** [September 28, 2014, 7:06pm UTC](https://community.particle.io/t/local-websocket-server/3856/9 "2014-09-28T19:06:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rpauto](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rpauto](https://community.particle.io/u/rpauto)\
**Post date:** [January 15, 2015, 9:29pm UTC](https://community.particle.io/t/local-websocket-server/3856/10 "2015-01-15T21:29:41Z")

</div>

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  
.o.d -DUSE\_STDPERIPH\_DRIVER -DSTM32F10X\_MD -DDFU\_BUILD\_ENABLE -DRELEASE\_BUILD -f  
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.

---

<div class="post-metadata">

**Author:** ![NaAl](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/naal/32/852_2.png) [@NaAl](https://community.particle.io/u/NaAl)\
**Post date:** [January 21, 2015, 8:32pm UTC](https://community.particle.io/t/local-websocket-server/3856/11 "2015-01-21T20:32:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rpauto](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rpauto](https://community.particle.io/u/rpauto)\
**Post date:** [January 21, 2015, 9:32pm UTC](https://community.particle.io/t/local-websocket-server/3856/12 "2015-01-21T21:32:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zachwilliams](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zachwilliams/32/4466_2.png) [@zachwilliams](https://community.particle.io/u/zachwilliams)\
**Post date:** [February 5, 2015, 6:16am UTC](https://community.particle.io/t/local-websocket-server/3856/13 "2015-02-05T06:16:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![NaAl](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/naal/32/852_2.png) [@NaAl](https://community.particle.io/u/NaAl)\
**Post date:** [February 20, 2015, 6:23pm UTC](https://community.particle.io/t/local-websocket-server/3856/14 "2015-02-20T18:23:22Z")

</div>

Hi @zachwilliams  
Nope, I just made it work, and left the hard work for someone else 😄  
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.  
/N

---

<div class="post-metadata">

**Author:** ![zachwilliams](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zachwilliams/32/4466_2.png) [@zachwilliams](https://community.particle.io/u/zachwilliams)\
**Post date:** [February 21, 2015, 12:04am UTC](https://community.particle.io/t/local-websocket-server/3856/15 "2015-02-21T00:04:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tripzero](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/tripzero/32/6302_2.png) [@tripzero](https://community.particle.io/u/tripzero)\
**Post date:** [July 31, 2015, 4:03am UTC](https://community.particle.io/t/local-websocket-server/3856/16 "2015-07-31T04:03:48Z")

</div>

anyone know if this works with the photon yet?

---

<div class="post-metadata">

**Author:** ![zachwilliams](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zachwilliams/32/4466_2.png) [@zachwilliams](https://community.particle.io/u/zachwilliams)\
**Post date:** [July 31, 2015, 4:15am UTC](https://community.particle.io/t/local-websocket-server/3856/17 "2015-07-31T04:15:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ssured](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@ssured](https://community.particle.io/u/ssured)\
**Post date:** [December 22, 2015, 9:19pm UTC](https://community.particle.io/t/local-websocket-server/3856/18 "2015-12-22T21:19:18Z")

</div>

Did anyone have success with web sockets and photon?
