Skip to content

Instantly share code, notes, and snippets.

@wybiral
Last active February 5, 2022 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wybiral/d26765153e46aa2d740ede8e1cc2eae7 to your computer and use it in GitHub Desktop.
Save wybiral/d26765153e46aa2d740ede8e1cc2eae7 to your computer and use it in GitHub Desktop.
Example of using WebSockets on an Espruino WiFi device for bidirectional communication.
/*
Example of using WebSockets on an Espruino WiFi device for bidirectional communication.
Fill in the WiFi SSID and password to connect to your local network and then the Espruino
console will print out the IP address of your device which you can connect to with a web
browser.
Video here: https://www.youtube.com/watch?v=xcecEODjxSE
*/
var wifi = require('Wifi');
var clients = [];
var WIFI_NAME = "[YOUR WIFI SSID HERE]";
var WIFI_OPTIONS = {
password: "[YOUR WIFI PASSWORD HERE]"
};
print("connecting...");
// Connect to WiFi
wifi.connect(WIFI_NAME, WIFI_OPTIONS, err => {
if (err !== null) {
throw err;
}
// Print IP address
wifi.getIP((err, info) => {
if (err !== null) {
throw err;
}
print(info.ip);
startServer();
});
});
// Create and start server
function startServer() {
const s = require('ws').createServer(pageHandler);
s.on('websocket', wsHandler);
s.listen(80);
}
// Page request handler
function pageHandler(req, res) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(`<html>
<head>
<script>
window.onload = () => {
var ws = new WebSocket('ws://' + location.host, 'protocolOne');
var btn = document.getElementById('btn');
var led = document.getElementById('led');
ws.onmessage = evt => {
btn.innerText = evt.data;
};
led.onchange = evt => {
ws.send(led.value);
};
};
</script>
</head>
<body>
<p>Button: <span id="btn">up</span></p>
<p>
LED on:
<select id="led">
<option>off</option><option>on</option>
</select>
</p>
</body>
</html>`);
}
// WebSocket request handler
function wsHandler(ws) {
clients.push(ws);
ws.on('message', msg => {
digitalWrite(LED1, msg == 'on');
});
ws.on('close', evt => {
var x = clients.indexOf(ws);
if (x > -1) {
clients.splice(x, 1);
}
});
}
// Send msg to all current websocket connections
function broadcast(msg) {
clients.forEach(cl => cl.send(msg));
}
// Watch for button events (rising and falling)
setWatch(evt => {
broadcast(evt.state ? 'down' : 'up');
}, BTN, {repeat: true, edge: 'both'});
@matthew-lawrence
Copy link

Davy, thank you for providing this great example and your Youtube video. I have been finding it very difficult to find any good content on Espruino, which is sad as it is such an exciting development platform. I'm wondering if you can provide some clarity for what I am seeing when I run the code on the Espruino IDE.

I receive warnings that the Modules wifi, net, and http are not loaded, but the code successfully runs an I am able to connect to the device webpage. Do you know why this would be? I have tested this on a esp8266 wemos d1 mini. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment