add sensors
This commit is contained in:
parent
7eb052c657
commit
2c55bf2b98
|
@ -8,9 +8,9 @@
|
|||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:esp01_1m]
|
||||
[env:d1_mini]
|
||||
platform = espressif8266
|
||||
board = esp01_1m
|
||||
board = d1_mini
|
||||
framework = arduino
|
||||
|
||||
monitor_port = /dev/ttyUSB0
|
||||
|
@ -19,5 +19,5 @@ monitor_speed = 115200
|
|||
|
||||
lib_deps =
|
||||
DHT sensor library@1.3.10
|
||||
Adafruit BMP085 Library@1.1.0
|
||||
BH1750@1.1.4
|
||||
Homie@3.0.0
|
176
src/main.cpp
176
src/main.cpp
|
@ -1,71 +1,42 @@
|
|||
//#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>
|
||||
#include <BH1750.h>
|
||||
|
||||
// data/homie/config.json hochladen mit platformio run --target uploadfs
|
||||
|
||||
/*
|
||||
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
|
||||
float humidity; //[%RH] DHT
|
||||
float temperature; //[deg C] DHT
|
||||
float light; //[Lux] BH1750
|
||||
bool movement //true bei pir output hight, false wenn low HC12?
|
||||
*/
|
||||
|
||||
|
||||
|
||||
Adafruit_BMP085 bmp180;
|
||||
|
||||
//GPIO2 is blue led
|
||||
DHT dht(13,DHT22,11); //default:11
|
||||
#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 12
|
||||
#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=500;
|
||||
unsigned long PIRdelay=100; //polling delay
|
||||
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_NAME "sensoresp3"
|
||||
#define FW_VERSION "1.0.0"
|
||||
|
||||
|
||||
|
@ -76,41 +47,25 @@ 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");
|
||||
}
|
||||
pinMode(PIRPIN, INPUT);
|
||||
|
||||
|
||||
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..");
|
||||
|
||||
|
@ -119,10 +74,8 @@ void setup() {
|
|||
Homie.setLoopFunction(loopHandler);
|
||||
|
||||
|
||||
sensorNode.advertise("temperatureDHT");
|
||||
sensorNode.advertise("temperature");
|
||||
sensorNode.advertise("humidity");
|
||||
sensorNode.advertise("temperatureBMP");
|
||||
sensorNode.advertise("pressure");
|
||||
sensorNode.advertise("light");
|
||||
sensorNode.advertise("motion");
|
||||
|
||||
|
@ -156,17 +109,6 @@ void loopHandler() {
|
|||
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");
|
||||
|
@ -179,8 +121,8 @@ void loopHandler() {
|
|||
|
||||
float temperatureDHT = dht.readTemperature();
|
||||
if (!(isnan(temperatureDHT) == 1)){ //success
|
||||
Homie.getLogger() << "temperatureDHT " << ": " << temperatureDHT << endl;
|
||||
sensorNode.setProperty("temperatureDHT").send(String(temperatureDHT));
|
||||
Homie.getLogger() << "temperature " << ": " << temperatureDHT << endl;
|
||||
sensorNode.setProperty("temperature").send(String(temperatureDHT));
|
||||
}
|
||||
|
||||
float humidityDHT = dht.readHumidity();
|
||||
|
@ -191,18 +133,7 @@ void loopHandler() {
|
|||
|
||||
|
||||
|
||||
//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();
|
||||
float light = lightMeter.readLightLevel(); // [lux]
|
||||
Homie.getLogger() << "light " << ": " << light << endl;
|
||||
sensorNode.setProperty("light").send(String(light));
|
||||
|
||||
|
@ -210,51 +141,4 @@ void loopHandler() {
|
|||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue