combined sourcode for mostly esp8266 sensor iot nodes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

1674 lines
48 KiB

//#define DEBUG
//Compile with platformio run --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
//GPIO2 is blue led on wemos_d1
#include "Arduino.h"
#include <Homie.h>
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
#define STATUSNODE
struct sensordata
{
unsigned long lastreadtime=0;
unsigned long readdelay=1000*10; //polling delay
float minchange=0; //send new value if difference to last sent value is greater than this
float lastsentvalue=0;
unsigned long lastsent=0;
unsigned long senddelaymax=1000*60*5; //maximum time until current value is send
};
#ifdef SENSOR_DHT22
// Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
#include <Adafruit_Sensor.h> //required for dht library
#include <DHT.h>
DHT dht(DHTPIN,DHT22,11); //default:11
struct sensordata dataDHT22_temperature; //struct values are changed in setup()
float value_temperatureDHT=0;
struct sensordata dataDHT22_humidity; //struct values are changed in setup()
float value_humidityDHT=0;
#endif
#ifdef SENSOR_BMP180
//Connect SCL to D1, SDA to D2, GND and 3v3
#include <Adafruit_BMP085.h>
bool bmp180init_ok=false;
Adafruit_BMP085 bmp180;
struct sensordata dataBMP180_temperature; //struct values are changed in setup()
float value_temperatureBMP=0;
struct sensordata dataBMP180_pressure; //struct values are changed in setup()
float value_pressureBMP=0;
#endif
#ifdef SENSOR_HTU21D
//Connect SCL to D1, SDA to D2, GND and 3v3
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#endif
#include "Adafruit_HTU21DF.h"
bool htu21dinit_ok=false;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
struct sensordata dataHTU21D_temperature; //struct values are changed in setup()
float value_temperatureHTU=0;
struct sensordata dataHTU21D_humidity; //struct values are changed in setup()
float value_humidityHTU=0;
#endif
#ifdef SENSOR_HS1101
struct sensordata dataHS1101;
float value_humidityHS1101=0;
#define HUMARRAYSIZE 11
//from HS1101 datasheet https://www.parallax.com/sites/default/files/downloads/27920-Humidity-Sensor-Datasheet.pdf
static const unsigned int out_humidity[] = {1000,900,800,700,600,500,400,300,200,100,0}; //*10, gets later devided by 10
static const unsigned int in_hs1101frequency[] = {6033,6186,6330,6468,6600,6728,6853,6976,7100,7224,7351};
float getHumidity_HS1101(int pin);
int get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int val);
#endif
#ifdef SENSOR_BH1750
//SCL=D1, SDA=D2
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#endif
#include <BH1750.h>
BH1750 lightMeter(0x23); //0x23 if addr connected to ground (=pin open), 0x5c if addr pulled high
bool bh1750init_ok=false;
sensordata dataBH1750;
float value_lightBH1750=0;
#endif
#ifdef SENSOR_ML8511
//ML8511 UV Sensor outputs an analog voltage. ML8511PIN needs to be an ADC pin
sensordata dataML8511;
float getUV_ML8511(int pin);
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
float value_uvML8511=0; //uvIntensity (mW/cm^2)
#endif
#ifdef SENSOR_PIR
// PIR Sensors HC-SR501
// pir sensor needs 5v through an inductor for filtering. output level is 3.3v
// 100nF capacitor should be soldered between pins 12 and 13 of BISS0001 to stop interference from esp causing false triggers (in some setups). source: https://www.letscontrolit.com/forum/viewtopic.php?t=671
// hc-sr501 should also be a few cm away from the esp. interference can cause false triggering
// poti closer to jumper is sensitivity (cw increases). other poti is pulse time (cw increases).
// time set to output around 30s pulse
sensordata dataPIR;
bool value_PIR=false;
#endif
#ifdef SENSOR_RADAR
// High/Low Output Radar Sensor
// For example: RCWL-0516 (needs 5v input (gnd, vin), 3.3v output level. high for 2seconds when movement detected)
sensordata dataRADAR;
bool value_RADAR=false;
#endif
#ifdef SENSOR_LDR
struct sensordata dataLDR;
float value_ldr=0;
#ifdef SENSOR_LDR_CALIB1
#define LDRARRAYSIZE 18
//black wire of ldr connects to A0 with 10k to gnd. red wire connects with 1k to gnd and 2k2 to 3v3
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)
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
#endif
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size); //for analog ldr light calculation
#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
*/
// 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
SoftwareSerial mhz19_swSerial;
#define BAUD_RATE_MHZ19 9600
#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
#ifdef SENSOR_SDS018
struct sensordata dataSDS018_pm25;
struct sensordata dataSDS018_pm10;
// SDS018 example: https://github.com/crystaldust/sds018/blob/master/sds018.ino
//SDS18 dust sensor for 2.5µm and 10µm
//Needs 5V
bool sds018_dustok=false;
float value_pm25=-1;
float value_pm10=-1;
#ifndef SOFTWARESERIAL_H
#include <SoftwareSerial.h>
#define SOFTWARESERIAL_H
#endif
SoftwareSerial sds018_swSerial;
#define BAUD_RATE_SDS018 9600
unsigned long lastread_sds018=0; //to save last read time for both readings
void readSDS018();
#endif
#ifdef SENSOR_TCS34725
//#include "Adafruit_TCS34725.h"
#include "tcs34725_agc.h" //class code from example https://github.com/adafruit/Adafruit_TCS34725/blob/master/examples/tcs34725autorange/tcs34725autorange.ino
//Connect SCL to D1, SDA to D2, GND and 3v3
//Maximum measurable light is around 20k Lux. (direct sunlight is easily above 20k Lux)
//Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); //initializer from standart class
tcs34725 tcs; //wrapper class with agc
bool tcs34725init_ok=false;
struct sensordata dataTCS34725_lux;
struct sensordata dataTCS34725_colortemp;
uint16_t value_colortemp, value_tcs_lux, value_tcs_r,value_tcs_g,value_tcs_b,value_tcs_c;
unsigned long lastread_tcs34725=0;
#define TCS34725_MINLUXFORCT 30 //send only colortemperature values if lux is at least this high
#ifndef TCS34725_LUXFACTOR
#define TCS34725_LUXFACTOR 1
#endif
#endif
#ifdef SENSOR_VL53L1X
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#endif
#include <VL53L1X.h>
VL53L1X vl53l1x;
bool vl53l1xinit_ok=false;
struct sensordata dataVL53L1X;
uint16_t value_vl53l1x_range;
unsigned long lastread_vl53l1x=0;
VL53L1X::RangeStatus lastsentvalue_vl53l1x_status;
#endif
#ifdef SENSOR_ANEMOMETER
//uses ATS177 Latched hall sensor for rotation sensing
sensordata dataAnemometer;
unsigned long anemometer_lasttimereset=0;
uint16_t anemometer_pulsecounter=0; //counted pulses since last reset
#define ANEMOMETER_DEBOUNCETIME 15 //15ms between pulses is approx 85m/s windspeed
unsigned long anemometer_lastpulse_fordebounce=0;
float value_anemometer=0; // [m/s]
void ICACHE_RAM_ATTR interrupt_anemometer();
void updateAnemometer();
#endif
#ifdef SENSOR_RAINGAUGE
//uses ATS177 Latched Hall Sensor for rauge flip sensing
sensordata dataRaingauge;
unsigned long raingauge_lasttimereset=0;
uint16_t raingauge_pulsecounter=0; //counted pulses since last reset
bool raingauge_idleflag=true;
#define RAINGAUGE_DEBOUNCETIME 1000
unsigned long raingauge_lastpulse_fordebounce=0;
float value_raingauge=0; // [mm] or [L/m^2]
#define RAINGAUGE_FLIPAMOUNT 0.38888 //how much mm rain (L/m^2) per gauge flip. mL (rain to flip) / A (opening area)
//was 0.69292 until 201702
/* Calibration:
* Test1: 1000mL -> 259 Flips
* Test2: 1000mL -> 256 in ca 10min
* -> 3,9mL per Flip, opening diameter =113mm -> A=0,010028749
*/
void ICACHE_RAM_ATTR interrupt_raingauge();
void updateRaingauge();
#endif
// data/homie/config.json hochladen mit platformio run --target uploadfs
// config contains homie device name, mqtt ip and wifi credentials
HomieNode sensorNode("sensors", "Sensors","sensors"); //id, name, type
char tempstring[16]; //for dtostrf
void loopHandler();
void checkESPStatus();
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Booting");
delay(1000); //wait for sensors to get powered
#ifdef SENSOR_DHT22
Serial.println("initializing dht");
dht.begin();
#ifdef dataDHT22_temperature_minchange
dataDHT22_temperature.minchange=dataDHT22_temperature_minchange;
#endif
#ifdef dataDHT22_humidity_minchange
dataDHT22_humidity.minchange=dataDHT22_humidity_minchange;
#endif
#endif
#ifdef SENSOR_BMP180
Serial.println("initializing bmp180");
if (!bmp180.begin()){
Serial.println("#ERROR: BMP180 init fail\n");
}else{
bmp180init_ok=true; //stays false if init failed, sensor will not be read in loop
}
#ifdef dataBMP180_temperature_minchange
dataBMP180_temperature.minchange=dataBMP180_temperature_minchange;
#endif
#ifdef dataBMP180_pressure_minchange
dataBMP180_pressure.minchange=dataBMP180_pressure_minchange;
#endif
#endif
#ifdef SENSOR_HTU21D
Serial.println("initializing htu21d");
if (!htu.begin()) {
Serial.println("#ERROR: HTU21D init fail\n");
}else{
htu21dinit_ok=true; //stays false if init failed, sensor will not be read in loop
}
#ifdef dataHTU21D_temperature_minchange
dataHTU21D_temperature.minchange=dataHTU21D_temperature_minchange;
#endif
#ifdef dataHTU21D_temperature_senddelaymax
dataHTU21D_temperature.senddelaymax=dataHTU21D_temperature_senddelaymax;
#endif
#ifdef dataHTU21D_humidity_minchange
dataHTU21D_humidity.minchange=dataHTU21D_humidity_minchange;
#endif
#ifdef dataHTU21D_humidity_senddelaymax
dataHTU21D_humidity.senddelaymax=dataHTU21D_humidity_senddelaymax;
#endif
#endif
#ifdef SENSOR_HS1101
Serial.println("initializing hs1101");
#ifdef dataHS1101_minchange
dataHS1101.minchange=dataHS1101_minchange;
#endif
#endif
#ifdef SENSOR_BH1750
Serial.println("initializing bh1750");
Wire.begin();
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("BH1750 Advanced begin"));
bh1750init_ok=true;
} else {
Serial.println(F("Error initialising BH1750"));
}
#ifdef dataBH1750_minchange
dataBH1750.minchange=dataBH1750_minchange;
#endif
#ifdef dataBH1750_senddelaymax
dataBH1750.senddelaymax=dataBH1750_senddelaymax;
#endif
#endif
#ifdef SENSOR_ML8511
Serial.println("initializing ml8511");
pinMode(ML8511PIN, INPUT);
#ifdef dataML8511_minchange
dataML8511.minchange=dataML8511_minchange;
#endif
#endif
#ifdef SENSOR_PIR
Serial.println("initializing pir");
pinMode(PIRPIN, INPUT_PULLUP);
#ifdef dataPIR_readdelay
dataPIR.readdelay=dataPIR_readdelay;
#endif
#ifdef dataPIR_senddelaymax
dataPIR.senddelaymax=dataPIR_senddelaymax;
#endif
#endif
#ifdef SENSOR_RADAR
Serial.println("initializing radar");
pinMode(RADARPIN, INPUT);
#ifdef dataRADAR_readdelay
dataRADAR.readdelay=dataRADAR_readdelay;
#endif
#ifdef dataRADAR_senddelaymax
dataRADAR.senddelaymax=dataRADAR_senddelaymax;
#endif
#endif
#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
#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);
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
#ifdef SENSOR_SDS018
Serial.println("initializing sds018");
sds018_swSerial.begin(BAUD_RATE_SDS018, SWSERIAL_8N1, SDS018_SERIAL_RX, SDS018_SERIAL_TX, false, 256);
#ifdef dataSDS018_pm25_minchange
dataSDS018_pm25.minchange=dataSDS018_pm25_minchange;
#endif
#ifdef dataSDS018_pm10_minchange
dataSDS018_pm10.minchange=dataSDS018_pm10_minchange;
#endif
#endif
#ifdef SENSOR_TCS34725
Serial.println("initializing tcs34725");
if (!tcs.begin()) {
Serial.println("No TCS34725 found!");
}else{
tcs34725init_ok=true;
}
#ifdef dataTCS34725_lux_minchange
dataTCS34725_lux.minchange=dataTCS34725_lux_minchange;
#endif
#ifdef dataTCS34725_lux_senddelaymax
dataTCS34725_lux.senddelaymax=dataTCS34725_lux_senddelaymax;
#endif
#ifdef dataTCS34725_colortemp_minchange
dataTCS34725_colortemp.minchange=dataTCS34725_colortemp_minchange;
#endif
#endif
#ifdef SENSOR_VL53L1X
Serial.println("initializing vl53l1x");
vl53l1x.setTimeout(500);
if (!vl53l1x.init()) {
Serial.println("No vl53l1x found!");
}else{
vl53l1xinit_ok=true;
vl53l1x.setDistanceMode(VL53L1X::Long);
vl53l1x.setMeasurementTimingBudget(50000);
vl53l1x.startContinuous(1000); //This period should be at least as long as the timing budget.
}
#ifdef dataVL53L1X_minchange
dataVL53L1X.minchange=dataVL53L1X_minchange;
#endif
#ifdef dataVL53L1X_senddelaymax
dataVL53L1X.senddelaymax=dataVL53L1X_senddelaymax;
#endif
#ifdef dataVL53L1X_readdelay
dataVL53L1X.readdelay=dataVL53L1X_readdelay;
#endif
#endif
#ifdef SENSOR_ANEMOMETER
pinMode(ANEMOMETERPIN,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ANEMOMETERPIN),interrupt_anemometer,CHANGE); //anemometer interrupt
#ifdef dataAnemometer_minchange
dataAnemometer.minchange=dataAnemometer_minchange;
#endif
#ifdef dataAnemometer_readdelay
dataAnemometer.readdelay=dataAnemometer_readdelay;
#endif
#ifdef dataAnemometer_senddelaymax
dataAnemometer.senddelaymax=dataAnemometer_senddelaymax;
#endif
#endif
#ifdef SENSOR_RAINGAUGE
pinMode(RAINGAUGEPIN,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RAINGAUGEPIN),interrupt_raingauge,CHANGE); //anemometer interrupt
#ifdef dataRaingauge_senddelaymax
dataRaingauge.senddelaymax=dataRaingauge_senddelaymax;
#endif
#ifdef dataRaingauge_readdelay
dataRaingauge.readdelay=dataRaingauge_readdelay;
#endif
#endif
// ##### Advertise topics below here #####
//Homie_setFirmware(FW_NAME, FW_VERSION);
//Homie_setBrand(FW_NAME);
Homie_setFirmware(FW_NAME, FW_VERSION);
Homie.setLoopFunction(loopHandler);
#ifdef STATUSNODE
//to return some stuff about status, errors etc.
Serial.println("Using status node");
sensorNode.advertise("status");
#endif
#ifdef SENSOR_DHT22
#ifndef SENSOR_BMP180
sensorNode.advertise("temperature");
#else
sensorNode.advertise("temperature_dht");
#endif
sensorNode.advertise("humidity");
#endif
#ifdef SENSOR_HS1101
#if defined(SENSOR_DHT22)
sensorNode.advertise("humidity_hs1101");
#else
sensorNode.advertise("humidity");
#endif
#endif
#ifdef SENSOR_BH1750
sensorNode.advertise("light");
lightMeter.readLightLevel(); //make first reading, could be 0
#endif
#ifdef SENSOR_ML8511
sensorNode.advertise("uv");
analogRead(ML8511PIN); //first read adc. just to avoid problems
#endif
#ifdef SENSOR_LDR
sensorNode.advertise("light");
analogRead(LDR_PIN); //first reading could be false
#endif
#ifdef SENSOR_PIR
sensorNode.advertise("motion");
#endif
#ifdef SENSOR_RADAR
sensorNode.advertise("radar");
#endif
#ifdef SENSOR_BMP180
sensorNode.advertise("temperature");
sensorNode.advertise("pressure");
#endif
#ifdef SENSOR_HTU21D
sensorNode.advertise("temperature_htu");
sensorNode.advertise("humidity_htu");
#endif
#ifdef SENSOR_MHZ19
sensorNode.advertise("co2");
#ifdef MHZ19CALIBRATIONTOPIC
sensorNode.advertise("mhz19calibration").settable(mhz19calibrationHandler);
#endif
#endif
#ifdef SENSOR_SDS018
sensorNode.advertise("dust_pm25");
sensorNode.advertise("dust_pm10");
#endif
#ifdef SENSOR_TCS34725
#if defined(SENSOR_LDR) || defined(SENSOR_BH1750)
sensorNode.advertise("light_tcs");
#else
sensorNode.advertise("light");
#endif
sensorNode.advertise("colortemp");
#endif
#ifdef SENSOR_VL53L1X
sensorNode.advertise("tofstatus");
sensorNode.advertise("tofrange");
sensorNode.advertise("tofpeaksignal");
sensorNode.advertise("tofambient");
#endif
#ifdef SENSOR_ANEMOMETER
sensorNode.advertise("windspeed");
#endif
#ifdef SENSOR_RAINGAUGE
sensorNode.advertise("rain");
#endif
Serial.println("connecting..");
Homie.setup();
Serial.println("");
Serial.println("connected"); //wird nicht ausgegeben. keine ahnung warum.
}
void loop() {
Homie.loop();
}
#ifdef SENSOR_DHT22
void loop_DHT22_temperature()
{
sensordata &d=dataDHT22_temperature;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_temperatureDHT = dht.readTemperature();
if (fabs(d.lastsentvalue-value_temperatureDHT)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending DHT22_temperature. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!(isnan(value_temperatureDHT) == 1)){ //success
#ifndef SENSOR_BMP180
sensorNode.setProperty("temperature").send(String(value_temperatureDHT));
Homie.getLogger() << "temperature " << ": " << value_temperatureDHT << endl;
#else
sensorNode.setProperty("temperature_dht").send(String(value_temperatureDHT));
Homie.getLogger() << "temperature_dht " << ": " << value_temperatureDHT << endl;
#endif
d.lastsentvalue=value_temperatureDHT;
}
d.lastsent=millis();
}
}
void loop_DHT22_humidity()
{
sensordata &d=dataDHT22_humidity;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_humidityDHT = dht.readHumidity();
if (fabs(d.lastsentvalue-value_humidityDHT)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending DHT22_humidity. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!(isnan(value_humidityDHT) == 1)){ //success
Homie.getLogger() << "humidity " << ": " << value_humidityDHT << endl;
sensorNode.setProperty("humidity").send(String(value_humidityDHT));
d.lastsentvalue=value_humidityDHT;
}
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_BMP180
void loop_BMP180_temperature()
{
sensordata &d=dataBMP180_temperature;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_temperatureBMP = bmp180.readTemperature();
if (fabs(d.lastsentvalue-value_temperatureBMP)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending BMP180_temperature. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!(isnan(value_temperatureBMP) == 1)){ //success
sensorNode.setProperty("temperature").send(String(value_temperatureBMP));
Homie.getLogger() << "temperature " << ": " << value_temperatureBMP << endl;
d.lastsentvalue=value_temperatureBMP;
}
d.lastsent=millis();
}
}
void loop_BMP180_pressure()
{
sensordata &d=dataBMP180_pressure;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_pressureBMP = bmp180.readPressure()/100.0;
if (fabs(d.lastsentvalue-value_pressureBMP)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending BMP180_pressure. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!(isnan(value_pressureBMP) == 1)){ //success
Homie.getLogger() << "pressure " << ": " << value_pressureBMP << endl;
sensorNode.setProperty("pressure").send(String(value_pressureBMP));
d.lastsentvalue=value_pressureBMP;
}
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_HTU21D
void loop_HTU21D_temperature()
{
sensordata &d=dataHTU21D_temperature;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_temperatureHTU = htu.readTemperature();
if (fabs(d.lastsentvalue-value_temperatureHTU)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending HTU21D_temperature. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!(isnan(value_temperatureHTU) == 1)){ //success
sensorNode.setProperty("temperature_htu").send(String(value_temperatureHTU));
Homie.getLogger() << "temperature_htu " << ": " << value_temperatureHTU << endl;
d.lastsentvalue=value_temperatureHTU;
}
d.lastsent=millis();
}
}
void loop_HTU21D_humidity()
{
sensordata &d=dataHTU21D_humidity;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_humidityHTU = htu.readHumidity();
if (fabs(d.lastsentvalue-value_humidityHTU)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending HTU21D_humidity. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!(isnan(value_humidityHTU) == 1)){ //success
sensorNode.setProperty("humidity_htu").send(String(value_humidityHTU));
Homie.getLogger() << "humidity_htu " << ": " << value_humidityHTU << endl;
d.lastsentvalue=value_humidityHTU;
}
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_HS1101
void loop_HS1101() {
sensordata &d=dataHS1101;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_humidityHS1101 = getHumidity_HS1101(HS1101PIN); //hum %
if (fabs(d.lastsentvalue-value_humidityHS1101)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending HS1101. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
#if defined(SENSOR_DHT22)
Homie.getLogger() << "humidity hs1101 " << ": " << value_humidityHS1101 << endl;
sensorNode.setProperty("humidity_hs1101").send(String(value_humidityHS1101));
#else
Homie.getLogger() << "humidity " << ": " << value_humidityHS1101 << endl;
sensorNode.setProperty("humidity").send(String(value_humidityHS1101));
#endif
d.lastsentvalue=value_humidityHS1101;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_BH1750
void loop_BH1750()
{
sensordata &d=dataBH1750;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_lightBH1750 = lightMeter.readLightLevel(); // [lux]
if (fabs(d.lastsentvalue-value_lightBH1750)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending BH1750. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "light " << ": " << value_lightBH1750 << endl;
sensorNode.setProperty("light").send(String(value_lightBH1750));
d.lastsentvalue=value_lightBH1750;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_ML8511
void loop_ML8511()
{
sensordata &d=dataML8511;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
Serial.print("analogRead="); Serial.println(analogRead(ML8511PIN));
value_uvML8511 = getUV_ML8511(ML8511PIN); //uvIntensity (mW/cm^2)
if (fabs(d.lastsentvalue-value_uvML8511)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending uv ML8511. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "uv " << ": " << value_uvML8511 << endl;
sensorNode.setProperty("uv").send(String(value_uvML8511));
d.lastsentvalue=value_uvML8511;
d.lastsent=millis();
}
}
#endif
#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_PIR
void loop_PIR()
{
sensordata &d=dataPIR;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (digitalRead(PIRPIN) != value_PIR){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { //send current value after some long time
Serial.print("Sending motion. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (digitalRead(PIRPIN)){
Homie.getLogger() << "motion " << ": " << "true" << endl;
sensorNode.setProperty("motion").send(String("true"));
value_PIR=true;
}else{
Homie.getLogger() << "motion " << ": " << "false" << endl;
sensorNode.setProperty("motion").send(String("false"));
value_PIR=false;
}
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_RADAR
void loop_RADAR()
{
sensordata &d=dataRADAR;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (digitalRead(RADARPIN) != value_RADAR){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { //send current value after some long time
Serial.print("Sending motion radar. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (digitalRead(RADARPIN)){
Homie.getLogger() << "radar " << ": " << "true" << endl;
sensorNode.setProperty("radar").send(String("true"));
value_RADAR=true;
}else{
Homie.getLogger() << "motion " << ": " << "false" << endl;
sensorNode.setProperty("radar").send(String("false"));
value_RADAR=false;
}
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
void loop_SDS018_pm25()
{
sensordata &d=dataSDS018_pm25;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_sds018+d.readdelay)) {
readSDS018(); //reads values into value_pm25 und value_pm10
}
//Homie.getLogger() << "read pm25: " << value_pm25 << ".read pm10: " << value_pm10 << " status=" << dust_ok << endl;
if (fabs(d.lastsentvalue-value_pm25)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending SDS018_pm25. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "read pm25: " << value_pm25 << " status=" << sds018_dustok << endl;
if (sds018_dustok){ //send no dust values if sensor not ok
sensorNode.setProperty("dust_pm25").send(String(value_pm25));
}else{
Homie.getLogger() << "sds018 dust not ok. didnt sent" << endl;
}
d.lastsentvalue=value_pm25;
d.lastsent=millis();
}
}
void loop_SDS018_pm10()
{
sensordata &d=dataSDS018_pm10;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_sds018+d.readdelay)) {
readSDS018(); //reads values into value_pm25 und value_pm10
}
//Homie.getLogger() << "read pm25: " << value_pm25 << ".read pm10: " << value_pm10 << " status=" << dust_ok << endl;
if (fabs(d.lastsentvalue-value_pm10)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending SDS018_pm10. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "read pm10: " << value_pm10 << " status=" << sds018_dustok << endl;
if (sds018_dustok){ //send no dust values if sensor not ok
sensorNode.setProperty("dust_pm10").send(String(value_pm10));
}else{
Homie.getLogger() << "sds018 dust not ok. didnt sent" << endl;
}
d.lastsentvalue=value_pm10;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_TCS34725
void loop_TCS34725_lux()
{
sensordata &d=dataTCS34725_lux;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
tcs.getData();
lastread_tcs34725=millis();
if (tcs.isSaturated){
Serial.println("Warning: tcs34725 is saturated");
#ifdef STATUSNODE
sensorNode.setProperty("status").send("TCS34725 is saturated");
#endif
}
}
//value_tcs_lux = tcs.calculateLux(value_tcs_r, value_tcs_g, value_tcs_b);
uint16_t _value_tcs_lux = tcs.lux*TCS34725_LUXFACTOR;
if (!tcs.isSaturated && _value_tcs_lux<65535){ //sometimes false high reading accur around 65535 sometimes less. with isSaturated check only 65535 values appeared.
value_tcs_lux = _value_tcs_lux;
}
if (abs((int)d.lastsentvalue-value_tcs_lux)>=d.minchange){ //int abs
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending TCS Lux. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "light tcs " << ": " << value_tcs_lux << endl;
#if defined(SENSOR_LDR) || defined(SENSOR_BH1750)
sensorNode.setProperty("light_tcs").send(String(value_tcs_lux));
#else
sensorNode.setProperty("light").send(String(value_tcs_lux));
#endif
d.lastsentvalue=value_tcs_lux;
d.lastsent=millis();
}
}
void loop_TCS34725_colortemp()
{
sensordata &d=dataTCS34725_colortemp;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
tcs.getData();
lastread_tcs34725=millis();
if (tcs.isSaturated){
Serial.println("Warning: tcs34725 is saturated");
}
}
// colorTemp = tcs.calculateColorTemperature(r, g, b);
//value_colortemp = tcs.calculateColorTemperature_dn40(value_tcs_r, value_tcs_g, value_tcs_b, value_tcs_c);
if (!tcs.isSaturated){
value_colortemp = tcs.ct; //with agc
}
if (abs((int)d.lastsentvalue-value_colortemp)>=d.minchange){ //int abs
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending TCS colortemp. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "colortemp tcs " << ": " << value_colortemp << endl;
if (tcs.lux>=TCS34725_MINLUXFORCT) {
if (value_colortemp > 1) {
sensorNode.setProperty("colortemp").send(String(value_colortemp));
}else{
Homie.getLogger() << "didn't send tcs ct because value is too low" << endl;
sensorNode.setProperty("colortemp").send(String(-1));
}
}else{
Homie.getLogger() << "didn't send tcs ct because light too low: " << tcs.lux << "lux" << endl;
sensorNode.setProperty("colortemp").send(String(-1));
}
d.lastsentvalue=value_colortemp;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_VL53L1X
void loop_VL53L1X()
{
sensordata &d=dataVL53L1X;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_vl53l1x+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
vl53l1x.read();
lastread_vl53l1x=millis();
}
value_vl53l1x_range=vl53l1x.ranging_data.range_mm;
/* for debugging
Serial.print("range: ");
Serial.print(vl53l1x.ranging_data.range_mm);
Serial.print("\tstatus: ");
Serial.print(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
Serial.print("\tstatus=");
Serial.print(vl53l1x.ranging_data.range_status);
Serial.print("\tpeak signal: ");
Serial.print(vl53l1x.ranging_data.peak_signal_count_rate_MCPS);
Serial.print("\tambient: ");
Serial.print(vl53l1x.ranging_data.ambient_count_rate_MCPS);
Serial.println();
*/
if (abs((int)d.lastsentvalue-value_vl53l1x_range)>=d.minchange){ //int abs
_changed=true;
}
if (lastsentvalue_vl53l1x_status!=vl53l1x.ranging_data.range_status) { //sensor status changed
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending VL53L1X range. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "range vl53l1x " << ": " << value_vl53l1x_range << endl;
sensorNode.setProperty("tofstatus").send(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
sensorNode.setProperty("tofrange").send(String(value_vl53l1x_range));
sensorNode.setProperty("tofpeaksignal").send(String(vl53l1x.ranging_data.peak_signal_count_rate_MCPS));
sensorNode.setProperty("tofambient").send(String(vl53l1x.ranging_data.ambient_count_rate_MCPS));
d.lastsentvalue=value_vl53l1x_range;
lastsentvalue_vl53l1x_status=vl53l1x.ranging_data.range_status;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_ANEMOMETER
void loop_anemometer()
{
sensordata &d=dataAnemometer;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
uint16_t _anepulsesPerMinute=anemometer_pulsecounter/((millis()-anemometer_lasttimereset)/60000.0);
value_anemometer = _anepulsesPerMinute*ANEMOMETER_PPMtoMPS;
if (abs((int)d.lastsentvalue-value_anemometer)>=d.minchange){ //int abs
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending windspeed. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "windspeed tcs " << ": " << value_anemometer << endl;
sensorNode.setProperty("windspeed").send(String(value_anemometer));
//reset when sent. makes it more accurate but keeps fast response
anemometer_pulsecounter=0; //reset counter
anemometer_lasttimereset=millis();
d.lastreadtime=millis(); //also set lastread time to avoid having 1 count with a low time = high windspeed
d.lastsentvalue=value_anemometer;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_RAINGAUGE
void loop_raingauge()
{
sensordata &d=dataRaingauge;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis()-raingauge_lasttimereset > d.senddelaymax) {
raingauge_idleflag=true; //raingauge didn't flip for a long time
}
if (raingauge_pulsecounter>0){ //if rg flipped
if (raingauge_idleflag) { //last flip is before reset time
value_raingauge=raingauge_pulsecounter*RAINGAUGE_FLIPAMOUNT; //set to fixed amount if flip was exactly at that time
raingauge_idleflag=false;
}else{
value_raingauge=3600000/(millis()-raingauge_lasttimereset)/raingauge_pulsecounter*RAINGAUGE_FLIPAMOUNT;
raingauge_idleflag=false;
}
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending rain. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!_changed) { //no flip since a long time
value_raingauge=0; //set to no rain
}
Homie.getLogger() << "rain " << ": " << value_raingauge << endl;
sensorNode.setProperty("rain").send(String(value_raingauge));
//reset when sent. makes it more accurate but keeps fast response
raingauge_pulsecounter=0; //reset counter
raingauge_lasttimereset=millis();
d.lastsentvalue=value_raingauge;
d.lastsent=millis();
}
}
#endif
void loopHandler() {
#ifdef SENSOR_DHT22
loop_DHT22_temperature();
loop_DHT22_humidity();
#endif
#ifdef SENSOR_BMP180
if (bmp180init_ok) {
loop_BMP180_temperature();
loop_BMP180_pressure();
}
#endif
#ifdef SENSOR_HTU21D
if (htu21dinit_ok) {
loop_HTU21D_temperature();
loop_HTU21D_humidity();
}
#endif
#ifdef SENSOR_HS1101
loop_HS1101();
#endif
#ifdef SENSOR_BH1750
if (bh1750init_ok) {
loop_BH1750();
}
#endif
#ifdef SENSOR_ML8511
loop_ML8511();
#endif
#ifdef SENSOR_LDR
loop_LDR();
#endif
#ifdef SENSOR_PIR
loop_PIR();
#endif
#ifdef SENSOR_RADAR
loop_RADAR();
#endif
#ifdef SENSOR_MHZ19
loop_MHZ19();
#endif
#ifdef SENSOR_SDS018
loop_SDS018_pm25();
loop_SDS018_pm10();
#endif
#ifdef SENSOR_TCS34725
if (tcs34725init_ok) {
loop_TCS34725_lux();
loop_TCS34725_colortemp();
}
#endif
#ifdef SENSOR_VL53L1X
if (vl53l1xinit_ok) {
loop_VL53L1X();
}
#endif
#ifdef SENSOR_ANEMOMETER
loop_anemometer();
#endif
#ifdef SENSOR_RAINGAUGE
loop_raingauge();
#endif
}
void checkESPStatus()
{
if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss
{
ESP.reset();
}
}
//////////////////////////////////////////////////////////////////////////////
// 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
void readSDS018()
{
lastread_sds018=millis();
// https://github.com/crystaldust/sds018/blob/master/sds018.ino
uint8_t mData = 0;
uint8_t mPkt[10] = {0};
uint8_t mCheck = 0;
while( sds018_swSerial.available() > 0 ) {
//Serial.println("serial available");
for( int i=0; i<10; ++i ) {
mPkt[i] = sds018_swSerial.read();
//Serial.println( mPkt[i], HEX );
}
if( 0xC0 == mPkt[1] ) {
Serial.println("read density");
// Read dust density.
// Check
uint8_t sum = 0;
for( int i=2; i<=7; ++i ) {
sum += mPkt[i];
}
if( sum == mPkt[8] ) {
uint8_t pm25Low = mPkt[2];
uint8_t pm25High = mPkt[3];
uint8_t pm10Low = mPkt[4];
uint8_t pm10High = mPkt[5];
value_pm25 = ( ( pm25High * 256.0 ) + pm25Low ) / 10.0;
value_pm10 = ( ( pm10High * 256.0 ) + pm10Low ) / 10.0;
sds018_dustok=true;
/*Serial.print( "PM2.5: " );
Serial.print( pm25 );
Serial.print( "\nPM10 :" );
Serial.print( pm10 );
Serial.println();*/
}
}
sds018_swSerial.flush();
}
}
#endif
#ifdef SENSOR_HS1101
float getHumidity_HS1101(int pin) {
#define HS1101_SAMPLES 512
double freq = 0;
//for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, LOW, 1000);
for(unsigned int j=0; j<HS1101_SAMPLES; j++) freq+= 1000000/(pulseIn(pin, HIGH, 1000)+pulseIn(pin, LOW, 1000)); //both high and low because signal has not 50% duty cycle
freq=freq / HS1101_SAMPLES;
if (freq>2000 && freq<10000){ //in roughly valid range
return get_mapped(in_hs1101frequency,out_humidity,HUMARRAYSIZE, freq )/10.0;
}else{ //error
return -1;
}
}
#endif
#ifdef SENSOR_ML8511
float getUV_ML8511(int pin) {
float uvadc=0;
#define UVADCSAMPLES 32
for (uint16_t _s=0;_s<UVADCSAMPLES;_s++) {
uvadc += 3.06 / 1023 * analogRead(pin) ; //assumes 1023 = 3.069V (10-bit adc on esp8266)
}
uvadc=uvadc/UVADCSAMPLES; //average
return max(mapfloat(uvadc, 0.99, 2.8, 0.0, 15.0), 0.0F); //uvIntensity (mW/cm^2)
}
#endif
#ifdef SENSOR_ANEMOMETER
void ICACHE_RAM_ATTR interrupt_anemometer()
{
if (millis() - anemometer_lastpulse_fordebounce >= ANEMOMETER_DEBOUNCETIME) { //ignore if pulse came too fast
anemometer_pulsecounter++;
anemometer_lastpulse_fordebounce=millis();
}
}
#endif
#ifdef SENSOR_RAINGAUGE
void ICACHE_RAM_ATTR interrupt_raingauge()
{
if (millis() - raingauge_lastpulse_fordebounce >= RAINGAUGE_DEBOUNCETIME) { //ignore if pulse came too fast
raingauge_pulsecounter++;
raingauge_lastpulse_fordebounce=millis();
}
}
#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
/*##################################
* ######## HELPER FUNCTIONS ########
*/
//quelle: https://groups.google.com/forum/#!topic/souliss/1kMAltPB2ME[1-25]
int get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int val) //map with constrain
{
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
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]);
}
//The Arduino Map function but for floats
//From: http://forum.arduino.cc/index.php?topic=3922.0
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}