forked from bartwo/esp32_p1meter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathread_p1.ino
145 lines (124 loc) · 4.19 KB
/
read_p1.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
unsigned int crc16(unsigned int crc, unsigned char *buf, int len) {
for (int pos = 0; pos < len; pos++) {
crc ^= (unsigned int)buf[pos];
for (int i = 8; i != 0; i--) {
if ((crc & 0x0001) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
short getValueType(char *res, int len) {
if (len > 10) {
return -1; // treat longer than possible max int 2147483647 as strings
}
short dotsFound = 0;
for (int i = 0; i < len; i++) {
if (((res[i] < '0') || (res[i] > '9')) && (res[i] != '.' && res[i] != 0)) {
return -1; // non-numeric
}
if (res[i] == '.') {
dotsFound++;
}
}
if (dotsFound > 1) {
return -1; // more than 1 dot means something weird, not number for sure
}
return dotsFound; // 1 - float/double, 0 - integer
}
int findCharInArrayRev(char array[], char c, int len) {
for (int i = len - 1; i >= 0; i--) {
if (array[i] == c) {
return i;
}
}
return -1;
}
String getValue(char *buffer, int maxlen, char startchar, char endchar) {
int s = findCharInArrayRev(buffer, startchar, maxlen - 2);
int l = findCharInArrayRev(buffer, endchar, maxlen - 2) - s - 1;
if (l < 0) // end char not found
return String(NA);
char res[128];
memset(res, 0, sizeof(res));
if (strncpy(res, buffer + s + 1, l)) {
short numType = getValueType(res, l);
if (numType == 1) // float or double
return String((endchar == VALUE_END_CHAR ? VALUE_NUMERIC_MULTIPLIER : 1) * atof(res), VALUE_FLOAT_DECIMAL_PLACES);
else if (numType == 0) // integer
return String((endchar == VALUE_END_CHAR ? VALUE_NUMERIC_MULTIPLIER : 1) * atoi(res));
else
return String(res);
}
return "";
}
/**
* Decodes the telegram PER line. Not the complete message.
*/
bool decodeTelegram(int len) {
int startChar = findCharInArrayRev(telegram, '/', len);
int endChar = findCharInArrayRev(telegram, '!', len);
bool validCRCFound = false;
#ifdef DEBUG
for (int cnt = 0; cnt < len; cnt++) {
Serial.print(telegram[cnt]);
}
Serial.println("");
#endif
if (startChar >= 0) {
// * Start found. Reset CRC calculation
currentCRC = crc16(0x0000, (unsigned char *)telegram + startChar, len - startChar);
} else if (endChar >= 0) {
// * Add to crc calc
currentCRC = crc16(currentCRC, (unsigned char *)telegram + endChar, 1);
char messageCRC[5];
strncpy(messageCRC, telegram + endChar + 1, 4);
messageCRC[4] = 0; // * Thanks to HarmOtten (issue 5)
validCRCFound = (strtol(messageCRC, NULL, 16) == currentCRC);
debug(validCRCFound ? "CRC valid!" : "CRC Invalid!");
currentCRC = 0;
} else {
currentCRC = crc16(currentCRC, (unsigned char *)telegram, len);
}
// Loops throug all the telegramObjects to find the code in the telegram line
// If it finds the code the value will be stored in the object so it can later be sent to the mqtt broker
for (int i = 0; i < telegramObjects.size(); i++) {
if (strncmp(telegram, telegramObjects[i].code, strlen(telegramObjects[i].code)) == 0) {
String newValue = getValue(telegram, len, VALUE_START_CHAR, VALUE_END_CHAR);
if (newValue == String(NA)) {
newValue = getValue(telegram, len, VALUE_START_CHAR, VALUE_NO_UNITS_END_CHAR);
}
if (newValue != telegramObjects[i].value) {
telegramObjects[i].value = newValue;
telegramObjects[i].sendData = true;
}
debug((String) "Found a Telegram object: " + telegramObjects[i].name + " value: " + telegramObjects[i].value);
break;
}
}
return validCRCFound;
}
bool readP1Serial() {
if (Serial2.available()) {
debug("Serial2 is available. Memset telegram.");
memset(telegram, 0, sizeof(telegram));
while (Serial2.available()) {
// Reads the telegram until it finds a return character
// That is after each line in the telegram
int len = Serial2.readBytesUntil('\n', telegram, P1_MAXLINELENGTH);
telegram[len] = '\n';
telegram[len + 1] = 0;
bool result = decodeTelegram(len + 1);
// When the CRC is check which is also the end of the telegram
// if valid decode return true
if (result) {
return true;
}
}
}
return false;
}