-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp8266-bme280-MQTT.ino
231 lines (181 loc) · 5.99 KB
/
esp8266-bme280-MQTT.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
This sketch publishes temperature humidity and barometric pressure data from a bme280
device to a MQTT topic.
It also publish the battery voltage but you have to install a voltage divider. (see
https://en.wikipedia.org/wiki/Voltage_divider if needed).
Vin <---o
__|_
| |
| R1 |
|____|
| R2
o-------> Vout --> A0 Vout = --------- x Vin
__|_ (R1 + R2)
| |
| R2 | Vout MUST BE < 1 V !!!
|____|
| (I choose R1 = 470 k and R2 = 100 k
_|_ but feel free to choose your own values!)
/// Gnd
This sketch goes in deep sleep mode once the data has been sent to the MQTT
topic and wakes up periodically (configure SLEEP_DELAY_IN_SECONDS accordingly).
Hookup guide:
- connect D0 pin to RST pin in order to enable the ESP8266 to wake up periodically
(only after uploading the sketch!)
- connect the Vout of your voltage divider to NodeMCU A0
- connect the bme280:
BME280 GND --> NodeMCU GND
BME280 3.3V --> NodeMCU 3V3
BME280 SDA --> NodeMCU D3
BME280 SCL --> NodeMCU D2
This sketch is the result of the compilation of some diyers and arduinoers. Thanks to
them!
And of course it is in public domain.
Enjoy!
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SLEEP_DELAY_IN_SECONDS 60
Adafruit_BME280 bme;
float tempC = 1; // Temperature in Celsius degrees
float humidity = 1; // Relative Humidity in %
float pressHpa = 1; // Barometric Pressure in hectopascals
float level = 1; // Battery voltage in volts
char temperatureString[6];
char humidityString[6];
char pressHpaString[6];
char voltageString[6];
String responseJson;
const char* ssid = ".............."; // WiFi network SSID
const char* password = ".........."; // WiFi network password
const char* mqtt_server = "......."; // MQTT broker URL
const char* mqtt_username = ""; // MQTT client user name, if needed
const char* mqtt_password = ""; // MQTT client password, if needed
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// setup serial port
Serial.begin(115200);
// connecting to the bme280 device
if(!bme.begin())
{
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1)
{
yield();
delay(2000);
}
}
delay(500);
}
void setup_wifi() // Connecting to the WiFi network
{
while (WiFi.status() != WL_CONNECTED)
{
Serial.print("Connecting to WiFi network ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
}
void callback(char* topic, byte* payload, unsigned int length) // Reading received topic
{
Serial.print("Reveived topic [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
void connect_mqtt() //Connecting to the MQTT broker
{
while (!client.connected())
{
Serial.print("Connecting to MQTT broker at ");
Serial.print(mqtt_server);
Serial.print("...");
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_username, mqtt_password))
{
Serial.println(" connected!");
// Once connected, publish an announcement...
Serial.println("Sending topic(s)...");
client.publish("sensor/external", responseJson.c_str(), true);
client.publish("sensors/test/voltage", dtostrf(level/100, 2, 2, voltageString));
}
else
{
Serial.print("Connection failed, rc=");
Serial.print(client.state());
Serial.println(" will retry in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop()
{
// Reading temperature
tempC = bme.readTemperature();
// Reading barometric pressure
pressHpa = bme.readPressure() / 100.0;
// Reading humidity
humidity = bme.readHumidity();
// Reading voltage
float rawLevel = analogRead(A0);
level = map(rawLevel, 98, 192, 200, 400); // You need to adjust these values according to the voltage divider you install
Serial.println("");
Serial.println("Temperature:");
printValueAndUnits(tempC, " °C");
Serial.println("Barometric Pressure:");
printValueAndUnits(pressHpa, " hPa");
Serial.println("Relative Humidity:");
printValueAndUnits(humidity, " %");
Serial.println("Voltage: ");
printValueAndUnits(level/100, " V");
responseJson = "";
responseJson += "{";
responseJson += "\"temperature\":" + String(tempC) + ",";
responseJson += "\"humidity\":" + String(humidity) + ",";
responseJson += "\"pressure\":" + String(pressHpa) + ",";
responseJson += "\"voltage\":" + String(level/100);
responseJson += "}";
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
connect_mqtt();
delay(100);
Serial.println("Closing connection to MQTT broker...");
client.disconnect();
delay(100);
Serial.print("Closing connection to WiFi network ");
Serial.print(ssid);
Serial.println("...");
WiFi.disconnect();
delay(100);
Serial.print("Going to sleep for ");
Serial.print(SLEEP_DELAY_IN_SECONDS);
Serial.println(" seconds...");
ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
delay(100);
}
void printValueAndUnits(float value, String units)
{
Serial.print(" ");
Serial.print(value);
Serial.print(" ");
Serial.println(units);
Serial.println("");
}