-
Notifications
You must be signed in to change notification settings - Fork 9
/
P1Power.cpp
113 lines (104 loc) · 2.43 KB
/
P1Power.cpp
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
#include "P1Power.h"
P1Power::P1Power(HardwareSerial *serIn, int sid, byte t, int f) : BaseSensor(1000,sid,f) // 1000 is dummy. not used for P1
{
Type = t; // default = 24, same as ferraris sensor: log net consumption
serial = serIn;
}
void P1Power::Begin(byte i)
{
BaseSensor::Begin(i);
serial->begin(9600);
}
void P1Power::Reset()
{
Today = 0;
Midnight = (m1+m2) - (m3+m4);
BaseSensor::Reset();
}
void P1Power::CalculateActuals()
{
Actual = PowerUsage - PowerSolar;
if(Peak < abs(Actual)) Peak = Actual;
todayCnt = ((m1+m2)-(m3+m4)) - Midnight;
Today = todayCnt;
}
void P1Power::Loop(int m)
{
while(serial->available())
{
char c = serial->read();
if (c == '\n' || bufpos == BUFSIZE-1)
{
Serial.println(bufpos);
buffer[bufpos] = '\0';
bufpos = 0;
ParseBuffer();
}
else
{
buffer[bufpos++] = c&127;
}
}
}
void P1Power::ParseBuffer()
{
// buffer contains one line of text
long tl,tld;
int i,j;
if (readnextLine)
{
// gas actual counter: (00127.969)
if (sscanf(buffer,"(%ld.%ld)" , &tl, &tld) == 2)
{
GasUsage = tl * 1000 + tld;
readnextLine = false;
}
}
else
{
// Consumption: 1-0:1.8.1(00391.000*kWh)
// 1-0:1.8.2(00391.000*kWh)
// Production: 1-0:2.8.1(00391.000*kWh)
// 1-0:2.8.2(00391.000*kWh)
if (sscanf(buffer,"1-0:%d.8.%d(%ld.%ld" , &i, &j, &tl, &tld)==4)
{
tl *= 1000;
tl += tld;
if(i == 1)
{
if(j == 1) m1 = tl;
if(j == 2) m2 = tl;
}
if(i == 2)
{
if(j == 1) m3 = tl;
if(j == 2) m4 = tl;
}
}
// gas definition: 0-1:24.3.0
if (sscanf(buffer,"0-1:24.%d.%d",&i,&j) == 2)
{
if(i == 3 && j == 0) readnextLine = true; // the actual gas counter is found on the next line
}
// Actual Consumption: 1-0:1.7.0(0000.50*kW)
// Actual Generation: 1-0:2.7.0(0001.20*kW)
if (sscanf(buffer,"1-0:%d.7.0(%ld.%ld" , &i, &tl , &tld) == 3)
{
tl *= 1000;
tl += tld * 10;
if(i == 1) PowerUsage = tl;
if(i == 2) PowerSolar = tl;
}
}
}
void P1Power::Status(Print& client)
{
BaseSensor::Status(client);
client << F("<td>PowerUsage=") << PowerUsage;
client << F(" PowerSolar=") << PowerSolar;
client << F(" GasUsage=") << GasUsage;
client << F(" M1=") << m1;
client << F(" M2=") << m2;
client << F(" M3=") << m3;
client << F(" M4=") << m4;
}