Skip to content

Commit e736960

Browse files
authored
Merge pull request #115 from basmeerman/work/plan-15-mqtt-soc
feat: MQTT SoC injection topics (#112)
2 parents 3704586 + 4009352 commit e736960

File tree

6 files changed

+605
-1
lines changed

6 files changed

+605
-1
lines changed

SmartEVSE-3/src/esp32.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,31 @@ void mqtt_receive_callback(const String topic, const String payload) {
832832
break;
833833
// END PLAN-09
834834

835+
// BEGIN PLAN-15: SoC MQTT commands
836+
#if MODEM
837+
case MQTT_CMD_INITIAL_SOC:
838+
InitialSoC = cmd.initial_soc;
839+
RecomputeSoC();
840+
break;
841+
case MQTT_CMD_FULL_SOC:
842+
FullSoC = cmd.full_soc;
843+
RecomputeSoC();
844+
break;
845+
case MQTT_CMD_ENERGY_CAPACITY:
846+
EnergyCapacity = cmd.energy_capacity;
847+
RecomputeSoC();
848+
break;
849+
case MQTT_CMD_ENERGY_REQUEST:
850+
EnergyRequest = cmd.energy_request;
851+
RecomputeSoC();
852+
break;
853+
case MQTT_CMD_EVCCID_SET:
854+
strncpy(EVCCID, cmd.evccid, sizeof(EVCCID) - 1);
855+
EVCCID[sizeof(EVCCID) - 1] = '\0';
856+
break;
857+
#endif
858+
// END PLAN-15
859+
835860
default:
836861
return;
837862
}

SmartEVSE-3/src/mqtt_parser.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,62 @@ bool mqtt_parse_command(const char *prefix, const char *topic,
304304
return false;
305305
}
306306

307+
/* EV State of Charge and energy parameters */
308+
if (match_topic(prefix, topic, "/Set/InitialSoC")) {
309+
if (payload[0] == '\0') return false;
310+
char *endptr;
311+
long val = strtol(payload, &endptr, 10);
312+
if (*endptr != '\0') return false;
313+
if (val < -1 || val > 100) return false;
314+
out->cmd = MQTT_CMD_INITIAL_SOC;
315+
out->initial_soc = (int8_t)val;
316+
return true;
317+
}
318+
319+
if (match_topic(prefix, topic, "/Set/FullSoC")) {
320+
if (payload[0] == '\0') return false;
321+
char *endptr;
322+
long val = strtol(payload, &endptr, 10);
323+
if (*endptr != '\0') return false;
324+
if (val < -1 || val > 100) return false;
325+
out->cmd = MQTT_CMD_FULL_SOC;
326+
out->full_soc = (int8_t)val;
327+
return true;
328+
}
329+
330+
if (match_topic(prefix, topic, "/Set/EnergyCapacity")) {
331+
if (payload[0] == '\0') return false;
332+
char *endptr;
333+
long val = strtol(payload, &endptr, 10);
334+
if (*endptr != '\0') return false;
335+
if (val < -1 || val > 200000) return false;
336+
out->cmd = MQTT_CMD_ENERGY_CAPACITY;
337+
out->energy_capacity = (int32_t)val;
338+
return true;
339+
}
340+
341+
if (match_topic(prefix, topic, "/Set/EnergyRequest")) {
342+
if (payload[0] == '\0') return false;
343+
char *endptr;
344+
long val = strtol(payload, &endptr, 10);
345+
if (*endptr != '\0') return false;
346+
if (val < -1 || val > 200000) return false;
347+
out->cmd = MQTT_CMD_ENERGY_REQUEST;
348+
out->energy_request = (int32_t)val;
349+
return true;
350+
}
351+
352+
/* EV CC ID for session identification */
353+
if (match_topic(prefix, topic, "/Set/EVCCID")) {
354+
out->cmd = MQTT_CMD_EVCCID_SET;
355+
size_t len = strlen(payload);
356+
if (len >= sizeof(out->evccid))
357+
return false;
358+
strncpy(out->evccid, payload, sizeof(out->evccid) - 1);
359+
out->evccid[sizeof(out->evccid) - 1] = '\0';
360+
return true;
361+
}
362+
307363
if (match_topic(prefix, topic, "/Set/DiagProfile")) {
308364
out->cmd = MQTT_CMD_DIAG_PROFILE;
309365
if (strcmp(payload, "off") == 0 || strcmp(payload, "0") == 0) {

SmartEVSE-3/src/mqtt_parser.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ typedef enum {
3232
MQTT_CMD_DIAG_PROFILE,
3333
MQTT_CMD_MAINS_METER_TIMEOUT,
3434
MQTT_CMD_HOMEWIZARD_IP,
35+
MQTT_CMD_INITIAL_SOC,
36+
MQTT_CMD_FULL_SOC,
37+
MQTT_CMD_ENERGY_CAPACITY,
38+
MQTT_CMD_ENERGY_REQUEST,
39+
MQTT_CMD_EVCCID_SET,
3540
} mqtt_cmd_type_t;
3641

3742
// Mode values matching firmware MODE_NORMAL/MODE_SOLAR/MODE_SMART
@@ -59,7 +64,7 @@ typedef struct {
5964
struct { int32_t L1, L2, L3; } mains_meter;
6065
struct { int32_t L1, L2, L3; int32_t W; int32_t Wh; } ev_meter;
6166
int16_t home_battery_current; // MQTT_CMD_HOME_BATTERY_CURRENT
62-
char evccid[32]; // MQTT_CMD_REQUIRED_EVCCID
67+
char evccid[32]; // MQTT_CMD_REQUIRED_EVCCID and MQTT_CMD_EVCCID_SET
6368
struct { uint8_t index; uint8_t r, g, b; } color; // MQTT_CMD_COLOR
6469
uint8_t cable_lock; // MQTT_CMD_CABLE_LOCK
6570
uint8_t enable_c2; // MQTT_CMD_ENABLE_C2
@@ -72,6 +77,10 @@ typedef struct {
7277
uint8_t diag_profile; // MQTT_CMD_DIAG_PROFILE (0-5)
7378
uint16_t mains_meter_timeout; // MQTT_CMD_MAINS_METER_TIMEOUT (0, 10-3600)
7479
char homewizard_ip[16]; // MQTT_CMD_HOMEWIZARD_IP (IPv4 or empty)
80+
int8_t initial_soc; // MQTT_CMD_INITIAL_SOC (-1 or 0-100)
81+
int8_t full_soc; // MQTT_CMD_FULL_SOC (-1 or 0-100)
82+
int32_t energy_capacity; // MQTT_CMD_ENERGY_CAPACITY (-1 or 0-200000 Wh)
83+
int32_t energy_request; // MQTT_CMD_ENERGY_REQUEST (-1 or 0-200000 Wh)
7584
};
7685
} mqtt_command_t;
7786

SmartEVSE-3/test/native/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SOLAR_DEBUG_JSON_SRC := $(SRC_DIR)/solar_debug_json.c
3535
SESSION_LOG_SRC := $(SRC_DIR)/session_log.c
3636
DIAG_TELEMETRY_SRC := $(SRC_DIR)/diag_telemetry.c
3737
DIAG_MODBUS_SRC := $(SRC_DIR)/diag_modbus.c
38+
CAPACITY_PEAK_SRC := $(SRC_DIR)/capacity_peak.c
3839

3940
# Discover all test files
4041
TEST_SRCS := $(wildcard $(TEST_DIR)/test_*.c)
@@ -49,6 +50,9 @@ all: $(TEST_BINS)
4950
$(BUILD)/test_mqtt_parser: $(TEST_DIR)/test_mqtt_parser.c $(MQTT_PARSER_SRC) $(SRC_DIR)/mqtt_parser.h include/*.h | $(BUILD)
5051
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_mqtt_parser.c $(MQTT_PARSER_SRC)
5152

53+
$(BUILD)/test_mqtt_soc: $(TEST_DIR)/test_mqtt_soc.c $(MQTT_PARSER_SRC) $(SRC_DIR)/mqtt_parser.h include/*.h | $(BUILD)
54+
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_mqtt_soc.c $(MQTT_PARSER_SRC)
55+
5256
$(BUILD)/test_mqtt_publish: $(TEST_DIR)/test_mqtt_publish.c $(MQTT_PUBLISH_SRC) $(SRC_DIR)/mqtt_publish.h include/*.h | $(BUILD)
5357
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_mqtt_publish.c $(MQTT_PUBLISH_SRC)
5458

@@ -112,6 +116,9 @@ $(BUILD)/test_diag_modbus: $(TEST_DIR)/test_diag_modbus.c $(DIAG_MODBUS_SRC) $(S
112116
$(BUILD)/test_diag_replay: $(TEST_DIR)/test_diag_replay.c diag_loader.c include/diag_loader.h $(DIAG_TELEMETRY_SRC) $(SRC_DIR)/diag_telemetry.h include/*.h | $(BUILD)
113117
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_diag_replay.c diag_loader.c $(DIAG_TELEMETRY_SRC)
114118

119+
$(BUILD)/test_capacity_peak: $(TEST_DIR)/test_capacity_peak.c $(CAPACITY_PEAK_SRC) $(SRC_DIR)/capacity_peak.h include/*.h | $(BUILD)
120+
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_capacity_peak.c $(CAPACITY_PEAK_SRC)
121+
115122
$(BUILD)/test_api_staleness: $(TEST_DIR)/test_api_staleness.c $(EVSE_SRC) $(MQTT_PARSER_SRC) $(SRC_DIR)/evse_ctx.h $(SRC_DIR)/mqtt_parser.h include/*.h | $(BUILD)
116123
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_api_staleness.c $(EVSE_SRC) $(MQTT_PARSER_SRC)
117124

0 commit comments

Comments
 (0)