-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAlpha Slave Address and Baud Rate Finder.ino
1252 lines (956 loc) · 31.4 KB
/
Alpha Slave Address and Baud Rate Finder.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
/*
Name: Alpha Slave Address Finder.ino
Created: 22/Jan/2023
Author: Daniel Young
This file is part of Alpha2MQTT (A2M) which is released under GNU GENERAL PUBLIC LICENSE.
See file LICENSE or go to https://choosealicense.com/licenses/gpl-3.0/ for full license details.
Notes
First, go and customise options such as WIFI_SSID and the rest of it below.
*/
// Supporting files
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
// Output various levels of detail to the serial monitor
#define DEBUG
//#define DEBUG_LEVEL2
#define OUTPUT_SERIAL // Output Slave ID detail to the serial monitor (9600 baud)
#define OUTPUT_WIFIANDMQTT // Connect to WiFi and MQTT to output Slave ID detail as an MQTT message
#define OUTPUT_DISPLAY // Output Slave ID detail to the display
#if !defined OUTPUT_SERIAL && !defined OUTPUT_WIFIANDMQTT && !defined OUTPUT_DISPLAY
#error You need to specify at least one output type!
#endif
// These timers are used in the main loop.
#define ITERATE_INTERVAL 10000
#define DISPLAY_FINAL_OUTPUT_FOR 20000
#define UPDATE_STATUS_BAR_INTERVAL 500
// Update with your Wifi details
#define WIFI_SSID "Stardust"
#define WIFI_PASSWORD ""
// Update with your MQTT Broker details
#define MQTT_SERVER "192.168.1.135"
#define MQTT_PORT 1883
#define MQTT_USERNAME "Alpha" // Empty string for none.
#define MQTT_PASSWORD ""
// The device name is used as the MQTT base topic and presence on the network.
// If you need more than one Alpha2MQTT on your network, give them unique names.
#define DEVICE_NAME "Alpha2MQTT"
// x 50mS to wait for RS485 input chars. 300ms as per Modbus documentation, but I got timeouts on that. However 400ms works without issue
#define RS485_TRIES 8
#define INVERTER_BAUD_RATE 9600
// The ESP8266 has limited memory and so reserving lots of RAM to build a payload and MQTT buffer causes out of memory exceptions.
// 4096 works well given the RAM requirements of Alpha2MQTT.
// If you aren't using an ESP8266 you may be able to increase this.
// At 4096 (4095 usable) you can request up to around 70 to 80 registers on a schedule or by request.
// Alpha2MQTT on boot will request a buffer size of (MAX_MQTT_PAYLOAD_SIZE + MQTT_HEADER_SIZE) for MQTT, and
// MAX_MQTT_PAYLOAD_SIZE for building payloads. If these fail and your device doesn't boot, you can assume you've set this too high.
#define MAX_MQTT_PAYLOAD_SIZE 4096
#define MIN_MQTT_PAYLOAD_SIZE 512
#define MQTT_HEADER_SIZE 512
// We will test the SOC register as it is common across all
#define REG_BATTERY_HOME_R_SOC 0x0102 // 0.1/bit, 2 Bytes, Unsigned Short
#define REG_BATTERY_HOME_R_SOC_REGCOUNT 1
// MQTT message to fire out
#define MQTT_MES_ITERATE_SECOND_TEN "/iterate/second/ten"
// Number of Slave IDs to try
#define SLAVE_ID_START 0
#define SLAVE_ID_END 255
// Frame and Function Codes
#define MAX_FRAME_SIZE_ZERO_INDEXED 63
#define MIN_FRAME_SIZE_ZERO_INDEXED 4
#define MAX_FRAME_SIZE_RESPONSE_WRITE_SUCCESS_ZERO_INDEXED 7
#define MODBUS_FN_READDATAREGISTER 0x03
#define MODBUS_FN_WRITEDATAREGISTER 0x10
#define MODBUS_FN_WRITESINGLEREGISTER 0x06
#define FRAME_POSITION_SLAVE_ID 0
#define FRAME_POSITION_FUNCTION_CODE 1
// Wemos OLED Shield set up. 64x48, pins D1 and D2
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 _display(OLED_RESET);
// SoftwareSerial is used to create a second serial port, which will be deidcated to RS485.
// The built-in serial port remains available for flashing and debugging.
#define RS485_TX HIGH // Transmit control pin goes high
#define RS485_RX LOW // Receive control pin goes low
#define SERIAL_COMMUNICATION_CONTROL_PIN D5 // Transmission set pin
#define RX_PIN D6 // Serial Receive pin
#define TX_PIN D7 // Serial Transmit pin
// Ensure we stick to fixed values by forcing from a selection of values for a Modbus request & response
enum modbusRequestAndResponseStatusValues
{
preProcessing,
invalidFrame,
responseTooShort,
noResponse,
noMQTTPayload,
invalidMQTTPayload,
readDataRegisterSuccess,
slaveError,
payloadExceededCapacity,
addedToPayload,
notValidIncomingTopic
};
#define MODBUS_REQUEST_AND_RESPONSE_PREPROCESSING_MQTT_DESC "preProcessing"
#define MODBUS_REQUEST_AND_RESPONSE_INVALID_FRAME_MQTT_DESC "invalidFrame"
#define MODBUS_REQUEST_AND_RESPONSE_RESPONSE_TOO_SHORT_MQTT_DESC "responseTooShort"
#define MODBUS_REQUEST_AND_RESPONSE_NO_RESPONSE_MQTT_DESC "noResponse"
#define MODBUS_REQUEST_AND_RESPONSE_NO_MQTT_PAYLOAD_MQTT_DESC "noMQTTPayload"
#define MODBUS_REQUEST_AND_RESPONSE_INVALID_MQTT_PAYLOAD_MQTT_DESC "invalidMQTTPayload"
#define MODBUS_REQUEST_AND_RESPONSE_READ_DATA_REGISTER_SUCCESS_MQTT_DESC "readDataRegisterSuccess"
#define MODBUS_REQUEST_AND_RESPONSE_ERROR_MQTT_DESC "slaveError"
#define MODBUS_REQUEST_AND_RESPONSE_PAYLOAD_EXCEEDED_CAPACITY_MQTT_DESC "payloadExceededCapacity"
#define MODBUS_REQUEST_AND_RESPONSE_ADDED_TO_PAYLOAD_MQTT_DESC "addedToPayload"
#define MAX_CHARACTER_VALUE_LENGTH 21
#define MAX_MQTT_NAME_LENGTH 81
#define MAX_MQTT_STATUS_LENGTH 51
#define MAX_FORMATTED_DATA_VALUE_LENGTH 129
#define MAX_DATA_TYPE_DESC_LENGTH 20
#define MAX_FORMATTED_DATE_LENGTH 21
#define OLED_CHARACTER_WIDTH 11
// This is the request and return object for the sendModbus() function.
// Some vars are populated prior to the request, which can be used in the response to handle the data
// appropriately, for example typecasting and cleansing.
struct modbusRequestAndResponse
{
//uint8_t errorLevel;
uint8_t data[MAX_FRAME_SIZE_ZERO_INDEXED] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
uint8_t dataSize = 0;
uint8_t functionCode = 0;
};
// Device parameters
char _version[6] = "v1.00";
#ifdef OUTPUT_WIFIANDMQTT
// WiFi parameters
WiFiClient _wifi;
// MQTT parameters
PubSubClient _mqtt(_wifi);
// Buffer Size (and therefore payload size calc)
int _bufferSize;
int _maxPayloadSize;
// I want to declare this once at a modular level, keep the heap somewhere in check.
char* _mqttPayload;
#endif
SoftwareSerial* _RS485Serial;
// OLED variables
char _oledOperatingIndicator = '*';
char _oledLine2[OLED_CHARACTER_WIDTH] = "";
char _oledLine3[OLED_CHARACTER_WIDTH] = "";
char _oledLine4[OLED_CHARACTER_WIDTH] = "";
// Fixed char array for messages to the serial port
char _debugOutput[100];
uint8_t _alphaSlaveID = 0;
// Prototypes
void flushRS485();
modbusRequestAndResponseStatusValues listenResponse(modbusRequestAndResponse* resp);
bool checkForData();
modbusRequestAndResponseStatusValues sendModbus(uint8_t frame[], byte actualFrameSize, modbusRequestAndResponse* resp);
bool checkCRC(uint8_t frame[], byte actualFrameSize);
void calcCRC(uint8_t frame[], byte actualFrameSize);
void iterateSlaveIds(char* topic);
#ifdef OUTPUT_WIFIANDMQTT
modbusRequestAndResponseStatusValues addToPayload(char* addition);
void sendMqtt(char* topic);
void emptyPayload();
#endif
/*
setup
The setup function runs once when you press reset or power the board
*/
void setup()
{
// Set up serial for debugging using an appropriate baud rate
// This is for communication with the development environment, NOT the Alpha system
// See Definitions.h for this.
Serial.begin(9600);
// Configure LED for output
pinMode(LED_BUILTIN, OUTPUT);
// Set up the software serial for communicating with the MAX
// Configure the pin for controlling TX/RX (if using MAX485 with DE/RE pins)
pinMode(SERIAL_COMMUNICATION_CONTROL_PIN, OUTPUT);
// Set pin 'LOW' for 'Receive' mode
digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX);
_RS485Serial = new SoftwareSerial(RX_PIN, TX_PIN);
_RS485Serial->begin(INVERTER_BAUD_RATE);
// Half second wait to give things time to kick in
delay(500);
// Turn on the OLED
_display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize OLED with the I2C addr 0x3C (for the 64x48)
_display.clearDisplay();
_display.display();
updateOLED(false, "", "", _version);
// Configure WIFI
#ifdef OUTPUT_WIFIANDMQTT
setupWifi();
// Configure MQTT to the address and port specified above
_mqtt.setServer(MQTT_SERVER, MQTT_PORT);
#ifdef DEBUG
sprintf(_debugOutput, "About to request buffer");
Serial.println(_debugOutput);
#endif
for (_bufferSize = (MAX_MQTT_PAYLOAD_SIZE + MQTT_HEADER_SIZE); _bufferSize >= MIN_MQTT_PAYLOAD_SIZE + MQTT_HEADER_SIZE; _bufferSize = _bufferSize - 1024)
{
#ifdef DEBUG
sprintf(_debugOutput, "Requesting a buffer of : %d bytes", _bufferSize);
Serial.println(_debugOutput);
#endif
if (_mqtt.setBufferSize(_bufferSize))
{
_maxPayloadSize = _bufferSize - MQTT_HEADER_SIZE;
#ifdef DEBUG
sprintf(_debugOutput, "_bufferSize: %d,\r\n\r\n_maxPayload (Including null terminator): %d", _bufferSize, _maxPayloadSize);
Serial.println(_debugOutput);
#endif
// Example, 2048, if declared as 2048 is positions 0 to 2047, and position 2047 needs to be zero. 2047 usable chars in payload.
_mqttPayload = new char[_maxPayloadSize];
emptyPayload();
break;
}
else
{
#ifdef DEBUG
sprintf(_debugOutput, "Coudln't allocate buffer of %d bytes", _bufferSize);
Serial.println(_debugOutput);
#endif
}
}
// Connect to MQTT
mqttReconnect();
#endif
updateOLED(false, "", "", _version);
}
/*
loop
The loop function runs overand over again until power down or reset
*/
void loop()
{
// Refresh LED Screen, will cause the status asterisk to flicker
updateOLED(true, "", "", "");
#ifdef OUTPUT_WIFIANDMQTT
// Make sure WiFi is good
if (WiFi.status() != WL_CONNECTED)
{
setupWifi();
}
// make sure mqtt is still connected
if ((!_mqtt.connected()) || !_mqtt.loop())
{
mqttReconnect();
}
#endif
// Read and do appropriate actions
sendData();
}
void flushRS485()
{
_RS485Serial->flush();
// Not sure the delay needed.
//delay(200);
while (_RS485Serial->available())
{
_RS485Serial->read();
}
}
/*
sendModbus
Calculates the CRC for any given data frame and sends it over RS485.
Following the send, kicks off a synchronous listen for response
*/
modbusRequestAndResponseStatusValues sendModbus(uint8_t frame[], byte actualFrameSize, modbusRequestAndResponse* resp)
{
//Calculate the CRC and overwrite the last two bytes.
calcCRC(frame, actualFrameSize);
// Make sure there are no spurious characters in the in/out buffer.
flushRS485();
//Send
digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_TX);
_RS485Serial->write(frame, actualFrameSize);
// It's important to reset the SERIAL_COMMUNICATION_CONTROL_PIN as soon as
// we finish sending so that the serial port can start to buffer the response.
digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX);
return listenResponse(resp);
}
/*
setBaudRate()
Sets the baud rate for communication
*/
void setBaudRate(unsigned long baudRate)
{
_RS485Serial->flush();
_RS485Serial->begin(baudRate);
}
/*
listenResponse
Listens for a response and processes what it is given that data frame formats vary based on function codes
Returns data in a stucture and returns a result to guide onward processing.
*/
modbusRequestAndResponseStatusValues listenResponse(modbusRequestAndResponse* resp)
{
uint8_t inFrame[MAX_FRAME_SIZE_ZERO_INDEXED];
uint8_t inByteNumZeroIndexed = 0;
uint8_t inExpectedTotalBytesZeroIndexed = 0;
bool gotSlaveID = false;
bool gotFunctionCode = false;
bool gotData = false;
bool timedOut = false;
bool breakOut = false;
modbusRequestAndResponse dummy;
modbusRequestAndResponseStatusValues result = modbusRequestAndResponseStatusValues::preProcessing;
if (!resp)
{
resp = &dummy;
}
resp->dataSize = 0;
// On responses we know we are expecting at least 5 bytes (probably 6 for two byte error code) with a slave error, successes are longer
// But that depends on the function code returned, so when we get to the function code we can adjust expected total bytes
inExpectedTotalBytesZeroIndexed = MIN_FRAME_SIZE_ZERO_INDEXED;
while ((inByteNumZeroIndexed <= inExpectedTotalBytesZeroIndexed))
{
timedOut = !checkForData();
if (timedOut)
{
break;
}
inFrame[inByteNumZeroIndexed] = _RS485Serial->read();
#ifdef DEBUG_LEVEL2
sprintf(_debugOutput, "Byte number zero indexed: %d", inByteNumZeroIndexed);
Serial.println(_debugOutput);
#endif
//Process the byte
switch (inByteNumZeroIndexed)
{
case FRAME_POSITION_SLAVE_ID:
{
gotSlaveID = true;
#ifdef DEBUG_LEVEL2
sprintf(_debugOutput, "Slave ID: %d", inFrame[FRAME_POSITION_SLAVE_ID]);
Serial.println(_debugOutput);
#endif
// First byte is Slave ID. If not a match (unlikely) try again on the next byte.
if (inFrame[FRAME_POSITION_SLAVE_ID] != _alphaSlaveID)
{
gotSlaveID = false;
inByteNumZeroIndexed--;
}
break;
}
case FRAME_POSITION_FUNCTION_CODE:
{
gotFunctionCode = true;
// Second byte is Function Code.
resp->functionCode = inFrame[FRAME_POSITION_FUNCTION_CODE];
#ifdef DEBUG_LEVEL2
sprintf(_debugOutput, "Function Code: %d", inFrame[FRAME_POSITION_FUNCTION_CODE]);
Serial.println(_debugOutput);
#endif
// Whether the task was successful or not determines how many remaining bytes are expected
//
// A slave error is a function code not equal to one of the three request codes
if (resp->functionCode != MODBUS_FN_WRITEDATAREGISTER && resp->functionCode != MODBUS_FN_WRITESINGLEREGISTER && resp->functionCode != MODBUS_FN_READDATAREGISTER)
{
// Slave Error
// Slave Address, Function Code, Error Code (1 or 2 bytes) and 2 bytes crc
// I want to keep minimum frame size as is, which works on the assumption of a one byte error code
// But I want to cover off the unknown of a two byte error code and it is better to attempt a read of one extra byte
// If the Alpha system doesn't send it, we will just get a timeout. So here we will say we are expecting two bytes
// of data, yet still working on a min frame size of 5 bytes (0-4) to determine responses which are too short.
// When we get out of the reading process we will look at bytes returned in total and adjust.
#ifdef DEBUG_LEVEL2
Serial.println("Slave Error");
#endif
resp->dataSize = 1;
inExpectedTotalBytesZeroIndexed++;
}
else
{
// Success
//
// If a write success, we know expected bytes
if (resp->functionCode == MODBUS_FN_WRITEDATAREGISTER || resp->functionCode == MODBUS_FN_WRITESINGLEREGISTER)
{
#ifdef DEBUG_LEVEL2
//Serial.println("Write Success");
#endif
// In case of single register, high byte address (1), low byte address (1), high byte of data (1), low byte of data (1), (2)*crc
// In case of data register, high byte address (1), low byte address (1), high byte of reg count (1), low byte of reg count (1), (2)*crc
inExpectedTotalBytesZeroIndexed = MAX_FRAME_SIZE_RESPONSE_WRITE_SUCCESS_ZERO_INDEXED;
resp->dataSize = 4;
}
else
{
#ifdef DEBUG_LEVEL2
//Serial.println("Read Success");
#endif
// Success Read
// Get the next byte here so we know expected length
timedOut = !checkForData();
if (timedOut)
{
breakOut = true;
break;
}
inByteNumZeroIndexed++;
inFrame[inByteNumZeroIndexed] = _RS485Serial->read();
resp->dataSize = inFrame[inByteNumZeroIndexed];
// Assume numbytes is 2
// slave + func + numbytes + 2 + 2 crc = 7 bytes, which is 0 to 6 when zero indexed
// And 7 takeaway 3 bytes received is 4
inExpectedTotalBytesZeroIndexed = inFrame[inByteNumZeroIndexed] + 4;
}
}
break;
}
default:
{
gotData = true;
// If reading there's an extra byte in the form of data length
if (resp->functionCode == MODBUS_FN_READDATAREGISTER)
{
resp->data[inByteNumZeroIndexed - 3] = inFrame[inByteNumZeroIndexed];
}
else
{
resp->data[inByteNumZeroIndexed - 2] = inFrame[inByteNumZeroIndexed];
}
}
}
// This can only go true if we request the next byte for data bytes and it came back with nothing
if (breakOut)
{
break;
}
// Move to the next byte
inByteNumZeroIndexed++;
}
if (timedOut)
{
#ifdef DEBUG
sprintf(_debugOutput, "Timed Out (inByteNumZeroIndexed): %d", inByteNumZeroIndexed);
Serial.println(_debugOutput);
sprintf(_debugOutput, "Timed Out (gotSlaveID): %d", gotSlaveID);
Serial.println(_debugOutput);
sprintf(_debugOutput, "Timed Out (gotFunctionCode): %d", gotFunctionCode);
Serial.println(_debugOutput);
sprintf(_debugOutput, "Timed Out (resp->functionCode): %d", resp->functionCode);
Serial.println(_debugOutput);
sprintf(_debugOutput, "Timed Out (gotData): %d", gotData);
Serial.println(_debugOutput);
sprintf(_debugOutput, "Timed Out (resp->dataSize): %d", resp->dataSize);
Serial.println(_debugOutput);
#endif
}
// Check what to report back
if (inByteNumZeroIndexed == 0)
{
result = modbusRequestAndResponseStatusValues::noResponse;
}
else
{
if (gotSlaveID && gotFunctionCode && gotData)
{
if (resp->functionCode != MODBUS_FN_READDATAREGISTER && resp->functionCode != MODBUS_FN_WRITEDATAREGISTER && resp->functionCode != MODBUS_FN_WRITESINGLEREGISTER)
{
// This is the fix for not knowing how many bytes an error code is on a slave error
if (inByteNumZeroIndexed > MIN_FRAME_SIZE_ZERO_INDEXED)
{
// We got an extra byte
resp->dataSize = 2;
}
}
}
if (!timedOut && gotSlaveID && gotFunctionCode && gotData)
{
// If we haven't timed out, and got everything we expected from the packet,
// The previous loop will always end up +1 more than we have, as it ends with an increment.
// Bring it back in line.
inByteNumZeroIndexed--;
}
if (inByteNumZeroIndexed < MIN_FRAME_SIZE_ZERO_INDEXED)
{
result = modbusRequestAndResponseStatusValues::responseTooShort;
}
else if (checkCRC(inFrame, inByteNumZeroIndexed + 1))
{
if (resp->functionCode == MODBUS_FN_READDATAREGISTER)
{
result = modbusRequestAndResponseStatusValues::readDataRegisterSuccess;
}
else
{
result = modbusRequestAndResponseStatusValues::slaveError;
}
}
else
{
result = modbusRequestAndResponseStatusValues::invalidFrame;
}
}
if (result != modbusRequestAndResponseStatusValues::readDataRegisterSuccess)
{
#ifdef DEBUG_LEVEL2
sprintf(_debugOutput, "Coming out of listenResponse with an issue - Function code (%d) and %s: %d", resp->functionCode);
Serial.println(_debugOutput);
#endif
}
return result;
}
/*
checkCRC
Calculates the CRC for any given data frame and compares it to what came in
calcCRC is based on
https://github.com/angeloc/simplemodbusng/blob/master/SimpleModbusMaster/SimpleModbusMaster.cpp
*/
bool checkCRC(uint8_t frame[], byte actualFrameSize)
{
unsigned int calculated_crc, received_crc;
received_crc = ((frame[actualFrameSize - 2] << 8) | frame[actualFrameSize - 1]);
calcCRC(frame, actualFrameSize);
calculated_crc = ((frame[actualFrameSize - 2] << 8) | frame[actualFrameSize - 1]);
return (received_crc = calculated_crc);
}
/*
calcCRC
Calculates the CRC for any given data frame
calcCRC is based on
https://github.com/angeloc/simplemodbusng/blob/master/SimpleModbusMaster/SimpleModbusMaster.cpp
*/
void calcCRC(uint8_t frame[], byte actualFrameSize)
{
unsigned int temp = 0xffff, flag;
for (unsigned char i = 0; i < actualFrameSize - 2; i++)
{
temp = temp ^ frame[i];
for (unsigned char j = 1; j <= 8; j++)
{
flag = temp & 0x0001;
temp >>= 1;
if (flag)
temp ^= 0xA001;
}
}
// Bytes are reversed.
frame[actualFrameSize - 2] = temp & 0xff;
frame[actualFrameSize - 1] = temp >> 8;
}
/*
checkForData
Returns true if there is some data in the serial buffer, otherwise false
*/
bool checkForData()
{
int tries = 0;
while ((!_RS485Serial->available()) && (tries++ < RS485_TRIES))
{
delay(50);
}
if (tries >= RS485_TRIES)
{
Serial.println("Timeout waiting for RS485 response. Likely no more data coming.");
return false;
}
else
{
return true;
}
}
/*
setupWifi
Connect to WiFi
*/
#ifdef OUTPUT_WIFIANDMQTT
void setupWifi()
{
// We start by connecting to a WiFi network
#ifdef DEBUG
sprintf(_debugOutput, "Connecting to %s", WIFI_SSID);
Serial.println(_debugOutput);
#endif
// Set up in Station Mode - Will be connecting to an access point
WiFi.mode(WIFI_STA);
// And connect to the details defined at the top
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// And continually try to connect to WiFi. If it doesn't, the device will just wait here before continuing
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
updateOLED(false, "Connecting", "WiFi...", _version);
}
// Set the hostname for this Arduino
WiFi.hostname(DEVICE_NAME);
// Output some debug information
#ifdef DEBUG
Serial.print("WiFi connected, IP is");
Serial.print(WiFi.localIP());
#endif
// Connected, so ditch out with blank screen
updateOLED(false, "", "", _version);
}
#endif
/*
checkTimer
Check to see if the elapsed interval has passed since the passed in millis() value. If it has, return true and update the lastRun.
Note that millis() overflows after 50 days, so we need to deal with that too... in our case we just zero the last run, which means the timer
could be shorter but it's not critical... not worth the extra effort of doing it properly for once in 50 days.
*/
bool checkTimer(unsigned long* lastRun, unsigned long interval)
{
unsigned long now = millis();
if (*lastRun > now)
*lastRun = 0;
if (now >= *lastRun + interval)
{
*lastRun = now;
return true;
}
return false;
}
/*
updateOLED
Update the OLED. Use "NULL" for no change to a line or "" for an empty line.
Three parameters representing each of the three lines available for status indication - Top line functionality fixed
*/
void updateOLED(bool justStatus, const char* line2, const char* line3, const char* line4)
{
static unsigned long updateStatusBar = 0;
_display.clearDisplay();
_display.setTextSize(1);
_display.setTextColor(WHITE);
_display.setCursor(0, 0);
char line1Contents[OLED_CHARACTER_WIDTH];
char line2Contents[OLED_CHARACTER_WIDTH];
char line3Contents[OLED_CHARACTER_WIDTH];
char line4Contents[OLED_CHARACTER_WIDTH];
// Ensure only dealing with 10 chars passed in, and null terminate.
if (strlen(line2) > OLED_CHARACTER_WIDTH - 1)
{
strncpy(line2Contents, line2, OLED_CHARACTER_WIDTH - 1);
line2Contents[OLED_CHARACTER_WIDTH - 1] = 0;
}
else
{
strcpy(line2Contents, line2);
}
if (strlen(line3) > OLED_CHARACTER_WIDTH - 1)
{
strncpy(line3Contents, line3, OLED_CHARACTER_WIDTH - 1);
line3Contents[OLED_CHARACTER_WIDTH - 1] = 0;
}
else
{
strcpy(line3Contents, line3);
}
if (strlen(line4) > OLED_CHARACTER_WIDTH - 1)
{
strncpy(line4Contents, line4, OLED_CHARACTER_WIDTH - 1);
line4Contents[OLED_CHARACTER_WIDTH - 1] = 0;
}
else
{
strcpy(line4Contents, line4);
}
// Only update the operating indicator once per half second.
if (checkTimer(&updateStatusBar, UPDATE_STATUS_BAR_INTERVAL))
{
// Simply swap between space and asterisk every time we come here to give some indication of activity
_oledOperatingIndicator = (_oledOperatingIndicator == '*') ? ' ' : '*';
}
// There's ten characters we can play with, width wise.
sprintf(line1Contents, "Finding...");
_display.println(line1Contents);
// Next line
_display.setCursor(0, 12);
if (!justStatus)
{
_display.println(line2Contents);
strcpy(_oledLine2, line2Contents);
}
else
{
_display.println(_oledLine2);
}
_display.setCursor(0, 24);
if (!justStatus)
{
_display.println(line3Contents);
strcpy(_oledLine3, line3Contents);
}
else
{
_display.println(_oledLine3);
}
_display.setCursor(0, 36);
if (!justStatus)
{
_display.println(line4Contents);
strcpy(_oledLine4, line4Contents);
}
else
{
_display.println(_oledLine4);
}
// Refresh the display
_display.display();
}
/*
mqttReconnect
This function reconnects the ESP8266 to the MQTT broker
*/
#ifdef OUTPUT_WIFIANDMQTT
void mqttReconnect()
{
// Loop until we're reconnected
while (true)
{
_mqtt.disconnect(); // Just in case.
delay(200);
#ifdef DEBUG
Serial.print("Attempting MQTT connection...");
#endif
updateOLED(false, "Connecting", "MQTT...", _version);
delay(100);
// Attempt to connect
if (_mqtt.connect(DEVICE_NAME, MQTT_USERNAME, MQTT_PASSWORD))
{
Serial.println("Connected MQTT");
// Connected
break;
}
// Wait 1 second before retrying
delay(1000);
}
}
#endif
/*
addToPayload
Safely adds characters to the payload buffer, ensuring no overruns.
*/
#ifdef OUTPUT_WIFIANDMQTT
modbusRequestAndResponseStatusValues addToPayload(char* addition)
{
int targetRequestedSize = strlen(_mqttPayload) + strlen(addition);
// If max payload size is 2048 it is stored as (0-2047), however character 2048 (position 2047) is null terminator so 2047 chars usable usable
if (targetRequestedSize > _maxPayloadSize - 1)
{
// Safely print using snprintf
snprintf(_mqttPayload, _maxPayloadSize, "{\r\n \"mqttError\": \"Length of payload exceeds %d bytes. Length would be %d bytes.\"\r\n}", _maxPayloadSize - 1, targetRequestedSize);
return modbusRequestAndResponseStatusValues::payloadExceededCapacity;
}
else
{
// Add to the payload by sprintf back on itself with the addition
sprintf(_mqttPayload, "%s%s", _mqttPayload, addition);
return modbusRequestAndResponseStatusValues::addedToPayload;
}
}
#endif
/*
sendData
Runs once every loop, checks to see if the time period has elapsed for iteration
If so, fir eoff the code
*/
void sendData()
{
static unsigned long lastRunTenSeconds = 0;
// Update all parameters and send to MQTT.
if (checkTimer(&lastRunTenSeconds, ITERATE_INTERVAL))