Browse Source

add espsensor4 with mhz19

master
interfisch 2 years ago
parent
commit
79a314938a
  1. 43
      platformio.ini
  2. 114
      src/main.cpp

43
platformio.ini

@ -12,9 +12,10 @@ @@ -12,9 +12,10 @@
[platformio]
#For Config upload comment in data_dir line and flash with platformio run -t uploadfs --environment sensorespx
data_dir=data_sensoresp1
#data_dir=data_sensoresp3
#data_dir=data_sensoresp1
#data_dir=data_sensoresp3
data_dir=data_sensoresp4
#Arbeitszimmer
[env:sensoresp1]
@ -82,3 +83,41 @@ lib_deps = @@ -82,3 +83,41 @@ lib_deps =
adafruit/DHT sensor library@1.3.10
BH1750@1.1.4
Homie@3.0.0
#Schlafzimmer
[env:sensoresp4]
platform = espressif8266
board = d1_mini
framework = arduino
monitor_port = /dev/ttyUSB0
monitor_speed = 115200
build_flags =
-D SENSOR_DHT22
-D DHTPIN=D7
-D dataDHT22_temperature_minchange=0.1
-D dataDHT22_humidity_minchange=1.0
-D SENSOR_PIR
-D PIRPIN=D0
-D dataPIR_readdelay=100
-D dataPIR_senddelaymax=1000*60*10
-D SENSOR_BH1750
-D dataBH1750_minchange=10.0
-D dataBH1750_senddelaymax=1000*60*2
-D SENSOR_MHZ19
-D MHZ19_SERIAL_RX=D5
-D MHZ19_SERIAL_TX=D6
-D dataMHZ19_minchange=10
-D dataMHZ19_readdelay=10*1000
lib_deps =
adafruit/DHT sensor library@1.3.10
BH1750@1.1.4
MHZ19@0.0.1
EspSoftwareSerial@6.8.1
Homie@3.0.0

114
src/main.cpp

@ -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
Loading…
Cancel
Save