Howto write modules to storage?

Posted on
  • Sometimes uploading is taking too long because you use larges modules like MQTT. Is there an easy way to save the loaded module to storage. So next time you uses mqtt = require("MQTTS").

    s = require('Storage');
    mqtt = require("MQTT");
    /* output
    // save cached module 
    s.write("MQTTS", /* howto ?*/);
  • You can upload them using the normal Storage button in the middle of the IDE screen and uploading the file from eg­n.js to a file called MQTT

    I don't think the IDE is smart enough to know that the modules are actually installed on the device and to not re-upload them, but nothing stops you from renaming it MyMQTT on the device, and then doing require("MyMQTT");

  • Many thanks for your quick response. Yes the WebIDE will always reload the module. Small modules I manually store and add S to the stored name like in this case it will be MQTT-S. Missed that feature of the WebIDE to upload modules to strorage, will definitl give it a try.

    Copy paste from browser to the WebIDE does not look nice.

    1 Attachment

    • Bildschirmfoto 2024-04-19 um 22.17.32.jpg
  • use curl -O­n.jsto download that module and use this code to upload to storage.

    s = require("Storage");
    <content of file MQTT.min.js>
    s.write("MQTT-S",module.replace(/[\n\r]/­g, ''));

    Now you can use it like this:

    s = require("Storage");
    mqtt = require("MQTT-S");

    As this does not happen to often, I am fine with those manual steps ;-)

    1 Attachment

    • Bildschirmfoto 2024-04-19 um 22.49.57.jpg
  • Hmm, ist does not work with MQTT.min.js

    >mqtt = require("MQTT-S")
    Uncaught SyntaxError: Got EOF expected '}'
     at line 1 col 3902
    ....write(f(e.DISCONNECT<<4)+"")}catch(a­){return this._scktClo...
  • ok, there are 3 line of code in MQTT.js that cause this.

    line 234

    this.client.write(fromCharCode(TYPE.PING­RESP << 4) + "\x00");
    // change to 
    this.client.write(fromCharCode(TYPE.PING­RESP << 4,0));

    line 320

    this.client.write(fromCharCode(TYPE.DISC­ONNECT << 4) + "\x00");
    // change to
    this.client.write(fromCharCode(TYPE.DISC­ONNECT << 4, 0));

    line 386

    this.client.write(fromCharCode(TYPE.PING­REQ << 4) + "\x00");
    // change to 
    this.client.write(fromCharCode(TYPE.PING­REQ << 4,0));

    Just created a pr for this and added a screenshot using it as MQTTX-S without errors.

    1 Attachment

    • Bildschirmfoto 2024-04-20 um 06.25.05.jpg
  • I merged that PR as the code looks like an improvement, but the original code isn't broken. Uploading by copy/pasting into a templated str is a pretty bad idea as if you have any escape characters in strings they need double-escaping like "\\x00" or they will cause problems(as you noticed!)

    Upload via the IDE menu is much safer

  • This is the way I try to use:

    • load file into WebIDE ( see attached screenshots, ide complains )
    • change upload destination to storage
    • enter the new filename
    • upload

    Did the upload and got errors, guess I am missing something.

    >Uncaught ReferenceError: "exports" is not defined
     at line 1 col 6064
    ...tocol_level+i+r,n)},exports.create=fu­nction(t,e){return new ...

    3 Attachments

    • Bildschirmfoto 2024-04-23 um 07.46.54.jpg
    • Bildschirmfoto 2024-04-23 um 07.46.18.jpg
    • image.jpeg
  • I think you've misunderstood - there is an actual button for uploads!

    • Go to
    • Connect to your device
    • Click the Storage icon (4 discs, basically in the middle of the screen)
    • Click Upload Files
    • Upload your file using the file chooser

    Uploading your way will work but as you noted it's messy!

    In your case, you can ignore the "exports" is not defined error. It's because after upload the IDE is trying to run your module as if it's a normal app - but it's a module.

  • Wow, way to easy - many thanks for clarification!

    Is this possible via command line too?

    2 Attachments

    • Bildschirmfoto 2024-04-23 um 13.21.03.jpg
    • Bildschirmfoto 2024-04-23 um 13.22.06.jpg
  • Is this possible via command line too?


    --storage fn:data.bin    : Load 'data.bin' from disk and write it to Storage as 'fn'

    I think that works - can't remember for sure now - it's possible it attempts to write using Flash though

  • Yes, perfect. Many thanks for implementing all this super helpfull stuff.

    espruino --port /dev/tty.usbserial-54240369371  -b 115200 --storage MQTTCL-S:MQTT.min.js
    Upload Complete
    >mqtt = require('MQTTCL-S')
      create: function (t,e) { ... },
      connect: function (t) { ... }
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Howto write modules to storage?

Posted by Avatar for MaBe @MaBe