Skip to content

Commit 2fd5399

Browse files
authored
Merge pull request #4 from lewapek/feature/hardwareSerial
Adding support for HardwareSerial [fixes #3]
2 parents 056b1d5 + 60e26bd commit 2fd5399

File tree

5 files changed

+149
-23
lines changed

5 files changed

+149
-23
lines changed

README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Nova Fitness SDS dust sensors arduino library
22
Supports Nova Fitness SDS011 however should also work for other Nova Fitness SDS sensors as well.
33
This library attempts to provide easy-to-use abstraction over [Laser Dust Sensor Control Protocol V1.3](https://cdn.sparkfun.com/assets/parts/1/2/2/7/5/Laser_Dust_Sensor_Control_Protocol_V1.3.pdf).
4+
Each response coming from sensor is validated whether it has correct head, command id, checksum and tail.
45

56
## Quickstart
67
```
@@ -37,11 +38,36 @@ void loop() {
3738
delay(500);
3839
}
3940
```
40-
4141
For more examples see [examples](examples/) folder.
4242

43+
## Initilization
44+
Communication with sensor can be handled by SoftwareSerial or HardwareSerial. You can pass SoftwareSerial or HardwareSerial directly to the constructor or provide rx & tx pins (library will use SoftwareSerial then).
45+
46+
### Using tx and rx pins
47+
Communication will be implicitly handled by SoftwareSerial.
48+
```
49+
int rxPin = D1;
50+
int txPin = D2;
51+
SdsDustSensor sds(rxPin, txPin);
52+
sds.begin(); // you can pass custom baud rate as parameter (9600 by default)
53+
```
54+
55+
### Explicit SoftwareSerial
56+
```
57+
int rxPin = D1;
58+
int txPin = D2;
59+
SoftwareSerial softwareSerial(rxPin, txPin);
60+
SdsDustSensor sds(softwareSerial);
61+
sds.begin(); // you can pass custom baud rate as parameter (9600 by default)
62+
```
63+
64+
### Explicit HardwareSerial
65+
```
66+
SdsDustSensor sds(Serial1); // passing HardwareSerial as parameter
67+
sds.begin(); // you can pass custom baud rate as parameter (9600 by default)
68+
```
69+
4370
## Supported operations
44-
Each response coming from sensor is validated whether it has correct head, command id, checksum and tail. Communication with sensor is handled via SoftwareSerial.
4571
All operations listed in [Laser Dust Sensor Control Protocol V1.3](https://cdn.sparkfun.com/assets/parts/1/2/2/7/5/Laser_Dust_Sensor_Control_Protocol_V1.3.pdf) are fully supported. They are listed below:
4672
* read PM2.5, PM10 values (reads data from software serial, does not write anything to the serial, all other operations write date to the sensor and expect response - sensor should be set to "active" reporting mode),
4773
* query PM2.5, PM10 values (in opposite to above, this one sends command to sensor and it responds with PM2.5 and PM10 values - sensor should be set to "query" reporting mode),
@@ -59,12 +85,6 @@ All operations listed in [Laser Dust Sensor Control Protocol V1.3](https://cdn.s
5985

6086
Additionally you can read device id from every sensor response.
6187

62-
### Setting up the sensor
63-
```
64-
SdsDustSensor sds(rxPin, txPin);
65-
sds.begin();
66-
```
67-
6888
### Reading PM2.5 and PM10 values
6989
The following function (readPm()) checks whether there is available data sent from sensor, it does not send any request to sensor so it has to be in 'active' reporting mode.
7090
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "SdsDustSensor.h"
2+
3+
// tested on Arduino Leonardo with Serial1
4+
SdsDustSensor sds(Serial1); // passing HardwareSerial& as parameter
5+
6+
void setup() {
7+
Serial.begin(9600);
8+
sds.begin(); // this line will begin Serial1 with given baud rate (9600 by default)
9+
10+
Serial.println(sds.queryFirmwareVersion().toString()); // prints firmware version
11+
Serial.println(sds.setQueryReportingMode().toString()); // ensures sensor is in 'query' reporting mode
12+
}
13+
14+
void loop() {
15+
sds.wakeup();
16+
delay(30000); // working 30 seconds
17+
18+
PmResult pm = sds.queryPm();
19+
if (pm.isOk()) {
20+
Serial.print("PM2.5 = ");
21+
Serial.print(pm.pm25);
22+
Serial.print(", PM10 = ");
23+
Serial.println(pm.pm10);
24+
25+
// if you want to just print the measured values, you can use toString() method as well
26+
Serial.println(pm.toString());
27+
} else {
28+
Serial.print("Could not read values from sensor, reason: ");
29+
Serial.println(pm.statusToString());
30+
}
31+
32+
WorkingStateResult state = sds.sleep();
33+
if (state.isWorking()) {
34+
Serial.println("Problem with sleeping the sensor.");
35+
} else {
36+
Serial.println("Sensor is sleeping");
37+
delay(60000); // wait 1 minute
38+
}
39+
}

src/SdsDustSensor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "SdsDustSensor.h"
22

33
void SdsDustSensor::write(const Command &command) {
4-
sdsSerial->flush();
4+
sdsStream->flush();
55
for (int i = 0; i < Command::length; ++i) {
6-
sdsSerial->write(command.bytes[i]);
6+
sdsStream->write(command.bytes[i]);
77
#ifdef __DEBUG_SDS_DUST_SENSOR__
88
Serial.print("|");
99
Serial.print(command.bytes[i], HEX);
@@ -19,9 +19,9 @@ Status SdsDustSensor::readIntoBytes(byte responseId) {
1919
int checksum = 0;
2020
int readBytesQuantity = 0;
2121

22-
while ((sdsSerial->available() > 0) &&
23-
(sdsSerial->available() >= Result::lenght - readBytesQuantity)) {
24-
byte readByte = sdsSerial->read();
22+
while ((sdsStream->available() > 0) &&
23+
(sdsStream->available() >= Result::lenght - readBytesQuantity)) {
24+
byte readByte = sdsStream->read();
2525
response[readBytesQuantity] = readByte;
2626

2727
#ifdef __DEBUG_SDS_DUST_SENSOR__

src/SdsDustSensor.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,36 @@
2828

2929
#include "SdsDustSensorCommands.h"
3030
#include "SdsDustSensorResults.h"
31-
#include <SoftwareSerial.h>
31+
#include "Serials.h"
3232

3333
// #define __DEBUG_SDS_DUST_SENSOR__
3434

3535
class SdsDustSensor {
3636
public:
37-
SdsDustSensor(int pinRx, int pinTx, int baudRate = 9600):
38-
sdsSerial(new SoftwareSerial(pinRx, pinTx)),
39-
baudRate(baudRate) {}
37+
SdsDustSensor(int pinRx, int pinTx):
38+
abstractSerial(new Serials::InternalSoftware(pinRx, pinTx)) {
39+
sdsStream = abstractSerial->getStream();
40+
}
41+
42+
SdsDustSensor(SoftwareSerial &softwareSerial):
43+
abstractSerial(new Serials::Software(softwareSerial)) {
44+
sdsStream = abstractSerial->getStream();
45+
}
46+
47+
SdsDustSensor(HardwareSerial &hardwareSerial):
48+
abstractSerial(new Serials::Hardware(hardwareSerial)) {
49+
sdsStream = abstractSerial->getStream();
50+
}
4051

4152
~SdsDustSensor() {
42-
if (sdsSerial != NULL) {
43-
delete sdsSerial;
53+
if (abstractSerial != NULL) {
54+
abstractSerial->release();
55+
delete abstractSerial;
4456
}
4557
}
4658

47-
void begin() {
48-
sdsSerial->begin(baudRate);
59+
void begin(int baudRate = 9600) {
60+
abstractSerial->begin(baudRate);
4961
}
5062

5163
byte *getLastResponse() {
@@ -131,8 +143,8 @@ class SdsDustSensor {
131143

132144
private:
133145
byte response[Result::lenght];
134-
int baudRate;
135-
SoftwareSerial *sdsSerial = NULL;
146+
Stream *sdsStream = NULL;
147+
Serials::AbstractSerial *abstractSerial;
136148
};
137149

138150
#endif // __SDS_DUST_SENSOR_H__

src/Serials.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef __SDS_ABSTRACT_SERIAL_H__
2+
#define __SDS_ABSTRACT_SERIAL_H__
3+
4+
#include <SoftwareSerial.h>
5+
#include <HardwareSerial.h>
6+
7+
namespace Serials {
8+
9+
struct AbstractSerial {
10+
virtual void begin(int baudRate) ;
11+
virtual Stream *getStream();
12+
virtual void release() {
13+
// do nothing by default
14+
}
15+
};
16+
17+
struct Hardware: public AbstractSerial {
18+
Hardware(HardwareSerial &serial): serial(serial) {}
19+
20+
void begin(int baudRate) {
21+
serial.begin(baudRate);
22+
}
23+
24+
Stream *getStream() {
25+
return &serial;
26+
}
27+
28+
HardwareSerial &serial;
29+
};
30+
31+
struct Software: public AbstractSerial {
32+
Software(SoftwareSerial &serial): serial(serial) {}
33+
34+
void begin(int baudRate) {
35+
serial.begin(baudRate);
36+
}
37+
38+
Stream *getStream() {
39+
return &serial;
40+
}
41+
42+
SoftwareSerial &serial;
43+
};
44+
45+
struct InternalSoftware: public Software {
46+
InternalSoftware(const int &pinRx, const int &pinTx): Software(*(new SoftwareSerial(pinRx, pinTx))) {}
47+
48+
void release() {
49+
delete &serial;
50+
}
51+
};
52+
53+
}
54+
55+
#endif // __SDS_ABSTRACT_SERIAL_H__

0 commit comments

Comments
 (0)