Compare commits
No commits in common. "1693a84cf3290011a2990028a8c740b84ef9008e" and "bd25dc9b7c76f798287b08caeb5321be0c8f4024" have entirely different histories.
1693a84cf3
...
bd25dc9b7c
|
@ -1,90 +0,0 @@
|
||||||
// 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]);
|
|
||||||
}
|
|
|
@ -1,45 +0,0 @@
|
||||||
#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
|
|
||||||
|
|
|
@ -1,169 +0,0 @@
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,52 +0,0 @@
|
||||||
#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,6 +82,8 @@ 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
|
||||||
|
@ -89,10 +91,6 @@ 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
|
||||||
|
@ -121,10 +119,11 @@ build_flags =
|
||||||
-D SENSOR_HCSR501_PIN=D6
|
-D SENSOR_HCSR501_PIN=D6
|
||||||
|
|
||||||
-D SENSOR_LDR
|
-D SENSOR_LDR
|
||||||
-D SENSOR_LDR_CALIB_1
|
-D SENSOR_LDR_CALIB1
|
||||||
-D SENSOR_LDR_PIN=A0
|
-D LDR_PIN=A0
|
||||||
-D SENSOR_LDR_minchange=10.0
|
-D dataLDR_minchange=10.0
|
||||||
-D SENSOR_LDR_senddelaymax=1000*60*1
|
-D dataLDR_readdelay=1000*2
|
||||||
|
-D dataLDR_senddelaymax=1000*60*1
|
||||||
|
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
|
@ -144,11 +143,11 @@ monitor_port = /dev/ttyUSB0
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
build_flags =
|
build_flags =
|
||||||
-D SENSOR_MHZ19B
|
-D SENSOR_MHZ19
|
||||||
-D SENSOR_MHZ19B_SERIAL_RX=D5
|
-D MHZ19_SERIAL_RX=D5
|
||||||
-D SENSOR_MHZ19B_SERIAL_TX=D6
|
-D MHZ19_SERIAL_TX=D6
|
||||||
-D SENSOR_MHZ19B_minchange=10
|
-D dataMHZ19_minchange=10
|
||||||
-D SENSOR_MHZ19B_readdelay=10*1000
|
-D dataMHZ19_readdelay=10*1000
|
||||||
|
|
||||||
-D SENSOR_SDS018
|
-D SENSOR_SDS018
|
||||||
-D SDS018_SERIAL_RX=D7
|
-D SDS018_SERIAL_RX=D7
|
||||||
|
@ -203,7 +202,7 @@ monitor_speed = 115200
|
||||||
|
|
||||||
build_flags =
|
build_flags =
|
||||||
-D SENSOR_DHT22
|
-D SENSOR_DHT22
|
||||||
-D SENSOR_DHT22_PIN=D7
|
-D DHTPIN=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
|
||||||
|
|
||||||
|
@ -214,11 +213,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_MHZ19B
|
-D SENSOR_MHZ19
|
||||||
-D SENSOR_MHZ19B_SERIAL_RX=D5
|
-D MHZ19_SERIAL_RX=D5
|
||||||
-D SENSOR_MHZ19B_SERIAL_TX=D6
|
-D MHZ19_SERIAL_TX=D6
|
||||||
-D SENSOR_MHZ19B_minchange=10
|
-D dataMHZ19_minchange=10
|
||||||
-D SENSOR_MHZ19B_readdelay=10*1000
|
-D dataMHZ19_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,33 +173,67 @@
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
#ifdef SENSOR_LDR
|
||||||
#include "sensor_ldr.cpp"
|
struct sensordata dataLDR;
|
||||||
Sensor_LDR sensor_ldr(SENSOR_LDR_PIN);
|
float value_ldr=0;
|
||||||
|
#ifdef SENSOR_LDR_CALIB1
|
||||||
#ifndef SENSOR_LDR_minchange
|
#define LDRARRAYSIZE 18
|
||||||
#define SENSOR_LDR_minchange 10.0
|
//black wire of ldr connects to A0 with 10k to gnd. red wire connects with 1k to gnd and 2k2 to 3v3
|
||||||
#endif
|
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)
|
||||||
#ifndef SENSOR_LDR_senddelaymax
|
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
|
||||||
#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_MHZ19B
|
#ifdef SENSOR_MHZ19
|
||||||
#include "sensor_mhz19b.cpp"
|
struct sensordata dataMHZ19;
|
||||||
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
|
*/
|
||||||
#define SENSOR_MHZ19B_minchange 10.0
|
// 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]
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef SOFTWARESERIAL_H
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#define SOFTWARESERIAL_H
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_MHZ19B_senddelaymax
|
|
||||||
#define SENSOR_MHZ19B_senddelaymax 1000*60*10
|
SoftwareSerial mhz19_swSerial;
|
||||||
#endif
|
#define BAUD_RATE_MHZ19 9600
|
||||||
#ifndef SENSOR_MHZ19B_readdelay
|
|
||||||
#define SENSOR_MHZ19B_readdelay 1000*10
|
#define MHZ19CALIBRATIONTOPIC
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
@ -372,13 +406,41 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
#ifdef SENSOR_LDR
|
||||||
sensor_ldr.init();
|
Serial.println("initializing ldr");
|
||||||
sensor_ldr.setSettings(SENSOR_LDR_minchange,SENSOR_LDR_senddelaymax,SENSOR_LDR_readdelay);
|
pinMode(LDR_PIN, INPUT); //ldr
|
||||||
|
#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_MHZ19B
|
#ifdef SENSOR_MHZ19
|
||||||
sensor_mhz19b.init();
|
Serial.println("initializing mhz19");
|
||||||
sensor_mhz19b.setSettings(SENSOR_MHZ19B_minchange,SENSOR_MHZ19B_senddelaymax,SENSOR_MHZ19B_readdelay);
|
#ifdef dataMHZ19_minchange
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
@ -508,11 +570,21 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
#ifdef SENSOR_LDR
|
||||||
sensor_ldr.advertise(sensorNode);
|
sensorNode.advertise("light");
|
||||||
|
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
|
||||||
|
@ -555,6 +627,72 @@ 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()
|
||||||
{
|
{
|
||||||
|
@ -907,11 +1045,14 @@ void loopHandler() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_LDR
|
#ifdef SENSOR_LDR
|
||||||
sensor_ldr.sensorloop();
|
loop_LDR();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_MHZ19B
|
|
||||||
sensor_mhz19b.sensorloop();
|
|
||||||
|
|
||||||
|
#ifdef SENSOR_MHZ19
|
||||||
|
loop_MHZ19();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_SDS018
|
#ifdef SENSOR_SDS018
|
||||||
|
@ -952,6 +1093,83 @@ 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()
|
||||||
{
|
{
|
||||||
|
@ -1021,6 +1239,31 @@ 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