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

Added basic init and read methos for Mhz19 sensor #33

Merged
merged 22 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
af4fea3
fixed some ignore files isssues
hpsaturn Nov 16, 2020
2c13399
added PM1 print on main for PM1.0 on Panasonic sensor
hpsaturn Nov 16, 2020
eca169b
Merge branch 'master' of github.com:kike-canaries/canairio_sensorlib
hpsaturn Nov 30, 2020
42d2027
Merge pull request #30 from kike-canaries/devel
hpsaturn Dec 16, 2020
d368947
Merge branch 'master' of github.com:kike-canaries/canairio_sensorlib
hpsaturn Dec 16, 2020
5dd78b7
fixed some issue and library list missing
hpsaturn Dec 16, 2020
83dfc43
Merge branch 'master' of github.com:kike-canaries/canairio_sensorlib
hpsaturn Dec 16, 2020
0e9d26b
v0.1.6 support for DHT, am2320, sht31, aht10, bme280 sensors
hpsaturn Dec 16, 2020
1d2699b
Added basic init for Mhz19 sensor
hpsaturn Dec 29, 2020
af2bcde
Some minor changes, CO2humi added for other sensor
danielbernalb Dec 29, 2020
0534d23
Create Test
danielbernalb Dec 30, 2020
8d644fd
Add files via upload
danielbernalb Dec 30, 2020
00887a5
Added MHZ19 example
danielbernalb Dec 30, 2020
cdbc33a
Delete commented line
danielbernalb Dec 30, 2020
ed581eb
New CO2 mhz19 example
danielbernalb Dec 30, 2020
56f8bea
Minor change
danielbernalb Dec 30, 2020
eba521c
Minor changes
danielbernalb Dec 30, 2020
2d42464
New directory co2 mhz19 example
danielbernalb Dec 30, 2020
13a57ed
Example for arduino
danielbernalb Dec 30, 2020
05f4dda
Merge branch 'db/co2mhz19example' into db/co2mhz19
danielbernalb Dec 30, 2020
189cf01
Minor change example platformio
danielbernalb Dec 30, 2020
6c699d4
Merge pull request #34 from danielbernalb/db/co2mhz19
hpsaturn Dec 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Particle meter (PM) sensor manager for multiple (PM) sensors: Honeywell, Plantow
- [x] Debugging compatible with ESP32 log level
- [x] Added bme280, aht10, sht31, am2320 i2c sensors
- [x] Exposed public sub-libraries objects, sps30, aht10, etc.
- [ ] Added old DHT sensors
- [x] Added old DHT sensors
- [ ] BME680 support (from TTGO-T7 CanAirIO version)

