Ok so I created a basic webpage from which you can send commands to the device over a WS connection. The node js server acts as the host and the device + browser app act as clients. When a command is sent, it is broadcasted to all devices(Will change later). The command is then checked using a switch statement and an associated function is called on the device.
I will add functionality to send msgs to specific devices later on and post back to help the community.
My original idea was to be able to control device over websockets from my cloud server and have total remote control over it by building a front end API. I'm not sure if websockets is the right protocol for this though, I might need to switch to MQTT or a different communication protocol. As of right now, I'm only using 1 device, but I when I scale to maybe 1000 devices this setup might get messy.
What do you recommend Gordon?
Server Code (NODE.JS)
var WebSocketServer = require('ws').Server;
var express = require('express');
var path = require('path');
var app = express();
var server = require('http').createServer();
//express setup
app.use(express.static(path.join(__dirname, '/public')));
var wss = new WebSocketServer({server: server});
// Broadcast to all.
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
//On Initial Connection
wss.on('connection', function (ws) {
ws.send(JSON.stringify("Connection:Established"));
console.log('started client interval');
//Broadcast incoming Command
ws.on('message', function incoming(message) {
console.log('received: %s', message);
broadcast(message);
});
//On Close
ws.on('close', function () {
console.log('stopping client interval');
});
});
//Server settings
server.on('request', app);
server.listen(8080, function () {
console.log('Listening on http://localhost:8080');
});
//Broadcast Function
function broadcast(message) {
wss.clients.forEach(function each(client) {
client.send(message);
});
}
WebApp Code
index.html
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Tahoma, Geneva, sans-serif;
}
div {
display: inline;
}
'#message-form {
}'
//remove '' from above, I put them in to allow the post to be visible
</style>
</head>
<body>
<form id="message-form">
<input name="command" type="text" placeholder="Command"/>
<button>Send</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="frontend.js"></script>
</body>
</html>
front-end ws setup
var host = window.document.location.host.replace(/:.*/, '');
var ws = new WebSocket('ws://' + host + ':8080');
ws.onopen = function() {
console.log("Connected to server");
};
ws.onmessage = function(msg) {
console.log(msg.data);
};
jQuery('#message-form').on('submit', function (e){
e.preventDefault();
ws.send(jQuery('[name = command]').val());
});
ESPRUINO Code
var host = "192.168.2.106";
var WebSocket = require("ws");
var ws = new WebSocket(host, {
path: '/',
port: 8080, // default is 80
protocol: "echo-protocol", // websocket protocol name (default is none)
protocolVersion: 13, // websocket protocol version, default is 13
origin: 'Espruino',
keepAlive: 60,
headers: {
some: 'header',
'ultimate-question': 42
} // websocket headers to be used e.g. for auth (default is none)
});
ws.on('open', function() {
console.log("Connected to server");
});
ws.on('message', function(msg) {
console.log(msg);
switch (msg) {
case "ledOn":
{
digitalWrite(D5, 1);
break;
}
case "ledOff":
{
digitalWrite(D5, 0);
break;
}
default:
{
}
}
});
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Ok so I created a basic webpage from which you can send commands to the device over a WS connection. The node js server acts as the host and the device + browser app act as clients. When a command is sent, it is broadcasted to all devices(Will change later). The command is then checked using a switch statement and an associated function is called on the device.
I will add functionality to send msgs to specific devices later on and post back to help the community.
My original idea was to be able to control device over websockets from my cloud server and have total remote control over it by building a front end API. I'm not sure if websockets is the right protocol for this though, I might need to switch to MQTT or a different communication protocol. As of right now, I'm only using 1 device, but I when I scale to maybe 1000 devices this setup might get messy.
What do you recommend Gordon?
Server Code (NODE.JS)
WebApp Code
index.html
front-end ws setup
ESPRUINO Code