diff --git a/.gitmodules b/.gitmodules index 39244732..419ce256 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,12 @@ [submodule "libraries/IRremote"] path = libraries/IRremote url = git://github.com/z3t0/Arduino-IRremote.git +[submodule "libraries/SparkFun_BME280_Arduino_Library"] + path = libraries/SparkFun_BME280_Arduino_Library + url = https://github.com/sparkfun/SparkFun_BME280_Arduino_Library +[submodule "libraries/Adafruit_Sensor"] + path = libraries/Adafruit_Sensor + url = https://github.com/adafruit/Adafruit_Sensor +[submodule "libraries/DHT-sensor-library"] + path = libraries/DHT-sensor-library + url = https://github.com/adafruit/DHT-sensor-library diff --git a/OpenMQTTGateway.ino b/OpenMQTTGateway.ino index 5dd78ea2..1da7ce18 100644 --- a/OpenMQTTGateway.ino +++ b/OpenMQTTGateway.ino @@ -17,6 +17,7 @@ - 1technophile - crankyoldgit - glasruit + - HannesDi - Spudtater - prahjister - rickybrent @@ -160,6 +161,12 @@ void setup() lastReconnectAttempt = 0; + #ifdef ZsensorBME280 + setupZsensorBME280(); + #endif + #ifdef ZsensorBH1750 + setupZsensorBH1750(); + #endif #ifdef ZgatewayIR setupIR(); #endif @@ -223,7 +230,13 @@ void loop() #ifdef ESP8266 ArduinoOTA.handle(); #endif - + + #ifdef ZsensorBME280 + MeasureTempHumAndPressure(); //Addon to measure Temperature, Humidity, Pressure and Altitude with a Bosch BME280 + #endif + #ifdef ZsensorBH1750 + MeasureLightIntensity(); //Addon to measure Light Intensity with a BH1750 + #endif #ifdef ZsensorDHT MeasureTempAndHum(); //Addon to measure the temperature with a DHT #endif diff --git a/User_config.h b/User_config.h index be1f6560..75437027 100644 --- a/User_config.h +++ b/User_config.h @@ -30,6 +30,12 @@ */ /*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR WIRING TYPE BELOW----------------*/ +// Choose between "I2C_Wiring" OR "Classic_Wiring" +// Please Note: I2C Wiring and Classic Wiring uses two complete different Pins for all Modules, see PIN definitions at the end of this file +//#define I2C_Wiring // With Support for I2C Modules and the associated libraries BH1750 and BME280 +#define Classic_Wiring // Without Support for I2C Modules, legacy wiring V0.3.1 and below + /*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/ //MQTT Parameters definition #define mqtt_server "192.168.1.17" @@ -65,11 +71,23 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress /*-------------DEFINE THE MODULES YOU WANT BELOW----------------*/ //Addons and module management, comment the line if you don't use -//#define ZsensorDHT -#define ZgatewayRF -#define ZgatewayIR -#define ZgatewayBT -/*----------------------------OTHER PARAMETERS-----------------------------*/ +#ifdef ESP8266 // for nodemcu, weemos and esp8266 + //#define ZsensorDHT + #define ZgatewayRF + #define ZgatewayIR + #define ZgatewayBT + #ifdef I2C_Wiring // to use the sensor below the gateway should wired with I2CWiring, see PIN DEFINITIONS below + #define ZsensorBH1750 + #define ZsensorBME280 + #endif +#else // for arduino + W5100 + #define ZgatewayRF + #define ZgatewayIR + #define ZgatewayBT + //#define ZsensorDHT + //#define ZsensorBH1750 + //#define ZsensorBME280 +#endif/*----------------------------OTHER PARAMETERS-----------------------------*/ /*-------------------CHANGING THEM IS NOT COMPULSORY-----------------------*/ //variables to avoid duplicates for RF #define time_avoid_duplicate 3000 // if you want to avoid duplicate mqtt message received set this to > 0, the value is the time in milliseconds during which we don't publish duplicates @@ -150,22 +168,46 @@ RF supported protocols #define subjectBTtoMQTT "home/BTtoMQTT/" /*-------------------PIN DEFINITIONS----------------------*/ -#define IR_RECEIVER_PIN 2 // put 2 = D4 on nodemcu, 2 = D2 on arduino -#define RF_EMITTER_PIN 4 //put 4 = D2 on nodemcu, 4 = D4 on arduino - -#ifdef ESP8266 - #define IR_EMITTER_PIN 14 // 14 = D5 on nodemcu #define only usefull for ESP8266 - //RF PIN definition - #define RF_RECEIVER_PIN 5 // 5 = D1 on nodemcu - #define BT_RX D7 //ESP8266 RX connect HM-10 TX - #define BT_TX D6 //ESP8266 TX connect HM-10 RX -#else - //IMPORTANT NOTE: On arduino UNO connect IR emitter pin to D9 , comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library IRremote/IRremoteInt.h so as to free pin D3 for RF RECEIVER PIN - //RF PIN definition - #define RF_RECEIVER_PIN 1 // 1 = D3 on arduino - #define BT_RX 5 //arduino RX connect HM-10 TX - #define BT_TX 6 //arduino TX connect HM-10 RX +#ifdef I2C_Wiring // With Support for I2C Modules + #define DHT_RECEIVER_PIN 14 //on nodeMCU this is D5 GPIO14 + #define IR_RECEIVER_PIN 2 // put 2 = D4 on nodemcu, 2 = D2 on arduino + #define RF_EMITTER_PIN 15 //put 15 = D8 on nodemcu + + #ifdef ESP8266 + #define IR_EMITTER_PIN 16 // 16 = D0 on nodemcu #define only usefull for ESP8266 + //RF PIN definition + #define RF_RECEIVER_PIN 0 // 0 = D3 on nodemcu + #define BT_RX D7 //ESP8266 RX connect HM-10 TX + #define BT_TX D6 //ESP8266 TX connect HM-10 RX + #else + //IMPORTANT NOTE: On arduino UNO connect IR emitter pin to D9 , comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library IRremote/IRremoteInt.h so as to free pin D3 for RF RECEIVER PIN + //RF PIN definition + #define RF_RECEIVER_PIN 1 // 1 = D3 on arduino + #define BT_RX 5 //arduino RX connect HM-10 TX + #define BT_TX 6 //arduino TX connect HM-10 RX + #endif #endif + +#ifdef Classic_Wiring // Without Support for I2C Modules + #define DHT_RECEIVER_PIN 0 //on nodeMCU this is D3 GPIO0 + #define IR_RECEIVER_PIN 2 // put 2 = D4 on nodemcu, 2 = D2 on arduino + #define RF_EMITTER_PIN 4 //put 4 = D2 on nodemcu, 4 = D4 on arduino + + #ifdef ESP8266 + #define IR_EMITTER_PIN 14 // 14 = D5 on nodemcu #define only usefull for ESP8266 + //RF PIN definition + #define RF_RECEIVER_PIN 5 // 5 = D1 on nodemcu + #define BT_RX D7 //ESP8266 RX connect HM-10 TX + #define BT_TX D6 //ESP8266 TX connect HM-10 RX + #else + //IMPORTANT NOTE: On arduino UNO connect IR emitter pin to D9 , comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library IRremote/IRremoteInt.h so as to free pin D3 for RF RECEIVER PIN + //RF PIN definition + #define RF_RECEIVER_PIN 1 // 1 = D3 on arduino + #define BT_RX 5 //arduino RX connect HM-10 TX + #define BT_TX 6 //arduino TX connect HM-10 RX + #endif +#endif + //RF number of signal repetition #define RF_EMITTER_REPEAT 20 diff --git a/ZsensorBH1750.ino b/ZsensorBH1750.ino new file mode 100644 index 00000000..f5984306 --- /dev/null +++ b/ZsensorBH1750.ino @@ -0,0 +1,150 @@ +/* + OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + This is the Light Meter Addon: + - Measures ambient Light Intensity in Lux (lx), Foot Candela (ftcd) and Watt/m^2 (wattsm2) + - Required Hardware Module: BH1750 + + Connection Schemata: + -------------------- + + BH1750 ------> Arduino Uno ----------> ESP8266 + ============================================== + Vcc ---------> 5V -------------------> Vu (5V) + GND ---------> GND ------------------> GND + SCL ---------> Pin A5 ---------------> D1 + SDA ---------> Pin A4 ---------------> D2 + ADD ---------> N/C (Not Connected) --> N/C (Not Connected) + + Copyright: (c) Hans-Juergen Dinges + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifdef ZsensorBH1750 +#include "math.h" // Library for trig and exponential functions +#include "Wire.h" // Library for communication with I2C / TWI devices +#define bh1750_always true // if false when the current value for light Level (Lux) is the same as previous one don't send it by MQTT +#define TimeBetweenReadingBH1750 30000 + +/*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ +#define LUX "home/LIGHTtoMQTT/lux" +#define FTCD "home/LIGHTtoMQTT/ftcd" +#define WATTSM2 "home/LIGHTtoMQTT/wattsm2" + +//Time used to wait for an interval before resending measured values +unsigned long timebh1750 = 0; +int BH1750_i2c_addr = 0x23; // Light Sensor I2C Address + +void setupZsensorBH1750() +{ + Wire.begin(); + Wire.beginTransmission(BH1750_i2c_addr); + Wire.write(0x10); // Set resolution to 1 Lux + Wire.endTransmission(); + delay(300); +} + +// void loop() +void MeasureLightIntensity() +{ + if (millis() > (timebh1750 + TimeBetweenReadingBH1750)) {//retriving value of Lux, FtCd and Wattsm2 from BH1750 + timebh1750 = millis(); + unsigned int i=0; + static float persistedll; + static float persistedlf; + static float persistedlw; + unsigned int Lux; + float FtCd; + float Wattsm2; + + // Check if reads failed and exit early (to try again). + Wire.beginTransmission(BH1750_i2c_addr); + Wire.requestFrom(BH1750_i2c_addr, 2); + while(Wire.available()) // + { + i <<=8; + i|= Wire.read(); + } + if (Wire.endTransmission() != 0 ) { + trc(F("Failed to read from LightSensor BH1750!")); + }else{ + + // Calculate the Values + Lux = i/1.2; // Convert to Lux + FtCd = Lux/10.764; + Wattsm2 = Lux/683.0; + + /* + Useful Information ;-) + lux (lx) # 1 lx = 1 lm/m² = 1 cd·sr·m⁻². + meter-candle (m-cd) # 1 m·cd = 1 lx = 1 lm/m² = 1 cd·sr·m⁻². + centimeter-candle (cm-sd) # 1 m·cd = 1 lx = 1 lm/m² = 1 cd·sr·m⁻². + foot-candle (ft-c) # + phot (ph) # 1 ph = 1 lm/cm² = 10,000 lm/m² - 10,000 lx = 10 klx + nox (nx) # 1 nox = 1 millilux + candela steradin/meter2(cd·sr·m⁻²) # 1 lx = 1 lm/m² = 1 cd·sr·m⁻² + lumen/meter2 (lm·m⁻²) # 1 lx = 1 lm/m² = 1 cd·sr·m⁻² + lumen/centimeter2 (lm·cm⁻²) # 1 lm/cm² = 10,000 lx = 10,000 cd·sr·m⁻² + lumen/foot2 (lm·ft⁻²) # (lm·ft⁻²) + watt/centimeter2 at 555nm (W·cm⁻²) # + */ + + // Generate Lux + if(Lux != persistedll || bh1750_always){ + char lux[7]; + sprintf(lux,"%d",Lux); + trc(F("Sending Lux to MQTT")); + trc(String(lux)); + client.publish(LUX,lux); + }else{ + trc(F("Same lux don't send it")); + } + + // Generate FtCd + if(FtCd != persistedlf || bh1750_always){ + char ftcd[7]; + dtostrf(FtCd,4,2,ftcd); + trc(F("Sending FtCd to MQTT")); + trc(String(ftcd)); + client.publish(FTCD,ftcd); + }else{ + trc(F("Same ftcd don't send it")); + } + + // Generate Watts/m2 + if(Wattsm2 != persistedlw || bh1750_always){ + char wattsm2[7]; + dtostrf(Wattsm2,6,4,wattsm2); + trc(F("Sending Wattsm2 to MQTT")); + trc(String(wattsm2)); + client.publish(WATTSM2,wattsm2); + }else{ + trc(F("Same wattsm2 don't send it")); + } + + } + persistedll = Lux; + persistedlf = FtCd; + persistedlw = Wattsm2; + } +} + +#endif diff --git a/ZsensorBME280.ino b/ZsensorBME280.ino new file mode 100644 index 00000000..6e47bb6a --- /dev/null +++ b/ZsensorBME280.ino @@ -0,0 +1,223 @@ +/* + OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + This is the Climate Addon: + - Measures Temperature, Humidity and Pressure + - Generates Values for: Temperature in degrees C and F, Humidity in %, Pressure in Pa, Altitude in Meter and Feet + - Required Hardware Module: Bosch BME280 + - Required Library: SparkFun BME280 Library v1.1.0 by Marshall Taylor + + Connection Schemata: + -------------------- + + bme280 ------> Arduino Uno ----------> ESP8266 + ============================================== + Vcc ---------> 5V -------------------> Vu (5V) + GND ---------> GND ------------------> GND + SCL ---------> Pin A5 ---------------> D1 + SDA ---------> Pin A4 ---------------> D2 + + Copyright: (c) Hans-Juergen Dinges + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifdef ZsensorBME280 +#include "Wire.h" // Library for communication with I2C / TWI devices +#include +#include "SparkFunBME280.h" + +#define bme280_always true // if false when the current value for light Level (Lux) is the same as previous one don't send it by MQTT +#define TimeBetweenReadingbme280 30000 + +/*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ +#define TEMPBMEC "home/CLIMAtoMQTT/bme/tempc" +#define TEMPBMEF "home/CLIMAtoMQTT/bme/tempf" +#define HUMBME "home/CLIMAtoMQTT/bme/hum" +#define PRESSBME "home/CLIMAtoMQTT/bme/pa" +#define ALTIBMEM "home/CLIMAtoMQTT/bme/altim" +#define ALTIBMEFT "home/CLIMAtoMQTT/bme/altift" + +//Time used to wait for an interval before resending measured values +unsigned long timebme280 = 0; + +//Global sensor object +BME280 mySensor; + +void setupZsensorBME280() +{ + mySensor.settings.commInterface = I2C_MODE; + mySensor.settings.I2CAddress = 0x76; // Bosch BME280 I2C Address + + //***Operation settings*****************************// + + // runMode Setting - Values: + // ------------------------- + // 0, Sleep mode + // 1 or 2, Forced mode + // 3, Normal mode + mySensor.settings.runMode = 3; //Normal mode + + // tStandby Setting - Values: + // -------------------------- + // 0, 0.5ms + // 1, 62.5ms + // 2, 125ms + // 3, 250ms + // 4, 500ms + // 5, 1000ms + // 6, 10ms + // 7, 20ms + mySensor.settings.tStandby = 0; + + // Filter can be off or number of FIR coefficients - Values: + // --------------------------------------------------------- + // 0, filter off + // 1, coefficients = 2 + // 2, coefficients = 4 + // 3, coefficients = 8 + // 4, coefficients = 16 + mySensor.settings.filter = 0; + + // tempOverSample - Values: + // ------------------------ + // 0, skipped + // 1 through 5, oversampling *1, *2, *4, *8, *16 respectively + mySensor.settings.tempOverSample = 1; + + // pressOverSample - Values: + // ------------------------- + // 0, skipped + // 1 through 5, oversampling *1, *2, *4, *8, *16 respectively + mySensor.settings.pressOverSample = 1; + + // humidOverSample - Values: + // ------------------------- + // 0, skipped + // 1 through 5, oversampling *1, *2, *4, *8, *16 respectively + mySensor.settings.humidOverSample = 1; + + delay(10); // Gives the Sensor enough time to turn on (The BME280 requires 2ms to start up) + Serial.print("Bosch BME280 Initialized - Result of .begin(): 0x"); + Serial.println(mySensor.begin(), HEX); +} + +void MeasureTempHumAndPressure() +{ + + if (millis() > (timebme280 + TimeBetweenReadingbme280)) { + timebme280 = millis(); + unsigned int i=0; + static float persisted_bme_tempc; + static float persisted_bme_tempf; + static float persisted_bme_hum; + static float persisted_bme_pa; + static float persisted_bme_altim; + static float persisted_bme_altift; + + float BmeTempC = mySensor.readTempC(); + float BmeTempF = mySensor.readTempF(); + float BmeHum = mySensor.readFloatHumidity(); + float BmePa = mySensor.readFloatPressure(); + float BmeAltiM = mySensor.readFloatAltitudeMeters(); + float BmeAltiFt = mySensor.readFloatAltitudeFeet(); + + // Check if reads failed and exit early (to try again). + if (isnan(BmeTempC) || isnan(BmeTempF) || isnan(BmeHum) || isnan(BmePa) || isnan(BmeAltiM) || isnan(BmeAltiFt)) { + trc(F("Failed to read from Weather Sensor BME280!")); + }else{ + // Generate Temperature in degrees C + if(BmeTempC != persisted_bme_tempc || bme280_always){ + char bmetempc[7]; + dtostrf(BmeTempC,4,2,bmetempc); + trc(F("Sending Degrees C to MQTT")); + trc(String(bmetempc)); + client.publish(TEMPBMEC,bmetempc); + }else{ + trc(F("Same Degrees C don't send it")); + } + + // Generate Temperature in degrees F + if(BmeTempF != persisted_bme_tempf || bme280_always){ + char bmetempf[7]; + dtostrf(BmeTempF,4,2,bmetempf); + trc(F("Sending Degrees F to MQTT")); + trc(String(bmetempf)); + client.publish(TEMPBMEF,bmetempf); + }else{ + trc(F("Same Degrees F don't send it")); + } + + // Generate Humidity in percent + if(BmeHum != persisted_bme_hum || bme280_always){ + char bmehum[7]; + dtostrf(BmeHum,4,2,bmehum); + trc(F("Sending Humidity to MQTT")); + trc(String(bmehum)); + client.publish(HUMBME,bmehum); + }else{ + trc(F("Same Humidity don't send it")); + } + + // Generate Pressure in Pa + if(BmePa != persisted_bme_pa || bme280_always){ + char bmepa[7]; + dtostrf(BmePa,4,2,bmepa); + trc(F("Sending Pressure to MQTT")); + trc(String(bmepa)); + client.publish(PRESSBME,bmepa); + }else{ + trc(F("Same Pressure don't send it")); + } + + // Generate Altitude in Meter + if(BmeAltiM != persisted_bme_altim || bme280_always){ + char bmealtim[7]; + dtostrf(BmeAltiM,4,2,bmealtim); + trc(F("Sending Altitude Meter to MQTT")); + trc(String(bmealtim)); + client.publish(ALTIBMEM,bmealtim); + }else{ + trc(F("Same Altitude Meter don't send it")); + } + + // Generate Altitude in Feet + if(BmeAltiFt != persisted_bme_altift || bme280_always){ + char bmealtift[7]; + dtostrf(BmeAltiFt,4,2,bmealtift); + trc(F("Sending Altitude Feet to MQTT")); + trc(String(bmealtift)); + client.publish(ALTIBMEFT,bmealtift); + }else{ + trc(F("Same Altitude Feet don't send it")); + } + + } + persisted_bme_tempc = BmeTempC; + persisted_bme_tempf = BmeTempF; + persisted_bme_hum = BmeHum; + persisted_bme_pa = BmePa; + persisted_bme_altim = BmeAltiM; + persisted_bme_altift = BmeAltiFt; + + } +} + +#endif diff --git a/ZsensorDHT.ino b/ZsensorDHT.ino index 78197a3b..78be0cc4 100644 --- a/ZsensorDHT.ino +++ b/ZsensorDHT.ino @@ -32,12 +32,12 @@ #include #define dht_always true // if false when the current value for temp or hum is the same as previous one don't send it by MQTT #define TimeBetweenReading 30000 -DHT dht(D3,DHT22); //on nodeMCU this is D3 GPIO0 +DHT dht(DHT_RECEIVER_PIN,DHT22); // Defined in User_config.h /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define HUM1 "home/433toMQTT/dht1/hum" -#define TEMP1 "home/433toMQTT/dht1/temp" +#define HUM1 "home/DHTtoMQTT/dht1/hum" +#define TEMP1 "home/DHTtoMQTT/dht1/temp" //Time used to wait for an interval before resending temp and hum unsigned long timedht = 0; diff --git a/libraries/Adafruit_Sensor b/libraries/Adafruit_Sensor new file mode 160000 index 00000000..e985f225 --- /dev/null +++ b/libraries/Adafruit_Sensor @@ -0,0 +1 @@ +Subproject commit e985f2253a687ef377cde3dcfb1f788830d1bc09 diff --git a/libraries/DHT-sensor-library b/libraries/DHT-sensor-library new file mode 160000 index 00000000..c9789777 --- /dev/null +++ b/libraries/DHT-sensor-library @@ -0,0 +1 @@ +Subproject commit c97897771807613d456b318236e18a04b013410b diff --git a/libraries/SparkFun_BME280_Arduino_Library b/libraries/SparkFun_BME280_Arduino_Library new file mode 160000 index 00000000..9a2a24d2 --- /dev/null +++ b/libraries/SparkFun_BME280_Arduino_Library @@ -0,0 +1 @@ +Subproject commit 9a2a24d2b25db1f430d7b6b9ff62c388d72c60e1