Getting started with CLI on RaspberryPi

Posted on
  • Hi,
    I have installed espruino on Raspberry pi as per https://www.espruino.com/Raspberry+Pi+We­b+IDE
    If I run the CLI with --list then I can see a port.

    espruino --list
    
    PORTS:
      /dev/ttyAMA0
    
    

    I can also set this port on startup with

    espruino -p /dev/ttyAMA0
    
    Espruino Command-line Tool 0.0.26
    -----------------------------------
    

    What can I do next?

    James

  • Did you also run this?

    sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
    

    The single port that's being reported is a built-in physical serial port on the Raspberry Pi - it's not finding any Bluetooth devices - which could be because of the lack of the command above, or because bluetooth itself isn't working on the Pi

    When it does work, --list should show some addresses of the form aa:bb:cc:dd:ee that you can use - and then you should be able to communicate with the Puck.

    You can also run sudo hcitool lescan to see if the Bluetooth is able to see any of the Pucks.

  • Gordon,
    I have run that line of code but have since reinstalled espurino so will do it again tonight.
    Also sudo hcitool lescan is finding Bluetooth devices including Pucks so I am confident that the Pi 3 hardware is OK.
    I can see that there is lots of reference information here about the API and a number of example code snippits. Maybe I have missed something but what I haven't found and feel may be missing is some fundamentals about how I will then be able to interact with these pucks through the CLI. The detail will of course depend on what I want to do but as a starter for 10 I would like to be able to do some simple things like:

    • Connect to a puck and Disconnect
    • Check the battery level interactively
    • Activate LEDs interactively
    • Upload a simple code module to for example make LED flash in response to button press

    Thanks in advance
    James

  • It's worth trying to run espruino --list a few times, just in case. If you're using the original 1v88 firmware it doesn't send advertising packets quite quick enough, and that means that if some tools only wait a second to scan they might miss it.

    You could also run espruino --list --verbose and see if any errors get reported - Noble might be trying to say that there was some error - or it's possible that NPM totally failed to install Noble at all.

    There's info covering most of what you're after on the Espruino CLI tools page I think: https://github.com/espruino/EspruinoTool­s

    You can get the battery percentage using sudo bin/espruino-cli.js -p d9:aa:73:7c:7f:cf -e "print(Puck.getBatteryPercentage())" (for example - that's my Puck's address)

    At the moment there's nothing on the command-line to evaluate an expression and return just that, but you can easily strip it out:

    sudo bin/espruino-cli.js -p d9:aa:73:7c:7f:cf -e "print('>>>'+Puck.getBatteryPercentage()­)" | sed -ne "s/^.*>>>\(.*\)$/\1/p"
    

    However you can actually use espruino as an NPM module and can send commands and/or evaluate expressions straight in that

    First step is to get it connecting to your device though.

  • You could also run espruino --list --verbose and see if any errors get
    reported - Noble might be trying to say that there was some error - or
    it's possible that NPM totally failed to install Noble at all.

    Ah - that verbose switch was useful - that's exactly what's happening. Have tried repeating the install steps to no avail. Have tried apt-get update + upgrade. Have also tried npm rebuild.
    I am not sure if the error referencing 'Acorn Library' is important but the following error stating noble could not be loaded and the following error about 'binding.node' look more significant. Any ideas how I can resolve this?

    Error: The module '/usr/lib/node_modules/espruino/node_mod­ules/bluetooth-hci-socket/build/Release/­binding.node'
    was compiled against a different Node.js version using
    NODE_MODULE_VERSION 48. This version of Node.js requires
    NODE_MODULE_VERSION 51. Please try re-compiling or re-installing
    
    

    Grateful for any advice on how I could have sorted this in an orderly way but in the meantime I seem to have fixed this after a number of npm rebuild attempts and strangely a reboot.

    espruino --list
    Espruino Command-line Tool 0.0.26
    -----------------------------------
    
    PORTS:
      f2:91:92:05:39:xx (Puck.js 39xx)
      fe:ec:68:7b:6e:xx (Puck.js 6exx)
    
    

    Now off to find out how I control which of these 2 pucks I am connecting to.

  • Ahh ok, I guess you maybe updated node after you'd installed the espruino library. NPM updates were definitely the way to go - glad you got it sorted!

    All you should need now is espruino -p f2:91:92:05:39:xx and you'll get a terminal where you can send commands to the Puck.

  • Brilliant - That works - I am on a roll now and can get LEDs to come on and off at will.
    I wasn't expecting -p to accept the address / name of the puck. Having examined the help for espruiono I was expecting it to be looking for the port eg /dev/ttyAMA0 - I suspect this may catch others out.
    If there is more than one puck, espurino seemed unable to connect until I physically removed the first from the room. Is it possible that espurino may not always be disconnecting or do I need to send a disconnect command?

    I also notice that, unlike bash, when sending commands from in the CLI the cursor must be at the end of the line. This may also catch others out until they realise what's happening.

    Thanks for getting me started - I hope this helps others on this journey too.

  • I'll tweak the docs.

    It should definitely disconnect after the app has closed. Just having the process killed should really be enough to do it - so I'm not sure why you'd be having connection issues.

    It may just be chance - if you're still using the firmware they shipped with, they don't advertise that often and sometimes the app can have trouble finding them to connect to. The newer firmware will fix that.

    the cursor must be at the end of the line

    Yes, this is an Espruino thing - built into the firmware. Once you get used to it it's handy for multi-line editing, but if you've come straight from bash it can feel annoying at first

  • Just hit 'end' to go to the end of the line, or 'page down' to go right to the end of a multi-line thing. You can also just click with the mouse

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

Getting started with CLI on RaspberryPi

Posted by Avatar for JamesS @JamesS

Actions