Package: ocpp
OCPPPython 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 onrtd.InstallationYou can either the project install from Pypi:$pipinstallocppOr clone the project and install it manually using:$pipinstall.Quick startBelow you can find examples on how to create a simple OCPP 2.0 central system as
well as an OCPP 2.0 charge point.NoteTo run these examples the dependencywebsocketsis required! Install it by running:$pipinstallwebsocketsCentral systemThe 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 theCentral System documentation.importasyncioimportloggingimportwebsocketsfromdatetimeimportdatetimefromocpp.routingimportonfromocpp.v201importChargePointascpfromocpp.v201importcall_resultfromocpp.v201.enumsimportRegistrationStatusTypelogging.basicConfig(level=logging.INFO)classChargePoint(cp):@on('BootNotification')asyncdefon_boot_notification(self,charging_station,reason,**kwargs):returncall_result.BootNotificationPayload(current_time=datetime.utcnow().isoformat(),interval=10,status=RegistrationStatusType.accepted)asyncdefon_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']exceptKeyError:logging.info("Client hasn't requested any Subprotocol. ""Closing Connection")returnawaitwebsocket.close()ifwebsocket.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)returnawaitwebsocket.close()charge_point_id=path.strip('/')cp=ChargePoint(charge_point_id,websocket)awaitcp.start()asyncdefmain():server=awaitwebsockets.serve(on_connect,'0.0.0.0',9000,subprotocols=['ocpp2.0.1'])logging.info("WebSocket Server Started")awaitserver.wait_closed()if__name__=='__main__':asyncio.run(main())Charge pointimportasynciofromocpp.v201.enumsimportRegistrationStatusTypeimportloggingimportwebsocketsfromocpp.v201importcallfromocpp.v201importChargePointascplogging.basicConfig(level=logging.INFO)classChargePoint(cp):asyncdefsend_boot_notification(self):request=call.BootNotificationPayload(charging_station={'model':'Wallbox XYZ','vendor_name':'anewone'},reason="PowerUp")response=awaitself.call(request)ifresponse.status==RegistrationStatusType.accepted:print("Connected to central system.")asyncdefmain():asyncwithwebsockets.connect('ws://localhost:9000/CP_1',subprotocols=['ocpp2.0.1'])asws:cp=ChargePoint('CP_1',ws)awaitasyncio.gather(cp.start(),cp.send_boot_notification())if__name__=='__main__':asyncio.run(main())DebuggingPython’s default log level islogging.WARNING. As result most of the logs
generated by this package are discarded. To see the log output of this package
lower the log level tologging.DEBUG.importlogginglogging.basicConfig(level=logging.DEBUG)However, this approach defines the log level for the complete logging system.
In other words: the log level of all dependencies is set tologging.DEBUG.To lower the logs for this package only use the following code:importlogginglogging.getLogger('ocpp').setLevel(level=logging.DEBUG)logging.getLogger('ocpp').addHandler(logging.StreamHandler())LicenseExcept from the documents indocs/v16anddocs/v201everything is licensed underMIT.
©The Mobility HouseThe documents indocs/v16anddocs/v201are licensed under Creative Commons
Attribution-NoDerivatives 4.0 International Public License.