2018-01-27 16:52:48 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_18_pms5003.ino - PMS3003, PMS5003, PMS7003 particle concentration sensor support for Tasmota
|
2018-01-27 16:52:48 +00:00
|
|
|
|
2019-12-31 13:23:34 +00:00
|
|
|
Copyright (C) 2020 Theo Arends
|
2018-01-27 16:52:48 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_PMS5003
|
|
|
|
/*********************************************************************************************\
|
2019-10-16 11:10:48 +01:00
|
|
|
* PlanTower PMS3003, PMS5003, PMS7003 particle concentration sensor
|
|
|
|
* For background information see http://aqicn.org/sensor/pms5003-7003/ or
|
|
|
|
* http://aqicn.org/sensor/pms3003/
|
2018-05-10 16:21:26 +01:00
|
|
|
*
|
|
|
|
* Hardware Serial will be selected if GPIO3 = [PMS5003]
|
2019-10-16 11:10:48 +01:00
|
|
|
* You can either support PMS3003 or PMS5003-7003 at one time. To enable the PMS3003 support
|
|
|
|
* you must enable the define PMS_MODEL_PMS3003 on your configuration file.
|
2018-01-27 16:52:48 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_18 18
|
|
|
|
|
2018-01-27 16:52:48 +00:00
|
|
|
#include <TasmotaSerial.h>
|
|
|
|
|
2020-04-08 18:27:49 +01:00
|
|
|
#ifndef WARMUP_PERIOD
|
|
|
|
#define WARMUP_PERIOD 30 // Turn on PMSX003 XX-seconds before read in passive mode
|
|
|
|
#endif
|
|
|
|
|
2018-01-27 16:52:48 +00:00
|
|
|
TasmotaSerial *PmsSerial;
|
|
|
|
|
|
|
|
uint8_t pms_type = 1;
|
|
|
|
uint8_t pms_valid = 0;
|
|
|
|
|
2020-04-08 18:25:40 +01:00
|
|
|
enum PmsCommands
|
|
|
|
{
|
|
|
|
CMD_MODE_ACTIVE,
|
|
|
|
CMD_SLEEP,
|
|
|
|
CMD_WAKEUP,
|
|
|
|
CMD_MODE_PASSIVE,
|
|
|
|
CMD_READ_DATA
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t kPmsCommands[][7] PROGMEM = {
|
|
|
|
// 0 1 2 3 4 5 6
|
|
|
|
{0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71}, // pms_set_active_mode
|
|
|
|
{0x42, 0x4D, 0xE4, 0x00, 0x00, 0x01, 0x73}, // pms_sleep
|
|
|
|
{0x42, 0x4D, 0xE4, 0x00, 0x01, 0x01, 0x74}, // pms_wake
|
|
|
|
{0x42, 0x4D, 0xE1, 0x00, 0x00, 0x01, 0x70}, // pms_set_passive_mode
|
|
|
|
{0x42, 0x4D, 0xE2, 0x00, 0x00, 0x01, 0x71}}; // pms_passive_mode_read
|
|
|
|
|
2019-10-16 11:10:48 +01:00
|
|
|
struct pmsX003data {
|
2018-01-27 16:52:48 +00:00
|
|
|
uint16_t framelen;
|
|
|
|
uint16_t pm10_standard, pm25_standard, pm100_standard;
|
|
|
|
uint16_t pm10_env, pm25_env, pm100_env;
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
uint16_t reserved1, reserved2, reserved3;
|
|
|
|
#else
|
2018-01-27 16:52:48 +00:00
|
|
|
uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um, particles_100um;
|
|
|
|
uint16_t unused;
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-01-27 16:52:48 +00:00
|
|
|
uint16_t checksum;
|
|
|
|
} pms_data;
|
|
|
|
|
2018-05-10 16:21:26 +01:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2020-04-08 18:25:40 +01:00
|
|
|
size_t PmsSendCmd(uint8_t command_id)
|
|
|
|
{
|
|
|
|
return MhzSerial->write(kPmsCommands[command_id], sizeof(kPmsCommands[command_id]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool PmsReadData(void)
|
2018-01-27 16:52:48 +00:00
|
|
|
{
|
|
|
|
if (! PmsSerial->available()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while ((PmsSerial->peek() != 0x42) && PmsSerial->available()) {
|
|
|
|
PmsSerial->read();
|
|
|
|
}
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
if (PmsSerial->available() < 22) {
|
|
|
|
#else
|
2018-01-27 16:52:48 +00:00
|
|
|
if (PmsSerial->available() < 32) {
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-01-27 16:52:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
uint8_t buffer[22];
|
|
|
|
PmsSerial->readBytes(buffer, 22);
|
|
|
|
#else
|
2018-01-27 16:52:48 +00:00
|
|
|
uint8_t buffer[32];
|
|
|
|
PmsSerial->readBytes(buffer, 32);
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
|
|
|
uint16_t sum = 0;
|
2018-01-27 16:52:48 +00:00
|
|
|
PmsSerial->flush(); // Make room for another burst
|
|
|
|
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, 22);
|
|
|
|
#else
|
2019-01-17 16:48:34 +00:00
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, 32);
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-02-13 13:30:30 +00:00
|
|
|
|
2018-01-27 16:52:48 +00:00
|
|
|
// get checksum ready
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
for (uint32_t i = 0; i < 20; i++) {
|
|
|
|
#else
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 30; i++) {
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-01-27 16:52:48 +00:00
|
|
|
sum += buffer[i];
|
|
|
|
}
|
|
|
|
// The data comes in endian'd, this solves it so it works on all platforms
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
uint16_t buffer_u16[10];
|
|
|
|
for (uint32_t i = 0; i < 10; i++) {
|
|
|
|
#else
|
2018-01-27 16:52:48 +00:00
|
|
|
uint16_t buffer_u16[15];
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 15; i++) {
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-01-27 16:52:48 +00:00
|
|
|
buffer_u16[i] = buffer[2 + i*2 + 1];
|
|
|
|
buffer_u16[i] += (buffer[2 + i*2] << 8);
|
|
|
|
}
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
if (sum != buffer_u16[9]) {
|
|
|
|
#else
|
2018-01-27 16:52:48 +00:00
|
|
|
if (sum != buffer_u16[14]) {
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-01-27 16:52:48 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("PMS: " D_CHECKSUM_FAILURE));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
memcpy((void *)&pms_data, (void *)buffer_u16, 20);
|
|
|
|
#else
|
2018-01-27 16:52:48 +00:00
|
|
|
memcpy((void *)&pms_data, (void *)buffer_u16, 30);
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-01-27 16:52:48 +00:00
|
|
|
pms_valid = 10;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PmsSecond(void) // Every second
|
2018-01-27 16:52:48 +00:00
|
|
|
{
|
|
|
|
if (PmsReadData()) {
|
|
|
|
pms_valid = 10;
|
|
|
|
} else {
|
|
|
|
if (pms_valid) {
|
|
|
|
pms_valid--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PmsInit(void)
|
2018-01-27 16:52:48 +00:00
|
|
|
{
|
|
|
|
pms_type = 0;
|
2020-04-08 18:22:32 +01:00
|
|
|
if ((pin[GPIO_PMS5003_RX] < 99) && (pin[GPIO_PMS5003_TX] < 99))
|
|
|
|
{
|
|
|
|
PmsSerial = new TasmotaSerial(pin[GPIO_PMS5003_RX], pin[GPIO_PMS5003_TX], 1);
|
2018-05-10 16:21:26 +01:00
|
|
|
if (PmsSerial->begin(9600)) {
|
|
|
|
if (PmsSerial->hardwareSerial()) { ClaimSerial(); }
|
2018-01-27 16:52:48 +00:00
|
|
|
pms_type = 1;
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 18:22:32 +01:00
|
|
|
else if ((pin[GPIO_PMS5003_RX] < 99))
|
|
|
|
{
|
|
|
|
PmsSerial = new TasmotaSerial(pin[GPIO_PMS5003_RX], -1, 1);
|
|
|
|
if (PmsSerial->begin(9600))
|
|
|
|
{
|
|
|
|
if (PmsSerial->hardwareSerial())
|
|
|
|
{
|
|
|
|
ClaimSerial();
|
|
|
|
}
|
|
|
|
pms_type = 1;
|
|
|
|
}
|
|
|
|
}
|
2018-01-27 16:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
const char HTTP_PMS3003_SNS[] PROGMEM =
|
|
|
|
// "{s}PMS3003 " D_STANDARD_CONCENTRATION " 1 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
// "{s}PMS3003 " D_STANDARD_CONCENTRATION " 2.5 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
// "{s}PMS3003 " D_STANDARD_CONCENTRATION " 10 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
"{s}PMS3003 " D_ENVIRONMENTAL_CONCENTRATION " 1 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
"{s}PMS3003 " D_ENVIRONMENTAL_CONCENTRATION " 2.5 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
"{s}PMS3003 " D_ENVIRONMENTAL_CONCENTRATION " 10 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}";
|
|
|
|
#else
|
2019-03-19 16:31:43 +00:00
|
|
|
const char HTTP_PMS5003_SNS[] PROGMEM =
|
2018-01-28 11:42:42 +00:00
|
|
|
// "{s}PMS5003 " D_STANDARD_CONCENTRATION " 1 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
// "{s}PMS5003 " D_STANDARD_CONCENTRATION " 2.5 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
// "{s}PMS5003 " D_STANDARD_CONCENTRATION " 10 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
"{s}PMS5003 " D_ENVIRONMENTAL_CONCENTRATION " 1 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
"{s}PMS5003 " D_ENVIRONMENTAL_CONCENTRATION " 2.5 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
"{s}PMS5003 " D_ENVIRONMENTAL_CONCENTRATION " 10 " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
|
|
|
|
"{s}PMS5003 " D_PARTICALS_BEYOND " 0.3 " D_UNIT_MICROMETER "{m}%d " D_UNIT_PARTS_PER_DECILITER "{e}"
|
|
|
|
"{s}PMS5003 " D_PARTICALS_BEYOND " 0.5 " D_UNIT_MICROMETER "{m}%d " D_UNIT_PARTS_PER_DECILITER "{e}"
|
|
|
|
"{s}PMS5003 " D_PARTICALS_BEYOND " 1 " D_UNIT_MICROMETER "{m}%d " D_UNIT_PARTS_PER_DECILITER "{e}"
|
|
|
|
"{s}PMS5003 " D_PARTICALS_BEYOND " 2.5 " D_UNIT_MICROMETER "{m}%d " D_UNIT_PARTS_PER_DECILITER "{e}"
|
|
|
|
"{s}PMS5003 " D_PARTICALS_BEYOND " 5 " D_UNIT_MICROMETER "{m}%d " D_UNIT_PARTS_PER_DECILITER "{e}"
|
|
|
|
"{s}PMS5003 " D_PARTICALS_BEYOND " 10 " D_UNIT_MICROMETER "{m}%d " D_UNIT_PARTS_PER_DECILITER "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-01-27 16:52:48 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void PmsShow(bool json)
|
2018-01-27 16:52:48 +00:00
|
|
|
{
|
|
|
|
if (pms_valid) {
|
|
|
|
if (json) {
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
ResponseAppend_P(PSTR(",\"PMS3003\":{\"CF1\":%d,\"CF2.5\":%d,\"CF10\":%d,\"PM1\":%d,\"PM2.5\":%d,\"PM10\":%d}"),
|
|
|
|
pms_data.pm10_standard, pms_data.pm25_standard, pms_data.pm100_standard,
|
|
|
|
pms_data.pm10_env, pms_data.pm25_env, pms_data.pm100_env);
|
|
|
|
#else
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"PMS5003\":{\"CF1\":%d,\"CF2.5\":%d,\"CF10\":%d,\"PM1\":%d,\"PM2.5\":%d,\"PM10\":%d,\"PB0.3\":%d,\"PB0.5\":%d,\"PB1\":%d,\"PB2.5\":%d,\"PB5\":%d,\"PB10\":%d}"),
|
2018-01-27 16:52:48 +00:00
|
|
|
pms_data.pm10_standard, pms_data.pm25_standard, pms_data.pm100_standard,
|
|
|
|
pms_data.pm10_env, pms_data.pm25_env, pms_data.pm100_env,
|
|
|
|
pms_data.particles_03um, pms_data.particles_05um, pms_data.particles_10um, pms_data.particles_25um, pms_data.particles_50um, pms_data.particles_100um);
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-03-12 13:56:48 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-04-11 09:11:20 +01:00
|
|
|
if (0 == tele_period) {
|
|
|
|
DomoticzSensor(DZ_COUNT, pms_data.pm10_env); // PM1
|
|
|
|
DomoticzSensor(DZ_VOLTAGE, pms_data.pm25_env); // PM2.5
|
|
|
|
DomoticzSensor(DZ_CURRENT, pms_data.pm100_env); // PM10
|
|
|
|
}
|
2018-03-12 13:56:48 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
2018-01-27 16:52:48 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-10-27 10:13:24 +00:00
|
|
|
|
2019-10-16 11:10:48 +01:00
|
|
|
#ifdef PMS_MODEL_PMS3003
|
|
|
|
WSContentSend_PD(HTTP_PMS3003_SNS,
|
|
|
|
// pms_data.pm10_standard, pms_data.pm25_standard, pms_data.pm100_standard,
|
|
|
|
pms_data.pm10_env, pms_data.pm25_env, pms_data.pm100_env);
|
|
|
|
#else
|
|
|
|
WSContentSend_PD(HTTP_PMS5003_SNS,
|
2018-01-28 11:42:42 +00:00
|
|
|
// pms_data.pm10_standard, pms_data.pm25_standard, pms_data.pm100_standard,
|
2018-01-27 16:52:48 +00:00
|
|
|
pms_data.pm10_env, pms_data.pm25_env, pms_data.pm100_env,
|
|
|
|
pms_data.particles_03um, pms_data.particles_05um, pms_data.particles_10um, pms_data.particles_25um, pms_data.particles_50um, pms_data.particles_100um);
|
2019-10-16 11:10:48 +01:00
|
|
|
#endif // PMS_MODEL_PMS3003
|
2018-01-27 16:52:48 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns18(uint8_t function)
|
2018-01-27 16:52:48 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-01-27 16:52:48 +00:00
|
|
|
|
|
|
|
if (pms_type) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
PmsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
PmsSecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
PmsShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2018-01-27 16:52:48 +00:00
|
|
|
PmsShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_PMS5003
|