|
|
|
@ -1,7 +1,8 @@
@@ -1,7 +1,8 @@
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
|
|
|
|
|
//Compile with platformio run --environment sensorespx
|
|
|
|
|
//Spiffs data upload with: platformio run -t uploadfs --environment sensorespx
|
|
|
|
|
//Compile and upload: platformio run --environment sensorespx -t upload
|
|
|
|
|
//Spiffs data upload with (comment in data_dir line unter platformio section): platformio run --environment sensorespx -t uploadfs
|
|
|
|
|
|
|
|
|
|
/* DELETE BELOW
|
|
|
|
|
// DHT22
|
|
|
|
@ -26,7 +27,9 @@
@@ -26,7 +27,9 @@
|
|
|
|
|
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name
|
|
|
|
|
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
|
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_LDR |
|
|
|
|
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size); //for analog ldr light calculation
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
struct sensordata |
|
|
|
|
{ |
|
|
|
@ -73,8 +76,8 @@ struct sensordata
@@ -73,8 +76,8 @@ struct sensordata
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_PIR |
|
|
|
|
// PIR Sensors HC-SR501 (modified to put out shortest pulse time)
|
|
|
|
|
//pir sensor needs 5v. output level is 3.3v
|
|
|
|
|
// PIR Sensors HC-SR501 (modified to put out shortest pulse time short pins 5 and 6 of ic)
|
|
|
|
|
//pir sensor needs 5v through an inductor for filtering. output level is 3.3v
|
|
|
|
|
sensordata dataPIR; |
|
|
|
|
bool value_PIR=false; |
|
|
|
|
#endif |
|
|
|
@ -91,7 +94,28 @@ struct sensordata
@@ -91,7 +94,28 @@ struct sensordata
|
|
|
|
|
#endif |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_MHZ19 |
|
|
|
|
struct sensordata dataMHZ19; |
|
|
|
|
/*
|
|
|
|
|
* MHZ19 Library: https://platformio.org/lib/show/1620/SevSegSPI
|
|
|
|
|
* Software Serial Library: https://platformio.org/lib/show/168/EspSoftwareSerial
|
|
|
|
|
* SDS018 example: https://github.com/crystaldust/sds018/blob/master/sds018.ino
|
|
|
|
|
*/ |
|
|
|
|
// SW Serial
|
|
|
|
|
//SW Serial RX: to mhz19 tx (green cable)
|
|
|
|
|
//SW Serial TX: to mhz19 rx (blue cable)
|
|
|
|
|
//co2 sensor needs 5v
|
|
|
|
|
|
|
|
|
|
#include <SoftwareSerial.h> |
|
|
|
|
|
|
|
|
|
SoftwareSerial mhz19_swSerial; |
|
|
|
|
#define BAUD_RATE_MHZ19 9600 |
|
|
|
|
|
|
|
|
|
#include <MHZ19.h> |
|
|
|
|
MHZ19 mhz19; |
|
|
|
|
bool mhz19_ready=false; |
|
|
|
|
int value_co2=-1; //[ppm]
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
// data/homie/config.json hochladen mit platformio run --target uploadfs
|
|
|
|
|
// config contains homie device name, mqtt ip and wifi credentials
|
|
|
|
@ -132,7 +156,7 @@ void setup() {
@@ -132,7 +156,7 @@ void setup() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_BMP180 |
|
|
|
|
Serial.println("initializing bmp180"); |
|
|
|
|
Serial.println("initializing bmp180"); |
|
|
|
|
if (!bmp180.begin()){ |
|
|
|
|
Serial.println("#ERROR: BMP180 init fail\n\n"); |
|
|
|
|
} |
|
|
|
@ -174,16 +198,29 @@ void setup() {
@@ -174,16 +198,29 @@ void setup() {
|
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_LDR |
|
|
|
|
Serial.println("initializing ldr"); |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_MHZ19 |
|
|
|
|
Serial.println("initializing mhz19"); |
|
|
|
|
#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); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -221,6 +258,10 @@ void setup() {
@@ -221,6 +258,10 @@ void setup() {
|
|
|
|
|
sensorNode.advertise("temperature"); |
|
|
|
|
sensorNode.advertise("pressure"); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_MHZ19 |
|
|
|
|
sensorNode.advertise("co2"); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Serial.println("connecting.."); |
|
|
|
@ -442,6 +483,41 @@ void loop_PIR()
@@ -442,6 +483,41 @@ void loop_PIR()
|
|
|
|
|
} |
|
|
|
|
#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]
|
|
|
|
|
//Homie.getLogger() << "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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -470,6 +546,10 @@ void loopHandler() {
@@ -470,6 +546,10 @@ void loopHandler() {
|
|
|
|
|
loop_PIR(); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_MHZ19 |
|
|
|
|
loop_MHZ19(); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -491,6 +571,7 @@ void checkESPStatus()
@@ -491,6 +571,7 @@ 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) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
@ -520,4 +601,5 @@ int get_lux(const unsigned int* _in, const unsigned int* _out, byte size)
@@ -520,4 +601,5 @@ int get_lux(const unsigned int* _in, const unsigned int* _out, byte size)
|
|
|
|
|
|
|
|
|
|
// interpolate in the right segment for the rest
|
|
|
|
|
return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |