Websockets porting

Hi all, i’m received my cores just three days ago.
First of all - i’ve tried to use Web IDE and went to local compiling, because web IDE is really horrible to use - not telling anything correctly about errors in compilation…

So my plan is to connect Spark Core to Spacebrew which running on node.js server.
Spacebrew is a great project to connect all of things together, i’m already using it in BeagleBone and Raspberry Pi projects. It’s working on websockets, and guys, who develop it already created Arduino library, which allow to connect Arduino with ethernet shield and Spacebrew server.

So i’ve took this library and tried to port it to Spark. And now i have a problem - when Core tries to connect with node.js it’s reboots. As i found in debugging proccess - it’s reboots in Base64Encode operation.

Is there any existing base64encode libraries, which will work on Spark Core?

We don’t have that library available yet. Do you think you can use something like this? https://github.com/adamvr/arduino-base64/blob/master/Base64.cpp

Hi @ekbduffy,

Make sure you hit “Verify” when writing and compiling to make sure you see any errors in your code.

Thanks,
David

I’m trying to use exactly this :slight_smile: And on base64encode i’ve got something like reboot of of Core, but it’s not starting my program again, just breathing light of connection to cloud…

Ok, let’s try to be constructive:)
I have a project in web ide “spacebrewclient”.
It have header file "spacebrew.h"
it’s starting with:

#ifndef SPACEBREW_H
#define SPACEBREW_H
enum SBType { SB_BOOLEAN, SB_STRING, SB_RANGE };



struct PublisherNode {
  char *name;
  char *type;
  char *defaultValue;
  PublisherNode *next;
};

then in the middle of file this enum SBType goes in functions etc.
But when i try to Verify this project - web IDE says to me, that SBtype is undefined on line 5,6 and 7 … but there is nothing in that lines… As i said before - web ide can be usable, if verifier will show code, that it’s verifies…

And guys, you are amazing, so fast answers!

This is what exactly says Verifier :
In file included from spacebrew.cpp:1:0:
spacebrew.h:5:35: error: use of enum ‘SBType’ without previous declaration
spacebrew.h:6:35: error: use of enum ‘SBType’ without previous declaration

but this lines are empty.

Oh, that might be my fault. Right now the web IDE runs the pre-processor against .h and .cpp files, something I’m going to disable early next week so that things more closely match what people are used to with Arduino. I’m guessing it’s adding function declarations for you above that enum statement. To disable that, try adding this to the .h and .cpp files:

#pragma SPARK_NO_PREPROCESSOR

Thanks!
David

Ok, thank you. Also - i think the one of ways to resolve this problem - show whole code in web ide, like i see in local version.

1 Like

Hi @ekbduffy,

I think this would be helpful, I’ll add this to our backlog and try to find an effective way to communicate these kinds of changes.

Thanks,
David

1 Like

I’m still in trying to create websockets library. Faced a problem.
When i’m sending websocket handshake like:
“GET / HTTP/1.1”,
“Upgrade: WebSocket”,
“Connection: Upgrade”,
“Host: 192.168.1.146”,
“Sec-WebSocket-Origin: SparkWebSocketClient”,
“Sec-WebSocket-Version: 13”,
“Sec-WebSocket-Key: 1VTFj/CydlBCZDucDqw8eA==”,
"Sec-WebSocket-Protocol: ",
“HTTP/1.1 101”

By line with tcpclient.prinln() - i’ve got “400 Bad request”. In network monitor i see that there is a lot of frames, which are holding this lines separately.
When i look into browsers request in network monitor i’m seeing 1 frame with whole lines inside. So what i do:

String all = WebSocketClientStringTable[0];
all.concat(" \r\n");
all.concat(WebSocketClientStringTable[1]);
all.concat(" \r\n");
all.concat(WebSocketClientStringTable[2]);
all.concat(" \r\n");
all.concat(WebSocketClientStringTable[3]);
all.concat(" \r\n");
all.concat(WebSocketClientStringTable[4]);
all.concat(" \r\n");
all.concat(WebSocketClientStringTable[5]);
all.concat(" \r\n");
all.concat(WebSocketClientStringTable[6]);
all.concat(" \r\n");
all.concat(WebSocketClientStringTable[7]);
all.concat(" \r\n");
all.concat(WebSocketClientStringTable[8]);
all.concat(" \r\n");
tcpclient.println(all);
tcpclient.println();

And i’ve see a lot of frames with:
“TCPPayloadData: Binary Large Object (1 Bytes)” as said Network monitor.

Is there my mistake in something? I’ve tried to Serial.println(all) and successfully see handshake message…

OK, resolved this problem with sending by println one by one correct handshake.
But faced with same problem later.
My client sending
{“config”:{“name”:“Spark_Core1”,“description”:“Test build”,“publish”:{“messages”:[{“name”:“Start”,“type”:“boolean”,“default”:“false”}]},“subscribe”:{“messages”:[{“name”:“Start”,“type”:“boolean”},{“name”:“Stop”,“type”:“boolean”}]}}}
with just tcpclient.println(message);
on network monitor i see this:
TCPPayloadData: Binary Large Object (19 Bytes)

And there is no answers on this from Node.js. So i think node just didn’t understood what saying core.
How can i send long chars and not be interpreted as binary?

in code, that i’m porting to Spark sending function looks this way:

  int len = strlen(message);
  _client.write(0x81);
  if(len > 125) {
    _client.write(0xFE);
    _client.write(byte(len >> 8));
    _client.write(byte(len & 0xFF));
  } else {
    _client.write(0x80 | byte(len));
  }
  for(int i = 0; i < 4; i++) {
    _client.write((byte)0x00); // use 0x00 for mask bytes which is effectively a NOOP
  }
  _client.print(message);

Hi @ekbduffy,

Hmm, we might be getting into the weeds enough that it would help if you wanted to post your code thus far so we can see it in context?

Thanks!
David

I’ve done success handshake etc.
If someone interested, correct websocket handshake:

const char *WebSocketClientStringTable = {
			"GET / HTTP/1.1\x0d\x0a"
			"Upgrade: websocket\x0d\x0a"
			"Connection: Upgrade\x0d\x0a"
			"Host: 192.168.1.146:9000\x0d\x0a"
			"Origin: SparkWebSocketClient\x0d\x0a"
			"Sec-WebSocket-Key:  1VTFj/CydlBCZDucDqw8eA==\x0d\x0a"
			"Sec-WebSocket-Version: 13\x0d\x0a"
			"\x0d\x0a"};

  client.print(WebSocketClientStringTable); 
4 Likes