Skip to content

Commit 001883d

Browse files
authored
Merge pull request #70 from basmeerman/work/plan-04
feat: EVCC integration — IEC 61851 state, phase switching, HTTP API
2 parents 1aa1660 + 213a26f commit 001883d

File tree

12 files changed

+1811
-1039
lines changed

12 files changed

+1811
-1039
lines changed

SmartEVSE-3/src/esp32.cpp

Lines changed: 2 additions & 1038 deletions
Large diffs are not rendered by default.

SmartEVSE-3/src/esp32.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,29 @@ void setMode(uint8_t NewMode) ;
231231
void BuzzConfirmation(void);
232232
void BuzzError(void);
233233

234+
// HTTP handler helpers (used by http_handlers.cpp)
235+
uint8_t getErrorId(uint8_t ErrorCode);
236+
const char *getErrorNameWeb(uint8_t ErrorCode);
237+
void printRFID(char *buf, size_t bufsize);
238+
int StoreTimeString(String DelayedTimeStr, DelayedTimeStruct *DelayedTime);
239+
240+
// String arrays used by HTTP response
241+
extern const char StrStateNameWeb[15][17];
242+
extern const char StrErrorNameWeb[9][20];
243+
extern const char StrRFIDStatusWeb[8][20];
244+
245+
// MQTT cache (non-static, shared with http_handlers.cpp)
246+
#include "mqtt_publish.h"
247+
extern mqtt_cache_t mqtt_cache;
248+
249+
// Modem (conditional)
250+
#if MODEM
251+
void RecomputeSoC(void);
252+
#endif
253+
254+
// LCD PIN code (used by http_handlers.cpp)
255+
extern uint16_t LCDPin;
256+
234257
#if ENABLE_OCPP && defined(SMARTEVSE_VERSION) //run OCPP only on ESP32
235258
void ocppUpdateRfidReading(const unsigned char *uuid, size_t uuidLen);
236259
bool ocppIsConnectorPlugged();

SmartEVSE-3/src/http_api.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,68 @@
66
*/
77

88
#include "http_api.h"
9+
#include "evse_ctx.h"
910

1011
#ifndef MIN_CURRENT
1112
#define MIN_CURRENT 6
1213
#endif
1314

15+
/* Soft error flags that do not indicate an EVSE fault — these are
16+
* temporary operational conditions (current too low, no solar surplus)
17+
* and should not map to IEC 61851 state E. */
18+
#define SOFT_ERROR_MASK (LESS_6A | NO_SUN)
19+
20+
char evse_state_to_iec61851(int state, int error_flags) {
21+
/* Hard errors override the state-based mapping */
22+
if (error_flags & ~SOFT_ERROR_MASK)
23+
return 'E';
24+
25+
switch (state) {
26+
case STATE_A:
27+
return 'A';
28+
29+
case STATE_B:
30+
case STATE_B1:
31+
case STATE_COMM_B:
32+
case STATE_COMM_B_OK:
33+
case STATE_ACTSTART:
34+
case STATE_MODEM_REQUEST:
35+
case STATE_MODEM_WAIT:
36+
case STATE_MODEM_DONE:
37+
return 'B';
38+
39+
case STATE_C:
40+
case STATE_C1:
41+
case STATE_COMM_C:
42+
case STATE_COMM_C_OK:
43+
return 'C';
44+
45+
case STATE_D:
46+
return 'D';
47+
48+
case STATE_MODEM_DENIED:
49+
return 'E';
50+
51+
default:
52+
return 'F';
53+
}
54+
}
55+
56+
bool evse_charging_enabled(int state) {
57+
return (state == STATE_C || state == STATE_C1);
58+
}
59+
60+
const char *http_api_validate_phase_switch(const http_phase_switch_request_t *req,
61+
int enable_c2, int load_bl) {
62+
if (req->phases != 1 && req->phases != 3)
63+
return "Value not allowed!";
64+
if (enable_c2 == NOT_PRESENT)
65+
return "C2 contactor not present";
66+
if (load_bl >= 2)
67+
return "Value not allowed!";
68+
return NULL;
69+
}
70+
1471
bool http_api_parse_color(int r_val, int g_val, int b_val,
1572
uint8_t *r_out, uint8_t *g_out, uint8_t *b_out) {
1673
if (r_val < 0 || r_val > 255 || g_val < 0 || g_val > 255 || b_val < 0 || b_val > 255)

SmartEVSE-3/src/http_api.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@ const char *http_api_validate_mqtt_heartbeat(int value);
8686
// Validate mqtt_change_only (0 or 1).
8787
const char *http_api_validate_mqtt_change_only(int value);
8888

89+
// Phase switch request — parsed from POST /settings "phases" parameter.
90+
typedef struct {
91+
int phases; // Requested phase count: 1 or 3
92+
} http_phase_switch_request_t;
93+
94+
// Validate a phase switch request.
95+
// enable_c2: current EnableC2 setting (NOT_PRESENT=0 means no C2 hardware).
96+
// load_bl: load balancing role (>=2 means slave, cannot switch).
97+
// Returns NULL on success, or a static error message on failure.
98+
const char *http_api_validate_phase_switch(const http_phase_switch_request_t *req,
99+
int enable_c2, int load_bl);
100+
101+
// Map internal EVSE state + error flags to an IEC 61851-1 state letter (A-F).
102+
// Hard errors (CT_NOCOMM, TEMP_HIGH, EV_NOCOMM, RCM_TRIPPED, etc.) override to 'E'.
103+
// Soft errors (LESS_6A, NO_SUN) are temporary and do NOT override the state.
104+
// NOSTATE or unrecognized values return 'F' (not available).
105+
char evse_state_to_iec61851(int state, int error_flags);
106+
107+
// Derive whether charging is actively enabled from internal state.
108+
// Returns true when the EVSE is delivering energy (STATE_C or STATE_C1).
109+
bool evse_charging_enabled(int state);
110+
89111
#ifdef __cplusplus
90112
}
91113
#endif

0 commit comments

Comments
 (0)