Skip to content

Commit 59ae640

Browse files
committed
Merge branch 'master' into jeremypoulter/issue29
2 parents 3c22f70 + cfb1cbe commit 59ae640

File tree

8 files changed

+499
-219
lines changed

8 files changed

+499
-219
lines changed

platformio.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ build_flags_openevse_tft =
112112
-D WIFI_PIXEL_NUMBER=1
113113
-D WIFI_BUTTON=0
114114
-D WIFI_BUTTON_PRESSED_STATE=LOW
115-
-D I2C_SDA=21
116-
-D I2C_SCL=22
115+
-D I2C_SDA=22
116+
-D I2C_SCL=21
117117
-D ENABLE_MCP9808
118118
-D ENABLE_PN532
119119
build_partitions = min_spiffs.csv
120120
build_partitions_debug = min_spiffs_debug.csv
121121
build_partitions_16mb = openevse_16mb.csv
122122

123-
neopixel_lib = adafruit/Adafruit NeoPixel@1.11.0
123+
neopixel_lib = adafruit/Adafruit NeoPixel@1.12.2
124124

125125
gfx_display_libs =
126126

src/energy_meter.cpp

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void EnergyMeterData::deserialize(StaticJsonDocument<capacity> &doc)
119119
// old OpenEvse total_energy imported flag
120120
imported = doc["im"];
121121
}
122-
if (doc.containsKey("el") && doc["el"].is<uint32_t>())
122+
if (doc.containsKey("el") && doc["el"].is<double>())
123123
{
124124
// elapsed
125125
elapsed = doc["el"];
@@ -134,7 +134,6 @@ void EnergyMeterData::deserialize(StaticJsonDocument<capacity> &doc)
134134
EnergyMeter::EnergyMeter() : _last_upd(0),
135135
_write_upd(0),
136136
_rotate_upd(0),
137-
_elapsed(0),
138137
_switch_state(0){};
139138

140139
EnergyMeter::~EnergyMeter()
@@ -230,51 +229,23 @@ bool EnergyMeter::update()
230229
if (_monitor->isCharging())
231230
{
232231
// increment elapsed time
233-
_data.elapsed += dms / 1000U;
232+
_data.elapsed += dms / 1000.0;
234233

235234
DBUGLN("Energy Meter: Incrementing");
236235
// accumulate data
237-
uint32_t mv = _monitor->getVoltage() * 1000;
238-
uint32_t ma = _monitor->getAmps() * 1000;
239-
240-
/*
241-
* The straightforward formula to compute 'milliwatt-seconds' would be:
242-
* mws = (mv/1000) * (ma/1000) * dms;
243-
*
244-
* However, this has some serious drawbacks, namely, truncating values
245-
* when using integer math. This can introduce a lot of error!
246-
* 5900 milliamps -> 5.9 amps (float) -> 5 amps (integer)
247-
* 0.9 amps difference / 5.9 amps -> 15.2% error
248-
*
249-
* The crazy equation below helps ensure our intermediate results always
250-
* fit in a 32-bit unsigned integer, but retain as much precision as
251-
* possible throughout the calculation. Here is how it was derived:
252-
* mws = (mv/1000) * (ma/1000) * dms;
253-
* mws = (mv/(2**3 * 5**3)) * (ma/(2**3 * 5**3)) * dms;
254-
* mws = (mv/2**3) * (ma/(2**3) / 5**6 * dms;
255-
* mws = (mv/2**4) * (ma/(2**2) / 5**6 * dms;
256-
*
257-
* By breaking 1000 into prime factors of 2 and 5, and shifting things
258-
* around, we almost match the precision of floating-point math.
259-
*
260-
* Using 16 and 4 divisors, rather than 8 and 8, helps precision because
261-
* mv is always greater than ~100000, but ma can be as low as ~6000.
262-
*
263-
* A final note- the divisions by factors of 2 are done with right shifts
264-
* by the compiler, so the revised equation, although it looks quite
265-
* complex, only requires one divide operation.
266-
*/
267-
double mws = (mv / 16) * (ma / 4) / 15625 * dms;
236+
double v = _monitor->getVoltage();
237+
double a = _monitor->getAmps();
238+
double mws = v * a * dms;
268239
if (config_threephase_enabled())
269240
{
270241
// Multiply calculation by 3 to get 3-phase energy.
271242
mws *= 3;
272243
}
273244

274245
// convert to w/h
275-
double wh = mws / 3600000UL;
246+
double wh = mws / 3600000;
276247
_data.session += wh;
277-
double kwh = wh / 1000UL;
248+
double kwh = wh / 1000;
278249
_data.total += kwh;
279250
DBUGVAR(_data.session);
280251
_data.daily += kwh;
@@ -318,7 +289,7 @@ bool EnergyMeter::publish()
318289

319290
void EnergyMeter::createEnergyMeterJsonDoc(JsonDocument &doc)
320291
{
321-
doc["session_elapsed"] = _data.elapsed; // sec
292+
doc["session_elapsed"] = (uint32_t)_data.elapsed; // sec
322293
doc["session_energy"] = _data.session; // wh
323294
doc["total_energy"] = _data.total; // kwh
324295
doc["total_day"] = _data.daily; // kwh

src/energy_meter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class EnergyMeterData
3939
double weekly; // kwh
4040
double monthly; // kwh
4141
double yearly; // kwh
42-
uint32_t elapsed; // sec
42+
double elapsed; // sec
4343
uint32_t switches; // homw many switches the relay/contactor got
4444
bool imported; // has imported old counter already
4545
EnergyMeterDate date;
@@ -57,7 +57,6 @@ class EnergyMeter
5757
uint32_t _write_upd;
5858
uint32_t _event_upd;
5959
uint32_t _rotate_upd;
60-
uint32_t _elapsed;
6160
uint8_t _switch_state; // 0: Undefined, 1: Enabled, 2: Disabled
6261

6362
EvseMonitor *_monitor;

src/evse_man.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class EvseManager : public MicroTasks::Task
348348
bool publishEnergyMeter() {
349349
return _monitor.publishEnergyMeter();
350350
}
351-
void createEnergyMeterJsonDoc(JsonDocument &doc) {
351+
void createEnergyMeterJsonDoc(JsonDocument &doc) {
352352
_monitor.createEnergyMeterJsonDoc(doc);
353353
}
354354
long getFaultCountGFCI() {

src/ocpp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ void OcppTask::loadEvseBehavior() {
447447
LCD_DISPLAY("Aborted / no EV");
448448
break;
449449
case MicroOcpp::TxNotification::DeAuthorized:
450-
LCD_DISPLAY("Card unkown");
450+
LCD_DISPLAY("Card unknown");
451451
break;
452452
case MicroOcpp::TxNotification::RemoteStart:
453453
if (!evse->isVehicleConnected()) {

0 commit comments

Comments
 (0)