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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <PubSubClient.h>
#include "setenv.h"
const uint8_t i2c_SDA = 0;
const uint8_t i2c_SCL = 1;
const uint8_t pinBMEminus = 3; // питание датчика -
const uint8_t pinBMEplus = 19; // питание датчика +
const uint8_t pinVcc = 4; // напряжение батареи
TwoWire i2cBME280 = TwoWire(0);
Adafruit_BME280 bme;
WiFiClient espClient;
PubSubClient client(espClient);
//------------------------------------------------
const char *ssid = WiFi_SSID;
const char *pass = WiFi_PASS;
const char *mqtt_client = MQTT_CLIENT_N;
const char *mqtt_client2 = MQTT_CLIENT_T;
const char *mqtt_user = MQTT_USER;
const char *mqtt_pass = MQTT_PASS;
const char *mqtt_server = MQTT_SERVER;
const char *mqtt_port = MQTT_PORT;
const char *outTopicTemp = "/Temp"; // топик
const char *outTopicPres = "/Pres"; // топик
const char *outTopicHum = "/Hum"; // топик
const char *outTopicVcc = "/Vcc"; // топик
//------------------------------------------------
uint8_t countConnect = 20; // кол-во попыток соединения
uint16_t countPause = 500; // пауза между попытками
uint32_t timeSleep = 60000000; // время сна
uint16_t TimeBeforeBedtime = 500; // время до засыпания
uint8_t countMaxSleep = 10; // не передавать данные не более
// countMaxSleep х timeSleep (10 мин)
// Структура для хранения данных в RTC памяти:
RTC_DATA_ATTR struct
{
float t; // температура
float v; // напряжение
uint16_t p; // давление
uint8_t h; // влажность
uint8_t countSleep; // счетчик предельного кол-ва циклов сна
uint16_t bufRoom[3] = {0, 0, 0};
byte countRoom = 0;
} data;
// Разница при которой будут отправляться данные:
float diffTemp = 0.25; // разница температур
int diffPres = 5; // разница давлений
int diffHum = 5; // разница влажностей
float diffVcc = 0.1; // разница зарядки акб
//-----------------------------------
// inline bool mqtt_subscribe(PubSubClient &client, const String &topic)
// {
// Serial.print("Subscribing to: ");
// Serial.println(topic);
// return client.subscribe(topic.c_str());
// }
//-----------------------------------
inline bool mqtt_publish(PubSubClient &client, const String &topic, const String &value)
{
Serial.print(topic);
Serial.print(" = ");
Serial.println(value);
return client.publish(topic.c_str(), value.c_str());
}
//-----------------------------------
// Отправка данных на MQTT сервер:
void mqttDataOut(float temp, uint16_t pres, uint8_t hum, float vcc)
{
String topic = "/";
topic += mqtt_client2;
topic += outTopicTemp;
while (!mqtt_publish(client, topic, (String)temp))
{
mqtt_publish(client, topic, (String)temp);
}
topic = "/";
topic += mqtt_client2;
topic += outTopicPres;
while (!mqtt_publish(client, topic, (String)pres))
{
mqtt_publish(client, topic, (String)pres);
}
topic = "/";
topic += mqtt_client2;
topic += outTopicHum;
while (!mqtt_publish(client, topic, (String)hum))
{
mqtt_publish(client, topic, (String)hum);
}
topic = "/";
topic += mqtt_client2;
topic += outTopicVcc;
while (!mqtt_publish(client, topic, (String)vcc))
{
mqtt_publish(client, topic, (String)vcc);
}
}
//-----------------------------------
// Подключение к MQTT серверу:
bool reconnect()
{
client.setServer(mqtt_server, String(mqtt_port).toInt());
Serial.println(mqtt_server);
while (!(client.connect(mqtt_client, mqtt_user, mqtt_pass)) && countConnect--)
{
Serial.print(countConnect);
Serial.print('>');
delay(countPause);
}
if (client.connected())
{
return true;
}
else
{
return false;
}
}
//-----------------------------------
// Подключение к сети WiFi:
bool setupWiFi(const char *wifi_ssid, const char *wifi_pass)
{
WiFi.begin(wifi_ssid, wifi_pass);
Serial.println("\n=================");
Serial.print("Setup WiFi: ");
Serial.println(ssid);
while ((WiFi.status() != WL_CONNECTED) && countConnect--)
{
Serial.print(countConnect);
Serial.print('>');
delay(countPause);
}
if (WiFi.status() == WL_CONNECTED)
{
// индикация IP:
Serial.println();
Serial.println(WiFi.localIP());
// индикация силы сигнала:
int8_t dBm = WiFi.RSSI();
Serial.print("RSSI dBm = ");
Serial.println(dBm);
uint8_t quality_RSSI = 2 * (dBm + 100);
if (quality_RSSI >= 100)
quality_RSSI = 100;
Serial.print("RSSI % = ");
Serial.println(quality_RSSI);
Serial.println("=================");
return true;
}
else
{
return false;
}
}
//-----------------------------------
// медиана на 3 значения со своим буфером для усреднения ADC:
uint16_t medianRoom(uint16_t newValRoom)
{
if (!(data.bufRoom[0] + data.bufRoom[1] + data.bufRoom[2]))
data.bufRoom[0] = data.bufRoom[1] = data.bufRoom[2] = newValRoom;
data.bufRoom[data.countRoom] = newValRoom;
if (data.countRoom++ >= 2)
data.countRoom = 0;
uint16_t dataRoom = (max(data.bufRoom[0], data.bufRoom[1]) == max(data.bufRoom[1], data.bufRoom[2]))
? max(data.bufRoom[0], data.bufRoom[2])
: max(data.bufRoom[1], min(data.bufRoom[0], data.bufRoom[2]));
return dataRoom;
}
//-----------------------------------
// Читаем данные с датчика BME280:
bool readData()
{
pinMode(pinBMEplus, OUTPUT);
digitalWrite(pinBMEplus, HIGH);
pinMode(pinBMEminus, OUTPUT);
digitalWrite(pinBMEminus, LOW);
delay(1);
i2cBME280.begin(i2c_SDA, i2c_SCL, 100000);
// Инициализация BME280 по адресу = 0х76 !!!
if (!bme.begin(0x76, &i2cBME280))
{
Serial.println("Could not find a valid BME280 sensor !");
pinMode(pinBMEplus, INPUT);
digitalWrite(pinBMEplus, LOW);
pinMode(pinBMEminus, INPUT);
digitalWrite(pinBMEminus, LOW);
delay(1);
return false;
}
float temp = bme.readTemperature();
uint16_t pres = (bme.readPressure() * 0.7500637554192 / 100);
uint8_t hum = bme.readHumidity();
pinMode(pinBMEplus, INPUT);
digitalWrite(pinBMEplus, LOW);
pinMode(pinBMEminus, INPUT);
digitalWrite(pinBMEminus, LOW);
delay(1);
// Читаем ADC:
uint16_t dataVcc = 0;
for (uint8_t i = 0; i < 3; ++i)
{
dataVcc += analogRead(pinVcc);
}
// Пересчет ADC в напряжение АКБ:
// 2.8 - максимальное напряжение на считываемой ножке
// 4095 - количество отсчетов ADC
// 3.6 - максимальное напряжение АКБ LiFePo4
float vcc = 0;
vcc = 2.8 / 4095 * medianRoom(dataVcc / 3) * 3.6 / 2.8;
// Если данные с датчика и напряжение питания изменились больше чем разница,
// то заносим данные в память RTC и сбрасываем предельный счетчик циклов сна:
if (abs(data.t - temp) >= diffTemp || abs(data.p - pres) >= diffPres ||
abs(data.h - hum) >= diffHum || abs(data.v - vcc) >= diffVcc || data.countSleep <= 1)
{
data.t = temp;
data.p = pres;
data.h = hum;
data.v = vcc;
data.countSleep = countMaxSleep - 1;
return true;
}
else
{
if (--data.countSleep)
{
Serial.println("\nДанные изменились незначительно !\n");
return false;
}
else
{
return true;
}
}
}
//-----------------------------------
void setup()
{
Serial.begin(115200);
if (!readData())
{
esp_deep_sleep(timeSleep);
}
if (!setupWiFi(ssid, pass))
{
esp_deep_sleep(timeSleep);
}
if (!reconnect())
{
esp_deep_sleep(timeSleep);
}
mqttDataOut(data.t, data.p, data.h, data.v);
Serial.println("=================");
Serial.flush();
delay(TimeBeforeBedtime);
esp_deep_sleep(timeSleep);
}
void loop() {}
|