I there any way to send other data types? I want to send with my Espruino Pico float (it's bytes), but it's always sending as text.
I know how to handle the text, it's just that would take less bytes to send if it were always 8 bytes from it's float data type.
Yes, socket.write expects a string (or does a simple toString on whatever data type you give it). The simplest way around it is to use E.toString(...), for instance:
If you did socket.write(E.toString([0,1,2,3,4])) you'd send just the 5 bytes 0..4.
You could use String.fromCharCode(...) which is standard JS, but E.toString is a bit easier and more efficient when dealing with arrays.
Sending floats is a bit more tricky, but you have 2 main options:
var a = new Float32Array(1); // one element array
This got added as of 1v92, and it lets you store and read all kinds of data really conveniently:
var d = new DataView(new ArrayBuffer(8)); // 8 byte array buffer
d.setFloat32(0/*byte offset*/, 765.3532564);
This would be good information to add to the net module documentation!
Thanks! Yes, I'll add a note to do that.
Don't worry about formatting, just type in the text and we'll take care of making sense of it. We will auto-convert links, and if you put asterisks around words we will make them bold.
For a full reference visit the Markdown syntax.
© Espruino, powered by microcosm.
Report a problem