-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.cpp
345 lines (301 loc) · 8.83 KB
/
app.cpp
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
// this app connects as MQTT client by subroutine to the local TinyMqtt broker
//
#include <stdio.h>
#include <Arduino.h>
//#include <PubSubClient.h>
#include "TinyMqtt.h"
#define MYAPP "r2d-fx"
#define VERSIONDATE "v221207"
const char* sketch_version = VERSIONDATE MYAPP " @";
#define USETLS
// below settings control the blink LED
#define BLINKTIME 1*1000L // time motor is opening
#define INCREMENT 1000L
#define INCREMENTMAX 10000L
#define MQTTdebug(x) MQTTpublish(MYAPP "/event/state",x, false)
#define UP digitalWrite(R1, HIGH);digitalWrite(R2, LOW)
#define DOWN digitalWrite(R2, HIGH);digitalWrite(R1, LOW)
#define STOP digitalWrite(R2, LOW);digitalWrite(R1, LOW)
#define SPEAK_SET "ich stelle mich auf sie ein."
unsigned long movetime = 0L;
unsigned long starttime;
unsigned long LEDtime = 0L;
extern MqttBroker broker;
#define LED 23
#define R1 33
#define R2 25 // Configurable, see typical pin layout above
// also IR output - 22 is IR in
// end flush settings
String MQTTserver; // gets the alternatives
#ifdef USETLS
#include <WiFiClientSecure.h>
WiFiClientSecure MyWifiClient;
#define mqttPort 8883
#else
WiFiClient MyWifiClient;
#define mqttPort 1883
#endif
MqttClient *MQTTclient;
const char *mqttUser = "mqttuser";
const char *mqttPassword = "mqttpass";
bool hadMQTT = false; // for MQTT re-connection tries
byte present = 0; // current presence status
bool ontoilet = false;
bool MQTTpublish(const char* topic, const char* msg, bool retain = false)
{
return MQTTclient->publish(Topic(topic), msg, strlen(msg));
}
void LEDblink()
{
digitalWrite(LED, LOW);
LEDtime = millis();
}
void estimateSettings(int bodySize, bool wheelchair)
{
float h_sitdown = 0;
float t_sitdown = 0;
float h_standup = 0;
float t_standup = 0;
unsigned long h1, h2;
if (bodySize == 0)
{
movetime = INCREMENTMAX;
starttime = millis();
DOWN;
Serial.printf("estimation reset\n");
return;
}
if (!wheelchair)
{
bodySize = _max(_min(bodySize, 200), 150) - 150; // 150-200cm only
Serial.printf("size %d\n", bodySize);
h_sitdown = 50 + ((float)bodySize / 8.); // start with ~3 up to ~8
t_sitdown = 3;
h_standup = 53 + ((float)bodySize / 8.);
t_standup = 6;
}
else
{
h_sitdown = 48;
t_sitdown = 0;
h_standup = 50;
t_standup = 0;
}
h_sitdown = _min(h_sitdown, 55);
h_standup = _min(h_standup, 65);
h_sitdown -= 47;
h_standup -= 47;
movetime = (unsigned long) ((float)INCREMENT * h_sitdown);
starttime = millis();
UP;
Serial.printf("### estimation results %f and %f, %lu\n", h_sitdown, h_standup, movetime);
MQTTdebug((String("### estimated hs=") + String(h_sitdown) + String(" hu=") + String(h_standup) + String(" t=") + String(movetime)).c_str());
/*char msg[100];
Serial.printf(msg, "{\"height\": %3.1f, \"tilt\": %3.1f}", h_sitdown, t_sitdown);
MQTTpublish("lift/command/move", msg, false);*/
//endheight = h_standup;
/*Serial.printf(msg, "{\"height\": %3.1f, \"tilt\": %3.1f}", h_standup, t_standup);
MQTTpublish("lift/config/end_position", msg, false);*/
MQTTpublish("tts/speak", SPEAK_SET, false);
}
// look up value position in message
// TODO: check ArduinoJSON use?
char *getJSONval(const char *data, const char *member, unsigned int length)
{
if (length == 0) return NULL;
char *val = strstr(data, member);
if (val)
{
val = strstr(val, ":");
if (val)
{
while ((*val) && (((*val) == ':') || ((*val) == ' ') || ((*val) == '\'') || ((*val) == '"')))
{
//Serial.printf(*val);
val++;
}
//Serial.printf("%s\n",);
return val;
}
}
return NULL;
}
// MQTT presence data change
void processstatus(const char* msg, const char* topic, unsigned int length)
{
char *val = NULL;
float h = -1, t = -1;
Serial.printf("@presence status = %d\n", present);
#ifdef SOMEPRESENCE
if (present == 0)
{
present = 1;
MQTTpublish("presence/event/somepresent", "1", false);
}
lastpresence = millis(); // remember last presence data
#endif
if (present == 2)
{
val = getJSONval(msg, "height", length);
if (val)
{
if (sscanf(val, "%f", &h) || sscanf(val + 1, "%f", &h))
{
val = getJSONval(msg, "on_toilet", length);
if (val)
{
if (*val == '1') ontoilet = true;
}
val = getJSONval(msg, "wheelchair", length);
if (val)
{
int height = h;
bool wheelchair = (*val == '1');
//MQTTdebug((String("estimate=") + String(estimation)).c_str());
//MQTTdebug((String("adjust h=") + String(height) + String(" w=") + String(wheelchair)).c_str());
Serial.printf("try estimate %d, %d\n", height, wheelchair);
estimateSettings(height, wheelchair);
present = 3;
}
}
}
}
else
{
if (present < 2) present++;
//MQTTdebug((String("p=") + String(present)).c_str());
Serial.printf("%s\n", "else presence");
}
}
//void mqttCallback(char *topic, byte *payload, unsigned int len) //for Pub
void mqttCallback(const MqttClient* /* source */, const Topic& Topic, const char* payload, size_t len)
{
char msg[300];
const char *topic = Topic.c_str();
Serial.printf("Message arrived in topic: ");
Serial.printf("%s %d\n", topic, len);
Serial.printf("Message:");
for (int i = 0; i < min(len, sizeof(msg)); i++)
{
msg[i] = (char)(payload[i]);
Serial.printf("%c", (payload[i]));
}
msg[len] = 0;
Serial.printf("\n");
if (strstr(topic, MYAPP "/command"))
{
if (strstr((const char*)msg, "R1ON"))
{
digitalWrite(R1, HIGH);
LEDblink();
}
if (strstr((const char*)msg, "R2ON"))
{
digitalWrite(R2, HIGH);
LEDblink();
}
if (strstr((const char*)msg, "R1OFF"))
{
digitalWrite(R1, LOW);
LEDblink();
}
if (strstr((const char*)msg, "R2OFF"))
{
digitalWrite(R2, LOW);
LEDblink();
}
}
else if (strstr(topic, MYAPP "/query"))
{
snprintf(msg, sizeof(msg), "%s%s", sketch_version, WiFi.localIP().toString().c_str());
MQTTpublish(MYAPP "/info", msg, false);
}
// watch presence
else if (strstr(topic, "presence/event/present"))
{
Serial.printf("presence user ");
if (msg[0] == '1')
{
if (present == 0) present = 1; // avoid reset on 2nd person entering at the same time
}
else
{
ontoilet = false;
present = 0;
movetime = INCREMENTMAX;
starttime = millis();
DOWN;
MQTTdebug("Go back to default");
Serial.printf("Go back to default\n");
}
Serial.printf("%d\n", present);
}
// watch presence data
else if (strstr(topic, "presence/event/status"))
{
processstatus(msg, topic, len);
}
else if (strstr(topic, MYAPP "/estimate"))
{
char* val;
int w = 0;
float h;
val = getJSONval(msg, "height", len);
if (val)
{
if (sscanf(val, "%f", &h) || sscanf(val + 1, "%f", &h))
{
val = getJSONval(msg, "wheelchair", len);
if (val)
{
if (sscanf(val, "%d", &w) || sscanf(val + 1, "%d", &w))
estimateSettings((int)h, w == 1);
}
}
}
}
}
// try to connect with MQTT server
void connectMqtt(void)
{
String clientId = "R2D2lift" + WiFi.macAddress();
MQTTclient->subscribe("presence/event/present");
MQTTclient->subscribe(MYAPP "/query");
MQTTclient->subscribe(MYAPP "/command");
MQTTclient->subscribe(MYAPP "/estimate");
MQTTclient->subscribe("presence/event/status");
MQTTpublish(MYAPP "/connected", "1", true);
Serial.printf("Now listening to MQTT ...\n");
}
//*****************************************************************************************//
void setup_Client() {
digitalWrite(R1, LOW);
digitalWrite(R2, LOW);
digitalWrite(LED, HIGH);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(LED, OUTPUT);
MQTTclient = new MqttClient(&broker, "r2d-fx"); // client connects _locally_ as subroutine to broker
MQTTclient->setCallback(mqttCallback);
connectMqtt(); // subscribe
Serial.printf("Relay module\n"); //shows in serial that it is ready to read
}
//*****************************************************************************************//
void loop_Client(void) {
if ((LEDtime != 0L) && ((millis() - LEDtime) > BLINKTIME))
{
LEDtime = 0L;
digitalWrite(LED, HIGH);
}
if (movetime != 0L)
{
Serial.printf("%lu\n", millis() - starttime);
if (((millis() - starttime) > movetime))
{
STOP;
movetime = 0L;
Serial.printf("stop\n");
MQTTdebug("stopped");
}
}
}