forked from RoySpringer/esp32_p1meter
-
Notifications
You must be signed in to change notification settings - Fork 10
/
read_p1.ino
170 lines (150 loc) · 4.22 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
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
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;
}
bool isNumber(char *res, int len)
{
for (int i = 0; i < len; i++)
{
if (((res[i] < '0') || (res[i] > '9')) && (res[i] != '.' && res[i] != 0))
{
return false;
}
}
return true;
}
int findCharInArrayRev(char array[], char c, int len)
{
for (int i = len - 1; i >= 0; i--)
{
if (array[i] == c)
{
return i;
}
}
return -1;
}
long 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;
char res[16];
memset(res, 0, sizeof(res));
if (strncpy(res, buffer + s + 1, l))
{
if (endchar == '*')
{
if (isNumber(res, l))
return (1000 * atof(res));
}
else if (endchar == ')')
{
if (isNumber(res, l))
return atof(res);
}
}
return 0;
}
/**
* 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.print("\n");
#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);
#ifdef DEBUG
if (validCRCFound)
Serial.println(F("CRC Valid!"));
else
Serial.println(F("CRC Invalid!"));
#endif
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 send to the mqtt broker
for (int i = 0; i < NUMBER_OF_READOUTS; i++)
{
if (strncmp(telegram, telegramObjects[i].code, strlen(telegramObjects[i].code)) == 0)
{
long newValue = getValue(telegram, len, telegramObjects[i].startChar, telegramObjects[i].endChar);
if (newValue != telegramObjects[i].value)
{
telegramObjects[i].value = newValue;
telegramObjects[i].sendData = true;
}
break;
#ifdef DEBUG
Serial.println((String) "Found a Telegram object: " + telegramObjects[i].name + " value: " + telegramObjects[i].value);
#endif
}
}
return validCRCFound;
}
bool readP1Serial()
{
if (Serial2.available())
{
#ifdef DEBUG
Serial.println("Serial2 is available");
Serial.println("Memset telegram");
#endif
memset(telegram, 0, sizeof(telegram));
while (Serial2.available())
{
// Reads the telegram untill 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;
}