Compare commits
3 Commits
05907eb59d
...
90b1185795
Author | SHA1 | Date |
---|---|---|
interfisch | 90b1185795 | |
interfisch | ea0f2183d2 | |
interfisch | b88fa10d53 |
|
@ -0,0 +1,65 @@
|
|||
//Connect SCL to D1, SDA to D2, GND and 3v3
|
||||
#include "sensor_bh1750.h"
|
||||
|
||||
|
||||
|
||||
Sensor_BH1750::Sensor_BH1750()
|
||||
{
|
||||
BH1750 bh1750(0x23); //0x23 if addr connected to ground (=pin open), 0x5c if addr pulled high
|
||||
}
|
||||
|
||||
void Sensor_BH1750::init() //Things to be done during setup()
|
||||
{
|
||||
Serial.println("initializing bh1750");
|
||||
Wire.begin();
|
||||
if (bh1750->begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
||||
Serial.println(F("BH1750 Advanced begin"));
|
||||
init_ok=true;
|
||||
} else {
|
||||
Serial.println(F("Error initialising BH1750"));
|
||||
}
|
||||
bh1750->readLightLevel(); //make first reading, could be 0
|
||||
}
|
||||
|
||||
//Also called during setup()
|
||||
void Sensor_BH1750::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||
{
|
||||
data.minchange=minchange;
|
||||
data.senddelaymax=senddelaymax;
|
||||
data.readdelay=readdelay;
|
||||
}
|
||||
|
||||
|
||||
//Called during setup
|
||||
void Sensor_BH1750::advertise(HomieNode& p_sensorNode)
|
||||
{
|
||||
sensorNode = &p_sensorNode;
|
||||
sensorNode->advertise("light");
|
||||
}
|
||||
|
||||
void Sensor_BH1750::sensorloop()
|
||||
{
|
||||
if (init_ok) {
|
||||
sensordata &d=data;
|
||||
|
||||
bool _changed=false;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
d.value = bh1750->readLightLevel(); // [lux]
|
||||
if (fabs(d.lastsentvalue-d.value)>=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");
|
||||
|
||||
Homie.getLogger() << "light " << ": " << d.value << endl;
|
||||
sensorNode->setProperty("light").send(String(d.value));
|
||||
d.lastsentvalue=d.value;
|
||||
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef SENSOR_BH1750_H
|
||||
#define SENSOR_BH1750_H
|
||||
|
||||
#include "sensordata.h"
|
||||
#include <Homie.h>
|
||||
|
||||
|
||||
#include <BH1750.h>
|
||||
#include <Wire.h>
|
||||
|
||||
|
||||
class Sensor_BH1750
|
||||
{
|
||||
|
||||
private:
|
||||
BH1750 *bh1750;
|
||||
HomieNode *sensorNode; //reference to HomieNode
|
||||
|
||||
struct sensordata data; //struct values are changed in setup()
|
||||
|
||||
bool init_ok;
|
||||
|
||||
public:
|
||||
Sensor_BH1750();
|
||||
|
||||
void init();
|
||||
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
|
||||
void advertise(HomieNode& p_sensorNode);
|
||||
void sensorloop();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
|
||||
#include "sensor_hs1101.h"
|
||||
|
||||
|
||||
Sensor_HS1101::Sensor_HS1101(int pin)
|
||||
{
|
||||
hs1101pin=pin;
|
||||
struct sensordata data;
|
||||
|
||||
//out_humidity[] = {1000,900,800,700,600,500,400,300,200,100,0}; //*10, gets later devided by 10
|
||||
//in_hs1101frequency[] = {6033,6186,6330,6468,6600,6728,6853,6976,7100,7224,7351};
|
||||
}
|
||||
|
||||
void Sensor_HS1101::init() //Things to be done during setup()
|
||||
{
|
||||
Serial.println("initializing HS1101");
|
||||
init_ok=true;
|
||||
}
|
||||
|
||||
|
||||
//Also called during setup()
|
||||
void Sensor_HS1101::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||
{
|
||||
data.minchange=minchange;
|
||||
data.senddelaymax=senddelaymax;
|
||||
data.readdelay=readdelay;
|
||||
}
|
||||
|
||||
//Called during setup
|
||||
void Sensor_HS1101::advertise(HomieNode& p_sensorNode)
|
||||
{
|
||||
sensorNode = &p_sensorNode;
|
||||
#ifdef SENSOR_DHT22
|
||||
sensorNode->advertise("humidity_hs1101");
|
||||
#else
|
||||
sensorNode->advertise("humidity");
|
||||
#endif
|
||||
}
|
||||
|
||||
void Sensor_HS1101::sensorloop()
|
||||
{
|
||||
if (init_ok) {
|
||||
|
||||
sensordata &d=data;
|
||||
|
||||
bool _changed=false;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
d.value = getHumidity_HS1101(hs1101pin); //hum %
|
||||
if (fabs(d.lastsentvalue-d.value)>=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");
|
||||
|
||||
#if defined(SENSOR_DHT22)
|
||||
Homie.getLogger() << "humidity hs1101 " << ": " << d.value << endl;
|
||||
sensorNode->setProperty("humidity_hs1101").send(String(d.value));
|
||||
#else
|
||||
Homie.getLogger() << "humidity " << ": " << d.value << endl;
|
||||
sensorNode->setProperty("humidity").send(String(d.value));
|
||||
#endif
|
||||
|
||||
d.lastsentvalue=d.value;
|
||||
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float Sensor_HS1101::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;
|
||||
}
|
||||
}
|
||||
|
||||
//quelle: https://groups.google.com/forum/#!topic/souliss/1kMAltPB2ME[1-25]
|
||||
int Sensor_HS1101::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]);
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
#ifndef SENSOR_HS1101_H
|
||||
#define SENSOR_HS1101_H
|
||||
|
||||
#include "sensordata.h"
|
||||
#include <Homie.h>
|
||||
|
||||
|
||||
|
||||
class Sensor_HS1101
|
||||
{
|
||||
|
||||
private:
|
||||
HomieNode *sensorNode; //reference to HomieNode
|
||||
|
||||
struct sensordata data; //struct values are changed in setup()
|
||||
|
||||
bool init_ok;
|
||||
int hs1101pin;
|
||||
float getHumidity_HS1101(int pin);
|
||||
int get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int val);
|
||||
|
||||
#define HUMARRAYSIZE 11
|
||||
//from HS1101 datasheet https://www.parallax.com/sites/default/files/downloads/27920-Humidity-Sensor-Datasheet.pdf
|
||||
const unsigned int out_humidity[HUMARRAYSIZE] = {1000,900,800,700,600,500,400,300,200,100,0}; //*10, gets later devided by 10
|
||||
const unsigned int in_hs1101frequency[HUMARRAYSIZE] = {6033,6186,6330,6468,6600,6728,6853,6976,7100,7224,7351};
|
||||
//unsigned int out_humidity[HUMARRAYSIZE];
|
||||
//unsigned int in_hs1101frequency[HUMARRAYSIZE];
|
||||
|
||||
public:
|
||||
Sensor_HS1101(int pin);
|
||||
|
||||
|
||||
void init();
|
||||
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
|
||||
void advertise(HomieNode& p_sensorNode);
|
||||
void sensorloop();
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
//value as uvIntensity (mW/cm^2)
|
||||
#include "sensor_ml8511.h"
|
||||
|
||||
|
||||
|
||||
Sensor_ML8511::Sensor_ML8511(int pin)
|
||||
{
|
||||
ml8511pin=pin;
|
||||
}
|
||||
|
||||
void Sensor_ML8511::init() //Things to be done during setup()
|
||||
{
|
||||
Serial.println("initializing ml8511");
|
||||
pinMode(ml8511pin, INPUT);
|
||||
analogRead(ml8511pin); //first read adc. just to avoid problems
|
||||
init_ok=true;
|
||||
}
|
||||
|
||||
//Also called during setup()
|
||||
void Sensor_ML8511::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||
{
|
||||
data.minchange=minchange;
|
||||
data.senddelaymax=senddelaymax;
|
||||
data.readdelay=readdelay;
|
||||
}
|
||||
|
||||
//Called during setup
|
||||
void Sensor_ML8511::advertise(HomieNode& p_sensorNode)
|
||||
{
|
||||
sensorNode = &p_sensorNode;
|
||||
sensorNode->advertise("uv");
|
||||
}
|
||||
|
||||
void Sensor_ML8511::sensorloop()
|
||||
{
|
||||
if (init_ok) {
|
||||
sensordata &d=data;
|
||||
|
||||
bool _changed=false;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
Serial.print("analogRead="); Serial.println(analogRead(ml8511pin));
|
||||
d.value = getUV_ML8511(ml8511pin); //uvIntensity (mW/cm^2)
|
||||
if (fabs(d.lastsentvalue-d.value)>=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");
|
||||
|
||||
Homie.getLogger() << "uv " << ": " << d.value << endl;
|
||||
sensorNode->setProperty("uv").send(String(d.value));
|
||||
d.lastsentvalue=d.value;
|
||||
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
float Sensor_ML8511::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)
|
||||
}
|
||||
|
||||
//The Arduino Map function but for floats
|
||||
//From: http://forum.arduino.cc/index.php?topic=3922.0
|
||||
float Sensor_ML8511::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;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#ifndef SENSOR_ML8511_H
|
||||
#define SENSOR_ML8511_H
|
||||
|
||||
#include "sensordata.h"
|
||||
#include <Homie.h>
|
||||
|
||||
|
||||
|
||||
class Sensor_ML8511
|
||||
{
|
||||
|
||||
private:
|
||||
HomieNode *sensorNode; //reference to HomieNode
|
||||
|
||||
int ml8511pin;
|
||||
|
||||
struct sensordata data; //struct values are changed in setup()
|
||||
|
||||
bool init_ok;
|
||||
|
||||
float getUV_ML8511(int pin);
|
||||
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
|
||||
|
||||
public:
|
||||
Sensor_ML8511(int pin);
|
||||
|
||||
|
||||
void init();
|
||||
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
|
||||
void advertise(HomieNode& p_sensorNode);
|
||||
void sensorloop();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -43,12 +43,12 @@ build_flags =
|
|||
-D TCS34725_LUXFACTOR=0.3 #measured with luxmeter. with half tennis ball was 1.7ev less. 1/2^1.7=0,3078
|
||||
|
||||
-D SENSOR_HS1101
|
||||
-D HS1101PIN=D5
|
||||
-D dataHS1101_minchange=10
|
||||
-D SENSOR_HS1101_PIN=D5
|
||||
-D SENSOR_HS1101_humidity_minchange=10
|
||||
|
||||
-D SENSOR_ML8511
|
||||
-D ML8511PIN=A0
|
||||
-D dataML8511_minchange=0.2
|
||||
-D SENSOR_ML8511_PIN=A0
|
||||
-D SENSOR_ML8511_minchange=0.2
|
||||
|
||||
-D SENSOR_ANEMOMETER
|
||||
-D ANEMOMETERPIN=D6 #Light Blue thicker cable (in distribution box)
|
||||
|
@ -82,30 +82,27 @@ monitor_port = /dev/ttyUSB0
|
|||
monitor_speed = 115200
|
||||
|
||||
build_flags =
|
||||
-D SENSOR_DHT22
|
||||
-D SENSOR_DHT22_PIN=D7
|
||||
-D SENSOR_DHT22_temperature_minchange=0.2
|
||||
-D SENSOR_DHT22_humidity_minchange=2.0
|
||||
|
||||
-D SENSOR_HS1101
|
||||
-D SENSOR_HS1101_PIN=D5
|
||||
-D SENSOR_HS1101_humidity_minchange=10
|
||||
|
||||
|
||||
-D SENSOR_BMP180
|
||||
-D SENSOR_BMP180_temperature_minchange=0.2
|
||||
-D SENSOR_BMP180_pressure_minchange=0.5
|
||||
-D SENSOR_BH1750
|
||||
-D SENSOR_BH1750_light_minchange=10.0
|
||||
-D SENSOR_BH1750_light_senddelaymax=1000*60*2
|
||||
|
||||
-D SENSOR_ML8511
|
||||
-D SENSOR_ML8511_PIN=A0
|
||||
-D SENSOR_ML8511_minchange=0.2
|
||||
|
||||
-D SENSOR_HTU21D
|
||||
-D dataHTU21D_temperature_minchange=0.2
|
||||
-D dataHTU21D_temperature_senddelaymax=1000*60*20
|
||||
-D dataHTU21D_humidity_minchange=1.0
|
||||
-D dataHTU21D_humidity_senddelaymax=1000*60*30
|
||||
|
||||
|
||||
|
||||
lib_deps =
|
||||
DHT sensor library@1.3.10
|
||||
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
|
||||
Homie@3.0.0
|
||||
Adafruit BMP085 Library@1.1.0
|
||||
adafruit/Adafruit HTU21DF Library @ ^1.0.5
|
||||
claws/BH1750@1.1.4
|
||||
|
||||
#Arbeitszimmer
|
||||
[env:sensoresp1]
|
||||
|
@ -136,7 +133,7 @@ build_flags =
|
|||
-D LDR_PIN=A0
|
||||
-D dataLDR_minchange=10.0
|
||||
-D dataLDR_readdelay=1000*2
|
||||
-D dataBH1750_senddelaymax=1000*60*1
|
||||
-D dataLDR_senddelaymax=1000*60*1
|
||||
|
||||
|
||||
lib_deps =
|
||||
|
|
231
src/main.cpp
231
src/main.cpp
|
@ -103,42 +103,57 @@
|
|||
#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);
|
||||
#include "sensor_hs1101.cpp"
|
||||
Sensor_HS1101 sensor_hs1101(SENSOR_HS1101_PIN);
|
||||
|
||||
#ifndef SENSOR_HS1101_humidity_minchange
|
||||
#define SENSOR_HS1101_humidity_minchange 2.0
|
||||
#endif
|
||||
#ifndef SENSOR_HS1101_humidity_senddelaymax
|
||||
#define SENSOR_HS1101_humidity_senddelaymax 10*60*1000
|
||||
#endif
|
||||
#ifndef SENSOR_HS1101_humidity_readdelay
|
||||
#define SENSOR_HS1101_humidity_readdelay 10000
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BH1750
|
||||
//SCL=D1, SDA=D2
|
||||
#ifndef WIRE_H
|
||||
#include <Wire.h>
|
||||
#define WIRE_H
|
||||
#include "sensor_bh1750.cpp"
|
||||
Sensor_BH1750 sensor_bh1750;
|
||||
|
||||
#ifndef SENSOR_BH1750_light_minchange
|
||||
#define SENSOR_BH1750_light_minchange 10
|
||||
#endif
|
||||
#ifndef SENSOR_BH1750_light_senddelaymax
|
||||
#define SENSOR_BH1750_light_senddelaymax 5*60*1000
|
||||
#endif
|
||||
#ifndef SENSOR_BH1750_light_readdelay
|
||||
#define SENSOR_BH1750_light_readdelay 1000*10
|
||||
#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)
|
||||
#include "sensor_ml8511.cpp"
|
||||
Sensor_ML8511 sensor_ml8511(SENSOR_ML8511_PIN);
|
||||
|
||||
#ifndef SENSOR_ML8511_minchange
|
||||
#define SENSOR_ML8511_minchange 0.2
|
||||
#endif
|
||||
#ifndef SENSOR_ML8511_senddelaymax
|
||||
#define SENSOR_ML8511_senddelaymax 5*60*1000
|
||||
#endif
|
||||
#ifndef SENSOR_ML8511_readdelay
|
||||
#define SENSOR_ML8511_readdelay 1000*10
|
||||
#endif
|
||||
#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
|
||||
// 100nF capacitor SENSOR_ML8511_minchangeshould 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
|
||||
|
@ -360,35 +375,18 @@ void setup() {
|
|||
|
||||
|
||||
#ifdef SENSOR_HS1101
|
||||
Serial.println("initializing hs1101");
|
||||
#ifdef dataHS1101_minchange
|
||||
dataHS1101.minchange=dataHS1101_minchange;
|
||||
#endif
|
||||
sensor_hs1101.init();
|
||||
sensor_hs1101.setSettings(SENSOR_HS1101_humidity_minchange,SENSOR_HS1101_humidity_senddelaymax,SENSOR_HS1101_humidity_readdelay);
|
||||
#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
|
||||
sensor_bh1750.init();
|
||||
sensor_bh1750.setSettings(SENSOR_BH1750_light_minchange,SENSOR_BH1750_light_senddelaymax,SENSOR_BH1750_light_readdelay);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_ML8511
|
||||
Serial.println("initializing ml8511");
|
||||
pinMode(ML8511PIN, INPUT);
|
||||
#ifdef dataML8511_minchange
|
||||
dataML8511.minchange=dataML8511_minchange;
|
||||
#endif
|
||||
sensor_ml8511.init();
|
||||
sensor_ml8511.setSettings(SENSOR_ML8511_minchange,SENSOR_ML8511_senddelaymax,SENSOR_ML8511_readdelay);
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -558,21 +556,15 @@ void setup() {
|
|||
#endif
|
||||
|
||||
#ifdef SENSOR_HS1101
|
||||
#ifdef SENSOR_DHT22
|
||||
sensorNode.advertise("humidity_hs1101");
|
||||
#else
|
||||
sensorNode.advertise("humidity");
|
||||
#endif
|
||||
sensor_hs1101.advertise(sensorNode);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BH1750
|
||||
sensorNode.advertise("light");
|
||||
lightMeter.readLightLevel(); //make first reading, could be 0
|
||||
sensor_bh1750.advertise(sensorNode);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_ML8511
|
||||
sensorNode.advertise("uv");
|
||||
analogRead(ML8511PIN); //first read adc. just to avoid problems
|
||||
sensor_ml8511.advertise(sensorNode);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_LDR
|
||||
|
@ -640,102 +632,6 @@ void loop() {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#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()
|
||||
{
|
||||
|
@ -1191,17 +1087,15 @@ void loopHandler() {
|
|||
#endif
|
||||
|
||||
#ifdef SENSOR_HS1101
|
||||
loop_HS1101();
|
||||
sensor_hs1101.sensorloop();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BH1750
|
||||
if (bh1750init_ok) {
|
||||
loop_BH1750();
|
||||
}
|
||||
sensor_bh1750.sensorloop();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_ML8511
|
||||
loop_ML8511();
|
||||
sensor_ml8511.sensorloop();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_LDR
|
||||
|
@ -1381,33 +1275,6 @@ void readSDS018()
|
|||
}
|
||||
#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
|
||||
|
|
Loading…
Reference in New Issue