-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIION-Monitor-Arduino.ino
More file actions
129 lines (92 loc) · 2.56 KB
/
IION-Monitor-Arduino.ino
File metadata and controls
129 lines (92 loc) · 2.56 KB
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
// Copyright © 2018 Stanislav Hnatiuk. All rights reserved.
#define SERIAL_ENABLED 0
#define TEMPERATURE_ENABLED 1
#define ENERGY_ENABLED 1
#define ETHERNET_ENABLED 1
#include "ThreadLite.h"
#if TEMPERATURE_ENABLED
#include "Temperature.h"
void temperatureUpdate();
Temperature tmpr = Temperature(0x28);
Thread temperatureT = Thread(temperatureUpdate, 1);
#endif // TEMPERATURE_ENABLED
#if ENERGY_ENABLED
#include "Energy.h"
void consumptionUpdate();
Energy enrg;
Thread consumptionT = Thread(consumptionUpdate, 150);
#endif // ENERGY_ENABLED
#if ETHERNET_ENABLED
#include "request.h"
#endif // ETHERNET_ENABLED
//! Настройка устройства.
void setup() {
#if SERIAL_ENABLED
Serial.begin(9600);
while (!Serial) {}
Serial.println("Serial enabled.");
#endif // SERIAL_ENABLED
#if ETHERNET_ENABLED
startServer();
#endif // ETHERNET_ENABLED
#if TEMPERATURE_ENABLED
tmpr.find();
#endif // TEMPERATURE_ENABLED
}
//! Основная программа.
void loop() {
#if ETHERNET_ENABLED
checkRequest();
#endif
#if TEMPERATURE_ENABLED
temperatureT.run();
#endif // TEMPERATURE_ENABLED
#if ENERGY_ENABLED
consumptionT.run();
#endif // ENERGY_ENABLED
}
#if ENERGY_ENABLED
//! Процесс обновления буфера потребления энергии.
void consumptionUpdate() {
enrg.read();
}
#endif // ENERGY_ENABLED
#if TEMPERATURE_ENABLED
//! Процесс обновления буфера температур.
void temperatureUpdate() {
static bool state = true;
if (state) {
// Режим преобразования. Длительность 750+ миллисекунд.
temperatureT.setInterval(999);
state = !state;
tmpr.conversion();
} else {
// Режим считывания.
temperatureT.setInterval(1);
state = !state;
tmpr.read();
}
}
#endif // TEMPERATURE_ENABLED
#if ETHERNET_ENABLED
//! Вывод данных в ответ на запрос.
void responsePrint(EthernetClient& client, const BaseSensor* obj) {
for (uint8_t i = 0; i < obj->getCount(); ++i) {
client.print(obj->getAddr(i));
client.print(" ");
client.println(obj->getValue(i));
}
}
#endif // ETHERNET_ENABLED
#if ETHERNET_ENABLED
//! Ответить на запрос.
void requestResponse(EthernetClient& client) {
client.println("HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Type: text/plain\n");
#if TEMPERATURE_ENABLED
responsePrint(client, &tmpr);
#endif // TEMPERATURE_ENABLED
#if ENERGY_ENABLED
responsePrint(client, &enrg);
#endif // ENERGY_ENABLED
}
#endif // ETHERNET_ENABLED