Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sleep Mode and SoftwareSerial Issue #14

Closed
xtendch opened this issue Mar 29, 2019 · 2 comments
Closed

Sleep Mode and SoftwareSerial Issue #14

xtendch opened this issue Mar 29, 2019 · 2 comments
Labels

Comments

@xtendch
Copy link

xtendch commented Mar 29, 2019

FYI I was facing the same problem as described in #6. I solved it by adding a flush() on the serial port used for printing status BEFORE calling pms.sleep(). (In #6 author recommended adding a flush on pms serial port after calling pms.sleep() which was not helping.)

Adjusted advanced example using SoftwareSerial:

#include <SoftwareSerial.h> // using SoftwareSerial might create problems in activating sleep mode, see additional Serial.flush(); below
#include "PMS.h"

SoftwareSerial serialDust(11, 12); // RX, TX

PMS pms(serialDust);
PMS::DATA data;

void setup()
{
  // Serial Port setups
  Serial.begin(9600);   // used for print output
  
  serialDust.begin(9600); // software serial for dust sensor
  pms.passiveMode();    // Switch to passive mode
}

void loop()
{
  Serial.println("Waking up, wait 30 seconds for stable readings...");
  pms.wakeUp();
  delay(30000);

  Serial.println("Send read request...");
  pms.requestRead();
  
  if (pms.readUntil(data)) {
    Serial.print("PM 1.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_1_0);

    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);

    Serial.print("PM 10.0 (ug/m3): ");
    Serial.println(data.PM_AE_UG_10_0);

    Serial.println();
  } else {
    Serial.println("No data.");
  }

  Serial.println("Going to sleep for 60 seconds.");
  Serial.flush(); // IMPORTANT when using software serial
  pms.sleep();
  delay(60000);
}

Tested on Arduino Nano with PMS7003 sensor.

Otherwise, thanks a lot for setting up this very clean library!

@fu-hsi
Copy link
Owner

fu-hsi commented Mar 31, 2019

Thanks.
I won't fix this because it's a workaround for the error caused by another library.
I recommend use better implementations like:

  • AltSoftSerial,
  • NeoSWSerial.

I leave issue opened for a while, to help others to find this solution.

Regards.

@fu-hsi fu-hsi added the wontfix label Mar 31, 2019
@fu-hsi fu-hsi closed this as completed May 17, 2019
@kevinstadler
Copy link

Just to add to this, like @xtendch I had an issue with commands (in my case pms.wakeUp()) not sending properly over SoftwareSerial unless I ran swserial.flush() just BEFORE the PMS command. (AltSoftSerial and NeoSWSerial libraries were not an option as neither work on the ESP8266).

#include <SoftwareSerial.h>
SoftwareSerial soft(D5, D7); // RX TX
#include "PMS.h"
PMS pms(soft);

void setup() {
  soft.begin(9600);
  // first pms command does not arrive reliably unless we flush the swserial port first
  soft.flush();
  // wake up from previous sleep (see below)
  pms.wakeUp();

  // do stuff

  pms.sleep(); // works fine
  ESP.sleep(600e6); // send into deep sleep, reset after 10 minutes brings us back to beginning of setup()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants