1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,158 @@ |
1 |
+unsigned int CRC16(unsigned int crc, unsigned char *buf, int len) |
|
2 |
+{ |
|
3 |
+ for (int pos = 0; pos < len; pos++) |
|
4 |
+ { |
|
5 |
+ crc ^= (unsigned int)buf[pos]; |
|
6 |
+ |
|
7 |
+ for (int i = 8; i != 0; i--) |
|
8 |
+ { |
|
9 |
+ if ((crc & 0x0001) != 0) |
|
10 |
+ { |
|
11 |
+ crc >>= 1; |
|
12 |
+ crc ^= 0xA001; |
|
13 |
+ } |
|
14 |
+ else |
|
15 |
+ { |
|
16 |
+ crc >>= 1; |
|
17 |
+ } |
|
18 |
+ } |
|
19 |
+ } |
|
20 |
+ return crc; |
|
21 |
+} |
|
22 |
+ |
|
23 |
+bool isNumber(char *res, int len) |
|
24 |
+{ |
|
25 |
+ for (int i = 0; i < len; i++) |
|
26 |
+ { |
|
27 |
+ if (((res[i] < '0') || (res[i] > '9')) && (res[i] != '.' && res[i] != 0)) |
|
28 |
+ { |
|
29 |
+ return false; |
|
30 |
+ } |
|
31 |
+ } |
|
32 |
+ return true; |
|
33 |
+} |
|
34 |
+ |
|
35 |
+int findCharInArrayRev(char array[], char c, int len) |
|
36 |
+{ |
|
37 |
+ for (int i = len - 1; i >= 0; i--) |
|
38 |
+ { |
|
39 |
+ if (array[i] == c) |
|
40 |
+ { |
|
41 |
+ return i; |
|
42 |
+ } |
|
43 |
+ } |
|
44 |
+ |
|
45 |
+ return -1; |
|
46 |
+} |
|
47 |
+ |
|
48 |
+long getValue(char *buffer, int maxlen, char startchar, char endchar) |
|
49 |
+{ |
|
50 |
+ int s = findCharInArrayRev(buffer, startchar, maxlen - 2); |
|
51 |
+ int l = findCharInArrayRev(buffer, endchar, maxlen - 2) - s - 1; |
|
52 |
+ |
|
53 |
+ char res[16]; |
|
54 |
+ memset(res, 0, sizeof(res)); |
|
55 |
+ |
|
56 |
+ if (strncpy(res, buffer + s + 1, l)) |
|
57 |
+ { |
|
58 |
+ if (endchar == '*') |
|
59 |
+ { |
|
60 |
+ if (isNumber(res, l)) |
|
61 |
+ return (1000 * atof(res)); |
|
62 |
+ } |
|
63 |
+ else if (endchar == ')') |
|
64 |
+ { |
|
65 |
+ if (isNumber(res, l)) |
|
66 |
+ return atof(res); |
|
67 |
+ } |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ return 0; |
|
71 |
+} |
|
72 |
+ |
|
73 |
+/** |
|
74 |
+ Decodes the telegram PER line. Not the complete message. |
|
75 |
+*/ |
|
76 |
+bool decodeTelegram(int len) |
|
77 |
+{ |
|
78 |
+ int startChar = findCharInArrayRev(telegram, '/', len); |
|
79 |
+ int endChar = findCharInArrayRev(telegram, '!', len); |
|
80 |
+ bool validCRCFound = false; |
|
81 |
+ |
|
82 |
+ for (int cnt = 0; cnt < len; cnt++) |
|
83 |
+ { |
|
84 |
+ Serial.print(telegram[cnt]); |
|
85 |
+ } |
|
86 |
+ Serial.print("\n"); |
|
87 |
+ |
|
88 |
+ if (startChar >= 0) |
|
89 |
+ { |
|
90 |
+ // * Start found. Reset CRC calculation |
|
91 |
+ currentCRC = CRC16(0x0000, (unsigned char *)telegram + startChar, len - startChar); |
|
92 |
+ } |
|
93 |
+ else if (endChar >= 0) |
|
94 |
+ { |
|
95 |
+ // * Add to crc calc |
|
96 |
+ currentCRC = CRC16(currentCRC, (unsigned char *)telegram + endChar, 1); |
|
97 |
+ |
|
98 |
+ char messageCRC[5]; |
|
99 |
+ strncpy(messageCRC, telegram + endChar + 1, 4); |
|
100 |
+ |
|
101 |
+ messageCRC[4] = 0; |
|
102 |
+ validCRCFound = (strtol(messageCRC, NULL, 16) == currentCRC); |
|
103 |
+ |
|
104 |
+ currentCRC = 0; |
|
105 |
+ } |
|
106 |
+ else |
|
107 |
+ { |
|
108 |
+ currentCRC = CRC16(currentCRC, (unsigned char *)telegram, len); |
|
109 |
+ } |
|
110 |
+ |
|
111 |
+ // Loops through all the telegramObjects to find the code in the telegram line |
|
112 |
+ // If it finds the code the value will be stored in the object so it can later be send to the mqtt broker |
|
113 |
+ for (int i = 0; i < NUMBER_OF_READOUTS; i++) |
|
114 |
+ { |
|
115 |
+ if (strncmp(telegram, telegramObjects[i].code, strlen(telegramObjects[i].code)) == 0) |
|
116 |
+ { |
|
117 |
+ telegramObjects[i].value = getValue(telegram, len, telegramObjects[i].startChar, telegramObjects[i].endChar); |
|
118 |
+ } |
|
119 |
+ } |
|
120 |
+ |
|
121 |
+ return validCRCFound; |
|
122 |
+} |
|
123 |
+ |
|
124 |
+void readP1Serial() |
|
125 |
+{ |
|
126 |
+ if (Serial.available()) |
|
127 |
+ { |
|
128 |
+ memset(telegram, 0, sizeof(telegram)); |
|
129 |
+ while (Serial.available()) |
|
130 |
+ { |
|
131 |
+ ESP.wdtDisable(); |
|
132 |
+ // Reads the telegram untill it finds a return character |
|
133 |
+ // That is after each line in the telegram |
|
134 |
+ int len = Serial.readBytesUntil('\n', telegram, P1_MAXLINELENGTH); |
|
135 |
+ ESP.wdtEnable(1); |
|
136 |
+ |
|
137 |
+ telegram[len] = '\n'; |
|
138 |
+ telegram[len + 1] = 0; |
|
139 |
+ yield(); |
|
140 |
+ |
|
141 |
+ bool result = decodeTelegram(len + 1); |
|
142 |
+ // When the CRC is checked (which is also the end of the telegram), |
|
143 |
+ // all the data collected will be send to the mqtt broker |
|
144 |
+ if (useCRC) { |
|
145 |
+ if (result) |
|
146 |
+ { |
|
147 |
+ LAST_UPDATE_SENT = millis(); |
|
148 |
+ sendDataToBroker(); |
|
149 |
+ } |
|
150 |
+ } |
|
151 |
+ else |
|
152 |
+ { |
|
153 |
+ LAST_UPDATE_SENT = millis(); |
|
154 |
+ sendDataToBroker(); |
|
155 |
+ } |
|
156 |
+ } |
|
157 |
+ } |
|
158 |
+} |