Modules - what am I doing wrong

Posted on
Page
of 2
/ 2
Next
  • Hi There

    I am afraid I am complete newbee of Espruino, so please forgive me if this is some thing silly. I am hoping to use module but not have much luck

    I am on Mac OsX and using the web IDE, I can see the projects so I think i am using the correct program.

    on my root folder I've set up a project folder called 'Espruino' and have gone into the setting -> project and set the sandbox as ~/Espruino this created lots of folders including modules, I copied my module in there

    now when i run my first line of code ie

    const PoweredUP = require("node-poweredup");

    it say the module can not be found am I doing something daft ?

    Any Help would greatly appreciated

    Cheers

    James

  • Fri 2019.12.06

    Hi James, any more detail revealed in
    WebIDE >> Settings >> Console

    Is node-poweredup.js in folder \Modules and calling code.js in \?????

    Not at development PC, on lunch break, more later this eve


    EDIT:
    Calling code.js file would reside in \Projects

  • Hi Robin

    Thankyou that helped,

    It looks the module i am trying to use if the wrong type ie node module and there doesn't appear to have .js or .min.js file. I install the module via NPM, is there simple way around this

    Many Thanks

    James

  • Fri 2019.12.06

    I did a quick update of post #1

    Hi @user106443, now that the 504 Gatway errors (main web site fine just forum) are over (2 hour wait 9-11pm CST) I'm able to respond.

    From the line of calling code in post #1, it appears you are well versed and may have already persued this help page:

    http://www.espruino.com/Modules

     

    'is there simple way around this'

    James, I don't want to mislead with the unknown, I just don't have enough experience with Node or NPM to provide a suitable response.


    'the module i am trying to use if the wrong type'

    However, I dare try:

    My S.W.A.G. response, would changing the extention of the node file to .js and placing maually in the \Modules folder work?



    Should the above link not provide enough detail, and a running example is still required, I could whip together a sample demo 'Hello World' test page if that would help.

  • If you want to use nodejs modules use nodejs, e.g. on a pc, rpi or opi.
    Espruino has limited js/ts compatibility, and runs typically on hardware with very limited memory, and as such most nodejs modules are simply to big to be includes and using non-supported js/ts. Espruino typically uses modules to provide and interface for I2C/1-wire/serial chips, see https://www.espruino.com/modules/
    I'm pretty sure it's impossible to use the mentioned module on Espruino.

  • @user106443

    I'm pretty sure it's impossible to use the mentioned module on Espruino.

    as @maze1980 points out, because I may use ES5/ES6 things that are not implemented yet or are impractical to implement in Espruino, last but not least because of the resources on the mcs used, but also because of the way Espruino JavaScript interpreter genuinely works.

    NodeJs lives in a different context and makes therefore different presumptions. One of them is: memory is practically limit less - even more so for short periods - and code is 'compiled' into virtual machine code. These is perfect fine thinking for servers or browsers where NodeJs runs and browserified modules run.

    Transpilers can help to 'bring things down' to implemented functionality, but that covers only half the story.

  • If the module of interest has just one JS file, you can rename that to the module name and stick it straight in the modules folder and it should work. If it's a multi-file module chances are it's too big to run on Espruino :(

    There was a (beta) NPM module loader in the IDE for a while, but it was such a nightmare it got removed. A good 95% of NPM modules (if not more) pull in so many dependencies they fill up Espruino's memory before you write any code

  • From this example https://github.com/nathankellenicki/node-poweredup#nodejs-sample-usage and this source of the module https://github.com/nathankellenicki/node-poweredup/blob/master/src/poweredup-node.ts there is no chance this will work in Espruino as is.

    However maybe the misunderstanding is even bigger, @user106443 what Espruino device you want to use to run your newly written code? What are you trying to do? Espruino IDE is not meant to be running your javascript code directly in the browser on your Mac OS X. It is targeted for some small device like puck.js

  • Thanks for everyone for the reply, Yes it looks like the the modules is multi files, and it seems very complicated to try and combine into one.

    The project is to try and make a remote for daughters bluetooth lego train, it had working in browser so was hoping to port it over but nothing is ever that simple

    I am currently trying to see adapting 'Bluetooth LE HID Keyboards' library work. I just need to try and find the correct modifier keys

    Wish me luck

  • Hi All

    Just a little update, I've managed to hack in the node-module when it's running on my mac and got it print me the code for certain commands, so it should be a little simpler

    If anyone could point me in right direct for the best way to this raw data to my device that would be grand

    "Code to Send: 00001624-1212-efde-1623-785feabcd123 Buffer 81 00 11 51 00 64"

    I am very greatful for any help

    James

  • Great little fun project!

    Did you notice some particular packet size(s)? Do you know abut the layout of the packages - what is at what position in the package and what the value means? Is it sent binary or in hex char? Does the communication happened paired or just advertised w/ address in pay load?

    00001624-1212-efde-1623-785feabcd123 looks like 16 bytes in hex... about right to (still) fit into an small packet or even an ad(vertisement).

    Looks like this 'spot' may provide insights... https://github.com/LEGO/lego-ble-wireless-protocol-docs

    PS: James it looks that you have two differen Espruino forum IDs - user106443 and user104759 - is that correct? First started the thread, latter used in last two posts of yours...

  • Thanks the lego link very helpful

    Yes it looks like the message it send contains the 16 bytes in hex followed buffer string that appears to vary in length. ie Buffer 05 00 01 02 02 or 0a 00 41 00 00 01 00 00 00 01

    sorry didn't realise i was using two account the previous message was sent from my work.

  • you mean 16 bytes in binary? 32 nibbles... otherwise it would be 32 bytes... I guess you used hex for a display of an Uint8...

    ...yep... 'buffer' is self-describing message: 1st byte is length in Uint8... (message length includes the length byte)...

    Protocol is GATT - means communication in connected state - see https://learn.adafruit.com/introduction-to-bluetooth-low-energy/gatt (anyway a helpful link... things broken down and explained for someone or something like me / my brain).

    Of course, for BLE all roads lead to https://www.bluetooth.com/specifications/

  • Hi All

    Thanks allobjects that helped but my progress is still quite slow, i have managed to connect to the train and disconnect but can't work out the best way to send the buffer. I am still trying to get my head around javascript so maybe missing something quite obvious

    here's my current code

    var devices;
    var gatt;
    var DevName="e0:7d:ea:0c:03:29";
    var serviceUUID="00001623-1212-efde-1623-785feabcd123";
    var characteristicUUID="00001624-1212-efde-1623-785feabcd123";
    var MessageToSend=[0x81, 0x00, 0x11, 0x51, 0x00, 0x64];
    pinMode(D15, 'input_pulldown');
    global.LED2=D2;

    NRF.connect("e0:7d:ea:0c:03:29").then(function(g) {
    console.log("Starting connecting");
    gatt = g;
    return gatt.getPrimaryService("00001623-1212-efde-1623-785feabcd123");
    }).then(function(service) {
    return service.getCharacteristic("00001624-1212-efde-1623-785feabcd123");
    }).then(function(characteristic) {
    return characteristic.writeValue(MessageToSend);
    }).then(function() {
    //gatt.disconnect();
    console.log("Done!");
    });

    function send_message() {
    console.log("Moving Train");
    NRF.nfcSend(new Uint8Array([0x81, 0x00, 0x11, 0x51, 0x00, 0x64]));
    }

    function train_disconnect() {
    console.log("Disconnecting");
    gatt.disconnect();
    }

    setInterval(function() {
    digitalWrite(LED2, digitalRead(D15));
    if (digitalRead(D15) == 1) {

    send_message();
    

    }
    console.log("Button pressed", digitalRead(D15));
    }, 300);

    any advice would be greatly appreciated

    Cheers

    James

  • Hello James, any chance you would re-edit your code posting so that line numbers will appear along with color hilighting?

    Hilight the code block, then click the < / > code button between the Quote button and Preview button,

    or,

    Place three backticks (below the tilde key and next to the numeral 1 key) just before and just after the code block.

    This will enhance and make reading easier.

  • Thanks For have a look Robin is this a better layout ?

    var devices;
    var gatt;
    var DevName="e0:7d:ea:0c:03:29";
    var serviceUUID="00001623-1212-efde-1623-785feabcd123";
    var characteristicUUID="00001624-1212-efde-1623-785feabcd123";
    var MessageToSend=[0x81, 0x00, 0x11, 0x51, 0x00, 0x64];
    pinMode(D15, 'input_pulldown');
    global.LED2=D2;
    
    NRF.connect("e0:7d:ea:0c:03:29").then(function(g) {
      console.log("Starting connecting");
      gatt = g;
      return gatt.getPrimaryService("00001623-1212-efde-1623-785feabcd123");
    }).then(function(service) {
      return service.getCharacteristic("00001624-1212-efde-1623-785feabcd123");
    }).then(function(characteristic) {
      return characteristic.writeValue(MessageToSend);
    }).then(function() {
      //gatt.disconnect();
      console.log("Done!");
    });
    
    function send_message() {
      console.log("Moving Train");
      NRF.nfcSend(new Uint8Array([0x81, 0x00, 0x11, 0x51, 0x00, 0x64]));
    }
    
    function train_disconnect() {
      console.log("Disconnecting");
      gatt.disconnect();
    }
    
    setInterval(function() {
      digitalWrite(LED2, digitalRead(D15));
      if (digitalRead(D15) == 1) {
        send_message();
      }
      console.log("Button pressed", digitalRead(D15));
    }, 300);
    
  • @user104759

    perfect! It make code much more readable and easier to discuss.

  • Sorry To keep asking,

    I am seem to making some progress using a paired back node script and now rewriting in to use on the espruino, however I am stuck finding a way of making 'Buffer.from' working, Is there espruino equivalent ?

    I am looking to run this line

    const sounddata = Buffer.from([0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x01, 0x05]);
    
  • Sun 2019.12.15

    'Is there espruino equivalent ?'

    It is actually a Javascript language method.

    W3Schools is where I go.

    https://www.w3schools.com/jsref/jsref_from.asp

    There is even a 'Try It' button to click!

    Reference link for Array handling:

    http://www.espruino.com/Reference#l_Array_Array

  • Thanks for replying

    unfortantly it does like Array.from unless is need to done a slight different way in espruino

    Uncaught Error: Function "from" not found!
    at line 7 col 19
    var myArr = Array.from("ABCDEFG");

  • My mistake, should have double checked using the exact link I provided. Muddled with 'find'

    Try this forum post with tons of examples and links:

    http://forum.espruino.com/conversations/337287/

    I think this is what you are after:

    a = new Uint8Array([1,2,3,4,5,6,7,8,9,10]);
    

    http://www.espruino.com/Reference#Uint16Array

  • Thanks Robin, that eventually worked however i realise i was missing a step and had to send a two different commands to make it work.

    I managed to get me remote made in time for christmas so thank you all for you help, here a video of my remote in action

    https://www.instagram.com/p/B6inY2XgasT/?igshid=16amjui8jcves

    I hope you all had great a christmas

  • Thr 2019.12.26

    Thank you and Merry Christmas James @user104759 to you and your family.

    James, now that is one really cool project for the kiddies!

    and, . . . I'm sure you had as much fun getting it to work, . . . didn't you! ;-)
    excellent job on the woodwork controller



    Now the train has an Espruino onboard, time to one-up that toy manufacturer, and add some LED headlights, train whistle (not quite sure how to do that - maybe there is a sound module), electro-magnetic disconnect coupling, and simulated smoke??



    Had an idea, would you take the time to create a forum post under the 'Projects' heading to show off this result? Including both some static images along with the video link. Having (all) most of the code would really assist others to get a head start in doing other similar projects. Now, it would be really cool if that project could be extended using the Blockly graphical programming model, (click < / > icon at bottom of the WebIDE) so that the kiddies could 'program' the running train the way they want, perhaps? Just a thought . . . .

  • @user104759

    great to hear that the Christmas celebration was elevated by this fun application... Thanks for the video. Enjoyed it very much.

  • That's amazing - thanks for posting the results! The controller looks awesome too!

    Would you be willing to post up your finished code in some form? Having a way to control LEGO sounds like it'd be amazingly useful for loads of people.

    By the way I noticed in the code sample above you had NRF.nfcSend - that would be for low-level NFC (not Bluetooth) so I wonder if you'd actually meant characteristic.writeValue?

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Modules - what am I doing wrong

Posted by Avatar for user106443 @user106443

Actions