├── .gitignore ├── LICENSE ├── README.md ├── config.example.py ├── daemon.py ├── requirements.txt ├── run-tests.sh ├── run.sh └── smarttwc ├── __init__.py ├── fakemaster.py ├── master.py ├── serial ├── __init__.py └── messages.py └── tools └── __init__.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | 103 | config.py 104 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Wido den Hollander 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # smarttwc 2 | Smart (Tesla) charging with a (EU) Tesla Wall Connector (TWC) using Python 3 code 3 | 4 | # How does it work? 5 | SmartTWC is able to run in *Master* mode for a Tesla Wall Connector and by sending it messages over RS-485 it can 6 | adjust the maximum allowed current the TWC will advertise to the car. 7 | 8 | # TWCManager 9 | A lot of credits go to [TWCManager](https://github.com/cdragon/TWCManager) for figuring out the protocol of the Tesla HPWC. 10 | 11 | # Requirements 12 | This code has been written for Debian Linux running on a Raspberry Pi. It could and should work on all Linux platforms, but 13 | that has not been tested. 14 | 15 | The Python code is written for Python >=3.5 and requires a few additional libraries. These can be found in *requirements.txt* 16 | -------------------------------------------------------------------------------- /config.example.py: -------------------------------------------------------------------------------- 1 | SERIAL_DEVICE = '/dev/ttyUSB0' 2 | SERIAL_BAUD_RATE = 9600 3 | MAX_CURRENT = 10 4 | -------------------------------------------------------------------------------- /daemon.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import config 3 | import logging 4 | from threading import Event 5 | from smarttwc import Master 6 | 7 | logger = logging.basicConfig(level=logging.INFO) 8 | 9 | master = Master(config.SERIAL_DEVICE, 10 | config.SERIAL_BAUD_RATE, 11 | config.MAX_CURRENT) 12 | master.daemon = True 13 | master.start() 14 | 15 | event = Event() 16 | while master.is_alive(): 17 | try: 18 | event.wait(0.1) 19 | except: 20 | master.join() 21 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pyserial 2 | -------------------------------------------------------------------------------- /run-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | cd `dirname $0` 5 | 6 | ./daemon.py 7 | 8 | -------------------------------------------------------------------------------- /smarttwc/__init__.py: -------------------------------------------------------------------------------- 1 | from .master import Master 2 | from .fakemaster import FakeMaster 3 | 4 | -------------------------------------------------------------------------------- /smarttwc/fakemaster.py: -------------------------------------------------------------------------------- 1 | from smarttwc.serial.messages import Messages 2 | 3 | 4 | class FakeMaster: 5 | def __init__(self, device, max_current=10): 6 | self.device = device 7 | self.max_current = max_current 8 | 9 | def send_master_linkready1(self): 10 | self.device.write(Messages.LINK_READY_1.value.encode()) 11 | 12 | def send_master_linkready2(self): 13 | self.device.write(Messages.LINK_READY_2.value.encode()) 14 | -------------------------------------------------------------------------------- /smarttwc/master.py: -------------------------------------------------------------------------------- 1 | import threading 2 | import serial 3 | import logging 4 | import time 5 | from .fakemaster import FakeMaster 6 | 7 | 8 | class Master(threading.Thread): 9 | alive = True 10 | interval = 0.1 11 | 12 | num_init_msg = 10 13 | 14 | def __init__(self, serial_device, serial_baud_rate, max_current): 15 | super(Master, self).__init__() 16 | self.event = threading.Event() 17 | self.alive = True 18 | self.serial_device = serial_device 19 | self.serial_baud_rate = serial_baud_rate 20 | self.max_current = max_current 21 | self.dev = serial.Serial(serial_device, serial_baud_rate) 22 | 23 | def run(self): 24 | fakemaster = FakeMaster(self.dev, self.max_current) 25 | 26 | logging.info('Master thread starting up') 27 | while self.alive: 28 | 29 | if self.num_init_msg > 5: 30 | logging.info('Sending linkready1') 31 | fakemaster.send_master_linkready1() 32 | time.sleep(0.1) 33 | self.num_init_msg -= 1 34 | elif self.num_init_msg > 0: 35 | logging.info('Sending linkready2') 36 | fakemaster.send_master_linkready2() 37 | time.sleep(0.1) 38 | self.num_init_msg -= 1 39 | 40 | self.event.wait(self.interval) 41 | 42 | def join(self, timeout=None): 43 | self.alive = False 44 | self.dev.close() 45 | super(Master, self).join(timeout) 46 | -------------------------------------------------------------------------------- /smarttwc/serial/__init__.py: -------------------------------------------------------------------------------- 1 | from .messages import Messages 2 | -------------------------------------------------------------------------------- /smarttwc/serial/messages.py: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | 3 | 4 | class Messages(Enum): 5 | FAKE_TW_CID = '\x77\x77' 6 | MASTER_SIGN = '\x77' 7 | SLAVE_SIGN = '\x77' 8 | LINK_READY_1 = '\xFC\xE1{0}{1}\x00\x00\x00\x00\x00\x00\x00\x00'.format(FAKE_TW_CID, MASTER_SIGN) 9 | LINK_READY_2 = '\xFB\xE2{0}{1}\x00\x00\x00\x00\x00\x00\x00\x00'.format(FAKE_TW_CID, MASTER_SIGN) 10 | SLAVE_LINK_READY = '\xFD\xE2{0}{1}\x1F\x40\x00\x00\x00\x00\x00\x00'.format(FAKE_TW_CID, SLAVE_SIGN) 11 | -------------------------------------------------------------------------------- /smarttwc/tools/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------