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.
145 lines
3.4 KiB
145 lines
3.4 KiB
//#define DEBUG |
|
|
|
|
|
#include "Arduino.h" |
|
|
|
#include <Adafruit_Sensor.h> //required for dht library |
|
#include <DHT.h> |
|
#include <Wire.h> |
|
#include <BH1750.h> |
|
|
|
// data/homie/config.json hochladen mit platformio run --target uploadfs |
|
|
|
/* |
|
float humidity; //[%RH] DHT |
|
float temperature; //[deg C] DHT |
|
float light; //[Lux] BH1750 |
|
bool movement //true bei pir output hight, false wenn low HC12? |
|
*/ |
|
|
|
|
|
|
|
//GPIO2 is blue led |
|
#define DHTPIN D7 // Digital pin connected to the DHT sensor |
|
//DHT dht(13,DHT22,11); //default:11 |
|
DHT dht(DHTPIN,DHT22,11); //default:11 |
|
|
|
#define PIRPIN D6 //pir sensor needs 5v. output level is 3.3v |
|
|
|
BH1750 lightMeter(0x23); |
|
|
|
//timings |
|
unsigned long lastsensorreadtime=0; |
|
unsigned long sensorupdatedelay=60000; //delay for reading and transmitting |
|
unsigned long lastPIRtime=0; |
|
unsigned long PIRdelay=100; //polling delay |
|
bool motion=false; |
|
|
|
|
|
#include <Homie.h> |
|
#define FW_NAME "sensoresp3" |
|
#define FW_VERSION "1.0.0" |
|
|
|
|
|
|
|
HomieNode sensorNode("sensors", "Sensors","sensors"); |
|
|
|
|
|
char tempstring[16]; //for dtostrf |
|
|
|
|
|
void loopHandler(); |
|
|
|
void setup() { |
|
Serial.begin(115200); |
|
Serial.println(); |
|
Serial.println("Welcome"); |
|
|
|
pinMode(PIRPIN, INPUT_PULLUP); |
|
|
|
Serial.println("initializing dht"); |
|
dht.begin(); // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed |
|
|
|
Serial.println("initializing bh1750"); |
|
Wire.begin(); //initialize i2c. SDA=D2, SCL=D1 |
|
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) { |
|
Serial.println(F("BH1750 Advanced begin")); |
|
} else { |
|
Serial.println(F("Error initialising BH1750")); |
|
} |
|
|
|
Serial.println("connecting.."); |
|
|
|
Homie_setFirmware(FW_NAME, FW_VERSION); |
|
Homie_setBrand(FW_NAME); |
|
Homie.setLoopFunction(loopHandler); |
|
|
|
|
|
sensorNode.advertise("temperature"); |
|
sensorNode.advertise("humidity"); |
|
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() >= (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() << "temperature " << ": " << temperatureDHT << endl; |
|
sensorNode.setProperty("temperature").send(String(temperatureDHT)); |
|
} |
|
|
|
float humidityDHT = dht.readHumidity(); |
|
if (!(isnan(humidityDHT) == 1)){ //success |
|
Homie.getLogger() << "humidity " << ": " << humidityDHT << endl; |
|
sensorNode.setProperty("humidity").send(String(humidityDHT)); |
|
} |
|
|
|
|
|
|
|
float light = lightMeter.readLightLevel(); // [lux] |
|
Homie.getLogger() << "light " << ": " << light << endl; |
|
sensorNode.setProperty("light").send(String(light)); |
|
|
|
|
|
|
|
lastsensorreadtime=millis(); |
|
} |
|
}
|
|
|