Compare commits
2 Commits
bd25dc9b7c
...
1693a84cf3
Author | SHA1 | Date |
---|---|---|
interfisch | 1693a84cf3 | |
interfisch | 53471a9c8a |
|
@ -0,0 +1,90 @@
|
||||||
|
// High/Low Output LDR Sensor
|
||||||
|
// For example: RCWL-0516 (needs 5v input (gnd, vin), 3.3v output level. high for 2seconds when movement detected)
|
||||||
|
#include "sensor_ldr.h"
|
||||||
|
|
||||||
|
|
||||||
|
Sensor_LDR::Sensor_LDR(int p)
|
||||||
|
{
|
||||||
|
pin=p;
|
||||||
|
sensordata data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensor_LDR::init() //Things to be done during setup()
|
||||||
|
{
|
||||||
|
Serial.println("initializing LDR");
|
||||||
|
pinMode(pin, INPUT);
|
||||||
|
analogRead(pin); //first reading could be false
|
||||||
|
init_ok=true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Also called during setup()
|
||||||
|
void Sensor_LDR::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||||
|
{
|
||||||
|
data.minchange=minchange;
|
||||||
|
data.senddelaymax=senddelaymax;
|
||||||
|
data.readdelay=readdelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Called during setup
|
||||||
|
void Sensor_LDR::advertise(HomieNode& p_sensorNode)
|
||||||
|
{
|
||||||
|
sensorNode = &p_sensorNode;
|
||||||
|
sensorNode->advertise("light");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensor_LDR::sensorloop()
|
||||||
|
{
|
||||||
|
if (init_ok) {
|
||||||
|
sensordata &d=data;
|
||||||
|
|
||||||
|
bool _changed=false;
|
||||||
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||||
|
d.value = get_lux(in_ldr, out_ldr, LDR_ARRAYSIZE)/10.0; //read light level in lux
|
||||||
|
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
||||||
|
_changed=true;
|
||||||
|
}
|
||||||
|
d.lastreadtime=millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||||
|
Serial.print("Sending LDR. reason=");
|
||||||
|
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||||
|
|
||||||
|
Homie.getLogger() << "light " << ": " << d.value << endl;
|
||||||
|
sensorNode->setProperty("light").send(String(d.value));
|
||||||
|
d.lastsentvalue=d.value;
|
||||||
|
|
||||||
|
d.lastsent=millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Calculate lux based on rawADC reading from LDR returns value in lux/10
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//quelle: https://groups.google.com/forum/#!topic/souliss/1kMAltPB2ME[1-25]
|
||||||
|
int Sensor_LDR::get_lux(const unsigned int* _in, const unsigned int* _out, byte size)
|
||||||
|
{
|
||||||
|
// take care the value is within range
|
||||||
|
// val = constrain(val, _in[0], _in[size-1]);
|
||||||
|
unsigned int val = analogRead(pin);
|
||||||
|
#ifdef DEBUG //DEBUG++++++++++++++++
|
||||||
|
Serial.print("LDR RAW=: ");
|
||||||
|
Serial.println(val);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (val <= _in[0]) return _out[0];
|
||||||
|
if (val >= _in[size-1]) return _out[size-1];
|
||||||
|
|
||||||
|
// search right interval
|
||||||
|
byte pos = 1; // _in[0] allready tested
|
||||||
|
while(val > _in[pos]) pos++;
|
||||||
|
|
||||||
|
// this will handle all exact "points" in the _in array
|
||||||
|
if (val == _in[pos]) return _out[pos];
|
||||||
|
|
||||||
|
// interpolate in the right segment for the rest
|
||||||
|
return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
#ifndef SENSOR_LDR_H
|
||||||
|
#define SENSOR_LDR_H
|
||||||
|
|
||||||
|
#include "sensordata.h"
|
||||||
|
#include <Homie.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Sensor_LDR
|
||||||
|
{
|
||||||
|
|
||||||
|
private:
|
||||||
|
HomieNode *sensorNode; //reference to HomieNode
|
||||||
|
|
||||||
|
int pin;
|
||||||
|
|
||||||
|
struct sensordata data; //struct values are changed in setup()
|
||||||
|
|
||||||
|
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//wemos d1 mini, black wire of ldr connects to A0 with 10k to gnd. red wire connects with 1k to gnd and 2k2 to 3v3
|
||||||
|
#ifdef SENSOR_LDR_CALIB_1
|
||||||
|
#define LDRARRAYSIZE 18
|
||||||
|
const unsigned int out_ldr[18] = {0, 30, 50, 60, 130, 170, 250, 420, 780, 1300,2600, 5000, 5350, 7700, 10900, 12000, 17000,20000}; // x10 (i.e. gets later divided by 10)
|
||||||
|
const unsigned int in_ldr[18] = {0, 12, 100, 150, 350, 400, 450, 650, 730, 780, 840, 930, 948 , 970, 993, 1005, 1019, 1023}; // 0 - 1023
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool init_ok;
|
||||||
|
public:
|
||||||
|
Sensor_LDR(int p);
|
||||||
|
|
||||||
|
|
||||||
|
void init();
|
||||||
|
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
|
||||||
|
void advertise(HomieNode& p_sensorNode);
|
||||||
|
void sensorloop();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -0,0 +1,169 @@
|
||||||
|
#include "sensor_mhz19b.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Sensor_MHZ19B::Sensor_MHZ19B(int prx,int ptx)
|
||||||
|
{
|
||||||
|
pin_rx=prx;
|
||||||
|
pin_tx=ptx;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* MHZ19 Library: https://platformio.org/lib/show/1620/SevSegSPI
|
||||||
|
* Software Serial Library: https://platformio.org/lib/show/168/EspSoftwareSerial
|
||||||
|
|
||||||
|
*/
|
||||||
|
// SW Serial
|
||||||
|
//SW Serial RX: to mhz19 tx (green cable)
|
||||||
|
//SW Serial TX: to mhz19 rx (blue cable)
|
||||||
|
//co2 sensor needs 5v. Maybe better to Connect USB 5V directly (not through wemos d1 onboard diode which gives only 4.7V! at '5V' output)
|
||||||
|
|
||||||
|
//if ABC is disabled (see in setup function) sensor should be calibrated manually. leave outdoors (=400ppm) with no direct sunlight for >20min, then connect HD pin to GND for at least 7 seconds.
|
||||||
|
/* Pinout (view from top, connector at the bottom)
|
||||||
|
* Vin, GND, NC, PWM
|
||||||
|
* | | | |
|
||||||
|
* /-----------------\
|
||||||
|
* | |
|
||||||
|
* | |
|
||||||
|
* | |
|
||||||
|
* | |
|
||||||
|
* \-----------------/
|
||||||
|
* | | | | |
|
||||||
|
* Vo Rx Tx NC HD
|
||||||
|
*
|
||||||
|
* [Connector]
|
||||||
|
*/
|
||||||
|
|
||||||
|
//value will be in [ppm]
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensor_MHZ19B::init() //Things to be done during setup()
|
||||||
|
{
|
||||||
|
Serial.println("initializing MHZ19B");
|
||||||
|
mhz19_swSerial->begin(BAUD_RATE_MHZ19, SWSERIAL_8N1, pin_rx, pin_tx, false, 256);
|
||||||
|
mhz19->setSerial(mhz19_swSerial);
|
||||||
|
|
||||||
|
uint8_t mhz19abctries=10;
|
||||||
|
while(!mhz19->disableABC() && mhz19abctries>0) { //disable automatic baseline correction (abc does calibration every 24h -> needs to have 400ppm co2 level sometime during that time)
|
||||||
|
delay(500); //wait some time for mhz to be initialized
|
||||||
|
Serial.print("disableABC Failed! try="); Serial.println(mhz19abctries);
|
||||||
|
mhz19abctries--;
|
||||||
|
}
|
||||||
|
if (mhz19abctries>0) {
|
||||||
|
Serial.println("mhz19 abc disabled successfully");
|
||||||
|
init_ok=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Also called during setup()
|
||||||
|
void Sensor_MHZ19B::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||||
|
{
|
||||||
|
data.minchange=minchange;
|
||||||
|
data.senddelaymax=senddelaymax;
|
||||||
|
data.readdelay=readdelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Sensor_MHZ19B::mhz19calibrationHandler(const HomieRange& range, const String& value) {
|
||||||
|
if (range.isRange) {
|
||||||
|
return false; //if range is given but index is not in allowed range
|
||||||
|
}
|
||||||
|
Homie.getLogger() << "mhz19 calibration " << ": " << value << endl;
|
||||||
|
|
||||||
|
if (value=="zero") {
|
||||||
|
mhz19->calibrateZero();
|
||||||
|
Homie.getLogger() << "mhz19 calibration " << ": " << value << endl;
|
||||||
|
#ifdef STATUSNODE
|
||||||
|
sensorNode->setProperty("status").send("MHZ19 Zero Calibration triggered");
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
Homie.getLogger() << "Value outside range" << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Called during setup
|
||||||
|
void Sensor_MHZ19B::advertise(HomieNode& p_sensorNode)
|
||||||
|
{
|
||||||
|
sensorNode = &p_sensorNode;
|
||||||
|
sensorNode->advertise("co2");
|
||||||
|
#ifdef MHZ19CALIBRATIONTOPIC
|
||||||
|
sensorNode->advertise("mhz19calibration").settable(&Sensor_MHZ19B::mhz19calibrationHandler)); //not working!!! TODO: Fix it
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensor_MHZ19B::sensorloop()
|
||||||
|
{
|
||||||
|
if (init_ok) {
|
||||||
|
sensordata &d=data;
|
||||||
|
|
||||||
|
bool _changed=false;
|
||||||
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||||
|
mhz19_ready=mhz19->isReady();
|
||||||
|
//d.value=mhz19->readValue(); //[ppm]
|
||||||
|
d.value=mhz19_readValue_reimplemented(mhz19_swSerial, mhz19); //[ppm] reimplemented function to fix no response issue
|
||||||
|
Homie.getLogger() << "read co2 " << ": " << d.value << " status=" << mhz19_ready << endl;
|
||||||
|
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
||||||
|
_changed=true;
|
||||||
|
}
|
||||||
|
d.lastreadtime=millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||||
|
Serial.print("Sending MHZ19. reason=");
|
||||||
|
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||||
|
|
||||||
|
Homie.getLogger() << "co2 " << ": " << d.value << endl;
|
||||||
|
if (mhz19_ready){ //send no co2 values if not warmed up. can take several miniutes
|
||||||
|
sensorNode->setProperty("co2").send(String(d.value));
|
||||||
|
}else{
|
||||||
|
Homie.getLogger() << "co2 not ready. didnt sent" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
d.lastsentvalue=d.value;
|
||||||
|
|
||||||
|
d.lastsent=millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
byte Sensor_MHZ19B::mhz19_getCheckSum(byte* packet) {
|
||||||
|
byte checksum = 0;
|
||||||
|
for(uint8_t i = 1; i < 8; i++) {
|
||||||
|
checksum += packet[i];
|
||||||
|
}
|
||||||
|
checksum = 0xff - checksum;
|
||||||
|
checksum += 1;
|
||||||
|
return checksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Sensor_MHZ19B::mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz19Ref) { //same function as in mhz19 library from klevytskyi, but with delay between cmd send and response check
|
||||||
|
byte CMD_READ[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; // Read command
|
||||||
|
unsigned int co2 = -1;
|
||||||
|
unsigned char response[9];
|
||||||
|
_streamRef->write(CMD_READ, 9);
|
||||||
|
|
||||||
|
unsigned long _startwait=millis();
|
||||||
|
while (millis()-_startwait<100) { //wait for mhz19 to send response
|
||||||
|
//wait
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_streamRef->available()) {
|
||||||
|
_streamRef->readBytes(response, 9);
|
||||||
|
|
||||||
|
byte crc = mhz19_getCheckSum(response);
|
||||||
|
|
||||||
|
if (response[0] == 0xFF && response[1] == CMD_READ[2] && response[8] == crc) {
|
||||||
|
unsigned int responseHigh = (unsigned int) response[2];
|
||||||
|
unsigned int responseLow = (unsigned int) response[3];
|
||||||
|
unsigned int ppm = (256*responseHigh) + responseLow;
|
||||||
|
co2 = ppm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return co2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
#ifndef SENSOR_MHZ19B_H
|
||||||
|
#define SENSOR_MHZ19B_H
|
||||||
|
|
||||||
|
#include "sensordata.h"
|
||||||
|
#include <Homie.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#include <MHZ19.h>
|
||||||
|
|
||||||
|
|
||||||
|
class Sensor_MHZ19B
|
||||||
|
{
|
||||||
|
|
||||||
|
private:
|
||||||
|
HomieNode *sensorNode; //reference to HomieNode
|
||||||
|
|
||||||
|
struct sensordata data; //struct values are changed in setup()
|
||||||
|
|
||||||
|
int pin_rx;
|
||||||
|
int pin_tx;
|
||||||
|
|
||||||
|
bool init_ok;
|
||||||
|
|
||||||
|
int mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz19Ref);
|
||||||
|
byte mhz19_getCheckSum(byte* packet);
|
||||||
|
|
||||||
|
|
||||||
|
bool mhz19_ready;
|
||||||
|
|
||||||
|
MHZ19 *mhz19;
|
||||||
|
SoftwareSerial *mhz19_swSerial;
|
||||||
|
#define BAUD_RATE_MHZ19 9600
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//#define MHZ19CALIBRATIONTOPIC //TODO: fix it
|
||||||
|
|
||||||
|
public:
|
||||||
|
Sensor_MHZ19B(int prx, int ptx);
|
||||||
|
|
||||||
|
void init();
|
||||||
|
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
|
||||||
|
void advertise(HomieNode& p_sensorNode);
|
||||||
|
void sensorloop();
|
||||||
|
|
||||||
|
bool mhz19calibrationHandler(const HomieRange& range, const String& value);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -82,8 +82,6 @@ monitor_port = /dev/ttyUSB0
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
build_flags =
|
build_flags =
|
||||||
-D SENSOR_HCSR501
|
|
||||||
-D SENSOR_HCSR501_PIN=D0
|
|
||||||
|
|
||||||
|
|
||||||
-D SENSOR_RADAR
|
-D SENSOR_RADAR
|
||||||
|
@ -91,6 +89,10 @@ build_flags =
|
||||||
-D SENSOR_RADAR_readdelay=100
|
-D SENSOR_RADAR_readdelay=100
|
||||||
-D SENSOR_RADAR_senddelaymax=1000*60*10
|
-D SENSOR_RADAR_senddelaymax=1000*60*10
|
||||||
|
|
||||||
|
-D SENSOR_LDR
|
||||||
|
-D SENSOR_LDR_PIN=A0
|
||||||
|
-D SENSOR_LDR_CALIB_1
|
||||||
|
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
|
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
|
||||||
|
@ -119,11 +121,10 @@ build_flags =
|
||||||
-D SENSOR_HCSR501_PIN=D6
|
-D SENSOR_HCSR501_PIN=D6
|
||||||
|
|
||||||
-D SENSOR_LDR
|
-D SENSOR_LDR
|
||||||
-D SENSOR_LDR_CALIB1
|
-D SENSOR_LDR_CALIB_1
|
||||||
-D LDR_PIN=A0
|
-D SENSOR_LDR_PIN=A0
|
||||||
-D dataLDR_minchange=10.0
|
-D SENSOR_LDR_minchange=10.0
|
||||||
-D dataLDR_readdelay=1000*2
|
-D SENSOR_LDR_senddelaymax=1000*60*1
|
||||||
-D dataLDR_senddelaymax=1000*60*1
|
|
||||||
|
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
|
@ -143,11 +144,11 @@ monitor_port = /dev/ttyUSB0
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
build_flags =
|
build_flags =
|
||||||
-D SENSOR_MHZ19
|
-D SENSOR_MHZ19B
|
||||||
-D MHZ19_SERIAL_RX=D5
|
-D SENSOR_MHZ19B_SERIAL_RX=D5
|
||||||
-D MHZ19_SERIAL_TX=D6
|
-D SENSOR_MHZ19B_SERIAL_TX=D6
|
||||||
-D dataMHZ19_minchange=10
|
-D SENSOR_MHZ19B_minchange=10
|
||||||
-D dataMHZ19_readdelay=10*1000
|
-D SENSOR_MHZ19B_readdelay=10*1000
|
||||||
|
|
||||||
-D SENSOR_SDS018
|
-D SENSOR_SDS018
|
||||||
-D SDS018_SERIAL_RX=D7
|
-D SDS018_SERIAL_RX=D7
|
||||||
|
@ -202,7 +203,7 @@ monitor_speed = 115200
|
||||||
|
|
||||||
build_flags =
|
build_flags =
|
||||||
-D SENSOR_DHT22
|
-D SENSOR_DHT22
|
||||||
-D DHTPIN=D7
|
-D SENSOR_DHT22_PIN=D7
|
||||||
-D dataDHT22_temperature_minchange=0.2
|
-D dataDHT22_temperature_minchange=0.2
|
||||||
-D dataDHT22_humidity_minchange=1.0
|
-D dataDHT22_humidity_minchange=1.0
|
||||||
|
|
||||||
|
@ -213,11 +214,11 @@ build_flags =
|
||||||
-D dataBH1750_minchange=10.0
|
-D dataBH1750_minchange=10.0
|
||||||
-D dataBH1750_senddelaymax=1000*60*2
|
-D dataBH1750_senddelaymax=1000*60*2
|
||||||
|
|
||||||
-D SENSOR_MHZ19
|
-D SENSOR_MHZ19B
|
||||||
-D MHZ19_SERIAL_RX=D5
|
-D SENSOR_MHZ19B_SERIAL_RX=D5
|
||||||
-D MHZ19_SERIAL_TX=D6
|
-D SENSOR_MHZ19B_SERIAL_TX=D6
|
||||||
-D dataMHZ19_minchange=10
|
-D SENSOR_MHZ19B_minchange=10
|
||||||
-D dataMHZ19_readdelay=10*1000
|
-D SENSOR_MHZ19B_readdelay=10*1000
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
adafruit/DHT sensor library@1.3.10
|
adafruit/DHT sensor library@1.3.10
|
||||||
|
|
307
src/main.cpp
307
src/main.cpp
|
@ -173,67 +173,33 @@
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
#ifdef SENSOR_LDR
|
||||||
struct sensordata dataLDR;
|
#include "sensor_ldr.cpp"
|
||||||
float value_ldr=0;
|
Sensor_LDR sensor_ldr(SENSOR_LDR_PIN);
|
||||||
#ifdef SENSOR_LDR_CALIB1
|
|
||||||
#define LDRARRAYSIZE 18
|
#ifndef SENSOR_LDR_minchange
|
||||||
//black wire of ldr connects to A0 with 10k to gnd. red wire connects with 1k to gnd and 2k2 to 3v3
|
#define SENSOR_LDR_minchange 10.0
|
||||||
static const unsigned int out_ldr[] = {0, 30, 50, 60, 130, 170, 250, 420, 780, 1300,2600, 5000, 5350, 7700, 10900, 12000, 17000,20000}; // x10 (i.e. gets later divided by 10)
|
#endif
|
||||||
static const unsigned int in_ldr[] = {0, 12, 100, 150, 350, 400, 450, 650, 730, 780, 840, 930, 948 , 970, 993, 1005, 1019, 1023}; // 0 - 1023
|
#ifndef SENSOR_LDR_senddelaymax
|
||||||
|
#define SENSOR_LDR_senddelaymax 1000*60*1
|
||||||
|
#endif
|
||||||
|
#ifndef SENSOR_LDR_readdelay
|
||||||
|
#define SENSOR_LDR_readdelay 1000*2
|
||||||
#endif
|
#endif
|
||||||
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size); //for analog ldr light calculation
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_MHZ19
|
#ifdef SENSOR_MHZ19B
|
||||||
struct sensordata dataMHZ19;
|
#include "sensor_mhz19b.cpp"
|
||||||
/*
|
Sensor_MHZ19B sensor_mhz19b(SENSOR_MHZ19B_SERIAL_RX,SENSOR_MHZ19B_SERIAL_TX);
|
||||||
* MHZ19 Library: https://platformio.org/lib/show/1620/SevSegSPI
|
|
||||||
* Software Serial Library: https://platformio.org/lib/show/168/EspSoftwareSerial
|
|
||||||
|
|
||||||
*/
|
#ifndef SENSOR_MHZ19B_minchange
|
||||||
// SW Serial
|
#define SENSOR_MHZ19B_minchange 10.0
|
||||||
//SW Serial RX: to mhz19 tx (green cable)
|
|
||||||
//SW Serial TX: to mhz19 rx (blue cable)
|
|
||||||
//co2 sensor needs 5v. Maybe better to Connect USB 5V directly (not through wemos d1 onboard diode which gives only 4.7V! at '5V' output)
|
|
||||||
|
|
||||||
//if ABC is disabled (see in setup function) sensor should be calibrated manually. leave outdoors (=400ppm) with no direct sunlight for >20min, then connect HD pin to GND for at least 7 seconds.
|
|
||||||
/* Pinout (view from top, connector at the bottom)
|
|
||||||
* Vin, GND, NC, PWM
|
|
||||||
* | | | |
|
|
||||||
* /-----------------\
|
|
||||||
* | |
|
|
||||||
* | |
|
|
||||||
* | |
|
|
||||||
* | |
|
|
||||||
* \-----------------/
|
|
||||||
* | | | | |
|
|
||||||
* Vo Rx Tx NC HD
|
|
||||||
*
|
|
||||||
* [Connector]
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef SOFTWARESERIAL_H
|
|
||||||
#include <SoftwareSerial.h>
|
|
||||||
#define SOFTWARESERIAL_H
|
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef SENSOR_MHZ19B_senddelaymax
|
||||||
SoftwareSerial mhz19_swSerial;
|
#define SENSOR_MHZ19B_senddelaymax 1000*60*10
|
||||||
#define BAUD_RATE_MHZ19 9600
|
#endif
|
||||||
|
#ifndef SENSOR_MHZ19B_readdelay
|
||||||
#define MHZ19CALIBRATIONTOPIC
|
#define SENSOR_MHZ19B_readdelay 1000*10
|
||||||
|
|
||||||
#include <MHZ19.h>
|
|
||||||
MHZ19 mhz19;
|
|
||||||
bool mhz19_ready=false;
|
|
||||||
int value_co2=-1; //[ppm]
|
|
||||||
|
|
||||||
int mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz19Ref); //declare function
|
|
||||||
|
|
||||||
#ifdef MHZ19CALIBRATIONTOPIC
|
|
||||||
bool mhz19calibrationHandler(const HomieRange& range, const String& value);
|
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -406,41 +372,13 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
#ifdef SENSOR_LDR
|
||||||
Serial.println("initializing ldr");
|
sensor_ldr.init();
|
||||||
pinMode(LDR_PIN, INPUT); //ldr
|
sensor_ldr.setSettings(SENSOR_LDR_minchange,SENSOR_LDR_senddelaymax,SENSOR_LDR_readdelay);
|
||||||
#ifdef dataLDR_readdelay
|
|
||||||
dataLDR.readdelay=dataLDR_readdelay;
|
|
||||||
#endif
|
|
||||||
#ifdef dataLDR_senddelaymax
|
|
||||||
dataLDR.senddelaymax=dataLDR_senddelaymax;
|
|
||||||
#endif
|
|
||||||
#ifdef dataLDR_minchange
|
|
||||||
dataLDR.minchange=dataLDR_minchange;
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_MHZ19
|
#ifdef SENSOR_MHZ19B
|
||||||
Serial.println("initializing mhz19");
|
sensor_mhz19b.init();
|
||||||
#ifdef dataMHZ19_minchange
|
sensor_mhz19b.setSettings(SENSOR_MHZ19B_minchange,SENSOR_MHZ19B_senddelaymax,SENSOR_MHZ19B_readdelay);
|
||||||
dataMHZ19.minchange=dataMHZ19_minchange;
|
|
||||||
#endif
|
|
||||||
#ifdef dataMHZ19_readdelay
|
|
||||||
dataMHZ19.readdelay=dataMHZ19_readdelay;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mhz19_swSerial.begin(BAUD_RATE_MHZ19, SWSERIAL_8N1, MHZ19_SERIAL_RX, MHZ19_SERIAL_TX, false, 256);
|
|
||||||
mhz19.setSerial(&mhz19_swSerial);
|
|
||||||
|
|
||||||
uint8_t mhz19abctries=10;
|
|
||||||
while(!mhz19.disableABC() && mhz19abctries>0) { //disable automatic baseline correction (abc does calibration every 24h -> needs to have 400ppm co2 level sometime during that time)
|
|
||||||
delay(500); //wait some time for mhz to be initialized
|
|
||||||
Serial.print("disableABC Failed! try="); Serial.println(mhz19abctries);
|
|
||||||
mhz19abctries--;
|
|
||||||
}
|
|
||||||
if (mhz19abctries>0) {
|
|
||||||
Serial.println("mhz19 abc disabled successfully");
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -570,21 +508,11 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
#ifdef SENSOR_LDR
|
||||||
sensorNode.advertise("light");
|
sensor_ldr.advertise(sensorNode);
|
||||||
analogRead(LDR_PIN); //first reading could be false
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef SENSOR_MHZ19B
|
||||||
|
sensor_mhz19b.advertise(sensorNode);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_MHZ19
|
|
||||||
sensorNode.advertise("co2");
|
|
||||||
#ifdef MHZ19CALIBRATIONTOPIC
|
|
||||||
sensorNode.advertise("mhz19calibration").settable(mhz19calibrationHandler);
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_SDS018
|
#ifdef SENSOR_SDS018
|
||||||
|
@ -627,72 +555,6 @@ void loop() {
|
||||||
Homie.loop();
|
Homie.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
|
||||||
void loop_LDR()
|
|
||||||
{
|
|
||||||
sensordata &d=dataLDR;
|
|
||||||
|
|
||||||
bool _changed=false;
|
|
||||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
||||||
value_ldr = get_lux(in_ldr, out_ldr, LDRARRAYSIZE)/10.0; //read light level in lux
|
|
||||||
if (fabs(d.lastsentvalue-value_ldr)>=d.minchange){
|
|
||||||
_changed=true;
|
|
||||||
}
|
|
||||||
d.lastreadtime=millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
|
||||||
Serial.print("Sending LDR. reason=");
|
|
||||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
|
||||||
checkESPStatus();
|
|
||||||
|
|
||||||
Homie.getLogger() << "light " << ": " << value_ldr << endl;
|
|
||||||
sensorNode.setProperty("light").send(String(value_ldr));
|
|
||||||
d.lastsentvalue=value_ldr;
|
|
||||||
|
|
||||||
d.lastsent=millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_MHZ19
|
|
||||||
void loop_MHZ19()
|
|
||||||
{
|
|
||||||
sensordata &d=dataMHZ19;
|
|
||||||
|
|
||||||
bool _changed=false;
|
|
||||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
||||||
mhz19_ready=mhz19.isReady();
|
|
||||||
//value_co2=mhz19.readValue(); //[ppm]
|
|
||||||
value_co2=mhz19_readValue_reimplemented(&mhz19_swSerial, &mhz19); //[ppm] reimplemented function to fix no response issue
|
|
||||||
Homie.getLogger() << "read co2 " << ": " << value_co2 << " status=" << mhz19_ready << endl;
|
|
||||||
if (fabs(d.lastsentvalue-value_co2)>=d.minchange){
|
|
||||||
_changed=true;
|
|
||||||
}
|
|
||||||
d.lastreadtime=millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
|
||||||
Serial.print("Sending MHZ19. reason=");
|
|
||||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
|
||||||
checkESPStatus();
|
|
||||||
|
|
||||||
Homie.getLogger() << "co2 " << ": " << value_co2 << endl;
|
|
||||||
if (mhz19_ready){ //send no co2 values if not warmed up. can take several miniutes
|
|
||||||
sensorNode.setProperty("co2").send(String(value_co2));
|
|
||||||
}else{
|
|
||||||
Homie.getLogger() << "co2 not ready. didnt sent" << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
d.lastsentvalue=value_co2;
|
|
||||||
|
|
||||||
d.lastsent=millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SENSOR_SDS018
|
#ifdef SENSOR_SDS018
|
||||||
void loop_SDS018_pm25()
|
void loop_SDS018_pm25()
|
||||||
{
|
{
|
||||||
|
@ -1045,14 +907,11 @@ void loopHandler() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
#ifdef SENSOR_LDR
|
||||||
loop_LDR();
|
sensor_ldr.sensorloop();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef SENSOR_MHZ19B
|
||||||
|
sensor_mhz19b.sensorloop();
|
||||||
|
|
||||||
#ifdef SENSOR_MHZ19
|
|
||||||
loop_MHZ19();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_SDS018
|
#ifdef SENSOR_SDS018
|
||||||
|
@ -1093,83 +952,6 @@ void checkESPStatus()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Calculate lux based on rawADC reading from LDR returns value in lux/10
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
//quelle: https://groups.google.com/forum/#!topic/souliss/1kMAltPB2ME[1-25]
|
|
||||||
#ifdef SENSOR_LDR
|
|
||||||
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size)
|
|
||||||
{
|
|
||||||
|
|
||||||
// take care the value is within range
|
|
||||||
// val = constrain(val, _in[0], _in[size-1]);
|
|
||||||
|
|
||||||
|
|
||||||
unsigned int val = analogRead(LDR_PIN);
|
|
||||||
#ifdef DEBUG //DEBUG++++++++++++++++
|
|
||||||
Serial.print("LDR RAW=: ");
|
|
||||||
Serial.println(val);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (val <= _in[0]) return _out[0];
|
|
||||||
if (val >= _in[size-1]) return _out[size-1];
|
|
||||||
|
|
||||||
|
|
||||||
// search right interval
|
|
||||||
byte pos = 1; // _in[0] allready tested
|
|
||||||
while(val > _in[pos]) pos++;
|
|
||||||
|
|
||||||
|
|
||||||
// this will handle all exact "points" in the _in array
|
|
||||||
if (val == _in[pos]) return _out[pos];
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// interpolate in the right segment for the rest
|
|
||||||
return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_MHZ19
|
|
||||||
byte mhz19_getCheckSum(byte* packet) {
|
|
||||||
byte checksum = 0;
|
|
||||||
for(uint8_t i = 1; i < 8; i++) {
|
|
||||||
checksum += packet[i];
|
|
||||||
}
|
|
||||||
checksum = 0xff - checksum;
|
|
||||||
checksum += 1;
|
|
||||||
return checksum;
|
|
||||||
}
|
|
||||||
|
|
||||||
int mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz19Ref) { //same function as in mhz19 library from klevytskyi, but with delay between cmd send and response check
|
|
||||||
byte CMD_READ[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; // Read command
|
|
||||||
unsigned int co2 = -1;
|
|
||||||
unsigned char response[9];
|
|
||||||
_streamRef->write(CMD_READ, 9);
|
|
||||||
|
|
||||||
unsigned long _startwait=millis();
|
|
||||||
while (millis()-_startwait<100) { //wait for mhz19 to send response
|
|
||||||
//wait
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_streamRef->available()) {
|
|
||||||
_streamRef->readBytes(response, 9);
|
|
||||||
|
|
||||||
byte crc = mhz19_getCheckSum(response);
|
|
||||||
|
|
||||||
if (response[0] == 0xFF && response[1] == CMD_READ[2] && response[8] == crc) {
|
|
||||||
unsigned int responseHigh = (unsigned int) response[2];
|
|
||||||
unsigned int responseLow = (unsigned int) response[3];
|
|
||||||
unsigned int ppm = (256*responseHigh) + responseLow;
|
|
||||||
co2 = ppm;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return co2;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SENSOR_SDS018
|
#ifdef SENSOR_SDS018
|
||||||
void readSDS018()
|
void readSDS018()
|
||||||
{
|
{
|
||||||
|
@ -1239,31 +1021,6 @@ void ICACHE_RAM_ATTR interrupt_raingauge()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* #################################
|
|
||||||
* ########### topic handler #######
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef MHZ19CALIBRATIONTOPIC
|
|
||||||
bool mhz19calibrationHandler(const HomieRange& range, const String& value) {
|
|
||||||
if (range.isRange) {
|
|
||||||
return false; //if range is given but index is not in allowed range
|
|
||||||
}
|
|
||||||
Homie.getLogger() << "mhz19 calibration " << ": " << value << endl;
|
|
||||||
|
|
||||||
if (value=="zero") {
|
|
||||||
mhz19.calibrateZero();
|
|
||||||
Homie.getLogger() << "mhz19 calibration " << ": " << value << endl;
|
|
||||||
#ifdef STATUSNODE
|
|
||||||
sensorNode.setProperty("status").send("MHZ19 Zero Calibration triggered");
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
Homie.getLogger() << "Value outside range" << endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*##################################
|
/*##################################
|
||||||
|
|
Loading…
Reference in New Issue