🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    M5Paper: how to detect USB power?

    Cores
    2
    4
    3.6k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      holofloh
      last edited by

      Hi there. I'm stucking in a problem that drives me a bit crazy since days: How to detect USB power in M5paper?
      The project: A station table for swiss public transport that shows me the next few trains and busses from the station near my home with destination and departure time including delay information. Swiss public transport ist usually very accurate in time but sometimes a train has +1 ... +5 minutes delay. As the station is very close I tend to leave my home as late as possible. This display will help to determine the required stress level while preparing my backpack and taking my shoes on.
      How it is done: I get the stationboard data from API transport.opendata.ch and pick the required information out of the returned JSON file. A prototyp in UIFlow works but need a lot of time to parse the JSON so now I am coding with the arduino environment which does it much faster (very much faster!). The M5Paper shall do the following: wake up by RTC, connect to my Wifi, get the JSON, parse it, update display and go to sleep for one minute until RTC wakes it up for the next round. Save as much power as possible to keep the battery time as long as possible.
      Now the Problem: While plugged to USB for charging, the M5Paper does not stay in sleep mode. It wakes up immediately and update every few seconds. This penetrates the transport API much more than required and I want to avoid that. So the idea is a simple if (usbpower) then sleep(60seconds) else shutdown(60seconds) but there is no official way to detect if charging or not in M5Paper (some other M5 devices have that). My first idea of getting battery voltage is not reliable enough as sometimes it returns high and sometimes low values while charging. Anyone has an idea?

      1 Reply Last reply Reply Quote 0
      • felmueF
        felmue
        last edited by

        Hello @holofloh

        what you can try is the following. First call the function for a shutdown (which will succeed on battery power, but fail when running from USB), so after that use some delay() to prevent too frequent updates.

        You should get an idea what I mean from this example here (about line 100).

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        1 Reply Last reply Reply Quote 0
        • H
          holofloh
          last edited by

          Thanks Felix,

          I was thinking way to far to solve this problem. The end of my main loop looks now like this:

          // If on battery power the system go to sleep and waiting for the wakeup call by RTC
          // If on USB power the system stay awake so undo what shutdown() did and waste some time before repeating the main loop
              M5.shutdown(sleepseconds);
              M5.enableMainPower();
              M5.RTC.clearIRQ();
              delay(sleepseconds*1000);
          

          it seems to work.

          1 Reply Last reply Reply Quote 0
          • felmueF
            felmue
            last edited by

            Hello @holofloh

            cool, glad it worked.

            Thanks
            Felix

            GPIO translation table M5Stack / M5Core2
            Information about various M5Stack products.
            Code examples

            1 Reply Last reply Reply Quote 0
            • First post
              Last post