Programming Wirelessly

Posted on
  • Hi all,

    Suppose I have 25 espruinos communicating over ANT+, one is connected to the computer and gets a new function (or an updated function). What it the best way to send this to the other 25? Assuming I can send messages, should I create a function that does an eval of the function string, saves it as a local variable, uses function.replaceWith or function = to overwrite the existing functions and calls save()? Is there a better way to program my espruino's wirelessly, other than connecting to each individually and using the webide?

    Regards,

    Alex

    EDIT: local variable containing temp function will ofcourse be deleted after overwrite is complete.

  • Hi... Yes, sending the code string to eval is probably the best method. Obviously if you're using some kind of non-secured system you might want to think a bit about security though!

    Executing something using eval can actually alter the state of the root scope (the value doesn't have to be returned), so you can actually just do:

    eval("function foo() { console.log('foo'); }");
    foo();
    

    The other option if you want something more physical is to save the following code:

    function onInit() { eval(require("fs").readFile("boot.js"); }
    

    And then to put any code you have on the SD card.

  • Hi Gordon, thank you for the answer :). I'll go with your first suggestion!

  • Hi Gordon,

    Could I use the method the espruino WebIDE uses? That way I can completely reprogram the espruino, as opposed to the method you mentioned where I have to be careful that the handler functions are not deleted.

    Over UART code comes in, in pieces. How is this handled with the webIDE? I've been looking through the EspruinoTools git, are there any prefixes/tokens that are passed in between functions so the espruino can eval functions once they are complete?

    Regards,

    Alex

  • Well, Espruino itself is quite smart about getting functions in via the console. It counts brackets and only executes if all open brackets have been closed.

    The WebIDE actually does some relatively simple stuff:

    • It checks for require statements and pulls in any external libraries
    • If you've written any code with slightly odd formatting - eg. where there is a newline in the middle of a statement where there are no open brackets - it changes the newline into a special 'alt-enter' character.
    • It surrounds everything with echo(0);\n and \necho(1);\n and sends it over

    There's maybe some other stuff, but those are the basics.

    I'd started work on a simple command-line tool to use EspruinoTools to this with node.js (see http://www.github.com/espruino/espruino-­tools). That might be a good way of automating everything? It still needs a bit of work to get command-line arguments parsed, but the basics of communication should be there.

    There's also https://www.npmjs.org/package/espruino-c­li which exists right now, but that doesn't use the Web IDE's code, so it could be a bit out of date now.

  • Hi Gordon,

    Thanks for the explaination. I have attached custom communication modules to the espruino (to Serial2 currently). How can I tell the Espruino to treat the data coming in from there as if it is from the console?

    Cheers

  • If you do Serial2.setConsole(); then it'll move the console over to that port.

  • Great, thanks!

  • Just to add, the node module that actually works for uploading is: https://www.npmjs.com/package/node-espru­ino

    But I have now updated https://github.com/espruino/espruino-too­ls so that it actually works pretty well for programming now.

    Even if you don't add it as a node module it'll work as:

     node index.js -p /dev/ttyACM1 mycode.js
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Programming Wirelessly

Posted by Avatar for Alex @Alex

Actions