Is my Espruino sleeping?

Posted on
  • All my code is in an

    onInit() 
    

    My project requires my device to connect to the WiFi. If my device is plugged into USB on my PC it works great and the red led is lit all the time.

    If my device is plugged into a USB wall socket, it begins brightly lit. Then after a while it starts to flash on and off quickly. My code fails to execute it seems.

    The led is set by the setSleepIndicator function call.

    Espruino WiFi v1.05

  • Check http://www.espruino.com/Troubleshooting#­i-typed-save-but-my-connected-device-doe­sn-t-work-at-power-on
    and the next two items in the troubleshooting.

    Do you have any console.log or print in your code?

  • As @AkosLukacs says that's most likely the problem.

    Other things to watch out for:

    • Have you tried using a different wall adaptor? Some see that very little power is drawn by the device and then try and go into a 'sleep' mode.
    • Are you using setDeepSleep? If so I'd remove it for now - setDeepSleep will be ignored when connected to USB, but when disconnected it'll take effect and if you have a WiFi connection open it may interfere
  • So i've read the docs and I do indeed have console logs in my code, so my plan is to remove those, but I've somehow hit another issue. When I try to send code to my board i'm getting the following error.

    New interpreter error: FIFO_FULL
    Connection error: No 'ready' after AT+RST
    

    This is odd as it has been working from the web IDE just fine recently. Here's the source. I have a raspberry pi4 running nodered which reads sensors around the house, some of these send data via MQTT which i'm picking up with the Espruino Wifi.

    const neo = require('neopixel');
    const wifi = require('Wifi');
    const WIFI_NAME = 'BTHub6-3TFX';
    const WIFI_OPTIONS = { password : 'xxxxxxxx' };
    
    let delay = 0;
    let rgb = new Uint8ClampedArray(36);
    
    
    function onInit() {
      setSleepIndicator(LED1);
      const server = '192.168.1.64';
      const options = {
        client_id : 'random',
        keep_alive: 60,         // keep alive time in seconds
        port: 1883,             // port number
        clean_session: true,
        username: 'username',   // default is undefined
        password: 'password',   // default is undefined
        protocol_name: 'MQTT',  // or MQIsdp, etc..
        protocol_level: 4,      // protocol level
      };
      const mqtt = require('MQTT').create(server, options /*optional*/);
    
      mqtt.on('connected', () => {
        console.log('connected to broker');
    
        mqtt.subscribe('doorOpen');
        mqtt.subscribe('doorClosed');
        mqtt.subscribe('heartbeat');
      });
    
      mqtt.on('publish', (pub) => {
        console.log('topic: '+pub.topic);
        console.log('message: '+pub.message);
    
        switch(pub.topic) {
          case 'doorOpen':
            console.log('doorOpen');
            doorOpen();
            break;
    
          case 'doorClosed':
            console.log('doorClosed');
            doorClosed();
            break;
    
          case 'heartbeat':
            if(parseInt(pub.message) > 1) {
              heartbeat();
            }else{
              motion();
            }
            break;
    
          default:
            neo.write(B15, lights({r: 0, g: 0, b: 0}));
            break;
        }
    
      });
    
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, (err) => {
        if (err) {
          console.log('Connection error: '+err);
          return;
        }
        console.log('Connected!');
        mqtt.connect();
      });
    
      function lights(colors) {
        for (let i = 0; i < rgb.length;) {
          rgb[i++] = colors.g;
          rgb[i++] = colors.r;
          rgb[i++] = colors.b;
        }
        return rgb;
      }
    
      function doorOpen() {
        neo.write(B15, lights({r: 0, g: 0, b: 0}));
        update({r: 255, g: 0, b: 0});
      }
    
      function doorClosed() {
        neo.write(B15, lights({r: 0, g: 0, b: 0}));
        update({r: 0, g: 255, b: 0});
      }
    
      function heartbeat() {
        neo.write(B15, lights({r: 0, g: 0, b: 0}));
        update({r: 0, g: 0, b: 255});
      }
    
      function motion() {
        neo.write(B15, lights({r: 0, g: 0, b: 0}));
        update({r: 0, g: 255, b: 0});
      }
    
      function update(color) {
        neo.write(B15, lights(color));
        delay = setTimeout(() => {
          neo.write(B15, lights({r: 0, g: 0, b: 0}));
        }, 1000);
      }
    }
    
    onInit();
    
  • I'm also unable to update to v106 even though the red and green lights are pulsing as expected.

    Error Flashing: Can't find STM32 bootloader. Make sure the chip is reset into bootloader mode by holding down BTN1 while pressing RST.

    Is there a way I can hard reset on Espruino Wifi? I'm running into a few issues lately.

  • Putting all you code into onInit() is a good start. More later about that.

    Unfortunately then calling onInit() in the upload 'JS thread' defeats this precaution...

    Replace the onInit(); with setTimeout(onInit,999);. This allows the 'upload JS thread' to finish, restore usual console connection, and start your code.

    You still may run into issues if there is too much in onInit();. Having all your function definitions 'in root' individually is better in regard of memory and buffer consumption spikes.

    Only the startup of your code - especially when it deals with communication - has to be in the onInit() and putting it into a setTimeout() for the time of development makes it work.

    I'm sure you read simple explanation how to save code that espruino run on start?. (Even though Epruino has now more options to overcome issues of that sort of, the core has not chanted at all and is still very applicable.)

    Looking forward to hear from you! - ao

  • Putting all into onInit() can create issues with scopes of variables. If things are defined in onInit(), I always define the variable outside. That give me the options to go mixed - global and function local scoped references.

  • Hey AO good to hear from you. I only call onInit when I'm running in the web IDE.

    I remove this call when I run save(); in the IDE.

    I must admit the various methods of saving and running code on espruino don't make a lot of sense to a regular web developer such as myself. I'll need to do some reading up on this area.

  • Ok so now I have this very simple piece of my implementation and i'm still getting this dreaded error:

    New interpreter error: FIFO_FULL
    

    All I'm doing is the following:

    const wifi = require('Wifi');
    const WIFI_NAME = 'BTHub6-3TFX';
    const WIFI_OPTIONS = { password : 'mypassword' };
    
    function onInit() {
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, (err) => {
        if (err) {
          console.log('Connection error: '+err);
          return;
        }
        console.log('Connected!');
      });
    }
    

    I really don't understand why it just throws this error when I do anything with Wifi now, this was all working the other day.

    Any ideas guys?

  • please share process.env output

  • Oddly after several attempts using reset and save its started working again.

  • Ok so I've now tested this using different USB plugs, cables and computers. The result is always the same, after maybe one or two hours. The Espruino WiFi seems to stop executing, and there led that is being used for sleep indicator, starts to flicker quickly and is no longer brightly lit.

    Does this sound like it's trying to sleep?

    I even added a ping back to the raspberry pi to make sure the connection stays alive every 15 seconds.

  • Is it possible that you'd disabled reset before send in the Web IDE?

    If so it'd be trying to upload code, but the previous code would be running in the background so could be using CPU time and causing the upload to fail?

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

Is my Espruino sleeping?

Posted by Avatar for Coder2012 @Coder2012

Actions