change sensoresp1 to wemos d1
This commit is contained in:
parent
47a8c5c06b
commit
b5318de8a5
|
@ -8,6 +8,52 @@
|
|||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
# Flash upload with platformio run -t upload --environment sensorespx
|
||||
|
||||
[platformio]
|
||||
#For Config upload comment in data_dir line and flash with platformio run -t uploadfs --environment sensorespx
|
||||
data_dir=data_sensoresp1
|
||||
#data_dir=data_sensoresp3
|
||||
|
||||
|
||||
#Arbeitszimmer
|
||||
[env:sensoresp1]
|
||||
platform = espressif8266
|
||||
board = d1_mini
|
||||
framework = arduino
|
||||
|
||||
monitor_port = /dev/ttyUSB0
|
||||
monitor_speed = 115200
|
||||
|
||||
build_flags =
|
||||
-D SENSOR_DHT22
|
||||
-D DHTPIN=D7
|
||||
-D dataDHT22_temperature_minchange=0.2
|
||||
-D dataDHT22_humidity_minchange=1.0
|
||||
|
||||
-D SENSOR_BMP180
|
||||
-D dataBMP180_temperature_minchange=0.2
|
||||
-D dataBMP180_pressure_minchange=0.1
|
||||
|
||||
-D SENSOR_PIR
|
||||
-D PIRPIN=D6
|
||||
-D dataPIR_readdelay=100
|
||||
-D dataPIR_senddelaymax=1000*60*10
|
||||
|
||||
-D SENSOR_LDR
|
||||
-D SENSOR_LDR_CALIB1
|
||||
-D LDR_PIN=A0
|
||||
-D dataLDR_minchange=10.0
|
||||
-D dataLDR_readdelay=1000*2
|
||||
-D dataBH1750_senddelaymax=1000*60*1
|
||||
|
||||
|
||||
lib_deps =
|
||||
Adafruit BMP085 Library@1.1.0
|
||||
DHT sensor library@1.3.10
|
||||
Homie@3.0.0
|
||||
|
||||
|
||||
#Wohnzimmer
|
||||
[env:sensoresp3]
|
||||
platform = espressif8266
|
||||
|
@ -17,9 +63,6 @@ framework = arduino
|
|||
monitor_port = /dev/ttyUSB0
|
||||
monitor_speed = 115200
|
||||
|
||||
data_dir=data_sensoresp3
|
||||
upload_port=/dev/ttyUSB0
|
||||
|
||||
build_flags =
|
||||
-D SENSOR_DHT22
|
||||
-D DHTPIN=D7
|
||||
|
|
228
src/main.cpp
228
src/main.cpp
|
@ -1,6 +1,9 @@
|
|||
//#define DEBUG
|
||||
|
||||
/* Listed below are all possible defines with comments. Defines are done in platformio.ini under the sensors environment
|
||||
//Compile with platformio run --environment sensorespx
|
||||
//Spiffs data upload with: platformio run -t uploadfs --environment sensorespx
|
||||
|
||||
/* DELETE BELOW
|
||||
// DHT22
|
||||
#define SENSOR_DHT22
|
||||
#define DHTPIN D7 // Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
|
||||
|
@ -23,6 +26,7 @@
|
|||
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name
|
||||
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
|
||||
|
||||
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size); //for analog ldr light calculation
|
||||
|
||||
struct sensordata
|
||||
{
|
||||
|
@ -36,6 +40,7 @@ struct sensordata
|
|||
|
||||
|
||||
#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
|
||||
|
@ -44,10 +49,21 @@ struct sensordata
|
|||
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>
|
||||
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_BH1750
|
||||
//SCL=D1, SDA=D2
|
||||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
BH1750 lightMeter(0x23);
|
||||
|
@ -57,11 +73,26 @@ struct sensordata
|
|||
|
||||
|
||||
#ifdef SENSOR_PIR
|
||||
// PIR Sensors HC-SR501 (modified to put out shortest pulse time)
|
||||
//pir sensor needs 5v. output level is 3.3v
|
||||
sensordata dataPIR;
|
||||
bool value_PIR=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
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// data/homie/config.json hochladen mit platformio run --target uploadfs
|
||||
// config contains homie device name, mqtt ip and wifi credentials
|
||||
|
||||
|
@ -88,9 +119,6 @@ void setup() {
|
|||
Serial.println();
|
||||
Serial.println("Booting");
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef SENSOR_DHT22
|
||||
Serial.println("initializing dht");
|
||||
dht.begin();
|
||||
|
@ -98,13 +126,27 @@ void setup() {
|
|||
dataDHT22_temperature.minchange=dataDHT22_temperature_minchange;
|
||||
#endif
|
||||
#ifdef dataDHT22_humidity_minchange
|
||||
dataDHT22_humidity.minchange=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\n");
|
||||
}
|
||||
#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_BH1750
|
||||
Serial.println("initializing bh1750");
|
||||
Wire.begin(); //initialize i2c. SDA=D2, SCL=D1
|
||||
Wire.begin();
|
||||
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
||||
Serial.println(F("BH1750 Advanced begin"));
|
||||
} else {
|
||||
|
@ -120,6 +162,7 @@ void setup() {
|
|||
|
||||
|
||||
#ifdef SENSOR_PIR
|
||||
Serial.println("initializing pir");
|
||||
pinMode(PIRPIN, INPUT_PULLUP);
|
||||
#ifdef dataPIR_readdelay
|
||||
dataPIR.readdelay=dataPIR_readdelay;
|
||||
|
@ -129,7 +172,20 @@ void setup() {
|
|||
#endif
|
||||
#endif
|
||||
|
||||
Serial.println("connecting..");
|
||||
#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
|
||||
|
||||
|
||||
Homie_setFirmware(FW_NAME, FW_VERSION);
|
||||
Homie_setBrand(FW_NAME);
|
||||
|
@ -137,7 +193,12 @@ void setup() {
|
|||
|
||||
|
||||
#ifdef SENSOR_DHT22
|
||||
|
||||
#ifndef SENSOR_BMP180
|
||||
sensorNode.advertise("temperature");
|
||||
#else
|
||||
sensorNode.advertise("temperature_dht");
|
||||
#endif
|
||||
sensorNode.advertise("humidity");
|
||||
#endif
|
||||
|
||||
|
@ -146,13 +207,25 @@ void setup() {
|
|||
lightMeter.readLightLevel(); //make first reading, could be 0
|
||||
#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_BMP180
|
||||
sensorNode.advertise("temperature");
|
||||
sensorNode.advertise("pressure");
|
||||
#endif
|
||||
|
||||
|
||||
Serial.println("connecting..");
|
||||
Homie.setup();
|
||||
Serial.println("");
|
||||
Serial.println("connected"); //wird nicht ausgegeben. keine ahnung warum.
|
||||
}
|
||||
|
||||
|
@ -181,8 +254,15 @@ void loop_DHT22_temperature()
|
|||
checkESPStatus();
|
||||
|
||||
if (!(isnan(value_temperatureDHT) == 1)){ //success
|
||||
Homie.getLogger() << "temperature " << ": " << value_temperatureDHT << endl;
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
|
@ -219,6 +299,64 @@ void loop_DHT22_humidity()
|
|||
}
|
||||
#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_BH1750
|
||||
void loop_BH1750()
|
||||
{
|
||||
|
@ -247,6 +385,34 @@ void loop_BH1750()
|
|||
}
|
||||
#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()
|
||||
{
|
||||
|
@ -278,6 +444,8 @@ void loop_PIR()
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
void loopHandler() {
|
||||
|
||||
#ifdef SENSOR_DHT22
|
||||
|
@ -285,10 +453,18 @@ void loopHandler() {
|
|||
loop_DHT22_humidity();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BMP180
|
||||
loop_BMP180_temperature();
|
||||
loop_BMP180_pressure();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BH1750
|
||||
loop_BH1750();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_LDR
|
||||
loop_LDR();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_PIR
|
||||
loop_PIR();
|
||||
|
@ -309,3 +485,39 @@ void checkESPStatus()
|
|||
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]
|
||||
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]);
|
||||
}
|
Loading…
Reference in New Issue