Serial troubles when disconnected from USB

Posted on
  • I've bought a shiny new SIM7000 GSM/LTE module that I've managed to get working with Espruino Pico.

    My problem is that the USART interface stops working when I disconnect the Pico from the USB port and power it from a LiPo battery. I first used Serial1 with USB.setConsole(true), then tried Serial2 instead, without noticing any difference. The code runs fine, logging any errors to an array so I can check it later. But the USART connection goes quiet as soon as USB is disconnected – any AT commands sent to the module never get a response. If I connect it to the USB port again and use the Web IDE, everything is back to normal.

    What could be going on? Is there something obvious that I've missed when setting up USART?

    I should mention that I've actually made my own version of the AT module, using promises instead of callbacks, plus a few other changes that I needed in order to get it to work with SIM7000 (and to understand what it was doing – really good programming excercise!). While the bug could lie somewhere in that code, it does work perfectly fine when connected to USB.

    Relevant excerpts from my code (yes, no semicolons!):

    const USART = Serial2
    const BAUD = 115200
    const PWRKEY = B4
    const at = require('AT').setup(USART)
    // Enable the PWRKEY pin
    pinMode(PWRKEY, 'output')
    // Enable Espruino's deep sleep mode
    // Deal with any uncaught exceptions
    process.on('uncaughtException', logError)
    E.on('init', () => {
    function test() {
      digitalPulse(PWRKEY, LOW, 100) // Power on
      // Listen for "SMS Ready" URC, time out after 10 seconds
      at.listen('SMS Ready', 10000) // similar to at.register() in the official module
        .then(…) // do stuff
        .catch(logError) // logs error to an array for later retrieval
      // When connected to USB, it receives "SMS Ready" 100% of the time
      // When connected to battery only, the SIM7000 module powers on but at.listen() times out with nothing being received
      // When connected back to USB again (without rebooting), AT commands are received

    I'm programming the Pico from the Web IDE using "Save on Send: Direct to Flash".

    I've also tried putting USB.setConsole(true) at the very top of the code.

    Any help would be greatly appreciated!

  • Hi,

    The issue is actually setDeepSleep(true) - basically this stops Espruino's high speed oscillator from running when it's idle, but that also stops the UART from receiving any data. It works on USB because Espruino knows when it's connected to USB there's no issue with power consumption, so it doesn't sleep.

    The simplest solution would be to keep setDeepSleep(false) whenever the SIM7000 is on, and only turn setDeepSleep(true) when you've got the SIM7000 off.

    However, if the SIM7000 can implement RTS/CTS flow control, you can toggle setDeepSleep pretty easily. You just do something like:

    setWatch(function(e) {
      if (e.state) { // NOT ready to send
      } else { // ready to send
    }, RTS, {repeat:true});

    I'm pretty sure the polarity is normally inverted, but you might have to do some tests to make sure :)

  • Aha, that must be it!

    I had misunderstood deep sleep and thought things would work like normal from the time it wakes up until it's done doing stuff. Classic example of RTFM :)

    Espruino can't be woken by Serial/USART traffic (and will not receive data while in Deep Sleep). (…) To work around this, you'd need to implement RTS/CTS flow control in software (waking Espruino on RTS, and only setting CTS after setDeepSleep(0) has been called).

    SIM7000 does support RTS/CTS hardware flow control, so I'll try that. Thank you!

    Edit: The shield didn't though, so I ended up disabling/enabling it manually.

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

Serial troubles when disconnected from USB

Posted by Avatar for Joakim @Joakim