261 lines
6.5 KiB
C++
261 lines
6.5 KiB
C++
|
//#define DEBUG
|
||
|
|
||
|
//ESP8266MOD als SMD Platine mit 1M Flash board=esp01_1m, statt wemos_d1
|
||
|
|
||
|
/*
|
||
|
* To Update configuration (wifi credentials) from data/homie/config.json:
|
||
|
* Connect to serial. On ESP-12E connect flash jumper
|
||
|
* Apply Power to ESP
|
||
|
* Optional: upload sketch pio run -t upload
|
||
|
* Flash SPIFFS data: pio run -t uploadfs
|
||
|
* Remove jumper, (reboot)
|
||
|
*/
|
||
|
|
||
|
#include "Arduino.h"
|
||
|
|
||
|
#include "DHT.h"
|
||
|
#include <Wire.h>
|
||
|
#include <Adafruit_BMP085.h>
|
||
|
|
||
|
/*
|
||
|
float humidityDHT; //[%RH]
|
||
|
float temperatureDHT; //[deg C]
|
||
|
float temperatureBMP; ///[deg C]
|
||
|
float pressure; // [hPa] BMP180
|
||
|
float light; //[Lux] LDR
|
||
|
bool movement //true bei pir output hight, false wenn low
|
||
|
*/
|
||
|
|
||
|
|
||
|
|
||
|
Adafruit_BMP085 bmp180;
|
||
|
|
||
|
//GPIO2 is blue led
|
||
|
DHT dht(13,DHT22,11); //default:11
|
||
|
|
||
|
#define PIRPIN 12
|
||
|
|
||
|
//timings
|
||
|
unsigned long lastsensorreadtime=0;
|
||
|
unsigned long sensorupdatedelay=60000; //delay for reading and transmitting
|
||
|
unsigned long lastPIRtime=0;
|
||
|
unsigned long PIRdelay=500;
|
||
|
bool motion=false;
|
||
|
|
||
|
unsigned long lastLDRtime=0;
|
||
|
unsigned long LDRdelay=5000;
|
||
|
int LDR_readcounter=0;
|
||
|
double LDRLightcommulative=0;
|
||
|
|
||
|
|
||
|
struct Values {
|
||
|
uint8_t id;
|
||
|
float humidityDHT; //[%RH]
|
||
|
float temperatureDHT; //[deg C]
|
||
|
float temperatureBMP; ///[deg C]
|
||
|
float pressureBMP; // [hPa]
|
||
|
float lightLDR; //[Lux]
|
||
|
int movementPIR; //[seconds with movement]
|
||
|
long rssi; //wifi rssi
|
||
|
};
|
||
|
Values values;
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
#include <Homie.h>
|
||
|
#define FW_NAME "sensoresp1"
|
||
|
#define FW_VERSION "1.0.0"
|
||
|
|
||
|
|
||
|
|
||
|
HomieNode sensorNode("sensors", "Sensors","sensors");
|
||
|
|
||
|
|
||
|
char tempstring[16]; //for dtostrf
|
||
|
|
||
|
|
||
|
|
||
|
// Light calibration data
|
||
|
// out[] holds the values wanted in lux/10
|
||
|
|
||
|
//measured 20160709
|
||
|
#define LDRARRAYSIZE 18
|
||
|
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
|
||
|
|
||
|
|
||
|
void loopHandler();
|
||
|
double Light(int RawADC0);
|
||
|
double getAverageLight();
|
||
|
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size);
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(115200);
|
||
|
Serial.println();
|
||
|
Serial.println("Welcome");
|
||
|
|
||
|
pinMode(PIRPIN, INPUT); //pir movement sensor
|
||
|
Serial.println("initializing ldr pinmode");
|
||
|
pinMode(A0, INPUT); //ldr
|
||
|
|
||
|
values.id=1;
|
||
|
Serial.println("initializing dht");
|
||
|
dht.begin(); // data pin 2
|
||
|
//Wire.begin(12,14); //i2c for bmp180
|
||
|
Serial.println("initializing bmp180");
|
||
|
//bmp180.init();
|
||
|
if (!bmp180.begin()){
|
||
|
Serial.println("#ERROR: BMP180 init fail\n\n");
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
Serial.println("connecting..");
|
||
|
|
||
|
Homie_setFirmware(FW_NAME, FW_VERSION);
|
||
|
Homie_setBrand(FW_NAME);
|
||
|
Homie.setLoopFunction(loopHandler);
|
||
|
|
||
|
|
||
|
sensorNode.advertise("temperatureDHT");
|
||
|
sensorNode.advertise("humidity");
|
||
|
sensorNode.advertise("temperatureBMP");
|
||
|
sensorNode.advertise("pressure");
|
||
|
sensorNode.advertise("light");
|
||
|
sensorNode.advertise("motion");
|
||
|
|
||
|
|
||
|
Homie.setup();
|
||
|
Serial.println("connected"); //wird nicht ausgegeben. keine ahnung warum.
|
||
|
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
Homie.loop();
|
||
|
}
|
||
|
|
||
|
void loopHandler() {
|
||
|
|
||
|
if (millis() >= (lastPIRtime+PIRdelay)){
|
||
|
|
||
|
if (digitalRead(PIRPIN)){
|
||
|
if (!motion) { //changed?
|
||
|
Homie.getLogger() << "motion " << ": " << "true" << endl;
|
||
|
sensorNode.setProperty("motion").send(String("true"));
|
||
|
}
|
||
|
motion=true;
|
||
|
}else{
|
||
|
if (motion) { //changed?
|
||
|
Homie.getLogger() << "motion " << ": " << "false" << endl;
|
||
|
sensorNode.setProperty("motion").send(String("false"));
|
||
|
}
|
||
|
motion=false;
|
||
|
}
|
||
|
lastPIRtime=millis();
|
||
|
}
|
||
|
|
||
|
if (millis() >= (lastLDRtime+LDRdelay)){
|
||
|
|
||
|
LDR_readcounter++;
|
||
|
//LDRLightcommulative+=Light(analogRead(A0)); //read light value from adc
|
||
|
float ldr_read = get_lux(in_ldr, out_ldr, LDRARRAYSIZE)/10.0;
|
||
|
LDRLightcommulative+=ldr_read;
|
||
|
lastLDRtime=millis();
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
if (millis() >= (lastsensorreadtime+sensorupdatedelay))
|
||
|
{
|
||
|
Serial.println("Sending");
|
||
|
|
||
|
if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss
|
||
|
{
|
||
|
ESP.reset();
|
||
|
}
|
||
|
|
||
|
|
||
|
float temperatureDHT = dht.readTemperature();
|
||
|
if (!(isnan(temperatureDHT) == 1)){ //success
|
||
|
Homie.getLogger() << "temperatureDHT " << ": " << temperatureDHT << endl;
|
||
|
sensorNode.setProperty("temperatureDHT").send(String(temperatureDHT));
|
||
|
}
|
||
|
|
||
|
float humidityDHT = dht.readHumidity();
|
||
|
if (!(isnan(humidityDHT) == 1)){ //success
|
||
|
Homie.getLogger() << "humidity " << ": " << humidityDHT << endl;
|
||
|
sensorNode.setProperty("humidity").send(String(humidityDHT));
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
//values.temperatureBMP=bmp180.bmp085GetTemperature(bmp180.bmp085ReadUT());
|
||
|
float temperatureBMP = bmp180.readTemperature();
|
||
|
Homie.getLogger() << "temperatureBMP " << ": " << temperatureBMP << endl;
|
||
|
sensorNode.setProperty("temperatureBMP").send(String(temperatureBMP));
|
||
|
//values.pressureBMP=bmp180.bmp085GetPressure(bmp180.bmp085ReadUP())/100.0; //Pa in hPa
|
||
|
//values.pressureBMP=bmp180.readPressure()/100.0;
|
||
|
float pressureBMP=bmp180.readPressure()/100.0;
|
||
|
Homie.getLogger() << "pressure " << ": " << pressureBMP << endl;
|
||
|
sensorNode.setProperty("pressure").send(String(pressureBMP));
|
||
|
|
||
|
|
||
|
float light=getAverageLight();
|
||
|
Homie.getLogger() << "light " << ": " << light << endl;
|
||
|
sensorNode.setProperty("light").send(String(light));
|
||
|
|
||
|
|
||
|
|
||
|
lastsensorreadtime=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 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(A0);
|
||
|
#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]);
|
||
|
}
|
||
|
|
||
|
double getAverageLight(){ //average all readings and reset
|
||
|
double ldravg=LDRLightcommulative/LDR_readcounter;
|
||
|
LDR_readcounter=0;
|
||
|
LDRLightcommulative=0;
|
||
|
return ldravg;
|
||
|
}
|
||
|
|