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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPUpdateServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
#define PIN_RELAY D1
// Время работы (более понятные имена)
const uint8_t DAY_START_HOUR = 7; // Начало дня
const uint8_t NIGHT_START_HOUR = 23; // Начало ночи
const uint8_t DAY_WORK_MINUTES = 10; // Дневной интервал (минут)
const uint8_t NIGHT_WORK_MINUTES = 20; // Ночной интервал (минут)
const uint8_t DAY_START_MINUTE = 25; // Старт дневного интервала
// Ночные интервалы (для ясности)
const uint8_t NIGHT_INTERVAL1_START = 0;
const uint8_t NIGHT_INTERVAL2_START = 40;
// Интервал синхронизации (сейчас 30 минут для тестирования?)
const uint32_t SYNC_INTERVAL_MS = 30 * 60 * 1000; // 30 минут
// const char *ssid = "link";
const char *ssid = "MikroTik-2";
const char *pass = "dkfgf#*12091997";
const char *ntp_server = "ntp3.vniiftri.ru";
const char *host = "89.110.92.137";
const uint16_t port = 8000;
const char *address_page = "/";
const uint32_t utcOffsetInSeconds = 10800;
const uint32_t utcPeriodMseconds = 30 * 60 * 1000; // 86400000-р/сут; 604800000-р/нед
uint32_t lastSync = 0;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntp_server, utcOffsetInSeconds, utcPeriodMseconds);
const uint16_t TIME_SLEEP = 60000;
const uint8_t NUMBER_OF_ATTEMPTS = 20;
bool flagBegining = true;
const char *mqtt_client = "esp8266-761924";
const char *mqtt_client2 = "Villa_Septik";
const char *mqtt_user = "mqtt";
const char *mqtt_pass = "qwe1243";
const char *mqtt_server = "89.110.92.137";
const char *mqtt_port = "1883";
const char *outTopicData = "/data";
ESP8266WebServer server(80);
ESP8266HTTPUpdateServer httpUpdater;
WiFiClient espClient;
PubSubClient client(espClient);
//-----------------------------------
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());
}
//-----------------------------------
void mqttDataOut(uint8_t a)
{
String topic, topicValue;
topic = "/";
topic += mqtt_client2;
topic += outTopicData;
topicValue = a;
topicValue += ";;;;";
uint8_t count = NUMBER_OF_ATTEMPTS;
while (!mqtt_publish(client, topic, topicValue) && count--)
{
mqtt_publish(client, topic, topicValue);
delay(500);
}
}
//-----------------------------------
bool setupMQTT()
{
client.setServer(mqtt_server, String(mqtt_port).toInt());
Serial.print("MQTT connect : ");
Serial.println(mqtt_server);
uint8_t count = NUMBER_OF_ATTEMPTS;
while (!(client.connect(mqtt_client, mqtt_user, mqtt_pass)) && count--)
{
Serial.print(count);
Serial.print('>');
delay(500);
}
if (client.connected())
{
Serial.println("MQTT connected - OK !");
return true;
}
else
{
return false;
}
}
//===========================================
// подключение к WiFi
bool setupWiFi()
{
digitalWrite(LED_BUILTIN, LOW);
Serial.println("\n\nSetup WiFi: ");
WiFi.begin(ssid, pass);
uint8_t count = NUMBER_OF_ATTEMPTS;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(count);
Serial.print('>');
if (!count--)
return false;
delay(500);
}
digitalWrite(LED_BUILTIN, HIGH);
//-----------------------------------
// индикация IP
Serial.print("\nWiFi connected !\nIP: ");
Serial.println(WiFi.localIP());
//-----------------------------------
// индикация силы сигнала
int8_t RSSI_MAX = -50;
int8_t RSSI_MIN = -100;
int8_t dBm = WiFi.RSSI();
Serial.print("RSSI dBm = ");
Serial.println(dBm);
String l = "";
(dBm <= RSSI_MIN) ? l += 0 : (dBm >= RSSI_MAX) ? l += 100
: l += 2 * (dBm + 100);
Serial.print("RSSI % = ");
Serial.println(l);
return true;
}
//===========================================
// синхронизация с NTP:
bool syncNTP()
{
Serial.println("\nСинхронизируем время: ");
uint8_t count = NUMBER_OF_ATTEMPTS;
while (!timeClient.update())
{
Serial.print(count);
Serial.print('.');
if (!count--)
return false;
delay(500);
}
Serial.println(timeClient.getFormattedTime());
return true;
}
//===========================================
// Проверка валидности времени:
bool isTimeValid()
{
// Проверяем, что время получено (не эпоха 1970 года)
return timeClient.getEpochTime() > 1000000;
}
bool calculateRelayState()
{
uint8_t h = timeClient.getHours();
uint8_t m = timeClient.getMinutes();
// Дневной режим
if (h >= DAY_START_HOUR && h < NIGHT_START_HOUR)
{
return (m >= DAY_START_MINUTE &&
m < DAY_START_MINUTE + DAY_WORK_MINUTES);
}
// Ночной режим
if (m < NIGHT_WORK_MINUTES) // Первый интервал
return true;
if (m >= NIGHT_INTERVAL2_START &&
m < NIGHT_INTERVAL2_START + NIGHT_WORK_MINUTES) // Второй интервал
return true;
return false;
}
void controlRelay()
{
static bool lastRelayState = false;
bool newRelayState = calculateRelayState();
if (newRelayState != lastRelayState)
{
if (setupMQTT())
{
mqttDataOut(newRelayState);
Serial.println("Данные успешно отправлены");
}
digitalWrite(PIN_RELAY, newRelayState ? HIGH : LOW);
Serial.printf("Реле %s в %02d:%02d\n",
newRelayState ? "ВКЛ" : "ВЫКЛ",
timeClient.getHours(),
timeClient.getMinutes());
lastRelayState = newRelayState;
}
}
// void goToDeepSleep()
// {
// Serial.println("Переход в глубокий сон на 1 минуту...");
// server.close(); // Закрываем сервер перед сном
// delay(100);
// // Настраиваем пробуждение через TIME_SLEEP миллисекунд
// ESP.deepSleep(TIME_SLEEP * 1000); // deepSleep принимает микросекунды!
// }
//===========================================
void setup()
{
Serial.begin(115200);
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_RELAY, LOW);
timeClient.begin();
server.begin();
httpUpdater.setup(&server);
lastSync = millis();
}
//===========================================
void loop()
{
// Синхронизация времени
if (millis() - lastSync >= SYNC_INTERVAL_MS || flagBegining)
{
if (setupWiFi())
{
if (syncNTP())
{
Serial.println("Время успешно синхронизировано");
}
}
// Всегда отключаем Wi-Fi после синхронизации
// WiFi.disconnect();
// delay(100);
// WiFi.mode(WIFI_OFF);
// Serial.println("\nWi-Fi отключен для экономии энергии");
lastSync = millis();
flagBegining = false;
}
// Управление реле только если время валидно
if (isTimeValid())
{
controlRelay();
}
// if (!flagBegining && WiFi.getMode() == WIFI_OFF)
// {
// goToDeepSleep();
// }
server.handleClient();
delay(10);
}
// if (millis() - lastSync >= utcPeriodMseconds || flagBegining)
// {
// if (setupWiFi())
// syncNTP();
// WiFi.disconnect(); // Отключаемся от роутера
// delay(100); // Небольшая задержка для завершения процесса
// WiFi.mode(WIFI_OFF); // Полностью выключаем Wi-Fi
// Serial.println("Wi-Fi отключен.");
// lastSync = millis();
// }
// //-----------------------------
// // Работа по дневному тарифу:
// if (timeClient.getHours() >= TIME_DAY && timeClient.getHours() < TIME_NIGHT)
// {
// if (timeClient.getMinutes() >= TIME_BEGINNING_DAY && timeClient.getMinutes() < TIME_BEGINNING_DAY + TIME_WORK_DAY_MINUT)
// digitalWrite(PIN_RELAY, HIGH);
// }
// else
// {
// digitalWrite(PIN_RELAY, LOW);
// }
// //-----------------------------
// // Работа по ночному тарифу:
// if (timeClient.getHours() >= TIME_NIGHT && timeClient.getHours() < TIME_DAY)
// {
// if (timeClient.getMinutes() >= 0 && timeClient.getMinutes() < TIME_WORK_NIGHT_MINUT)
// {
// digitalWrite(PIN_RELAY, HIGH);
// }
// else
// {
// digitalWrite(PIN_RELAY, LOW);
// }
// if (timeClient.getMinutes() >= 40 && timeClient.getMinutes() < 40 + TIME_WORK_NIGHT_MINUT)
// {
// digitalWrite(PIN_RELAY, HIGH);
// }
// else
// {
// digitalWrite(PIN_RELAY, LOW);
// }
// }
// server.handleClient();
// delay(10);
//===========================================
|