result.wakeupPin() doesn't return a value

I’m trying to determine which pin is the reason for my device waking up, but the below code doesn’t work.
only the 'whichpin" prints in the Log and no int
The if/else block doesn’t trigger at all either.

I’m following the example here: Sleep 2.0 Question: SystemSleepResult or SleepResult - #6 by rickkas7

#include "Arduino.h"
#undef min
#undef max
#include "Particle.h"

SYSTEM_MODE(MANUAL);  
const pin_t MY_LED = D7;
const pin_t REED_PIN = D6;
SystemSleepConfiguration config;
SerialLogHandler logHandler(LOG_LEVEL_INFO);
void dmy()
{

  digitalWrite(MY_LED, HIGH);
}


void setup() {

  pinMode(MY_LED, OUTPUT);
  pinMode(REED_PIN, INPUT_PULLUP);
  attachInterrupt(REED_PIN, dmy, FALLING);
  Serial.begin(57600);
}


void loop() {
  static uint32_t ms = 0;
  Serial.printlnf("Loop started");
  delay(1000);

  config.mode(SystemSleepMode::STOP)
                    .duration(1min)
                    .flag(SystemSleepFlag::WAIT_CLOUD)
                    .gpio(WKP, FALLING)
                    .gpio(REED_PIN, FALLING)
                    .network(NETWORK_INTERFACE_CELLULAR, SystemSleepNetworkFlag::INACTIVE_STANDBY);
  if (millis() - ms > 5000) {
    digitalWrite(MY_LED, LOW);
    Serial.printlnf("Sleeping");
    System.sleep(config);
    ms = millis();
  }
  Serial.printlnf("I'm awake");
  SystemSleepResult result = System.sleep(config);
  if (result.wakeupReason() == SystemSleepWakeupReason::BY_GPIO) {

    pin_t whichPin = result.wakeupPin();
    Log.info("whichpin", whichPin);
    if(whichPin == D6){
      Log.info("D6 Wakeup received");
    }else if(whichPin == D8){
      Log.info("D8 Wakeup received");
    }
    delay(1000);
  }
}

That’s not how Log.info works, it’s sprintf-style formatting:

Will not work:

Log.info("whichpin", whichPin);

Use this instead:

Log.info("whichpin %d", (int)whichPin);
1 Like

Thank you @rickkas7 for the super fast response!

I had also messed up by basically calling sleep twice to store the result instead of store the result from the first sleep :man_facepalming:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.