Skip to content

Commit adcb539

Browse files
committed
initial commit
1 parent 8306b29 commit adcb539

File tree

10 files changed

+484
-0
lines changed

10 files changed

+484
-0
lines changed

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
language: python
2+
python:
3+
- "2.7"
4+
5+
# Cache PlatformIO packages using Travis CI container-based infrastructure
6+
sudo: false
7+
cache:
8+
directories:
9+
- "~/.platformio"
10+
11+
env:
12+
- PLATFORMIO_CI_SRC=examples/sds011-mqtt/sds011-mqtt.ino PLATFORMIO_CI_EXTRA_ARGS="--board=d1_mini"
13+
- CPPLINT=true
14+
15+
install:
16+
- pip install -U platformio
17+
- platformio lib -g install 346
18+
- pip install -U cpplint
19+
20+
script:
21+
- if [[ "$CPPLINT" ]]; then cpplint --repository=. --recursive --linelength=200 --filter=-build/include ./src; else platformio ci --lib="." $PLATFORMIO_CI_EXTRA_ARGS; fi
22+
23+
notifications:
24+
email:
25+
on_success: change
26+
on_failure: change

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# SDS011
2+
3+
[![Build Status](https://travis-ci.com/bertmelis/SDS011.svg?branch=master)](https://travis-ci.com/bertmelis/SDS011)
4+
5+
SDS011 particle matter sensor library for the Arduino framework for ESP8266.
6+
Not tested, but should also work on ESP32
7+
8+
This is yet another SDS011 library, this time completely non blocling. It does come with a `loop()`-method to poll the serial port.
9+
10+
## Installation
11+
12+
* For Arduino IDE: see [the Arduino Guide](https://www.arduino.cc/en/Guide/Libraries#toc4)
13+
14+
~~For Platformio: see the [Platfomio guide](http://docs.platformio.org/en/latest/projectconf/section_env_library.html)~~ Not yet registered
15+
16+
## Usage
17+
18+
You cannot use Serial on ESP8266 as there's only 1 full UART available which will be used by the sensor.
19+
20+
To do something useful you can combine this lib with MQTT (like [async-mqtt-client](https://github.com/marvinroger/async-mqtt-client)) as in the example below.
21+
22+
```C++
23+
#include <ESP8266WiFi.h>
24+
#include <Ticker.h>
25+
#include <AsyncMqttClient.h>
26+
#include <SDS011.h>
27+
28+
const char SSID[] = "My_WiFi";
29+
const char PASS[] = "My_Pass";
30+
const IPAddress BROKER = {192, 168, 1, 10};
31+
32+
SDS011 sds011;
33+
AsyncMqttClient mqttClient;
34+
Ticker mqttReconnectTimer;
35+
36+
WiFiEventHandler wifiConnectHandler;
37+
WiFiEventHandler wifiDisconnectHandler;
38+
Ticker wifiReconnectTimer;
39+
40+
bool connected = false;
41+
42+
void connectToWifi() {
43+
WiFi.begin(SSID, PASS);
44+
}
45+
46+
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
47+
connectToMqtt();
48+
}
49+
50+
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
51+
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
52+
wifiReconnectTimer.once(2, connectToWifi);
53+
}
54+
55+
void connectToMqtt() {
56+
mqttClient.connect();
57+
}
58+
59+
void onMqttConnected(bool sessionPresent) {
60+
connected = true;
61+
}
62+
63+
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
64+
connected = false;
65+
if (WiFi.isConnected()) {
66+
mqttReconnectTimer.once(2, connectToMqtt);
67+
}
68+
}
69+
70+
void setup() {
71+
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
72+
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
73+
74+
mqttClient.onConnect(onMqttConnected);
75+
mqttClient.onDisconnect(onMqttDisconnect);
76+
mqttClient.setServer(BROKER, 1883);
77+
78+
sds011.setup(&Serial);
79+
sds011.onData([](float pm25Value, float pm10Value) {
80+
if (connected) {
81+
mqttClient.publish("/SENSOR/PM2_5", 1, false, String(pm25Value, 1).c_str());
82+
mqttClient.publish("/SENSOR/PM10", 1, false, String(pm10Value, 1).c_str());
83+
}
84+
});
85+
sds011.onResponse([](){
86+
// command has been executed
87+
});
88+
sds011.onError([](int8_t error){
89+
// error happened
90+
// -1: CRC error
91+
});
92+
sds011.setWorkingPeriod(5);
93+
94+
connectToWifi();
95+
}
96+
97+
void loop() {
98+
sds011.loop();
99+
}
100+
101+
```
102+
103+
## To Do
104+
105+
- adjust readings based on humidity
106+
- possible timeout after sending a command
107+
- implement missing commands
108+
- multiple sensors (apparently the firmware supports this. However, they implemented hardware using 1-1 UART) ???
109+
110+
## Issues, improvements?
111+
112+
Please create a ticket or pull request.
628 KB
Binary file not shown.
828 KB
Binary file not shown.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <ESP8266WiFi.h>
2+
#include <Ticker.h>
3+
#include <AsyncMqttClient.h>
4+
#include <SDS011.h>
5+
6+
const char SSID[] = "My_WiFi";
7+
const char PASS[] = "My_Pass";
8+
const IPAddress BROKER = {192, 168, 1, 10};
9+
10+
SDS011 sds011;
11+
AsyncMqttClient mqttClient;
12+
Ticker mqttReconnectTimer;
13+
14+
WiFiEventHandler wifiConnectHandler;
15+
WiFiEventHandler wifiDisconnectHandler;
16+
Ticker wifiReconnectTimer;
17+
18+
bool connected = false;
19+
20+
void connectToWifi() {
21+
WiFi.begin(SSID, PASS);
22+
}
23+
24+
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
25+
connectToMqtt();
26+
}
27+
28+
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
29+
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
30+
wifiReconnectTimer.once(2, connectToWifi);
31+
}
32+
33+
void connectToMqtt() {
34+
mqttClient.connect();
35+
}
36+
37+
void onMqttConnected(bool sessionPresent) {
38+
connected = true;
39+
}
40+
41+
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
42+
connected = false;
43+
if (WiFi.isConnected()) {
44+
mqttReconnectTimer.once(2, connectToMqtt);
45+
}
46+
}
47+
48+
void setup() {
49+
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
50+
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
51+
52+
mqttClient.onConnect(onMqttConnected);
53+
mqttClient.onDisconnect(onMqttDisconnect);
54+
mqttClient.setServer(BROKER, 1883);
55+
56+
sds011.setup(&Serial);
57+
sds011.onData([](float pm25Value, float pm10Value) {
58+
if (connected) {
59+
mqttClient.publish("/SENSOR/PM2_5", 1, false, String(pm25Value, 1).c_str());
60+
mqttClient.publish("/SENSOR/PM10", 1, false, String(pm10Value, 1).c_str());
61+
}
62+
});
63+
sds011.onResponse([](){
64+
// command has been executed
65+
});
66+
sds011.onError([](int8_t error){
67+
// error happened
68+
// -1: CRC error
69+
});
70+
sds011.setWorkingPeriod(5);
71+
72+
connectToWifi();
73+
}
74+
75+
void loop() {
76+
sds011.loop();
77+
}

keywords.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#######################################
2+
# Datatypes (KEYWORD1)
3+
#######################################
4+
5+
SDS011 KEYWORD1
6+
7+
#######################################
8+
# Methods and Functions (KEYWORD2)
9+
#######################################
10+
11+
setup KEYWORD2
12+
onData KEYWORD2
13+
onResponse KEYWORD2
14+
onError KEYWORD2
15+
setReportMode KEYWORD2
16+
setWorkingMode KEYWORD2
17+
setWorkingPeriod KEYWORD2
18+
queryData KEYWORD2
19+
loop KEYWORD2
20+
21+
#######################################
22+
# Constants (LITERAL1)
23+
#######################################
24+
25+
#yourLITERAL LITERAL1

library.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "DHT",
3+
"version": "0.0.1",
4+
"keywords": "SDS011, dust, sensor, Arduino, ESP8266",
5+
"description": "Non blocking SDS011 sensor library for ESP8266",
6+
"homepage": "https://github.com/bertmelis/SDS011",
7+
"license": "MIT",
8+
"authors": {
9+
"name": "Bert Melis",
10+
"url": "https://github.com/bertmelis",
11+
"maintainer": true
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/bertmelis/SDS011.git",
16+
"branch": "master"
17+
},
18+
"frameworks": "arduino",
19+
"platforms": [
20+
"espressif8266"
21+
]
22+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SDS011
2+
version=0.0.1
3+
author=Bert Melis
4+
maintainer=Bert Melis
5+
sentence=Non blocking SDS011 sensor library for ESP8266
6+
paragraph=
7+
category=Sensors
8+
url=https://github.com/bertmelis/SDS011
9+
architectures=esp8266

0 commit comments

Comments
 (0)