matth-x / MicroOcppSimulator

GNU General Public License v3.0
97 stars 42 forks source link

Connection Error When Using MicroOcppSimulator with SteVe via WebSocket #17

Closed BradleyNeild closed 6 months ago

BradleyNeild commented 6 months ago

I am able to connect to SteVe locally using a simple websocket script, but when attempting to connect using MicroOcppSimulator with the same WebSocket URL, I receive a connection error. No logs appear in the SteVe console, so the connection attempt is not reaching SteVe.

373e3db 1 mongoose.c:403:mg_error 175 0x6 socket error 373e3db 1 MicroOcppMongooseClient.cpp:4 0x6 socket error [MO] info (MicroOcppMongooseClient.cpp:492): connection ws://localhost:8180/steve/websocket/CentralSystemService/wallbox -- error [MO] info (MicroOcppMongooseClient.cpp:492): connection ws://localhost:8180/steve/websocket/CentralSystemService/wallbox -- closed

I'm using Docker for both, SteVe 3.6.0, and the latest release of MicroOcppSimulator. image

Working code:

import logging
from datetime import datetime
import websockets
from ocpp.v16 import ChargePoint as cp
from ocpp.v16 import call
from ocpp.v16.enums import RegistrationStatus

logging.basicConfig(level=logging.INFO)

class ChargePoint(cp):
    async def send_boot_notification(self):
        request = call.BootNotificationPayload(
            charge_point_model="My Charge Point",
            charge_point_vendor="My Vendor"
        )
        response = await self.call(request)
        if response.status == RegistrationStatus.accepted:
            print("Connected to central system.")

    async def send_heartbeat(self):
        request = call.HeartbeatPayload()
        response = await self.call(request)
        print("Heartbeat response:", response)

async def main():
    async with websockets.connect(
        'ws://localhost:8180/steve/websocket/CentralSystemService/charger-01',
        subprotocols=['ocpp1.6']
    ) as ws:
        cp = ChargePoint('charger-01', ws)

        await asyncio.gather(cp.start(), cp.send_boot_notification())
        while True:
            await asyncio.sleep(900)
            await cp.send_heartbeat()

if __name__ == '__main__':
    asyncio.run(main())
mmzeynalli commented 6 months ago

If you have Steve and microOcpp in different docker networks, then the "localhost" for microOcpp docker is different, and won't see your application. I have tried to host a forward web socket with ngrok, and put ngrok link in the web view of the simulator, however, it still is not connected. The next step might be creating a network between two containers and use localhost, but have not tried that yet.

BradleyNeild commented 6 months ago

Creating a network in my SteVe docker-compose.yml and running MicroOcppSimulator on that network allowed me to connect with the IP given by the SteVe console. Thanks!

mmzeynalli commented 6 months ago

Would you be kind to show how you integrated simulator to your docker environment?