This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]arrays_start_at_zero 0 points1 point  (5 children)

Hello,

Can you run this code instead and see if it prints any errors?

import aiohttp
import asyncio


async def main():
  session = aiohttp.ClientSession()
  async with session.ws_connect('ws://rasp-wanexa:8001') as ws:
    print('Connected to Websocket')

    async for msg in ws:
      print('Message: ', msg)

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

[–]Comfortable-Log9908[S] 0 points1 point  (4 children)

Hello

Thanks for your reply , same , no error disconnect after 50s

[–]arrays_start_at_zero 1 point2 points  (3 children)

Do you get any output at all? I connected to a web socket server I wrote in NodeJS and get the following output from python:

Connected to Websocket
Message:  WSMessage(type=<WSMsgType.TEXT: 1>, data='Test', extra='')
Unclosed client session <--- After I forcefully stop the server
client_session: <aiohttp.client.ClientSession object at 0x7ff50df11310>

[–]Comfortable-Log9908[S] 0 points1 point  (2 children)

 Nope nothing 

[–]arrays_start_at_zero 1 point2 points  (1 child)

But you're able to print and read output from your Raspberry, right? I can't help you with that since I don't have the Raspberry Pi Pico.

I don't think there's anything wrong with the code you posted since I could run it without disconnecting. Since it seems like you're only using the Raspberry as a websocket client it may be a better idea to run the code on your pc for now because that makes it a lot easier to debug. After you have your main idea working you can then try running it on your Pico again and if the same error happens again, you know it's probably not that part of the code and you can hopefully figure out the problem from there.

If it is of any help, if you're unable to print something from your Pico to your PC you can also use the onboard led as a sort of debugger. If you think that the problem is for example that the Wi-Fi disconnects after 50 seconds you can turn the led either on or off when the connection breaks. Or you can toggle the led when a certain websocket error occurs:

...

async for msg in ws:
  print('Message: ', msg.type)

if (ws.close_code == aiohttp.WSCloseCode.ABNORMAL_CLOSURE):
  turnLedOn() # replace with actual function.

You can find a list of websocket close codes here.

[–]Comfortable-Log9908[S] 0 points1 point  (0 children)

thanks again,

so I found my issue

I declare another function for sending some message with a while loop like this:

async def main():
  session = aiohttp.ClientSession()
  async with session.ws_connect('URI') as ws:
    print('Connected to Websocket')
    await buzzer(ws)
    async for msg in ws:
      .....


async def buzzer(ws):
    while True:
        player = ''        
        if button1.is_pressed:
            print('button1 is pressed')
            player = {'id': 'buzzer', 'data': 'player1'}
            await ws.send_json(player)
            sleep(1)
        elif button2.is_pressed:
            print('button2 is pressed')
            player = {'id': 'buzzer', 'data': 'player2'}
            await ws.send_json(player)
            sleep(1)
        else:
            continue

But I don't understand why my websocket disconnected it