As the Espruino Pico KickStarter has now crossed the stretch goal for the implementation of sockets, I thought I'd better have a go at implementing them.
I had a dig around yesterday and found an old branch, and an hour ago I actually managed to get a socket API working... And now, I have a very simple piece of code that publishes the temperature via MQTT:
function mtStr(s) {
return String.fromCharCode(s.length>>8,s.length&255)+s;
}
function mtPacket(cmd, variable, payload) {
return String.fromCharCode(cmd, variable.length+payload.length)+variable+payload;
}
function mtpConnect(name) {
return mtPacket(0b00010000,
mtStr("MQTT")/*protocol name*/+
"\x04"/*protocol level*/+
"\x00"/*connect flag*/+
"\xFF\xFF"/*Keepalive*/, mtStr(name));
}
function mtpPub(topic, data) {
return mtPacket(0b00110001, mtStr(topic), data);
}
var client;
function onConnected() {
console.log('creating client');
client = require("net").connect({host : "192.168.1.50", port: 1883}, function() { //'connect' listener
console.log('client connected');
client.write(mtpConnect("Espruino"));
var intr = setInterval(function() {
console.log("Publishing");
client.write(mtpPub("a/b", E.getTemperature().toFixed(4)));
}, 2000);
client.on('data', function(data) {
console.log("[MQTT]"+data.split("").map(function(c) { return c.charCodeAt(0); }));
});
client.on('end', function() {
console.log('client disconnected');
clearInterval(intr);
});
});
}
// For CC3000 WiFi
var wlan = require("CC3000").connect();
wlan.connect( "BTHub4-5ZN2", "2f3b5659ad", function (s) {
if (s=="dhcp") {
console.log("My IP is "+wlan.getIP().ip);
onConnected();
}
});
// Or the following for ethernet
//SPI2.setup({mosi:B15,miso:B14,sck:B13});
//var eth = require("WIZnet").connect(SPI2,B10);
//onConnected();
This is obviously pretty basic, but hopefully I'll get some better examples built up over time - maybe using the MQTT library on NPM (but the 11 source files there do make me wonder if it'll ever fit in a microcontroller!).
This should work with the latest build when it gets automatically uploaded in an hour or so, on Espruino boards with both CC3000 WiFi and WIZnet wired Ethernet.
And just to show that it does actually work on the KickStarter board - although this one is a very early prototype:
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.
As the Espruino Pico KickStarter has now crossed the stretch goal for the implementation of sockets, I thought I'd better have a go at implementing them.
I had a dig around yesterday and found an old branch, and an hour ago I actually managed to get a socket API working... And now, I have a very simple piece of code that publishes the temperature via MQTT:
This is obviously pretty basic, but hopefully I'll get some better examples built up over time - maybe using the MQTT library on NPM (but the 11 source files there do make me wonder if it'll ever fit in a microcontroller!).
This should work with the latest build when it gets automatically uploaded in an hour or so, on Espruino boards with both CC3000 WiFi and WIZnet wired Ethernet.
And just to show that it does actually work on the KickStarter board - although this one is a very early prototype:
1 Attachment