Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Created March 24, 2026 15:35
Show Gist options
  • Select an option

  • Save WietseWind/7eedaad5543d8ae909124d83099cf6da to your computer and use it in GitHub Desktop.

Select an option

Save WietseWind/7eedaad5543d8ae909124d83099cf6da to your computer and use it in GitHub Desktop.
espruino.js
var on = false;
const blinkInterval = setInterval(function() {
on = !on;
LED1.write(on);
}, 500);
var WIFI_NAME = "Test";
var WIFI_OPTIONS = { password : "xxxx" };
var wifi = require("Wifi");
wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
if (err) {
console.log("Connection error: "+err);
return;
}
console.log("Connected!");
});
wifi.on('associated',function() { console.log("We're connected to an AP"); });
wifi.on('disconnected',function() { console.log("We disconnected"); });
var aps = [{
"ssid": "2G",
"authMode": "wpa2_psk",
"rssi": -79,
"mac": "xx:c4",
"channel": 1 }];
//wifi.scan(function (err, data) {
// console.log('wifi scandata', err, data) ;
// aps = data;
//});
var WebServer = require("WebServer");
function index_njs(req, res, uri, webs) {
return {
type: 'text/html',
content: '<html>' +
'<p>Hello from in memory server on a chip!</p>' +
'<p><b>Memory Usage: </b><br>' + JSON.stringify(process.memory()) + '</p>' +
//'<p><b>Flash Usage: </b><br>' + JSON.stringify(require('Flash').getFree()) + '</p>' +
'<p><b>Found Access points: </b><br>' + JSON.stringify(aps) + '</p>' +
'</html>'
};
}
var webs = new WebServer({
port: 80,
default_type: 'text/plain',
default_index: 'index.njs',
//file_system: '/some/path',
memory: {
//'info.html': {
// 'content': '<html>Hello World!</html>',
// 'type': 'text/html'
//},
'index.njs': {
'content': index_njs
},
'info.txt': {
'content': 'Hello World!'
},
}
});
webs.on('start', function (WebServer) {
console.log('WebServer listening on port ' + WebServer.port);
LED2.write(true);
});
webs.on('request', function (request, response, parsedUrl, WebServer) {
console.log('WebServer requested', parsedUrl);
LED1.write(true);
setTimeout(() => {
LED1.write(false);
}, 100)
});
webs.on('error', function (error, WebServer) {
console.log('WebServer error', error);
});
wifi.on('connected',function() {
console.log("We have an IP Address");
clearInterval(blinkInterval)
wifi.getIP(function (err, data){
console.log(err, data)
webs.createServer();
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment