diff --git a/doc/hardware.md b/doc/hardware.md index a6a3775..c992141 100644 --- a/doc/hardware.md +++ b/doc/hardware.md @@ -36,7 +36,7 @@ The open-plc-utils report the version MAC-QCA7500-2.8.0.30-01-20190707-CS. Contra: - Even with the modified configuration for pev (according to https://openinverter.org/forum/viewtopic.php?p=55120#p55120), the QCA7500 does not send SLAC parameter requests. This means: Cannot be used as pev. -### TPlink TL-PA4010P +### TPlink TL-PA4010P v5.0 This adaptor was suggested by https://openinverter.org/forum/viewtopic.php?p=37085#p37085 and there, successfully used to establish a communication to the CCS charger. @@ -67,6 +67,12 @@ How to modify: - connect cables to supply the device. Works with 12V, also works with 5V from an USB power bank. - connect cables and circuit (1nF and 150ohms in series) for connecting to the pilot line. +#### TPlink TL-PA4010P v2.3 +![image](https://github.com/user-attachments/assets/c98d100c-f7fc-4e41-a183-1b5eeebd43fa) +![image](https://github.com/user-attachments/assets/6fef8096-4373-42d9-9688-7d284f587940) + +Confirmed to be working as EVSE (needs patching via open-plc-utils) and supplied with 5V. + ## Controller for the PEV Besides the homeplug modem, there are additional parts necessary for a vehicle to perform CCS charging. Two of these are: 1. The inlet voltage measurement. 2. The control of CP state and relays. diff --git a/doc/pyPlc.ini.template b/doc/pyPlc.ini.template index dba1cf1..afa311e 100644 --- a/doc/pyPlc.ini.template +++ b/doc/pyPlc.ini.template @@ -44,6 +44,10 @@ light_bulb_demo = no # Possible values: yes or no soc_simulation = yes +# Precharge simulation +# In EVSE mode simulate precharging rather than using physical value +evse_simulate_precharge = yes + # Device selection for the digital outputs, for CP state and power relays # Possible options: # dieter: Serial controlled device, which controls the digital outputs. E.g. arduino from https://github.com/uhi22/dieter @@ -52,6 +56,7 @@ soc_simulation = yes digital_output_device = dieter #digital_output_device = beaglebone #digital_output_device = celeron55device +#digital_output_device = mqtt # Device to read the physically measured inlet voltage in PevMode @@ -62,6 +67,7 @@ digital_output_device = dieter #analog_input_device = dieter analog_input_device = none #analog_input_device = celeron55device +#analog_input_device = mqtt # Criteria for ending the PreCharge phase in PevMode @@ -89,6 +95,9 @@ u_delta_max_for_end_of_precharge = 10 # auto serial_port = auto serial_baud = 19200 +# MQTT broker and base topic in case MQTT is used +mqtt_broker = localhost +mqtt_topic = pyPlc # The target voltage used in the CurrentDemandRequest. # This is a value for first try-outs. Better would @@ -131,10 +140,13 @@ udp_syslog_enable = Yes # Set backend for obtaining charging parameters, we start with CHAdeMO CAN for now # Need to make a simulator device and maybe a celeron device? # Possible values: +# mqtt: pyPLC is used as a bridge between an MQTT broker and the car or EVSE # chademo: pyPLC is used as bridge between a CCS charger and a CHAdeMO* car. # Limitations/explanations here: https://openinverter.org/forum/viewtopic.php?p=57894#p57894 and # https://openinverter.org/forum/viewtopic.php?t=1063 (Is it possible to make a CCS to CHAdeMO adapter?) # none: all other use cases +#charge_parameter_backend = chademo +#charge_parameter_backend = mqtt charge_parameter_backend = none # REST callback for SoC states. Comment out to disable. Do not leave a trailing slash diff --git a/fsmEvse.py b/fsmEvse.py index 9ca7add..1a9d0af 100644 --- a/fsmEvse.py +++ b/fsmEvse.py @@ -27,8 +27,10 @@ class fsmEvse(): def publishStatus(self, s): self.callbackShowStatus(s, "evseState") + self.hardwareInterface.displayState(s) def publishSoCs(self, current_soc: int, full_soc: int = -1, energy_capacity: int = -1, energy_request: int = -1, evccid: str = "", origin: str = ""): + self.hardwareInterface.displaySoc(current_soc) if self.callbackSoCStatus is not None: self.callbackSoCStatus(current_soc, full_soc, energy_capacity, energy_request, self.evccid, origin) @@ -262,7 +264,12 @@ class fsmEvse(): self.simulatedPresentVoltage += 20 if (self.simulatedPresentVoltage= EVMaximumCurrentLimit: return EVMaximumCurrentLimit return self.accuMaxCurrent - elif getConfigValue("charge_parameter_backend")=="chademo": - return self.accuMaxCurrent #set by CAN + elif getConfigValue("charge_parameter_backend") in ["chademo", "mqtt"]: + return self.accuMaxCurrent #set by CAN or MQTT #todo: get max charging current from the BMS self.accuMaxCurrent = 10 return self.accuMaxCurrent def getAccuMaxVoltage(self): - if getConfigValue("charge_parameter_backend")=="chademo": - return self.accuMaxVoltage #set by CAN + if getConfigValue("charge_parameter_backend") in ["chademo", "mqtt"]: + return self.accuMaxVoltage #set by CAN or MQTT elif getConfigValue("charge_target_voltage"): self.accuMaxVoltage = getConfigValue("charge_target_voltage") else: @@ -209,12 +245,15 @@ class hardwareInterface(): def getSoc(self): if self.callbackShowStatus: self.callbackShowStatus(format(self.soc_percent,".1f"), "soc") - if (getConfigValue("digital_output_device")=="celeron55device"): + if (getConfigValue("digital_output_device") in ["celeron55device", "mqtt"]): return self.soc_percent #todo: get SOC from the BMS self.callbackShowStatus(format(self.simulatedSoc,".1f"), "soc") return self.simulatedSoc + def stopRequest(self): + return not self.enabled + def isUserAuthenticated(self): # If the user needs to authorize, fill this function in a way that it returns False as long as # we shall wait for the users authorization, and returns True if the authentication was successfull. @@ -239,16 +278,24 @@ class hardwareInterface(): # Port configuration according to https://github.com/jsphuebner/pyPLC/commit/475f7fe9f3a67da3d4bd9e6e16dfb668d0ddb1d6 GPIO.setup(PinPowerRelay, GPIO.OUT) #output for port relays GPIO.setup(PinCp, GPIO.OUT) #output for CP + + if (getConfigValue("digital_output_device") == "mqtt"): + self.mqttclient = mqtt.Client(client_id="pyplc") + self.mqttclient.on_connect = self.mqtt_on_connect + self.mqttclient.on_message = self.mqtt_on_message + self.mqttclient.connect(getConfigValue("mqtt_broker"), 1883, 60) - def __init__(self, callbackAddToTrace=None, callbackShowStatus=None, homeplughandler=None): + def __init__(self, callbackAddToTrace=None, callbackShowStatus=None, homeplughandler=None, mode=C_EVSE_MODE): self.callbackAddToTrace = callbackAddToTrace self.callbackShowStatus = callbackShowStatus self.homeplughandler = homeplughandler + self.mode = mode self.loopcounter = 0 self.outvalue = 0 self.simulatedSoc = 20.0 # percent self.demoAuthenticationCounter = 0 + self.enabled = True #Charging enabled self.inletVoltage = 0.0 # volts self.accuVoltage = 0.0 @@ -400,6 +447,9 @@ class hardwareInterface(): if (getConfigValue("digital_output_device")=="celeron55device"): self.mainfunction_celeron55device() + if (getConfigValue("digital_output_device")=="mqtt"): + self.mainfunction_mqtt() + if getConfigValueBool("exit_on_session_end"): # TODO: This is a hack. Do this in fsmPev instead and publish some # of these values into there if needed. @@ -477,7 +527,52 @@ class hardwareInterface(): if self.accuMaxCurrent != 0: self.addToTrace("CHAdeMO: No current limit update for over 1s, setting current to 0") self.accuMaxCurrent = 0 + + def mainfunction_mqtt(self): + self.mqttclient.loop(timeout=0.1) + # The callback for when the client receives a CONNACK response from the server. + def mqtt_on_connect(self, client, userdata, flags, rc): + self.addToTrace(f"MQTT connected with result code {rc}") + + # Subscribing in on_connect() means that if we lose the connection and + # reconnect then subscriptions will be renewed. + if self.mode == C_EVSE_MODE: + client.subscribe(getConfigValue("mqtt_topic") + "/charger_max_voltage") #todo + client.subscribe(getConfigValue("mqtt_topic") + "/charger_max_current") #todo + client.subscribe(getConfigValue("mqtt_topic") + "/charger_voltage") + client.subscribe(getConfigValue("mqtt_topic") + "/charger_current") + client.subscribe(getConfigValue("mqtt_topic") + "/enabled") + elif self.mode == C_EVSE_MODE: + client.subscribe(getConfigValue("mqtt_topic") + "/battery_voltage") + client.subscribe(getConfigValue("mqtt_topic") + "/target_voltage") + client.subscribe(getConfigValue("mqtt_topic") + "/target_current") + client.subscribe(getConfigValue("mqtt_topic") + "/soc") + client.subscribe(getConfigValue("mqtt_topic") + "/inlet_voltage") + + def mqtt_on_message(self, client, userdata, msg): + baseTopic = getConfigValue("mqtt_topic") + + if msg.topic == (f"{baseTopic}/battery_voltage"): + self.accuVoltage = float(msg.payload) + self.addToTrace("MQTT: Set battery voltage to %f V" % self.accuVoltage) + elif msg.topic == (f"{baseTopic}/target_voltage"): + self.accuMaxVoltage = float(msg.payload) + self.addToTrace("MQTT: Set target voltage to %f V" % self.accuMaxVoltage) + elif msg.topic == (f"{baseTopic}/target_current") or msg.topic == (f"{baseTopic}/charger_current"): + self.accuMaxCurrent = float(msg.payload) + self.addToTrace("MQTT: Set current request to %s A" % self.accuMaxCurrent) + elif msg.topic == (f"{baseTopic}/soc"): + self.simulatedSoc = float(msg.payload) + self.soc_percent = self.simulatedSoc + self.addToTrace("MQTT: Set SoC to %f %%" % self.simulatedSoc) + elif msg.topic == (f"{baseTopic}/inlet_voltage") or msg.topic == (f"{baseTopic}/charger_voltage"): + self.inletVoltage = float(msg.payload) #in EVSE mode this is present charger voltage + self.addToTrace("MQTT: Set present voltage to %f V" % self.inletVoltage) + elif msg.topic == (f"{baseTopic}/enabled"): + self.enabled = bool(int(msg.payload)) + self.addToTrace("MQTT: Setting enable flag to %d" % self.enabled) + def myPrintfunction(s): print("myprint " + s) @@ -488,6 +583,8 @@ if __name__ == "__main__": hw.mainfunction() if (i==20): hw.showOnDisplay("Hello", "A DEMO", "321.0V") + hw.setChargerParameters(500, 125) + hw.setChargerVoltageAndCurrent(360, 100) if (i==50): hw.setStateC() if (i==100): @@ -505,6 +602,6 @@ if __name__ == "__main__": hw.setRelay2Off() if (i==320): hw.showOnDisplay("This", "...is...", "DONE :-)") - sleep(0.03) + sleep(0.01) hw.close() print("finished.") diff --git a/simulateMqttBackend.py b/simulateMqttBackend.py new file mode 100644 index 0000000..9639e89 --- /dev/null +++ b/simulateMqttBackend.py @@ -0,0 +1,26 @@ +import paho.mqtt.client as mqtt +from configmodule import getConfigValue, getConfigValueBool + +def mqtt_on_connect(client, userdata, flags, rc): + client.subscribe(getConfigValue("mqtt_topic") + "/#") + +def mqtt_on_message(client, userdata, msg): + global simulatedInletVoltage + if msg.topic == getConfigValue("mqtt_topic") + "/fsm_state": + if "CableCheck" in msg.payload.decode("utf-8"): + simulatedInletVoltage = 0 + if "PreCharging" in msg.payload.decode("utf-8"): + client.publish(getConfigValue("mqtt_topic") + "/inlet_voltage", simulatedInletVoltage) + simulatedInletVoltage = simulatedInletVoltage + 15 + elif msg.topic == getConfigValue("mqtt_topic") + "/pev_voltage": + client.publish(getConfigValue("mqtt_topic") + "/inlet_voltage", msg.payload) + elif msg.topic == getConfigValue("mqtt_topic") + "/pev_current": + client.publish(getConfigValue("mqtt_topic") + "/target_current", msg.payload) + +simulatedInletVoltage = 0 +mqttclient = mqtt.Client() +mqttclient.on_connect = mqtt_on_connect +mqttclient.on_message = mqtt_on_message +mqttclient.connect(getConfigValue("mqtt_broker"), 1883, 60) + +mqttclient.loop_forever() diff --git a/starter.sh b/starter.sh index cdb6a85..baad3e8 100755 --- a/starter.sh +++ b/starter.sh @@ -39,9 +39,11 @@ sysctl net.ipv6.conf.eth0.keep_addr_on_down=1 # Todo: Why this needed? On raspberry, where the NetworkManager is not runnning, this disturbs, because # afterwards the pyPlc does not see the interfaces IPv6 address. # Todo: make this configurable, for the cases we need this. -# ip link set eth0 down +ip link set eth0 down +ip link set can0 down sleep 1 -# ip link set eth0 up +ip link set eth0 up +ip link set can0 up type can restart-ms 100 bitrate 500000 sleep 1 # show the addresses @@ -53,17 +55,25 @@ pwd # create directory for the log files. mkdir -p log # prepare the file names for the log files +pushd log +gzip *.log || true +popd date=$(date "+%Y-%m-%d_%H%M%S") -logfile=./log/"$date"_pevNoGui.log -tcpdump_logfile=./log/"$date"_tcpdump.pcap +#logfile=./log/"$date"_pevNoGui.log +#logfile=./log/pevNoGui.log +index=`cat log/logindex` +logfile=`printf "log/%04d_pevNoGui.log" $index` +tcpdump_logfile=`printf "log/%04d_tcpdump.pcap" $index` +index=$(($index + 1)) +echo $index > log/logindex echo "logfile: $logfile" echo "tcpdump_logfile: $tcpdump_logfile" # start the tcpdump -start_tcpdump "$tcpdump_logfile" +#start_tcpdump "$tcpdump_logfile" -echo "$date" >> "$logfile" +echo "$date" > "$logfile" git log --oneline -1 >> "$logfile" || echo "Not a git repo" >> "$logfile" ip addr >> "$logfile" pwd >> "$logfile"