Thanks @bko, good idea to use the core as a client to access a internet based server. This is a horribly bad hack that seems to work. Not fully finished but interesting how easy it is to trick the websocket protocol to keep the socket open using basic TCP.
Here is the serial output
successfully connected
HTTP/1.1 101 Switching Protocols
upgrade: websocket
connection: Upgrade
sec-websocket-accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
▒A▒A▒A▒A▒A▒A▒A▒A▒A▒Asuccessfully connected
HTTP/1.1 101 Switching Protocols
upgrade: websocket
connection: Upgrade
sec-websocket-accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
▒B▒B▒B▒B▒B▒B▒B▒B▒B▒B▒B▒B▒B▒B▒B▒B▒B
Here is the .ino file to flash to the core
TCPClient client;
char server[] = "my-cloud9-server.c9.io";
bool myUsbSerialDebugOn = true; // set to false when not hooked up to USB
int connectToMyServer(String myNothing) {
digitalWrite(D7, HIGH);
if (client.connect(server, 80)) {
client.write("GET / HTTP/1.1\r\n");
client.write("Host: my-cloud9-server.c9.io\r\n");
client.write("Upgrade: websocket\r\n");
client.write("Connection: Upgrade\r\n");
client.write("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n");
client.write("Sec-WebSocket-Version: 13\r\n");
client.write("\r\n");
digitalWrite(D7, LOW);
if (myUsbSerialDebugOn){
Serial.println("successfully connected");
}
return 1; // successfully connected
} else {
digitalWrite(D7, LOW);
if (myUsbSerialDebugOn){
Serial.println("failed to connect");
}
return -1; // failed to connect
}
}
void setup() {
pinMode(D7, OUTPUT);
Spark.function("connect", connectToMyServer);
digitalWrite(D7, HIGH);
delay(25000);
digitalWrite(D7, LOW);
if (myUsbSerialDebugOn){
Serial.begin(9600); // Initalize the USB Serial port
while(!Serial.available()) SPARK_WLAN_Loop();
Serial.println("Hello Computer");
}
}
void loop() {
digitalWrite(D7, LOW);
if (client.connected()) {
if (client.available()) {
char myIncoming = client.read();
if (myIncoming == 'A'){ digitalWrite(D7, HIGH);}
if (myUsbSerialDebugOn){
Serial.print(myIncoming);
// delay(2);
}
} else {
//digitalWrite(D7, LOW);
}
}
}
And here is the node js file on a cloud 9 server https://c9.io
var WebSocketServer = require("ws").Server
var http = require("http")
var express = require("express")
var app = express()
var port = process.env.PORT || 5000
app.use(express.static(__dirname + "/"))
var server = http.createServer(app)
server.listen(port)
console.log("http server listening on %d", port)
var wss = new WebSocketServer({server: server})
console.log("websocket server created")
wss.on("connection", function(ws) {
var id = setInterval(function() {
console.log("send ping")
//ws.send(JSON.stringify(new Date()), function() { })
ws.send("A",function() { }) // "A" = blink D7, "B" = don't blink
}, 2000)
console.log("websocket connection open " )
ws.on("close", function() {
console.log("websocket connection close")
clearInterval(id)
})
})
I still don’t get how to program sockets using node js. Seems really confusing how to find simple DOCS for node modules “ws” “http” “express” and I need to work on a web page controller instead of my 2 second interval timer. However, with this setup D7 blinks every 2 seconds. When I switch the send command to send something other than “A” D7 does not blink. It appears that the connection is staying live and the Core seems reasonably stable.