-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_mqtt.ino
223 lines (185 loc) · 5.8 KB
/
client_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
// Idea: https://github.com/flazer/weather-station
#include <ArduinoJson.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
#define FORCE_DEEPSLEEP
// Import settings
#include "config.h"
ESP8266WiFiMulti WiFiMulti;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
BME280I2C bme;
const String sw_version = "2021.6.0";
// SETUP
void setup() {
if (DEBUGGING == true) {
Serial.begin(115200);
splashScreen();
}
Wire.begin();
pinMode(A0, INPUT);
Serial.println("-----------");
Serial.print("Searching sensor...");
int sensorSearchCnt = 0;
while(!bme.begin()) {
sensorSearchCnt++;
Serial.print(".");
if (sensorSearchCnt > 4) {
Serial.println();
Serial.println(" Couldn't find sensor.");
Serial.println(" Retrying after DS.");
sensorSearchCnt = 0;
go_DS(5);
}
delay(100);
}
Serial.println();
switch (bme.chipModel()) {
case BME280::ChipModel_BME280:
Serial.println("Found sensor - Model: BME280");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found sensor - Model: BMP280");
Serial.println(" >> Humidity not available.");
break;
default:
Serial.println("Found unsupported sensor.");
break;
}
startWiFi();
}
void loop() {
runMQTT();
sendMQTT_v2();
// delay(150);
go_DS(min2sleep);
}
void sendMQTT_v2() {
float temp(NAN), hum(NAN), pres(NAN);
BME280::Mode_Forced;
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_hPa);
bme.read(pres, temp, hum, tempUnit, presUnit);
char* tempUnitSymb;
if (tempUnit == 0) { tempUnitSymb = "°C"; } else { tempUnitSymb = "°F"; }
StaticJsonDocument<200> doc;
JsonObject obj_0 = doc.createNestedObject("device");
obj_0["friendlyName"] = friendlyName;
obj_0["sensorType"] = "BME280";
obj_0["version"] = sw_version;
obj_0["debugging"] = DEBUGGING;
// obj_0["battery"] = batPer;
JsonObject obj_1 = doc.createNestedObject("values");
obj_1["temp"] = round(temp*10)/10;
obj_1["tempUnit"] = tempUnitSymb;
obj_1["hum"] = round(hum*10)/10;
obj_1["pres"] = round(pres);
obj_1["presUnit"] = "hPa";
// Start a new line
Serial.println();
// Generate the prettified JSON and send it to the Serial port.
serializeJsonPretty(doc, Serial);
char buffer[256];
serializeJson(doc, buffer);
Serial.println();
if (client.publish(topic_data_hassio, buffer) == true) {
Serial.println("Sent message");
} else {
Serial.println("Error sending message");
}
}
/**
* Establish WiFi-Connection
* If connection times out device will
* enter deep_sleep and restarts afterwards.
*/
void startWiFi() {
Serial.println("");
Serial.println("-------");
WiFi.mode(WIFI_STA);
Serial.println("(re)conneting wifi with following credentials:");
Serial.print("SSID: ");
Serial.println(wifi_ssid);
Serial.print("Key: ");
Serial.println(wifi_password);
Serial.print("Device Name: ");
Serial.println(espName);
WiFi.hostname(espName);
WiFiMulti.addAP(wifi_ssid, wifi_password);
int tryCnt = 0;
Serial.print("Connecting...");
while (WiFiMulti.run() != WL_CONNECTED) {
delay(100);
Serial.print(".");
tryCnt++;
if (tryCnt > 25) {
Serial.println("");
Serial.println("Couldn't connect to WiFi. >> Deep Sleep");
go_DS(5);
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP adress: ");
Serial.println(WiFi.localIP());
delay(100);
}
// Establish MQTT Connection
void runMQTT() {
Serial.println("");
Serial.println("-------");
Serial.println("Starting MQTT-Client with following credentials:");
Serial.print("Host: ");
Serial.println(mqtt_server);
Serial.print("Port: ");
Serial.println(mqtt_port);
Serial.print("User: ");
Serial.println(mqtt_user);
Serial.print("Password: ");
Serial.println(mqtt_password);
Serial.print("ClientID: ");
Serial.println(espName);
client.setServer(mqtt_server, mqtt_port);
while (!client.connected()) {
Serial.print("Attempting connection...");
if (client.connect(espName, mqtt_user, mqtt_password)) {
Serial.println(" >>Success!");
client.loop();
} else {
Serial.println(" >> Failed!");
Serial.println("Couldn't connect to MQTT-Server. >> Deep Sleep");
go_DS(5);
}
}
}
void go_DS(int minutes) {
#ifdef FORCE_DEEPSLEEP
Serial.print("Deep Sleep for ");
Serial.print(minutes);
Serial.println(" minutes.");
ESP.deepSleep(minutes * 60 * 1000000);
#endif
}
void splashScreen() {
for (int i = 0; i <= 5; i++)
Serial.println();
Serial.println("#######################################");
Serial.println("ESP8266 Weather Station");
Serial.println("Device:");
Serial.print("Device Name: ");
Serial.println(espName);
Serial.print("Client Version: ");
Serial.println(sw_version);
Serial.println("Config:");
Serial.print("Debugging enabled: ");
Serial.println(DEBUGGING);
Serial.print("Sensor Model: ");
Serial.println(bme.chipModel());
Serial.println("#######################################");
for (int i = 0; i < 2; i++)
Serial.println();
}