Skip to main content

Python package implementing the JSON version of the Open Charge Point Protocol (OCPP).

Project description

https://circleci.com/gh/mobilityhouse/ocpp/tree/master.svg?style=svg https://img.shields.io/pypi/pyversions/ocpp.svg https://img.shields.io/readthedocs/ocpp.svg

OCPP

Python package implementing the JSON version of the Open Charge Point Protocol (OCPP). Currently OCPP 1.6 (errata v4), OCPP 2.0 and OCPP 2.0.1 (Final Version) are supported.

You can find the documentation on rtd.

Installation

You can either the project install from Pypi:

$ pip install ocpp

Or clone the project and install it manually using:

$ pip install .

Quick start

Below you can find examples on how to create a simple OCPP 2.0 central system as well as an OCPP 2.0 charge point.

Central system

The code snippet below creates a simple OCPP 2.0 central system which is able to handle BootNotification calls. You can find a detailed explanation of the code in the Central System documentation_.

import asyncio
import logging
import websockets
from datetime import datetime

from ocpp.routing import on
from ocpp.v201 import ChargePoint as cp
from ocpp.v201 import call_result

logging.basicConfig(level=logging.INFO)


class ChargePoint(cp):
    @on('BootNotification')
    def on_boot_notitication(self, charging_station, reason, **kwargs):
        return call_result.BootNotificationPayload(
            current_time=datetime.utcnow().isoformat(),
            interval=10,
            status='Accepted'
        )


async def on_connect(websocket, path):
    """ For every new charge point that connects, create a ChargePoint
    instance and start listening for messages.
    """
    try:
        requested_protocols = websocket.request_headers[
            'Sec-WebSocket-Protocol']
    except KeyError:
        logging.info("Client hasn't requested any Subprotocol. "
                 "Closing Connection")
    if websocket.subprotocol:
        logging.info("Protocols Matched: %s", websocket.subprotocol)
    else:
        # In the websockets lib if no subprotocols are supported by the
        # client and the server, it proceeds without a subprotocol,
        # so we have to manually close the connection.
        logging.warning('Protocols Mismatched | Expected Subprotocols: %s,'
                        ' but client supports  %s | Closing connection',
                        websocket.available_subprotocols,
                        requested_protocols)
        return await websocket.close()

    charge_point_id = path.strip('/')
    cp = ChargePoint(charge_point_id, websocket)

    await cp.start()


async def main():
    server = await websockets.serve(
        on_connect,
        '0.0.0.0',
        9000,
        subprotocols=['ocpp2.0.1']
    )
    logging.info("WebSocket Server Started")
    await server.wait_closed()

if __name__ == '__main__':
    asyncio.run(main())

Charge point

import asyncio
import logging
import websockets

from ocpp.v201 import call
from ocpp.v201 import ChargePoint as cp

logging.basicConfig(level=logging.INFO)


class ChargePoint(cp):

   async def send_boot_notification(self):
       request = call.BootNotificationPayload(
               charging_station={
                   'model': 'Wallbox XYZ',
                   'vendor_name': 'anewone'
               },
               reason="PowerUp"
       )
       response = await self.call(request)

       if response.status == 'Accepted':
           print("Connected to central system.")


async def main():
   async with websockets.connect(
       'ws://localhost:9000/CP_1',
        subprotocols=['ocpp2.0.1']
   ) as ws:

       cp = ChargePoint('CP_1', ws)

       await asyncio.gather(cp.start(), cp.send_boot_notification())


if __name__ == '__main__':
   asyncio.run(main())

License

Except from the documents in docs/v16 and docs/v201 everything is licensed under MIT. © The Mobility House

The documents in docs/v16 and docs/v201 are licensed under Creative Commons Attribution-NoDerivatives 4.0 International Public License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ocpp-0.8.1.tar.gz (103.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ocpp-0.8.1-py3-none-any.whl (273.7 kB view details)

Uploaded Python 3

File details

Details for the file ocpp-0.8.1.tar.gz.

File metadata

  • Download URL: ocpp-0.8.1.tar.gz
  • Upload date:
  • Size: 103.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.0

File hashes

Hashes for ocpp-0.8.1.tar.gz
Algorithm Hash digest
SHA256 7548a92b1e147754be878ba44a41f4004e9f39ad570a8f3edd324394548fbb53
MD5 785d690b5e553b414f39e29275183647
BLAKE2b-256 ec26c3c5067748b40621f2968b8208754e388a16ce629d01e953258ee85b8040

See more details on using hashes here.

File details

Details for the file ocpp-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: ocpp-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 273.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.0

File hashes

Hashes for ocpp-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07f75264886c001e6d23bfc6bdebd66accc243dd7bff4f594cfb732f5d59e43f
MD5 56c96a13103dc17084ac79f5abc2d843
BLAKE2b-256 fbf6ff32f779e4a4a305ad33c0d069cd71928df7f2291c90b70db30cbda14ac1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page