diff --git a/README.md b/README.md index 9956032..9118ff4 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # PMS Library + Arduino library for Plantower PMS sensors. -Supports PMS x003 sensors (1003, 3003, 5003, 6003, 7003). +Supports PMS x003 sensors (1003, 3003, 5003, 6003, 7003, 9003, A003). + ## Installation -Just use Arduino Library Manager and search "PMS Library" in Sensors category. +Just use Arduino Library Manager and search "PMS Library". + ## Main assumptions - easy as possible, - minimal memory consumption, @@ -11,35 +14,36 @@ Just use Arduino Library Manager and search "PMS Library" in Sensors category. - supporting additional modes e.g.: sleeping, passive, active (depends on the sensor model). As a data source you can use **any object** that implements the **Stream class**, such as Wire, Serial, EthernetClient, e.t.c. + ## Basic example Read in active mode. -> Default mode is active after power up. In this mode sensor would send serial data to the host automatically. The active mode is divided into two sub-modes: stable mode and fast mode. If the concentration change is small the sensor would run at stable mode with the real interval of 2.3s. And if the change is big the sensor would be changed to fast mode automatically with the interval of 200~800ms, the higher of the concentration, the shorter of the interval. +> Default mode is active after power up. In this mode, the sensor would send serial data to the host automatically. The active mode is divided into two sub-modes: stable mode and fast mode. If the concentration change is small, the sensor would run at stable mode with the real interval of 2.3s. And if the change is big the sensor would be changed to fast mode automatically with the interval of 200~800ms, the higher of the concentration, the shorter of the interval. ```cpp #include "PMS.h" -PMS pms(Serial); +PMS pms(Serial1); // softwareserial is also ok. PMS::DATA data; void setup() { - Serial.begin(9600); // GPIO1, GPIO3 (TX/RX pin on ESP-12E Development Board) - Serial1.begin(9600); // GPIO2 (D4 pin on ESP-12E Development Board) + Serial.begin(9600); + Serial1.begin(9600); } void loop() { if (pms.read(data)) { - Serial1.print("PM 1.0 (ug/m3): "); - Serial1.println(data.PM_AE_UG_1_0); + Serial.print("PM 1.0 (ug/m3): "); + Serial.println(data.PM_AE_UG_1_0); - Serial1.print("PM 2.5 (ug/m3): "); - Serial1.println(data.PM_AE_UG_2_5); + Serial.print("PM 2.5 (ug/m3): "); + Serial.println(data.PM_AE_UG_2_5); - Serial1.print("PM 10.0 (ug/m3): "); - Serial1.println(data.PM_AE_UG_10_0); + Serial.print("PM 10.0 (ug/m3): "); + Serial.println(data.PM_AE_UG_10_0); - Serial1.println(); + Serial.println(); } // Do other stuff... @@ -62,43 +66,43 @@ Read in passive mode but not the best way (see additional remarks). ```cpp #include "PMS.h" -PMS pms(Serial); +PMS pms(Serial1); PMS::DATA data; void setup() { - Serial.begin(9600); // GPIO1, GPIO3 (TX/RX pin on ESP-12E Development Board) - Serial1.begin(9600); // GPIO2 (D4 pin on ESP-12E Development Board) + Serial.begin(9600); + Serial1.begin(9600); pms.passiveMode(); // Switch to passive mode } void loop() { - Serial1.println("Waking up, wait 30 seconds for stable readings..."); + Serial.println("Waking up, wait 30 seconds for stable readings..."); pms.wakeUp(); delay(30000); - Serial1.println("Send read request..."); + Serial.println("Send read request..."); pms.requestRead(); - Serial1.println("Reading data..."); + Serial.println("Reading data..."); if (pms.readUntil(data)) { - Serial1.print("PM 1.0 (ug/m3): "); - Serial1.println(data.PM_AE_UG_1_0); + Serial.print("PM 1.0 (ug/m3): "); + Serial.println(data.PM_AE_UG_1_0); - Serial1.print("PM 2.5 (ug/m3): "); - Serial1.println(data.PM_AE_UG_2_5); + Serial.print("PM 2.5 (ug/m3): "); + Serial.println(data.PM_AE_UG_2_5); - Serial1.print("PM 10.0 (ug/m3): "); - Serial1.println(data.PM_AE_UG_10_0); + Serial.print("PM 10.0 (ug/m3): "); + Serial.println(data.PM_AE_UG_10_0); } else { - Serial1.println("No data."); + Serial.println("No data."); } - Serial1.println("Going to sleep for 60 seconds."); + Serial.println("Going to sleep for 60 seconds."); pms.sleep(); delay(60000); } @@ -130,4 +134,4 @@ Please consider, that delay() function in examples is a blocking function. Try to avoid such a solution if your project requires it (see Expert.ino example in examples directory). For more accurate measurements, you can read several samples (in passive or active mode) and calculate the average. -> Stable data should be got at least 30 seconds after the sensor wakeup from the sleep mode because of the fan's performance. \ No newline at end of file +> Stable data should be got at least 30 seconds after the sensor wakeup from the sleep mode because of the fan's performance. diff --git a/examples/Advanced/Advanced.ino b/examples/Advanced/Advanced.ino index 6034a8f..79c96bf 100644 --- a/examples/Advanced/Advanced.ino +++ b/examples/Advanced/Advanced.ino @@ -1,6 +1,6 @@ #include "PMS.h" -PMS pms(Serial); +PMS pms(Serial1); PMS::DATA data; void setup() @@ -12,31 +12,31 @@ void setup() void loop() { - Serial1.println("Waking up, wait 30 seconds for stable readings..."); + Serial.println("Waking up, wait 30 seconds for stable readings..."); pms.wakeUp(); delay(30000); - Serial1.println("Send read request..."); + Serial.println("Send read request..."); pms.requestRead(); - Serial1.println("Wait max. 1 second for read..."); + Serial.println("Wait max. 1 second for read..."); if (pms.readUntil(data)) { - Serial1.print("PM 1.0 (ug/m3): "); - Serial1.println(data.PM_AE_UG_1_0); + Serial.print("PM 1.0 (ug/m3): "); + Serial.println(data.PM_AE_UG_1_0); - Serial1.print("PM 2.5 (ug/m3): "); - Serial1.println(data.PM_AE_UG_2_5); + Serial.print("PM 2.5 (ug/m3): "); + Serial.println(data.PM_AE_UG_2_5); - Serial1.print("PM 10.0 (ug/m3): "); - Serial1.println(data.PM_AE_UG_10_0); + Serial.print("PM 10.0 (ug/m3): "); + Serial.println(data.PM_AE_UG_10_0); } else { - Serial1.println("No data."); + Serial.println("No data."); } - Serial1.println("Going to sleep for 60 seconds."); + Serial.println("Going to sleep for 60 seconds."); pms.sleep(); delay(60000); -} \ No newline at end of file +} diff --git a/examples/Basic/Basic.ino b/examples/Basic/Basic.ino index b60f109..38ed898 100644 --- a/examples/Basic/Basic.ino +++ b/examples/Basic/Basic.ino @@ -1,6 +1,6 @@ #include "PMS.h" -PMS pms(Serial); +PMS pms(Serial1); PMS::DATA data; void setup() @@ -13,17 +13,17 @@ void loop() { if (pms.read(data)) { - Serial1.print("PM 1.0 (ug/m3): "); - Serial1.println(data.PM_AE_UG_1_0); + Serial.print("PM 1.0 (ug/m3): "); + Serial.println(data.PM_AE_UG_1_0); - Serial1.print("PM 2.5 (ug/m3): "); - Serial1.println(data.PM_AE_UG_2_5); + Serial.print("PM 2.5 (ug/m3): "); + Serial.println(data.PM_AE_UG_2_5); - Serial1.print("PM 10.0 (ug/m3): "); - Serial1.println(data.PM_AE_UG_10_0); + Serial.print("PM 10.0 (ug/m3): "); + Serial.println(data.PM_AE_UG_10_0); - Serial1.println(); + Serial.println(); } // Do other stuff... -} \ No newline at end of file +} diff --git a/examples/Expert/Expert.ino b/examples/Expert/Expert.ino index 6179419..fee2cda 100644 --- a/examples/Expert/Expert.ino +++ b/examples/Expert/Expert.ino @@ -4,7 +4,7 @@ // #define DEEP_SLEEP // GPIO2 (D4 pin on ESP-12E Development Board). -#define DEBUG_OUT Serial1 +#define DEBUG_OUT Serial // PMS_READ_INTERVAL (4:30 min) and PMS_READ_DELAY (30 sec) CAN'T BE EQUAL! Values are also used to detect sensor state. static const uint32_t PMS_READ_INTERVAL = 270000; @@ -13,12 +13,12 @@ static const uint32_t PMS_READ_DELAY = 30000; // Default sensor state. uint32_t timerInterval = PMS_READ_DELAY; -PMS pms(Serial); +PMS pms(Serial1); void setup() { // GPIO1, GPIO3 (TX/RX pin on ESP-12E Development Board) - Serial.begin(PMS::BAUD_RATE); + Serial1.begin(PMS::BAUD_RATE); DEBUG_OUT.begin(9600); // Switch to passive mode. @@ -73,7 +73,7 @@ void readData() PMS::DATA data; // Clear buffer (removes potentially old data) before read. Some data could have been also sent before switching to passive mode. - while (Serial.available()) { Serial.read(); } + while (Serial1.available()) { Serial.read(); } DEBUG_OUT.println("Send read request..."); pms.requestRead(); @@ -98,4 +98,4 @@ void readData() { DEBUG_OUT.println("No data."); } -} \ No newline at end of file +}