## Usage
Expand Down Expand Up @@ -77,7 +77,7 @@ void loop() {

```javascript
void setup() {
sensors.init(0,RX,TX); // generic sensor(default), custom RX, custom TX pines.
sensors.init(sensors.Auto,RX,TX); // generic sensor(default), custom RX, custom TX pines.
}
```

Expand Down Expand Up @@ -119,10 +119,15 @@ pio run --target upload

You first need to run the examples, install **arduino-cli** or the **Arduino IDE** with the following libraries:

Adafruit Unified Sensor
Adafruit AM2320 sensor library
[Sensirion library sps30](https://github.com/paulvha/sps30)
`CanAirIO Air Quality Sensors Library` (this library).
adafruit/Adafruit Unified Sensor @ 1.1.4
adafruit/Adafruit AM2320 sensor library @ 1.1.4
adafruit/Adafruit BME280 Library @ 2.1.2
adafruit/Adafruit BusIO @ 1.6.0
adafruit/Adafruit SHT31 Library @ 2.0.0
enjoyneering/AHT10 @ ^1.1.0
[DTH_nonblocking](https://github.com/hpsaturn/DHT_nonblocking.git)
[Sensirion library sps30](https://github.com/paulvha/sps30) @ 1.4.9
`CanAirIO Air Quality Sensors Library` (this library).

Also you need to add the **alternative links** for supporting the ESP32 boards:

Expand Down
1 change: 1 addition & 0 deletions examples/basic/basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void onSensorDataOk() {
Serial.print("-->[MAIN] PM1.0: "+sensors.getStringPM1());
Serial.print (" PM2.5: " + sensors.getStringPM25());
Serial.println(" PM10: " + sensors.getStringPM10());
Serial.println(" PM1: " + sensors.getStringPM1());
}

void onSensorDataError(const char * msg){
Expand Down
71 changes: 71 additions & 0 deletions examples/co2_mhz19/co2_mhz19.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @file main.cpp
* @author Antonio Vanegas @hpsaturn
* @date June 2018 - 2020
* @brief Particle meter sensor tests
* @license GPL3
*
* Full documentation:
* https://github.com/kike-canaries/canairio_sensorlib#canairio-air-quality-sensors-library
*
* Full implementation for WiFi and Bluetooth Air Quality fixed and mobile station:
* https://github.com/kike-canaries/canairio_firmware#canairio-firmware
*
* CanAirIO project:
* https://canair.io
*/

#include <Arduino.h>
#include <Sensors.hpp>

void onSensorDataOk() {
Serial.print ("-->[MAIN] PM1: "+sensors.getStringPM1());
Serial.print (" PM2.5: " + sensors.getStringPM25());
Serial.print (" PM10: " + sensors.getStringPM10());

Serial.print (" CO2: " + sensors.getStringCO2());
Serial.print (" CO2humi: " + String(sensors.getCO2humi()));
Serial.print (" CO2temp: " + String(sensors.getCO2temp()));

Serial.print (" H: "+ String(sensors.getHumidity()));
Serial.println (" T: " + String(sensors.getTemperature()));

}

void onSensorDataError(const char * msg){
Serial.println(msg);
}

/******************************************************************************
* M A I N
******************************************************************************/

void setup() {
Serial.begin(115200);
delay(200);
Serial.println("\n== Sensor test setup ==\n");

Serial.println("-->[SETUP] Detecting sensors..");

sensors.setSampleTime(5); // config sensors sample time interval
sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback
sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback
sensors.setDebugMode(true); // [optional] debug mode

// sensors.init(); // Auto detection of PM sensors (Honeywell, Plantower, Panasonic)
// sensors.init(sensors.Auto); // Auto detection of PM sensors (Honeywell, Plantower, Panasonic)
// sensors.init(sensors.Panasonic); // Force detection to Panasonic sensor
// sensors.init(sensors.Sensirion); // Force detection to Sensirion sensor
// sensors.init(sensors.Auto,mRX,mTX); // Auto detection and custom RX, TX pines
// sensors.init(sensors.Auto,PMS_RX,PMS_TX); // Auto detection, custom RX,TX and custom DHT config
sensors.init(sensors.Mhz19);

if(sensors.isPmSensorConfigured())
Serial.println("-->[SETUP] Sensor configured: " + sensors.getPmDeviceSelected());

delay(500);
}

void loop() {
sensors.loop(); // read sensor data and showed it
}
34 changes: 34 additions & 0 deletions examples/co2_mhz19/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
src_dir = .

[common_env_data]
framework = arduino
upload_speed = 1500000
monitor_speed = 115200
lib_deps =
hpsaturn/CanAirIO Air Quality Sensors Library

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = ${common_env_data.framework}
upload_speed = ${common_env_data.upload_speed}
monitor_speed = ${common_env_data.monitor_speed}
lib_deps = ${common_env_data.lib_deps}

[env:esp8266]
platform = espressif8266
board = esp12e
framework = ${common_env_data.framework}
monitor_speed = ${common_env_data.monitor_speed}
lib_deps = ${common_env_data.lib_deps}
6 changes: 3 additions & 3 deletions examples/platformio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ void onSensorDataOk() {
Serial.print ("-->[MAIN] PM1.0: "+sensors.getStringPM1());
Serial.print (" PM2.5: " + sensors.getStringPM25());
Serial.print (" PM10: " + sensors.getStringPM10());
Serial.print (" PM1: " + sensors.getStringPM1());
Serial.print (" T: " + String(sensors.getTemperature()));
Serial.println(" H: "+ String(sensors.getHumidity()));
Serial.print (" H: "+ String(sensors.getHumidity()));
Serial.println (" T: " + String(sensors.getTemperature()));

}

void onSensorDataError(const char * msg){
Expand Down
6 changes: 4 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CanAirIO Air Quality Sensors Library",
"version": "0.1.5",
"version": "0.1.6",
"homepage":"https://canair.io",
"keywords":
[
Expand Down Expand Up @@ -41,7 +41,9 @@
".travis.yml",
".gitignore",
"releases",
"examples/platformio/.vscode"
"examples/platformio/.vscode",
"examples/platformio/platformio.ini.default",
"examples/platformio/platformio.ini.devel"
]
},
"authors":
Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ lib_deps =
enjoyneering/AHT10 @ ^1.1.0
https://github.com/hpsaturn/DHT_nonblocking.git
paulvha/sps30 @ 1.4.9
wifwaf/MH-Z19 @ ^1.5.3
; hpsaturn/CanAirIO Air Quality Sensors Library

[esp32_common]
Expand Down
60 changes: 56 additions & 4 deletions src/Sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void Sensors::init(int pms_type, int pms_rx, int pms_tx) {

DEBUG("-->[SENSORS] sample time set to: ",String(sample_time).c_str());

if(!pmSensorInit(pms_type, pms_rx, pms_tx)){
if(!sensorSerialInit(pms_type, pms_rx, pms_tx)){
DEBUG("-->[E][PMSENSOR] init failed!");
}

Expand Down Expand Up @@ -131,6 +131,24 @@ String Sensors::getStringPM10() {
return String(output);
}

uint16_t Sensors::getCO2() {
return CO2;
}

String Sensors::getStringCO2() {
char output[5];
sprintf(output, "%04d", getCO2());
return String(output);
}

float Sensors::getCO2humi() {
return CO2humi;
}

float Sensors::getCO2temp() {
return CO2temp;
}

float Sensors::getHumidity() {
return humi;
}
Expand Down Expand Up @@ -269,6 +287,16 @@ bool Sensors::pmSensirionRead() {
return true;
}

bool Sensors:: pmMhz19Read() {
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm)
CO2temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius)
if(CO2>0){
DEBUG("-->[MHZ14-9] read > done!");
return true;
}
return false;
}

/**
* @brief read sensor data. Sensor selected.
* @return true if data is loaded from sensor
Expand All @@ -287,6 +315,12 @@ bool Sensors::pmSensorRead() {
return pmSensirionRead();
break;

case Mhz19:
return pmMhz19Read();
break;



default:
return false;
break;
Expand Down Expand Up @@ -385,7 +419,7 @@ void Sensors::pmSensirionErrorloop(char *mess, uint8_t r) {
* @param pms_rx PMS RX pin.
* @param pms_tx PMS TX pin.
**/
bool Sensors::pmSensorInit(int pms_type, int pms_rx, int pms_tx) {
bool Sensors::sensorSerialInit(int pms_type, int pms_rx, int pms_tx) {
// set UART for autodetection sensors (Honeywell, Plantower, Panasonic)
if (pms_type == Auto) {
DEBUG("-->[PMSENSOR] detecting Generic PM sensor..");
Expand All @@ -400,6 +434,10 @@ bool Sensors::pmSensorInit(int pms_type, int pms_rx, int pms_tx) {
DEBUG("-->[PMSENSOR] detecting Sensirion PM sensor..");
if(!serialInit(pms_type, 115200, pms_rx, pms_tx))return false;
}
else if (pms_type == Mhz19) {
DEBUG("-->[PMSENSOR] detecting Mhz19 sensor..");
if(!serialInit(pms_type, 9600, pms_rx, pms_tx))return false;
}

// starting auto detection loop
int try_sensor_init = 0;
Expand Down Expand Up @@ -432,6 +470,14 @@ bool Sensors::pmSensorAutoDetect(int pms_type) {
return true;
}
}

if (pms_type == Mhz19) {
if (pmMhz19Init()) {
device_selected = "MHZ19";
device_type = Mhz19;
return true;
}
}

if (pms_type <= Panasonic) {
if (pmGenericRead()) {
Expand All @@ -451,6 +497,12 @@ bool Sensors::pmSensorAutoDetect(int pms_type) {
return false;
}

bool Sensors:: pmMhz19Init() {
myMHZ19.begin(*_serial); // *Serial(Stream) refence must be passed to library begin().
myMHZ19.autoCalibration(); // Turn auto calibration ON (OFF autoCalibration(false))
return true;
}

bool Sensors::pmSensirionInit() {
// Begin communication channel
DEBUG("-->[SPS30] starting SPS30 sensor..");
Expand Down Expand Up @@ -557,8 +609,8 @@ void Sensors::dhtInit() {

/// Print some sensors values
void Sensors::printValues() {
char output[100];
sprintf(output, "PM1:%03d PM25:%03d PM10:%03d H:%03f%% T:%03f°C", pm1, pm25, pm10, humi, temp);
char output[200];
sprintf(output, "PM1:%03d PM25:%03d PM10:%03d CO2:%04d CO2humi:%03f%% CO2temp:%03f°C H:%03f%% T:%03f°C", pm1, pm25, pm10, CO2, CO2humi, CO2temp, humi, temp);
DEBUG("-->[SENSORS]", output);
}

Expand Down
22 changes: 19 additions & 3 deletions src/Sensors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <AHT10.h>
#include <Adafruit_SHT31.h>
#include <dht_nonblocking.h>
#include <MHZ19.h>
#include <sps30.h>
using namespace std;
#include <vector>
Expand Down Expand Up @@ -50,7 +51,7 @@ class Sensors {
public:

/// Supported devices. Auto is for Honeywell and Plantower sensors and similars
enum SENSOR_TYPE { Auto, Panasonic, Sensirion };
enum SENSOR_TYPE { Auto, Panasonic, Sensirion, Mhz19 };

/// SPS30 values. Only for Sensirion SPS30 sensor.
struct sps_values val;
Expand All @@ -73,6 +74,8 @@ class Sensors {
Adafruit_SHT31 sht31;
// DHT sensor
float dhthumi, dhttemp;
// Mhz19 sensor
MHZ19 myMHZ19;

void init(int pms_type = 0, int pms_rx = PMS_RX, int pms_tx = PMS_TX);
void loop();
Expand All @@ -90,7 +93,11 @@ class Sensors {
uint16_t getPM25();
uint16_t getPM4();
uint16_t getPM10();

uint16_t getCO2();

float getCO2humi();
float getCO2temp();

float getTemperature();
float getHumidity();
float getPressure();
Expand All @@ -107,6 +114,8 @@ class Sensors {
String getStringPM25();
String getStringPM4();
String getStringPM10();
String getStringCO2();
String getStringCO2temp();

private:

Expand Down Expand Up @@ -135,6 +144,10 @@ class Sensors {
float pres = 0.0; // Pressure
float alt = 0.0;
float gas = 0.0;

uint16_t CO2; // CO2 in ppm
float CO2humi = 0.0; // temperature of the CO2 sensor
float CO2temp = 0.0; // temperature of the CO2 sensor

void restart();
void am2320Init();
Expand All @@ -145,19 +158,22 @@ class Sensors {
void aht10Read();
void sht31Init();
void sht31Read();

void dhtInit();
void dhtRead();
bool dhtIsReady(float *temperature, float *humidity);

bool pmSensorInit(int pms_type, int rx, int tx);
bool sensorSerialInit(int pms_type, int rx, int tx);
bool pmSensorAutoDetect(int pms_type);
bool pmSensorRead();
bool pmGenericRead();
bool pmPanasonicRead();
bool pmSensirionRead();
bool pmMhz19Read();
void onPmSensorError(const char *msg);
void printValues();
bool pmSensirionInit();
bool pmMhz19Init();
void pmSensirionErrtoMess(char *mess, uint8_t r);
void pmSensirionErrorloop(char *mess, uint8_t r);
void getSensirionDeviceInfo();
Expand Down