EspruinoWiFi module builtin?

Posted on
Page
of 2
/ 2
Next
  • I've got a brand new EspruinoWifi and flashed v1.95 via web IDE.
    From http://www.espruino.com/Modules I take it that the module EspruinoWifi (or .. 0V25) is builtin, but this doesn't seem to be the case.
    Is this a glitch in the docs or in the code - or do I miss something?

  • I trust it is not built-in. You will need to have in the initialization part of your code a line like:

    wifi = require("EspruinoWiFi");
    
  • require or not to require does not determine if built-in or not... require gives you the access... This EspruinoWifi module is though in deed not built in.

  • Thanks for clarification., using save() for wifi and onInit() for the connection works well.

    Now I wonder why the not official ESP8266 gets such goodness built it, including a telnet server, and the official Espruino WiFi needs extra initialization to enable networking.

  • I'm working on building the module in, but you'll still have to require it even with that... But then your have to require ("WiFi") on ESP8266 too, so I'm not really sure what the difference is?

    And yes, Telnet too. It's just a few lines to enable on Espruino WiFi, but doing it properly (so it survives a 'reset') is hard, and I don't want to build it in until it works nicely

  • This is good news and telnet surviving reset would be a good reason for me to upgrade the firmware.
    Requiring EspruinoWiFi as builtin module can be done directly on the naked Espruino, no upload and save (e.g. through the Web IDE) involved. This makes the preparation of the device for wireless only operation easier - no big deal, but more comfortable.
    All the Espruinos are well designed, powerfull devices and your support in the forums makes them even greater :)

  • Thanks! I've recently added the ability to include JS modules so I should get the module included for 1v96.

    If you're interested in Telnet, this should work to enable it:

    var server = require("net").createServer(function(c) {
      console.log('server connected');
      c.write('Your welcome message!\r\n');
      LoopbackA.setConsole();
      LoopbackB.pipe(c);
      c.pipe(LoopbackB);
    }).listen(23);
    

    Although as above, you can't have reset() in your code or it'll break the connection.

  • And if this code, along with the wifi setup (all nicely wrapped in a function), is set e.g. by E.setBootCode(..) - will it come up again after reset()? Such that a new telnet session can be established without cycling power of the device?

  • Yes, absolutely! Just remember E.setBootCode(..., true) so it does come back after reset :)

  • If you now use a 'cutting edge' build for Espruino WiFi from http://www.espruino.com/binaries/travis/master/ it now has the EspruinoWiFi module built in.

    It's also accessible via require("Wifi") so it'll be more compatible with the WiFi library in the reference

  • Wow this is great - so there is just missing something like require('Wifi').save() to save connection data to survive power off or reboot

    Have you thought about a special section to store wifi connection data and other data eg mqtt server and topic or .....?

  • Should that be Wifi rather than WiFi ?

  • ESP8266 and ESP32 boards use require('Wifi')

    EspruinoWiFi boards use require('WiFi') and I guess Pico with ESP01 will follow, but this is up to Gordon.

  • EspruinoWiFi boards use require('WiFi')

    They really don't - it was literally just a typo in my original post, which I have now corrected :) Did you actually try it and notice a problem?

    I'm still not sure about the Wifi.save() functionality. Especially on the 512k ESP8266s, that 4k page of WiFi data would be way more useful if used to store JS rather than just 64 bytes of Wifi config. IMO there should be a more generic data store for variable data that isn't linked to Wifi (there is already FlashEEPROM though) - I think that might have been what you were asking @MaBe?

    I am working on something that'll open up the standard Espruino saved code area, and which will finally allow JS files to be written in chunks (so you can upload more data than there is RAM), so hopefully Wifi.save could move over to that and we could add something for the Espruino WiFi board - although I'm still not super sure about the auto-connect functionality given how easy it is to just do wifi.connect(username, password in onInit and know exactly when wifi is initialised and when it isn't.

  • Requiring works. However, connect() succeeds only 1 out of 3 times (power off and on again). Here is my code:

    // clean up before: E.setBootCode();
    
    function onInit() {
      setTimeout(function() {
        require('Wifi').connect('SSID',{password:'mypassword'}, function(err) {
          console.log(err);
          if (err) return;
          require('net').createServer((conn) => {
            conn.pipe(LoopbackA);
            LoopbackA.pipe(conn);
            LoopbackB.setConsole();
          }).listen(23);
        });
      },3000);
    }
    
    // wait a few seconds, then: save();
    

    With the 1v95 build connect() always succeeds.

  • Interesting... do you have any error messages, or does it just not print anything? Is it responsive? And can you connect via Telnet?

    When I tried it, Telnet worked fine every time. However there seemed to be an issue with it not being responsive over USB.

    Please could you try this and see if it works:

    function onInit() {
      setTimeout(function() {
        require('Wifi').connect('BTHub4-XFTX',{password:'735dfd986d'}, function(err) {
          console.log(err);
          if (err) return;
          LED2.set();
          require('net').createServer((conn) => {
            LED2.reset();
            LED1.set();
            conn.pipe(LoopbackA);
            conn.on('close',function() {USB.setConsole();});
            LoopbackA.pipe(conn);
            LoopbackB.setConsole();
          }).listen(23);
        });
        USB.setConsole();
      },3000);
    }
    

    I've added LED changes so you can see connection state, but the main thing is the USB.setConsole(); right at the end. It could be because of a bug in some recent changes I made.

  • Haha, i thought you did it intentionally .... because it matches EspruinoWiFi.

    It's time for a less memory consuming solution, because this will allow ESP01 one more page to save code.

  • In case it doesn't work it writes 'WiFi connect failed: FAIL'.

    Actually, the line conn.on('close',function() {USB.setConsole();}) made the Wifi connect work reliably (!).
    The line USB.setConsole() at the end causes Wifi connect to fail.

    Regarding the LEDs, I'd expect the green one to go on once the Wifi connection is established. It doesn't, even when I can telnet to Espruino. Doing LED2.set() from the console works.

  • although I'm still not super sure about the auto-connect functionality

    The auto connection functionality is really useful if you connect via telnet over IP. It means that you have a much faster connection than USB.

    It would be a shame to loose this functionality.

  • Actually, the line conn.on('close',function() {USB.setConsole();}) made the Wifi connect work

    To me, this implies that some other computer is maybe trying to connect to port 23 briefly on the Espruino WiFi? The Telnet example before never returned the console when you disconnected, so that's sort of what you'd expect - Espruino WiFi would stay running, but you wouldn't be able to communicate over WiFi without that line above.

    It would be a shame to loose this functionality.

    I'm not planning on removing it from ESP32/8266/etc. But yes, for Telnet it's super handy - but I think that's the main one? obviously if you've got code already on the device then in many cases (IMO) it makes it more readable to have code there that explicitly connects to WiFi. Most ESP8266 code I've seen starts with a wifi.connect,save kind of thing anyway, where maybe just calling wifi.connect in onInit would give the programmer a bit more control over what happens if connection failed.

  • I find it quite a good feature of not having to include the SSID and credentials to the actual device code. It doesn't belong there. I use onInit() { wifi.on('connected', function() {}) }.

    I wish I could do something like espruino -e 'wifi.connect($SSID, { password: $PASS }, () => wifi.save()) where I would be prompted to type in the password which would be sent to the board but I have yet to find a way which would not require saving the script to a file first.

  • Hopefully this new flash storage thing will open things up. At the very least you'll be able to put your WiFi credentials in setBootCode and have your code saved normally, but potentially we could also have a file for predefined variables that gets loaded automatically.

    You probably know this, but if you do onInit() { wifi.on('connected', function() {}) } then each time onInit gets called (eg, if you save() twice) you'll get another handler. Just having the on('connected') outside onInit would be fine.

  • @opichals you could wrap that in a bash file and pass in flags, no? I use a small Go utility for ESP8266 which does exactly this - and returns IP address so subsequently can connect via IDE over Telnet

  • @Gordon Thanks for the 'Storage' module! Its use should to be a great way of not having to duplicate the serialization (JSON.stringify should suffice?) and flash access in the jswrap_wifi_save/restore methods. I would still vote for a .wifiauth file (or a more generic .config perhaps?) which would get loaded and wifi connected after every reset independently of the E.setBootCode stuff.

    BTW What would you think of using a leading dot (or perhaps some other character of your liking) in the filename as a nice way of distinguishing internal files.

    Also thank you for the onInit() { on('connected', ...) } note, I haven't noticed that before for some reason.

    @Ollie I somehow didn't spend too much time but I think I could not find a way to pass arguments/env variable values to the espruino npm's cli .js script.

  • Yes, that's the idea - just JSON stringify and parse. We could use a leading dot - I guess it would make it easier to automatically remove system/non-system files.

    And yes, I will end up adding autoconnect for the WiFi at some point. Not sure when yet though!

About

EspruinoWiFi module builtin?

Posted by Avatar for Steffen @Steffen

Actions