forked from 1technophile/OpenMQTTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZsensorBME280.ino
207 lines (175 loc) · 6.55 KB
/
ZsensorBME280.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifdef ZsensorBME280
#include "Wire.h" // Library for communication with I2C / TWI devices
#include <stdint.h>
#include "SparkFunBME280.h"
//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();
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