Espruino command-line and NPM module

Posted on
  • Hi,

    Just to say that I finally got around to creating a NPM module for Espruino.

    If you have node.js and NPM installed you can just do:

    npm install -g espruino
    

    and you'll get the command-line tool.

    Instructions here

    You can also use it to do things from node.js on your PC:

    require('espruino').expr('/dev/ttyACM0', 'E.getTemperature()', function(temp) {
            console.log('Current temperature is '+temp); 
    });
    

    It still needs a bit of work, but hopefully this will be a good start.

  • just a little typo on the npm text:
    command-lune

    ?? when I'm using WinXP how do I get the specification of the port for:
    -p,--port /dev/ttyX

  • Thanks!

    It should just be COM12 or similar. The easiest way is to click connect in the Web IDE, and see what number it reports (but you could look in Windows' Devices and Printers menu).

  • Thanks @Gordon! Looks great, nice readme!

    Looking through the to-do section, I'm not sure if it's worth adding a file watching feature, since people will have their own that they use (maybe with gulp/grunt and their own development environment).

    Something that could be useful would be to write to a file instead of to the espruino board. As in, it grabs all the modules from the espruino website, assembles the "addcached" call, minifies, and then writes all these commands into a js file for someone to be able to upload later, or keep a copy of, or to debug. Basically, to act like browserify. :P

  • Thanks! Yes, writing to a file would be a good one - especially where the compiled/assembled code comes in.

    With the watch, it looks like it's just a few lines of code... For people that want it to 'just work' it might be a good idea to have built in - but yes, if it's more complex than that it'd make more sense to use something built for it.

  • Ok, there's now -o file.js for outputting the file, and also a proper terminal mode

  • Awesome!

  • I think the watch feature would be cool and useful. Keeps everything self contained and easy start. In the go version it was a very simple thing to implement as someone had provided a package. Same appears to be true for Node.js

    https://www.npmjs.com/package/filewatcher

    @Gordon I noticed you gave Espruingo a mention - thanks very much- but you know now I'll have to maintain it ;)

  • pretty cool ;)

  • Ohh, too bad I didn't know about this earlier.
    I ended up using the 'serialport' module directly instead...

    https://github.com/Tobbe/robotgame/blob/master/nodejs/app.js

  • Hmm, I'd still need serialport to do the port scanning, right?

  • Yes, it uses the serialport - if you're uploading code it does things like scanning for modules though.

  • I mean "serialport" as in the npm module :) https://github.com/voodootikigod/node-serialport

    I guess my real question was if https://www.npmjs.com/package/espruino would automatically find my espruino if I just plugged it in, or if I had to first figure out what port it was connected to (for example by scanning all ports with node-serialport, looking for something that looks like an espruino)

  • I mean "serialport" as in the npm module :)

    Yes, sorry - typo on my part. That's the lib it uses.

    automatically find my espruino if I just plugged it in

    No, if there are >0 ports, it'll pick the first one. It isn't able to test to see if an Espruino is on that port - although if it were possible (reported back by the OS?) that'd be really cool.

  • In the script I posted above I just loop over the ports doing

    if (port.manufacturer === "STMicroelectronics.") {
    

    Not very clever and I know for a fact that it doesn't work on my coworker's Ubuntu install (the same board reports manufacturer as "STMicroelectronics", i.e. without the dot at the end).

    Could try some ducktyping after that I guess by sending some commands and see if the reply looks like it comes from an Espruino :)

  • Ahh, great! - I reckon you could probably check the USB Product and Vendor IDs pretty reliably, and presumably they are available in a similar way.

    If you did do it, it'd be great if you could submit it as a change for: https://github.com/espruino/EspruinoTools/blob/gh-pages/bin/espruino-cli.js#L273

  • When I do a npm install -g espruino, I see the folder coming up in my node_modules folder for a while and then it is getting removed. The installation ends with a message to consult the log file. See attachement.


    1 Attachment

  • I'm afraid I'm not much of a node.js guru, but looking at the error messages it seems to be a problem installing contextify - presumably npm install -g contextify will fail for you?

    Maybe you could post up on the contextify issues page - or perhaps someone else has some ideas here?

  • Yes, contextify also fails. Error is:

    Error: Can't find Python executable "python", you can set the PYTHON env variable

    That probably explains it. I am on a Windows machine and didn't have Python installed. After installating Python I still get errors, so I better head over there...

    Thanks @Gordon

  • Ok, glad you're getting somewhere... When I get time, I have plans to try and reduce some of the dependencies the Web IDE has - and hopefully that'll make the installation easier on all platforms.

  • For future reference, he is some information for people who want to try running the Espruino command line version on Windows (10).

    The requirements are pretty well described here: http://www.serverpals.com/blog/building-using-node-gyp-with-visual-studio-express-2015-on-windows-10-pro-x64

    I had to install Python 2.7(.11). The latest version of Python did NOT work so get version 2.7. I already had Visual Studio 2015 Community Edition installed. Make sure you select the Visual C++ option during your install.

    Other requirements for me were:

    1. SET PYTHON=C:\Python27 (The installer put Python in that folder)
    2. Just to be sure I added the Python path also to my path with SET PATH=%PATH%;C:\Python27
    3. Open a Command Prompt with Administrative rights!

    After make sure everything was installed correctly I was able to use NPM install -g Espruino for a successful install. Unfortunately, I don't have an Espruino (or ESP8266) with me to test if it works. If nothing is connected to a serial port, then starting Espruino comes up with an error:

    C:\WINDOWS\system32>espruino
    Espruino Command-line Tool 0.0.13
    -----------------------------------
    
    C:\Program Files (x86)\nodejs\node_modules\espruino\bin\espruino-cli.js:277
            throw new Error("No Ports Found");
                  ^
    Error: No Ports Found
        at C:\Program Files (x86)\nodejs\node_modules\espruino\bin\espruino-cli.js:277:15
        at eval (eval at loadJS (C:\Program Files (x86)\nodejs\node_modules\espruino\index.js:9:15), <anonymous>:56:11)
        at eval (eval at loadJS (C:\Program Files (x86)\nodejs\node_modules\espruino\index.js:9:15), <anonymous>:13:12)
    
  • Works fine for me on Windows 10, Visual Studio Community 2015 and Node 4.2.3. Happily talking to Espruino. Very useful!

  • @conor Thank you for the update. Visual Studio is what I am planning to use it with too!

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

Espruino command-line and NPM module

Posted by Avatar for Gordon @Gordon

Actions