-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path3_AKTOREN.ino
362 lines (333 loc) · 8.35 KB
/
3_AKTOREN.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
class Actor
{
unsigned long powerLast; // Zeitmessung für High oder Low
int dutycycle_actor = 5000;
unsigned char OFF;
unsigned char ON;
public:
unsigned char pin_actor = 9; // the number of the LED pin
String argument_actor;
String name_actor;
unsigned char power_actor;
bool isOn;
bool isInverted = false;
bool switchable; // actors switchable on error events?
bool isOnBeforeError = false; // isOn status before error event
bool actor_state = true; // Error state actor
bool setGrafana = false;
// MQTT Publish
char actor_mqtttopic[50]; // Für MQTT Kommunikation
Actor(String pin, String argument, String aname, bool ainverted, bool aswitchable, bool agrafana)
{
change(pin, argument, aname, ainverted, aswitchable, agrafana);
}
void Update()
{
if (isPin(pin_actor))
{
if (isOn && power_actor > 0)
{
if (millis() > powerLast + dutycycle_actor)
{
powerLast = millis();
}
if (millis() > powerLast + (dutycycle_actor * power_actor / 100L))
{
digitalWrite(pin_actor, OFF);
}
else
{
digitalWrite(pin_actor, ON);
}
}
else
{
digitalWrite(pin_actor, OFF);
}
}
}
void change(const String &pin, const String &argument, const String &aname, const bool &ainverted, const bool &aswitchable, const bool &agrafana)
{
// Set PIN
if (isPin(pin_actor))
{
digitalWrite(pin_actor, HIGH);
pins_used[pin_actor] = false;
millis2wait(10);
}
pin_actor = StringToPin(pin);
if (isPin(pin_actor))
{
pinMode(pin_actor, OUTPUT);
digitalWrite(pin_actor, HIGH);
pins_used[pin_actor] = true;
}
isOn = false;
name_actor = aname;
if (argument_actor != argument)
{
mqtt_unsubscribe();
argument_actor = argument;
mqtt_subscribe();
// MQTT Publish
// argument.toCharArray(actor_mqtttopic, argument.length() + 1);
}
if (ainverted)
{
isInverted = true;
ON = HIGH;
OFF = LOW;
}
else
{
isInverted = false;
ON = LOW;
OFF = HIGH;
}
switchable = aswitchable;
actor_state = true;
isOnBeforeError = false;
setGrafana = agrafana;
}
/* MQTT Publish
void publishmqtt() {
if (client.connected()) {
StaticJsonDocument<256> doc;
JsonObject actorObj = doc.createNestedObject("Actor");
if (isOn) {
doc["State"] = "on";
doc["power"] = String(power_actor);
}
else
doc["State"] = "off";
char jsonMessage[100];
serializeJson(doc, jsonMessage);
char new_argument_actor[50];
new_argument_actor.toCharArray(argument_actor, new_argument_actor.length() + 1);
client.publish(new_argument_actor, jsonMessage);
}
}
*/
void mqtt_subscribe()
{
if (pubsubClient.connected())
{
char subscribemsg[50];
argument_actor.toCharArray(subscribemsg, 50);
DEBUG_MSG("Act: Subscribing to %s\n", subscribemsg);
pubsubClient.subscribe(subscribemsg);
}
}
void mqtt_unsubscribe()
{
if (pubsubClient.connected())
{
char subscribemsg[50];
argument_actor.toCharArray(subscribemsg, 50);
DEBUG_MSG("Act: Unsubscribing from %s\n", subscribemsg);
pubsubClient.unsubscribe(subscribemsg);
}
}
void handlemqtt(char *payload)
{
StaticJsonDocument<128> doc;
DeserializationError error = deserializeJson(doc, (const char *)payload);
if (error)
{
DEBUG_MSG("Act: handlemqtt deserialize Json error %s\n", error.c_str());
return;
}
if (doc["state"] == "off")
{
isOn = false;
power_actor = 0;
return;
}
if (doc["state"] == "on")
{
int newpower = doc["power"];
isOn = true;
power_actor = min(100, newpower);
power_actor = max(0, newpower);
return;
}
}
};
// Initialisierung des Arrays max 8
Actor actors[numberOfActorsMax] = {
Actor("", "", "", false, false, false),
Actor("", "", "", false, false, false),
Actor("", "", "", false, false, false),
Actor("", "", "", false, false, false),
Actor("", "", "", false, false, false),
Actor("", "", "", false, false, false),
Actor("", "", "", false, false, false),
Actor("", "", "", false, false, false)};
// Funktionen für Loop im Timer Objekt
void handleActors()
{
for (int i = 0; i < numberOfActors; i++)
{
actors[i].Update();
yield();
}
}
/* Funktionen für Web */
void handleRequestActors()
{
int id = server.arg(0).toInt();
StaticJsonDocument<1024> doc;
if (id == -1) // fetch all sensors
{
JsonArray actorsArray = doc.to<JsonArray>();
for (int i = 0; i < numberOfActors; i++)
{
JsonObject actorsObj = doc.createNestedObject();
actorsObj["name"] = actors[i].name_actor;
actorsObj["status"] = actors[i].isOn;
actorsObj["power"] = actors[i].power_actor;
actorsObj["mqtt"] = actors[i].argument_actor;
actorsObj["pin"] = PinToString(actors[i].pin_actor);
actorsObj["sw"] = actors[i].switchable;
actorsObj["state"] = actors[i].actor_state;
actorsObj["grafana"] = actors[i].setGrafana;
yield();
}
}
else
{
doc["name"] = actors[id].name_actor;
doc["mqtt"] = actors[id].argument_actor;
doc["sw"] = actors[id].switchable;
doc["inv"] = actors[id].isInverted;
doc["grafana"] = actors[id].setGrafana;
}
String response;
serializeJson(doc, response);
server.send(200, "application/json", response);
}
void handleSetActor()
{
int id = server.arg(0).toInt();
if (id == -1)
{
id = numberOfActors;
numberOfActors += 1;
if (numberOfActors >= numberOfActorsMax)
return;
}
String ac_pin = PinToString(actors[id].pin_actor);
String ac_argument = actors[id].argument_actor;
String ac_name = actors[id].name_actor;
bool ac_isinverted = actors[id].isInverted;
bool ac_switchable = actors[id].switchable;
bool ac_grafana = actors[id].setGrafana;
for (int i = 0; i < server.args(); i++)
{
if (server.argName(i) == "name")
{
ac_name = server.arg(i);
}
if (server.argName(i) == "pin")
{
ac_pin = server.arg(i);
}
if (server.argName(i) == "script")
{
ac_argument = server.arg(i);
}
if (server.argName(i) == "inv")
{
ac_isinverted = checkBool(server.arg(i));
}
if (server.argName(i) == "sw")
{
ac_switchable = checkBool(server.arg(i));
}
if (server.argName(i) == "grafana")
{
ac_grafana = checkBool(server.arg(i));
}
yield();
}
actors[id].change(ac_pin, ac_argument, ac_name, ac_isinverted, ac_switchable, ac_grafana);
saveConfig();
server.send(201, "text/plain", "created");
}
void handleDelActor()
{
int id = server.arg(0).toInt();
for (int i = id; i < numberOfActors; i++)
{
if (i == (numberOfActorsMax - 1)) // 5 - Array von 0 bis (numberOfActorsMax-1)
{
actors[i].change("", "", "", false, false, false);
}
else
{
actors[i].change(PinToString(actors[i + 1].pin_actor), actors[i + 1].argument_actor, actors[i + 1].name_actor, actors[i + 1].isInverted, actors[i + 1].switchable, actors[i + 1].setGrafana);
}
yield();
}
numberOfActors -= 1;
saveConfig();
server.send(200, "text/plain", "deleted");
}
void handlereqPins()
{
int id = server.arg(0).toInt();
String message;
if (id != -1)
{
message += F("<option>");
message += PinToString(actors[id].pin_actor);
message += F("</option><option disabled>──────────</option>");
}
for (int i = 0; i < numberOfPins; i++)
{
if (pins_used[pins[i]] == false)
{
message += F("<option>");
message += pin_names[i];
message += F("</option>");
}
yield();
}
server.send(200, "text/plain", message);
}
unsigned char StringToPin(String pinstring)
{
for (int i = 0; i < numberOfPins; i++)
{
if (pin_names[i] == pinstring)
{
return pins[i];
}
}
return 9;
}
String PinToString(unsigned char pinbyte)
{
for (int i = 0; i < numberOfPins; i++)
{
if (pins[i] == pinbyte)
{
return pin_names[i];
}
}
return "NaN";
}
bool isPin(unsigned char pinbyte)
{
bool returnValue = false;
for (int i = 0; i < numberOfPins; i++)
{
if (pins[i] == pinbyte)
{
returnValue = true;
goto Ende;
}
}
Ende:
return returnValue;
}