-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote-control.ino
466 lines (355 loc) · 11.2 KB
/
remote-control.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// -- import libraries
#include <ESP8266WiFi.h> // WiFi
#include <PubSubClient.h> // MQTT
#include <U8g2lib.h> // OLED display
// -- config
#include "config.h"
// -- define global objects
// wifi client:
WiFiClient wifiClient;
// mqtt client:
PubSubClient mqttClient(wifiClient);
// OLED display:
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, PIN_DISPLAY_SCL, PIN_DISPLAY_SDA, U8X8_PIN_NONE);
// -- define global variables:
uint8_t button_up_last_value;
uint8_t button_down_last_value;
bool mqtt_message_was_received = false;
char* mqtt_received_message;
char* mqtt_received_topic;
int speed_nominal = 0;
int speed_actual = 0;
char* direction_nominal = "FWD";
char* direction_actual = "FWD";
// -- functions
bool wifiConnect(const char* ssid, const char* password, const int wifi_connect_retry_delay = 500, const int wifi_connect_retry_timeout = 10000) {
int retry_delay = wifi_connect_retry_delay;
int retry_timeout = wifi_connect_retry_timeout;
if (LOG_TO_SERIAL) {
Serial.println();
Serial.print("Attempting to connect to WiFi ");
Serial.print(ssid);
Serial.print(".");
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED && retry_timeout > 0) {
if (LOG_TO_SERIAL) {
Serial.print(".");
}
// prepare next loop:
retry_timeout -= retry_delay;
delay(retry_delay);
}
if (retry_timeout <= 0) {
if (LOG_TO_SERIAL) {
Serial.println(" Timed out.");
}
return false;
}
if (LOG_TO_SERIAL) {
Serial.println(" Connected.");
Serial.print(" Assigned IP is ");
Serial.print(WiFi.localIP());
Serial.println(".");
}
return true;
}
bool mqttConnect(const char* device_id, PubSubClient mqttClient, const char* mqtt_server, const int mqtt_port, const int mqtt_connect_retry_delay = 500, const int mqtt_connect_retry_timeout = 10000) {
int retry_delay = mqtt_connect_retry_delay;
int retry_timeout = mqtt_connect_retry_timeout;
if (LOG_TO_SERIAL) {
Serial.print("Attempting to connect to MQTT broker at ");
Serial.print(mqtt_server);
Serial.print(":");
Serial.print(mqtt_port);
Serial.print(".");
}
// create a mqtt client id out of the device_id:
String mqttClientId = "robotrain-";
mqttClientId += String(device_id);
while (!mqttClient.connected() && retry_timeout > 0) {
// attempt to connect:
if (mqttClient.connect(mqttClientId.c_str())) {
break;
} else {
if (LOG_TO_SERIAL) {
Serial.print(".");
}
// prepare next loop:
retry_timeout -= (retry_delay + 1000); // see [1] below
delay(retry_delay);
}
}
// [1]: PubSubClient::connect() uses Arduinos client.connect() function
// which has a default timeout of 1000 ms. This adds to the timeout
// which is defined in config.h!
if (retry_timeout <= 0) {
if (LOG_TO_SERIAL) {
Serial.print(" Timed out. Error code ");
Serial.println(mqttClient.state());
}
return false;
}
if (LOG_TO_SERIAL) {
Serial.println(" Connected.");
Serial.print(" Client ID is ");
Serial.print(mqttClientId);
Serial.println(".");
}
return true;
}
bool mqttSendMessage(const char* topic, const char* message) {
// reconnect, if connectinon was lost:
if (!mqttClient.connected()) {
mqttConnect(DEVICE_ID, mqttClient, MQTT_SERVER, MQTT_PORT, MQTT_CONNECT_RETRY_DELAY, MQTT_CONNECT_RETRY_TIMEOUT);
// bool mqttConnect(const char* device_id, PubSubClient mqttClient, const char* mqtt_server, const int mqtt_port, const int mqtt_connect_retry_delay = 500, const int mqtt_connect_retry_timeout = 10000) {
}
// publish message on given topic:
if (LOG_TO_SERIAL) {
Serial.print("Publishing message '");
Serial.print(message);
Serial.print("' on topic '");
Serial.print(topic);
Serial.println("'.");
}
mqttClient.publish(topic, message, true);
}
void mqttMessageReceivedCallback(char* topic, const byte* payload, const unsigned int length) {
// convert byte* payload to char*:
char* result = (char*) payload;
// truncate to correct length:
if (length <= strlen(result)) {
result[length] = '\0';
}
// return value:
mqtt_message_was_received = true;
mqtt_received_topic = topic;
mqtt_received_message = result;
}
void sendSpeedNominal(int speed_nominal_percent) {
// send direction:
mqttSendMessage(
String(MQTT_TOPIC_DIRECTION_NOMINAL).c_str(),
direction_nominal
);
// send speed:
mqttSendMessage(
String(MQTT_TOPIC_SPEED_NOMINAL).c_str(),
String(speed_nominal_percent).c_str()
);
}
void drawSpeedNominal(int speed_nominal_percent) {
// calculate pixel values:
int scaling_factor;
int limiter_percent;
char* limiter_direction;
if (SPEED_NOMINAL_MAX > abs(SPEED_NOMINAL_MIN)) {
scaling_factor = SPEED_NOMINAL_MAX;
limiter_percent = abs(SPEED_NOMINAL_MIN);
limiter_direction = "REV";
} else {
scaling_factor = abs(SPEED_NOMINAL_MIN);
limiter_percent = SPEED_NOMINAL_MAX;
limiter_direction = "FWD";
}
uint8_t speed_nominal_pos = (int)(128.0 * ((float)abs(speed_nominal_percent) / scaling_factor));
uint8_t limiter_pos = 0;
if (direction_nominal == limiter_direction) {
limiter_pos = (int)(128.0 * ((float)abs(limiter_percent) / scaling_factor));
}
// limiter:
if (limiter_direction == direction_nominal) {
u8g2.setDrawColor(2);
u8g2.drawLine(limiter_pos, 0, limiter_pos, 15);
u8g2.drawLine(limiter_pos + 1, 8, 128, 8);
}
// nominal speed as two XOR triangles
// as overlay on the actual speed bar:
u8g2.setDrawColor(2);
// top triangle:
u8g2.drawTriangle(
speed_nominal_pos, 5,
speed_nominal_pos - 4, 0,
speed_nominal_pos + 4, 0
);
// bottom triangle:
u8g2.drawTriangle(
speed_nominal_pos, 11,
speed_nominal_pos + 4, 16,
speed_nominal_pos - 4, 16
);
// print texts:
u8g2.setFont(u8g2_font_unifont_tf);
u8g2.drawStr(0, 26, direction_nominal);
String speed_string = String(abs(speed_nominal)) + String("/");
if (direction_nominal == "FWD") {
speed_string += String(SPEED_NOMINAL_MAX);
} else {
speed_string += String(abs(SPEED_NOMINAL_MIN));
}
u8g2.drawStr(36, 26, speed_string.c_str());
}
void drawSpeedActual(int speed_actual_percent) {
// calculate pixel values:
int scaling_factor;
if (SPEED_NOMINAL_MAX > abs(SPEED_NOMINAL_MIN)) {
scaling_factor = SPEED_NOMINAL_MAX;
} else {
scaling_factor = abs(SPEED_NOMINAL_MIN);
}
uint8_t speed_actual_width = (int)(128.0 * ((float)abs(speed_actual_percent) / scaling_factor));
// actual speed as bar on top of display:
u8g2.drawBox(0, 0, speed_actual_width, 16);
}
/**
* This function is intended to draw the contents
* of the display once every loop.
*
* It has no input parameters as it takes the required
* data from global variables.
*/
void drawDisplay() {
u8g2.clearBuffer();
drawSpeedNominal(speed_nominal);
drawSpeedActual(speed_actual);
u8g2.sendBuffer();
}
void setup() {
// start serial:
Serial.begin(115200);
// init buttons:
pinMode(PIN_BUTTON_UP, INPUT);
pinMode(PIN_BUTTON_DOWN, INPUT);
// init display:
u8g2.begin();
// connect to wifi:
if (!wifiConnect(WIFI_SSID, WIFI_SECRET, WIFI_CONNECT_RETRY_DELAY, WIFI_CONNECT_RETRY_TIMEOUT)) {
if (LOG_TO_SERIAL) {
Serial.println("Could not connect to WiFi!");
}
}
// connect to mqtt broker:
mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
mqttClient.setCallback(mqttMessageReceivedCallback);
if(!mqttConnect(DEVICE_ID, mqttClient, MQTT_SERVER, MQTT_PORT, MQTT_CONNECT_RETRY_DELAY, MQTT_CONNECT_RETRY_TIMEOUT)) {
if (LOG_TO_SERIAL) {
Serial.println("Could not connect to MQTT broker");
}
}
mqttClient.subscribe(String(MQTT_TOPIC_SPEED_ACTUAL).c_str(), 0);
if (LOG_TO_SERIAL) {
Serial.print("Subscribed to topic '");
Serial.print(MQTT_TOPIC_SPEED_ACTUAL);
Serial.println("'.");
}
mqttClient.subscribe(String(MQTT_TOPIC_DIRECTION_ACTUAL).c_str(), 0);
if (LOG_TO_SERIAL) {
Serial.print("Subscribed to topic '");
Serial.print(MQTT_TOPIC_DIRECTION_ACTUAL);
Serial.println("'.");
}
// update display:
drawDisplay();
}
void loop() {
// -- read mqtt messages
mqttClient.loop();
if (mqtt_message_was_received) {
// read actual direction:
if (strcmp(mqtt_received_topic, MQTT_TOPIC_DIRECTION_ACTUAL) == 0) {
if (strcmp(mqtt_received_message, "FWD") == 0) {
direction_actual = "FWD";
} else if (strcmp(mqtt_received_message, "REV") == 0) {
direction_actual = "REV";
} else {
if (LOG_TO_SERIAL) {
Serial.println("ERROR: unexpected message received!");
Serial.print(" topic: ");
Serial.println(mqtt_received_topic);
Serial.print(" message: ");
Serial.println(mqtt_received_message);
}
}
}
// read actual speed:
if (strcmp(mqtt_received_topic, MQTT_TOPIC_SPEED_ACTUAL) == 0) {
if (
String(mqtt_received_message).toInt() <= SPEED_NOMINAL_MAX &&
String(mqtt_received_message).toInt() >= SPEED_NOMINAL_MIN
) {
speed_actual = String(mqtt_received_message).toInt();
} else {
if (LOG_TO_SERIAL) {
Serial.println("ERROR: received value for 'speed_nominal' out of range!");
Serial.print(" value: ");
Serial.println(String(mqtt_received_message).toInt());
}
}
// update display:
drawSpeedActual(speed_actual);
}
mqtt_message_was_received = false;
}
// -- read inputs
if (digitalRead(PIN_BUTTON_DOWN) == HIGH) {
// change direction if speed is zero:
if (speed_nominal == 0) {
if (direction_nominal == "FWD") {
direction_nominal = "REV";
} else {
direction_nominal = "FWD";
}
}
// change speed in forward mode:
if (direction_nominal == "FWD") {
if (speed_nominal - SPEED_NOMINAL_STEP > 0) {
speed_nominal -= SPEED_NOMINAL_STEP;
} else {
speed_nominal = 0;
}
}
// change speed in reversed mode:
if (direction_nominal == "REV") {
if (speed_nominal + SPEED_NOMINAL_STEP < 0) {
speed_nominal += SPEED_NOMINAL_STEP;
} else {
speed_nominal = 0;
}
}
// update display and send speed via mqtt:
sendSpeedNominal(speed_nominal);
// wait for falling edge:
while (digitalRead(PIN_BUTTON_DOWN) == HIGH) {
delay(100);
}
}
// read buttons:
if (digitalRead(PIN_BUTTON_UP) == HIGH) {
// change speed in forward mode:
if (direction_nominal == "FWD") {
if (speed_nominal + SPEED_NOMINAL_STEP < SPEED_NOMINAL_MAX) {
speed_nominal += SPEED_NOMINAL_STEP;
} else {
speed_nominal = SPEED_NOMINAL_MAX;
}
}
// change speed in reversed mode:
if (direction_nominal == "REV") {
if (speed_nominal - SPEED_NOMINAL_STEP > SPEED_NOMINAL_MIN) {
speed_nominal -= SPEED_NOMINAL_STEP;
} else {
speed_nominal = SPEED_NOMINAL_MIN;
}
}
// update display and send speed via mqtt:
sendSpeedNominal(speed_nominal);
// wait for falling edge:
while (digitalRead(PIN_BUTTON_UP) == HIGH) {
delay(10);
}
}
// -- update display
drawDisplay();
// -- delay next loop
delay(10);
}