From 2565435d0990b24c3665c520eebbeeaf916f9076 Mon Sep 17 00:00:00 2001 From: uhi22 Date: Sat, 13 May 2023 19:35:54 +0200 Subject: [PATCH] takeover script from celeron55, updated raspberry docu, path adaption --- c55_monitor_arduino.sh | 2 + c55_pev_foreground.sh | 2 + c55_service_pev.sh | 2 + c55_test_hardware.sh | 2 + doc/installation_on_raspberry.md | 55 +++++++++++++++++++++-- mytestsuite.py | 2 +- pevNoGui.py | 2 +- starter.sh | 76 +++++++++++++++++++++++++++++--- 8 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 c55_monitor_arduino.sh create mode 100644 c55_pev_foreground.sh create mode 100644 c55_service_pev.sh create mode 100644 c55_test_hardware.sh diff --git a/c55_monitor_arduino.sh b/c55_monitor_arduino.sh new file mode 100644 index 0000000..f17f8ed --- /dev/null +++ b/c55_monitor_arduino.sh @@ -0,0 +1,2 @@ +#!/bin/sh +picocom --baud 115200 -r -l -c -e x /dev/ttyS1 diff --git a/c55_pev_foreground.sh b/c55_pev_foreground.sh new file mode 100644 index 0000000..5500ffb --- /dev/null +++ b/c55_pev_foreground.sh @@ -0,0 +1,2 @@ +#!/bin/sh +sudo ./starter.sh diff --git a/c55_service_pev.sh b/c55_service_pev.sh new file mode 100644 index 0000000..8890e9f --- /dev/null +++ b/c55_service_pev.sh @@ -0,0 +1,2 @@ +#!/bin/sh +sudo systemctl start pev diff --git a/c55_test_hardware.sh b/c55_test_hardware.sh new file mode 100644 index 0000000..cbebcca --- /dev/null +++ b/c55_test_hardware.sh @@ -0,0 +1,2 @@ +#!/bin/sh +python hardwareInterface.py diff --git a/doc/installation_on_raspberry.md b/doc/installation_on_raspberry.md index 6e7af9b..3325b96 100644 --- a/doc/installation_on_raspberry.md +++ b/doc/installation_on_raspberry.md @@ -21,6 +21,11 @@ Install wireshark ``` Choose "yes" for the question whether other users shall be able to trace network traffic. +Install tcpdump, to be able to log the network traffic in background: +``` + sudo apt-get install tcpdump +``` + Clone the two github repositories, and compile the OpenV2Gx EXI decoder/encoder: ``` @@ -78,8 +83,8 @@ Try-out the cooperation of Python with the EXI encoder/decoder: ``` This should run some decoder/encoder tests and report in the end "Number of fails: 0". - -Start the EVSE `sudo python pyPlc.py E`. +As first test, use the simulation mode (no need for modems or other hardware). +Start the EVSE in simulation mode `sudo python pyPlc.py E S`. Open a second console window, and start here the pev in simulation mode `sudo python pyPlc.py P S`. We should see how the EVSE and PEV are talking to each other. @@ -106,7 +111,7 @@ In this file, we write the following, to configure the new service: [Service] Type=simple - ExecStart=/home/pi/myprogs/myPlc/starter.sh + ExecStart=/home/pi/myprogs/pyPlc/starter.sh Restart=on-abort [Install] @@ -146,6 +151,25 @@ The first value defines the overall disk space which is used by the service logs The next time we power-up the pi, even without a HDMI display and keyboard connected, the OLED should show the charge progress now. Using an RaspberryPi 3 without additional startup time optimization, the time from power-on until the start of SLAC is ~21 seconds. + + +### Further optimizations +To automatically create pcap network traces and log files, celeron55 developed some shell scripts, see https://github.com/celeron55/pyPLC +Concept: +* The starter.sh is started either by the user in the foreground by running c55_pev_foreground.sh, or the starter.sh is started by the +systemd, when the service is started by the user by running c55_service_pev.sh. +* The starter.sh creates a directory for the log files: pyPLC/logs +* The starter.sh defines log file names, which start with the date and time. One log file for the pyPlc itself, and one for the tcpdump.pcap. +* The starter.sh starts the tcpdump in the background, and afterwards the pyPlc. +* The stdout of the pyPlc is distributed by tee to the log file and to the console/journal. +* The pyPlc is configured to terminate when the plug is pulled. +* When the pyPlc terminates, the starter.sh stops the tcpdump. + + +Using environment variables for services: +https://www.baeldung.com/linux/systemd-services-environment-variables + + ## Disable Network Manager for the ethernet port With standard settings, the Raspberry tries to find an internet connection on the ethernet port, this means it tries to do things like DHCP and RouterSolicitation. This procedure may disturb the communication between the PEV and EVSE. That's why it is recommended to forbid the Network Manager to care for the eth0. @@ -154,6 +178,31 @@ Discussion: https://openinverter.org/forum/viewtopic.php?p=56342#p56342 Solution ideas: https://stackoverflow.com/questions/5321380/disable-network-manager-for-a-particular-interface or https://access.redhat.com/documentation/de-de/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/configuring-networkmanager-to-ignore-certain-devices_configuring-and-managing-networking Todo: which is the correct way on the raspberry? +Further discussion: +https://openinverter.org/forum/viewtopic.php?p=56434#p56434 + +1. Setup a new connection in NetworkManager nmcli con add connection.interface-name eth0 type ethernet connection.id CCS +2. This created a new /etc/NetworkManager/system-connections/ something +3. Edit this file, to have +``` + [ipv4] + method=disabled + [ipv6] + addr-gen-mode=stable-privacy + method=link-local +``` +4. Restart the NetworkManager +5. nmcli con up CCS +6. Check which connection the NetworkManager sees: +``` + nmcli device status + DEVICE TYPE STATE CONNECTION + wlan0 wifi connected My wifi + eth0 ethernet connected CCS +``` + +If (like in uhi's case) the Raspberry says for nmcli something like "The network manager is not executed.", then this should be also fine, +because it should not disturb the communication in this case. ## Nice-to-have: Password-less SSH connection from the Windows notebook to the Pi diff --git a/mytestsuite.py b/mytestsuite.py index 796db50..38aa8e1 100644 --- a/mytestsuite.py +++ b/mytestsuite.py @@ -16,7 +16,7 @@ TC_EVSE_Shutdown_during_CurrentDemand = 2000 TC_EVSE_Malfunction_during_CurrentDemand = 2001 # Here we configure, which test case should fire, and after which number of calls: -testsuite_testcase_number = TC_EVSE_Malfunction_during_CurrentDemand +testsuite_testcase_number = TC_EVSE_Shutdown_during_PreCharge testsuite_delayCycles = 5 diff --git a/pevNoGui.py b/pevNoGui.py index 5009ff6..4f861bf 100755 --- a/pevNoGui.py +++ b/pevNoGui.py @@ -1,4 +1,4 @@ - +#!/usr/bin/python3 # The non-GUI variant of the PEV side import time diff --git a/starter.sh b/starter.sh index 3590f7e..f543319 100755 --- a/starter.sh +++ b/starter.sh @@ -1,8 +1,72 @@ #!/bin/bash -date >> /home/pi/myprogs/pyPlc/test2.txt -pwd >> /home/pi/myprogs/pyPlc/test2.txt -cd /home/pi/myprogs/pyPlc/ -/usr/bin/python /home/pi/myprogs/pyPlc/pevNoGui.py -pwd >> /home/pi/myprogs/pyPlc/test2.txt -date >> /home/pi/myprogs/pyPlc/test3.txt +# Function to start tcpdump +start_tcpdump() { + INTERFACE="eth0" # Change this to the interface you want to monitor + OUTPUT_FILE="$1" + tcpdump -i "$INTERFACE" -w "$OUTPUT_FILE" &> /dev/null & + TCPDUMP_PID=$! + echo "Started tcpdump with PID $TCPDUMP_PID on interface $INTERFACE, output file: $OUTPUT_FILE" +} +# Function to stop tcpdump +stop_tcpdump() { + if [ ! -z "$TCPDUMP_PID" ]; then + echo "Stopping tcpdump with PID $TCPDUMP_PID" + kill -SIGINT "$TCPDUMP_PID" + fi +} +# Function to handle signals +signal_handler() { + echo "Caught signal, stopping tcpdump and exiting..." + stop_tcpdump + exit 1 +} +# Set trap for signals: SIGINT (2), SIGTERM (15) +trap 'signal_handler' 2 15 + +# Todo: explain this +set -euv + +# Keep all IPv6 addresses on the interface down event. +# Todo: flexible interface name. +sysctl net.ipv6.conf.eth0.keep_addr_on_down=1 + +# Shut down and activate the interface. +# Todo: Why this needed? +ip link set eth0 down +sleep 1 +ip link set eth0 up +sleep 1 + +# show the addresses +ip addr + +# todo: flexible path name +mkdir -p /home/pi/myprogs/pyPLC/log +# prepare the file names for the log files +date=$(date "+%Y-%m-%d_%H%M%S") +logfile=/home/pi/myprogs/pyPLC/log/"$date"_pevNoGui.log +tcpdump_logfile=/home/pi/myprogs/pyPLC/log/"$date"_tcpdump.pcap + +echo "logfile: $logfile" +echo "tcpdump_logfile: $tcpdump_logfile" + +# start the tcpdump +start_tcpdump "$tcpdump_logfile" + +echo "$date" >> "$logfile" +git log --oneline -1 >> "$logfile" || echo "Not a git repo" >> "$logfile" +ip addr >> "$logfile" +pwd >> "$logfile" +# Todo: flexible path name +cd /home/pi/myprogs/pyPLC/ +#/usr/bin/python3 /home/user/projects/pyPLC/pevNoGui.py | tee -a "$logfile" +#stdbuf -oL -eL /usr/bin/python3 /home/user/projects/pyPLC/pevNoGui.py | tee -a "$logfile" +#PYTHONUNBUFFERED=1 /usr/bin/python3 /home/user/projects/pyPLC/pevNoGui.py | stdbuf -oL -eL tee -a "$logfile" +PYTHONUNBUFFERED=1 /usr/bin/python3 /home/pi/myprogs/pyPLC/pevNoGui.py | tee -a "$logfile" +pwd >> "$logfile" +date >> "$logfile" + +# Stop the tcpdump when the pyPLC stopped: +stop_tcpdump +