forked from nonnos/AMConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMConnect.ino
1050 lines (961 loc) · 38.5 KB
/
AMConnect.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//======================================================================
// Program: AMConnect
//
// Description: Connects Husqvarna Automower Generaton 2 to MQTT.
// It allows status to be sent to MQTT and commands
// to be sent to the Automower.
//
//
// Requirements: Supported Automower (Generation 2)
// ESP32 (LOLIN32 used for development)
// Ublox Neo GPS (for positioning)
//
//
//======================================================================
#include <NMEAGPS.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <PubSubClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Preferences.h>
#include <uptime_formatter.h>
// Include config
#include "AMConnect_config.h"
// include stuff
#include "AMConnect.h"
// Define the client
WiFiClient espClient;
PubSubClient client(espClient);
Preferences preferences;
// GPS variables
NMEAGPS gps; // This parses the GPS characters
gps_fix fix; // This holds on to the latest values
// Timer variables for GPS polling
unsigned long gpsMillis;
unsigned int localGpsInterval;
// Timer variables for status polling
unsigned long pollMillis;
unsigned int localPollInterval;
uint8_t lastCommand[5] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static bool AMPresent = false;
void setup()
{
DEBUG_PORT.begin(9600);
while (!Serial)
;
DEBUG_PORT.print( F("AMConnect: started\n") );
// Start wifi
setup_wifi();
// Connect MQTT
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// Start the serialport to AutoMower
Serial1.begin(9600, SERIAL_8N1, AMSerialRX, AMSerialTX);
// Send status request to the automower
// Start the GPS port
if (true == GPS_ATTACHED) {
gpsPort.begin(9600);
}
// Set gpsMillis
gpsMillis = millis();
pollMillis = millis();
// start ArduinoOTA
ArduinoOTA.setHostname("AMClient");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
// Read/Save preferences at startup
savePreferences();
}
//--------------------------
// Main loop
void loop()
{
// Update MQTT topics
handle_uptime(); // Update Uptime topic
handle_wifirssi(); // Update WiFi RSSI topic
// Check if WiFi is connected
if (WiFi.status() != WL_CONNECTED) {
wifi_reconnect();
}
// Check if MQTT is connected
if (!client.connected()) {
mqtt_reconnect();
}
// Do the MQTT Client Loop
client.loop();
if (true == GPS_ATTACHED) {
// Start with the GPS
while (gps.available( gpsPort )) {
fix = gps.read();
if (fix.valid.location) {
if (millis() - gpsMillis >= localGpsInterval*1000) {
// Handle the GPS position
handle_gps();
// Set gpsMillis to millis(), so we know when gpsInterval has passed
gpsMillis = millis();
}
}
}
}
// And then the Automower
if(Serial1.available()){
// Handle the automower
handle_am();
}
// Poll mower for status
if (millis() - pollMillis >= localPollInterval*1000)
{
// do a poll for status
handle_command("getStatus");
// do a poll for Wifi RSSI
handle_command("getWifiRssi");
pollMillis = millis();
}
// Handle ota
ArduinoOTA.handle();
}
// Functions
void savePreferences()
{
preferences.begin("amPreferences", false);
if(readStoredPreferences == 1)
{
handle_debug(false, (String)"Using stored values from preferences, skipping config values...");
//Poll-interval
localPollInterval = preferences.getUInt("pollInterval");
handle_debug(true, "stored pollInterval is set to: " + preferences.getUInt("pollInterval"));
// GPS-interval
localGpsInterval = preferences.getUInt("gpsInterval");
handle_debug(true, "stored gpsInterval is set to: " + preferences.getUInt("gpsInterval"));
}
else
{
handle_debug(false, (String)"Using values from config, overwriting preferences...");
//Poll-interval
preferences.putUInt("pollInterval", pollInterval);
localPollInterval = pollInterval;
// GPS-interval
preferences.putUInt("gpsInterval", gpsInterval);
localGpsInterval = gpsInterval;
}
preferences.end();
}
String ip2Str(IPAddress ip){
String ip_str="";
for (int i=0; i<4; i++) {
ip_str += i ? "." + String(ip[i]) : String(ip[i]);
}
return ip_str;
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
handle_debug(false, "");
handle_debug(false, (String)"Connecting to: " + (String)ssid);
WiFi.begin(ssid, password, 0);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
DEBUG_PORT.print(".");
}
handle_debug(false, "");
handle_debug(false, (String)"WiFi connected. IP Address: " + ip2Str(WiFi.localIP()));
}
void callback(char* topic, byte* message, unsigned int length) {
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
handle_debug(false, (String)"Message arrived on topic: " + (String)topic + (String)". Message: " + (String)messageTemp);
//Commands
if ((String)topic == (String)mqtt_command_topic) {
// We got a command, lets handle it!
handle_command(messageTemp);
handle_debug(false, "Got command via MQTT");
}
//Preferences
if ((String)topic == (String)mqtt_preferences_topic) {
// We got a command, lets handle it!
handle_preferences(messageTemp);
handle_debug(false, "Got a preference via MQTT");
}
}
void wifi_reconnect() {
// Loop until we're reconnected
while ((WiFi.status() != WL_CONNECTED)) {
handle_debug(false, "Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
if (WiFi.status() != WL_CONNECTED) {
delay(5000);
}
}
}
void mqtt_reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
// Abort if no WiFi status not connected
if (WiFi.status() != WL_CONNECTED) {
return;
}
handle_debug(false, "Attempting MQTT connection...");
// Attempt to connect
if (client.connect("AMClient", mqtt_username, mqtt_password, mqtt_lwt_topic, 1, true, "Offline")) {
handle_debug(false, "MQTT connected");
// Subscribe to commands
client.subscribe(mqtt_command_topic);
// Subscribe to preferences
client.subscribe(mqtt_preferences_topic);
// Set LWT to Online
client.publish(mqtt_lwt_topic, "Online", true);
} else {
handle_debug(false, (String)"MQTT failed, rc=" + (String)client.state() + (String)". Try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void handle_gps() {
// Send the location to MQTT
char latitudeString[10];
dtostrf(fix.latitude(), 0, 6, latitudeString);
char longitudeString[10];
dtostrf(fix.longitude(), 0, 6, longitudeString);
const byte latitudeSize = sizeof latitudeString;
const byte longitudeSize = sizeof longitudeString;
const byte positionSize = latitudeSize + longitudeSize + 1;
char locationString[positionSize];
strncpy(locationString, latitudeString, positionSize);
strncat(locationString, ",", positionSize - strlen(locationString));
strncat(locationString, longitudeString, positionSize - strlen(locationString));
// locationString = latitudeString + "," + longitudeString;
handle_debug(true, (String)"Location: " + (String)locationString);
client.publish(mqtt_location_topic, locationString);
}
void handle_debug(bool sendmqtt, String debugmsg) {
// Handle the debug output
DEBUG_PORT.println(debugmsg);
// send to mqtt_command_topic
if (sendmqtt && client.connected())
{
char debugChar[50];
debugmsg.toCharArray(debugChar,50);
client.publish(mqtt_debug_topic, debugChar);
}
}
// Update RSSI topic
unsigned long lastUpdateTimeRssi = 0; // Variable to store the last update time
void handle_wifirssi() {
unsigned long currentTime = millis(); // Get the current time in milliseconds
// Check if 15 seconds have passed since the last update
if (currentTime - lastUpdateTimeRssi >= 15000) {
String wifirssi = (String)WiFi.RSSI() + " dBm";
// Check if MQTT client is connected
if (client.connected()) {
char statusChar[50];
wifirssi.toCharArray(statusChar, 50);
client.publish(mqtt_rssi_topic, statusChar);
}
lastUpdateTimeRssi = currentTime; // Update the last update time
}
}
// Update Uptime topic
unsigned long lastUpdateTimeUptime = 0; // Variable to store the last update time
void handle_uptime() {
unsigned long currentTime = millis(); // Get the current time in milliseconds
// Check if 15 seconds have passed since the last update
if (currentTime - lastUpdateTimeUptime >= 15000) {
String uptime = (String)uptime_formatter::getUptime();
// Check if MQTT client is connected
if (client.connected()) {
char statusChar[50];
uptime.toCharArray(statusChar, 50);
client.publish(mqtt_uptime_topic, statusChar);
}
lastUpdateTimeUptime = currentTime; // Update the last update time
}
}
void handle_status(int statusCode, String statusMsg) {
// Send to debug
handle_debug(true, statusMsg);
// send to mqtt_status_topic
if (client.connected())
{
char statusChar[50];
statusMsg.toCharArray(statusChar,50);
client.publish(mqtt_status_topic, statusChar);
}
}
void handle_hastatus(int statusCode, String statusMsg) {
// Send to debug
handle_debug(true, statusMsg);
// send to mqtt_status_topic
if (client.connected())
{
char statusChar[50];
statusMsg.toCharArray(statusChar,50);
client.publish(mqtt_ha_status_topic, statusChar);
}
}
void handle_am() {
uint8_t statusAutomower[5] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
uint8_t empty[5] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Serial1.readBytes(statusAutomower,5);
if(memcmp(statusAutomower, empty, 5) == 0)
{
handle_debug(true, "Unusable data received on serial");
AMPresent = false;
}
else
{
// values comes as DEC and not HEX
handle_debug(true, "Byte1: " + (String)(statusAutomower[0]) + " Byte2: " + (String)(statusAutomower[1]) + " Byte3: " + (String)(statusAutomower[2]) + " Byte4: " + (String)(statusAutomower[3]) + " Byte5: " + (String)(statusAutomower[4]));
AMPresent = true;
}
// Merge the last two bytes to status
unsigned int statusInt = statusAutomower[4] << 8 | statusAutomower[3];
handle_debug(true, "Status Code: " + (String)statusInt);
if (statusAutomower[0] == 0x0F)
{
// A command
if (statusAutomower[1] == 0x80)
{
// Keypresses
if (statusAutomower[2] == 0x5F)
{
// Keypress
switch (statusInt) {
case 0:
handle_debug(true, "Key 0 pressed");
break;
case 1:
handle_debug(true, "Key 1 pressed");
break;
case 2:
handle_debug(true, "Key 2 pressed");
break;
case 3:
handle_debug(true, "Key 3 pressed");
break;
case 4:
handle_debug(true, "Key 4 pressed");
break;
case 5:
handle_debug(true, "Key 5 pressed");
break;
case 6:
handle_debug(true, "Key 6 pressed");
break;
case 7:
handle_debug(true, "Key 7 pressed");
break;
case 8:
handle_debug(true, "Key 8 pressed");
break;
case 9:
handle_debug(true, "Key 9 pressed");
break;
case 10:
handle_debug(true, "Key Program A pressed");
break;
case 11:
handle_debug(true, "Key Program B pressed");
break;
case 12:
handle_debug(true, "Key Program C pressed");
break;
case 13:
handle_debug(true, "Key Home pressed");
break;
case 14:
handle_debug(true, "Key Man/Auto pressed");
break;
case 15:
handle_debug(true, "Key C pressed");
break;
case 16:
handle_debug(true, "Key Up pressed");
break;
case 17:
handle_debug(true, "Key Down pressed");
break;
case 18:
handle_debug(true, "Key YES pressed");
break;
default: //no valid parameter: send status
handle_debug(true, "Unkown keypress. Code: " + (String)statusInt);
break;
}
}
}
else if (statusAutomower[1] == 0x81)
{
// Mode set
if (statusAutomower[2] == 0x2C)
{
// Mode Set
switch (statusInt) {
case 0:
handle_debug(true, "Manual Mode");
break;
case 1:
handle_debug(true, "Auto Mode");
break;
case 3:
handle_debug(true, "Home Mode");
break;
case 4:
handle_debug(true, "Demo Mode");
break;
default: //no valid parameter: send status
handle_debug(true, "Unkown keypress. Code: " + (String)statusInt);
break;
}
}
}
else if (statusAutomower[1] == 0xCA)
{
// Timer actions
if (statusAutomower[2] == 0x4E)
{
// Timer actions
switch (statusInt) {
case 0:
handle_debug(true, "Timer activated");
break;
case 1:
handle_debug(true, "Timer deactivated");
break;
default: //no valid parameter: send status
handle_debug(true, "Unknown timer action. Code: " + (String)statusInt);
break;
}
}
}
else if (statusAutomower[1] == 0x00)
{
// Mowing time and strength
if (statusAutomower[2] == 0x38)
{
// Mowing time
handle_debug(true, "Mowing time: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x4D)
{
// rpm
handle_debug(true, "Mowing rpm: " + (String)statusInt);
}
}
else if (statusAutomower[1] == 0x01)
{
// Info, Battery and square mode
if (statusAutomower[2] == 0x34)
{
// Square mode procent
handle_debug(true, "Square mode procent: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x37)
{
// Square mode reference
handle_debug(true, "Square mode reference: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x38)
{
// Square mode status
handle_debug(true, "Square mode status: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xEB)
{
// Battery capacity (mA)
handle_debug(true, "Battery capacity (mA): " + (String)statusInt);
}
else if (statusAutomower[2] == 0xEC)
{
// Charging time
handle_debug(true, "Charging time: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xEF)
{
// Battery capacity mAh
handle_debug(true, "Battery capacity mAh: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xF0)
{
// Battery seek start capacity
handle_debug(true, "Battery seek start capacity: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xF1)
{
// Status
switch (statusInt) {
case 6: //Status
handle_status(statusInt, "Left wheel motor blocked"); // Linker Radmotor blockiert
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 12: //Status
handle_status(statusInt, "No loop signal"); // Kein Schleifensignal
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 16: //Status
handle_status(statusInt, "Outside working area");
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 18: //Status
handle_status(statusInt, "Low battery voltage"); // Niedrige Batteriespannung
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 24: //Status
handle_status(statusInt, "Wheel spinning");
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 28: //Status
handle_status(statusInt, "Manual charging needed"); // Ladestation blockiert
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 26: //Status
handle_status(statusInt, "Charging station blocked"); // Ladestation blockiert
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 34: //Status
handle_status(statusInt, "Mower lifted"); // Mäher hochgehoben
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 52: //Status
handle_status(statusInt, "Charging station no contact"); // Ladestation kein Kontakt
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 54: //Status
handle_status(statusInt, "Pin expired"); // Pin abgelaufen
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 56: //Status
handle_status(statusInt, "Left collision sensor defective");
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 58: //Status
handle_status(statusInt, "Right collision sensor defective");
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 1000: //Status
handle_status(statusInt, "Leaving charging station"); // Verlässt Ladestation
handle_hastatus(statusInt, "mowing"); // Works with HA MQTT Lawn Mower
break;
case 1002: //Status
handle_status(statusInt, "Mowing"); // Mähen
handle_hastatus(statusInt, "mowing"); // Works with HA MQTT Lawn Mower
break;
case 1006: //Status
handle_status(statusInt, "Mower starting"); // Mähwerk starten
handle_hastatus(statusInt, "mowing"); // Works with HA MQTT Lawn Mower
break;
case 1008: //Status
handle_status(statusInt, "Mower started"); // Mähwerk gestartet
handle_hastatus(statusInt, "mowing"); // Works with HA MQTT Lawn Mower
break;
case 1012: //Status
handle_status(statusInt, "Start mowing"); // Starte Mähwerk
handle_hastatus(statusInt, "mowing"); // Works with HA MQTT Lawn Mower
break;
case 1014: //Status
handle_status(statusInt, "Charging"); // Laden
handle_hastatus(statusInt, "docked"); // Works with HA MQTT Lawn Mower
break;
case 1016: //Status
handle_status(statusInt, "Waiting in charging station"); // Wartet in Ladestation
handle_hastatus(statusInt, "docked"); // Works with HA MQTT Lawn Mower
break;
case 1024: //Status
handle_status(statusInt, "Drives to charging station"); // Fährt in Ladestation
break;
case 1036: //Status
handle_status(statusInt, "Square mode"); // Viereckmodus
break;
case 1038: //Status
handle_status(statusInt, "Stuck"); // Festgefahren
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 1040: //Status
handle_status(statusInt, "Dodge/Evasion"); //Ausweichen
break;
case 1042: //Status
handle_status(statusInt, "Searching"); // Suchen
break;
case 1044: //Status
handle_status(statusInt, "Stop"); // Stop
handle_hastatus(statusInt, "paused"); // Works with HA MQTT Lawn Mower
break;
case 1048: //Status
handle_status(statusInt, "Docking"); // Andocken
handle_hastatus(statusInt, "docked"); // Works with HA MQTT Lawn Mower
break;
case 1050: //Status
handle_status(statusInt, "Leaving charging station"); // Verlässt Ladestation
handle_hastatus(statusInt, "mowing"); // Works with HA MQTT Lawn Mower
break;
case 1052: //Status
handle_status(statusInt, "Error"); // Fehler
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 1056: //Status
handle_status(statusInt, "Waiting for use"); // Wartet auf Einsatz
handle_hastatus(statusInt, "docked"); // Works with HA MQTT Lawn Mower
break;
case 1058: //Status
handle_status(statusInt, "Following limit cable"); // Begrenzung folgen
break;
case 1060: //Status
handle_status(statusInt, "N signal found"); // N-Signal gefunden
break;
case 1062: //Status
handle_status(statusInt, "Stuck"); // Festgefahren
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
case 1064: //Status
handle_status(statusInt, "Searching"); // Suchen
break;
case 1070: //Status
handle_status(statusInt, "Following guide cable"); // Suchschleife folgen
break;
case 1072: //Status
handle_status(statusInt, "Following loop"); // Schleife folgen
break;
default: //no valid parameter: send status
handle_status(statusInt, "Unknown statuscode"); // Unbekannt
handle_hastatus(statusInt, "error"); // Works with HA MQTT Lawn Mower
break;
}
}
}
else if (statusAutomower[1] == 0x02)
{
// Battery info
if (statusAutomower[2] == 0x33)
{
//
handle_debug(true, "Battery temperature: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x34)
{
//
handle_debug(true, "Last charging time: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x35)
{
//
handle_debug(true, "Battery charging temperature: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x36)
{
//
handle_debug(true, "Battery next temperature reading: " + (String)statusInt);
}
}
else if (statusAutomower[1] == 0x24)
{
// Wheel speeds
if (statusAutomower[2] == 0xBF)
{
//
handle_debug(true, "Right wheel speed: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xC0)
{
//
handle_debug(true, "Left wheel speed: " + (String)statusInt);
}
}
else if (statusAutomower[1] == 0x2E)
{
// Battery info
if (statusAutomower[2] == 0xE0)
{
//
handle_debug(true, "Battery Capacity Used (mAh): " + (String)statusInt);
}
else if (statusAutomower[2] == 0xEA)
{
//
handle_debug(true, "Speed of Knife engine: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xF4)
{
//
handle_debug(true, "Battery Voltage: " + (String)statusInt);
}
}
else if (statusAutomower[1] == 0x33)
{
// Firmware version
if (statusAutomower[2] == 0x90)
{
//
handle_debug(true, "Firmware version: " + (String)statusInt);
}
}
else if (statusAutomower[1] == 0x36)
{
// time and date
if (statusAutomower[2] == 0xB3)
{
//
handle_debug(true, "Current minute: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xB5)
{
//
handle_debug(true, "Current hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xB7)
{
//
handle_debug(true, "Current day: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xB9)
{
//
handle_debug(true, "Current month: " + (String)statusInt);
}
else if (statusAutomower[2] == 0xBD)
{
//
handle_debug(true, "Current year: " + (String)statusInt);
}
}
else if (statusAutomower[1] == 0x3A)
{
// Voice version
if (statusAutomower[2] == 0xC0)
{
//
handle_debug(true, "Voice version: " + (String)statusInt);
}
}
else if (statusAutomower[1] == 0x4A)
{
// Timers
if (statusAutomower[2] == 0x38)
{
//
handle_debug(true, "Week Timer1 Start Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x39)
{
//
handle_debug(true, "Week Timer1 Start Minute: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x3A)
{
//
handle_debug(true, "Week Timer1 Stop Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x3B)
{
//
handle_debug(true, "Week Timer1 Stop Minute: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x3C)
{
//
handle_debug(true, "Weekend Timer1 Start Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x3D)
{
//
handle_debug(true, "Weekend Timer1 Start Minute: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x3E)
{
//
handle_debug(true, "Weekend Timer1 Stop Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x3F)
{
//
handle_debug(true, "Weekend Timer1 Stop Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x40)
{
//
handle_debug(true, "Week Timer2 Start Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x41)
{
//
handle_debug(true, "Week Timer2 Start Minute: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x42)
{
//
handle_debug(true, "Week Timer2 Stop Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x43)
{
//
handle_debug(true, "Week Timer2 Stop Minute: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x44)
{
//
handle_debug(true, "Weekend Timer2 Start Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x45)
{
//
handle_debug(true, "Weekend Timer2 Start Minute: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x46)
{
//
handle_debug(true, "Weekend Timer2 Stop Hour: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x47)
{
//
handle_debug(true, "Weekend Timer2 Stop Minute: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x4E)
{
//
handle_debug(true, "Timer Status: " + (String)statusInt);
}
else if (statusAutomower[2] == 0x50)
{
//
handle_debug(true, "Timer Day: " + (String)statusInt);
}
}
}
}
void handle_preferences(String preferencePayload) {
preferences.begin("amPreferences", false);
if(preferencePayload.startsWith("gpsInterval:"))
{
preferences.putUInt("gpsInterval", preferencePayload.substring(12).toInt());
localGpsInterval = preferencePayload.substring(12).toInt();
handle_debug(true, "gpsInterval is set to: " + String(preferences.getUInt("gpsInterval"))); }
else if (preferencePayload.startsWith("pollInterval:"))
{
preferences.putUInt("pollInterval", preferencePayload.substring(13).toInt());
localPollInterval = preferencePayload.substring(13).toInt();
handle_debug(true, "pollInterval is set to: " + String(preferences.getUInt("pollInterval"))); }
else if( preferencePayload == "getGpsInterval")
{
const byte gpsSize = sizeof localGpsInterval;
char gpsChar[gpsSize];
itoa(localGpsInterval, gpsChar, 10);
client.publish(mqtt_prefstatus_topic, gpsChar);
handle_debug(true, "gpsInterval is set to: " + String(preferences.getUInt("gpsInterval"))); }
else if( preferencePayload = "getPollInterval")
{
const byte pollSize = sizeof localPollInterval;
char pollChar[pollSize];
itoa(localPollInterval, pollChar, 10);
client.publish(mqtt_prefstatus_topic, pollChar);
handle_debug(true, "pollInterval is set to: " + String(preferences.getUInt("pollInterval"))); }
else
{
handle_debug(true, "Unknown command recieved on automower/preferences topic: " + preferencePayload); }
preferences.end();
}
void handle_command(String command) {
bool AMCmd = true;
uint8_t commandAutomower[5] = { 0x0F, 0x01, 0xF1, 0x00, 0x00 };
if (command == "getMowingTime") { memcpy(commandAutomower, amcGetMowingTime, sizeof(commandAutomower)); }
else if (command == "getMowingStrength") { memcpy(commandAutomower, amcGetMowingStrength, sizeof(commandAutomower)); }
else if (command == "getSquareModeProcent") { memcpy(commandAutomower, amcGetSquareModeProcent, sizeof(commandAutomower)); }
else if (command == "getSquareModeReference") { memcpy(commandAutomower, amcGetSquareModeReference, sizeof(commandAutomower)); }
else if (command == "getSquareModeStatus") { memcpy(commandAutomower, amcGetSquareModeStatus, sizeof(commandAutomower)); }
else if (command == "getBatteryCapacity") { memcpy(commandAutomower, amcGetBatteryCapacity, sizeof(commandAutomower)); }
else if (command == "getBatteryChargingTime") { memcpy(commandAutomower, amcGetBatteryChargingTime, sizeof(commandAutomower)); }
else if (command == "getBatteryCapacitymAh") { memcpy(commandAutomower, amcGetBatteryCapacitymAh, sizeof(commandAutomower)); }
else if (command == "getBatterySeekStartCapacity") { memcpy(commandAutomower, amcGetBatterySeekStartCapacity, sizeof(commandAutomower)); }
else if (command == "getStatus") { memcpy(commandAutomower, amcGetStatus, sizeof(commandAutomower)); }
else if (command == "getBatteryTemperature") { memcpy(commandAutomower, amcGetBatteryTemperature, sizeof(commandAutomower)); }
else if (command == "getBatteryLastChargingTime") { memcpy(commandAutomower, amcGetBatteryLastChargingTime, sizeof(commandAutomower)); }
else if (command == "getBatteryChargingTemperature") { memcpy(commandAutomower, amcGetBatteryChargingTemperature, sizeof(commandAutomower)); }
else if (command == "getSpeedRight") { memcpy(commandAutomower, amcGetSpeedRight, sizeof(commandAutomower)); }
else if (command == "getSpeedLeft") { memcpy(commandAutomower, amcGetSpeedLeft, sizeof(commandAutomower)); }
else if (command == "getBatteryCapacityUsed") { memcpy(commandAutomower, amcGetBatteryCapacityUsed, sizeof(commandAutomower)); }
else if (command == "getSpeedKnife") { memcpy(commandAutomower, amcGetSpeedKnife, sizeof(commandAutomower)); }
else if (command == "getBatteryVoltage") { memcpy(commandAutomower, amcGetBatteryVoltage, sizeof(commandAutomower)); }
else if (command == "getFirmwareVersion") { memcpy(commandAutomower, amcGetFirmwareVersion, sizeof(commandAutomower)); }
else if (command == "getTimeSecond") { memcpy(commandAutomower, amcGetTimeSecond, sizeof(commandAutomower)); }
else if (command == "getTimeMinute") { memcpy(commandAutomower, amcGetTimeMinute, sizeof(commandAutomower)); }
else if (command == "getTimeHour") { memcpy(commandAutomower, amcGetTimeHour, sizeof(commandAutomower)); }
else if (command == "getDateDay") { memcpy(commandAutomower, amcGetDateDay, sizeof(commandAutomower)); }
else if (command == "getDateMonth") { memcpy(commandAutomower, amcGetDateMonth, sizeof(commandAutomower)); }
else if (command == "getDateYear") { memcpy(commandAutomower, amcGetDateYear, sizeof(commandAutomower)); }
else if (command == "getVoiceVersion") { memcpy(commandAutomower, amcGetVoiceVersion, sizeof(commandAutomower)); }
else if (command == "getWeekTimer1StartHour") { memcpy(commandAutomower, amcGetWeekTimer1StartHour, sizeof(commandAutomower)); }