0

I have been trying to develop an IoT-based application. I thought using WebSockets with my Raspberry Pi Pico W as the server would be a good idea to transfer information to my react application in real-time. I am new to socket programming and I am having issues transferring information between the React App and the Pico W. For some more context, my Server side code looks like the following:

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)

print('Listening On ' + str(addr))

while True:
    try:
        cl,addr = s.accept()
        print('Client connected from ' + str(addr))
        cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
        cl.send("Hello")
        time.sleep(3)
        cl.close()
    except:
        cl.close()
        break

In React Native Client-side code looks like follows:

import io from "socket.io-client"
import {useEffect } from 'react';

export default function App() {

  useEffect(() => {
    const socket_2 = io("http://<IP_ADDRESS_NOT_SHOWN_HERE>:80")
    socket_2.emit('message', 'data');
    socket_2.on('message', (data) => {
      console.log(data);
    });
    console.log(socket_2)
  }, []);

The interesting thing is that I am sure the server side is working because when I connect from the browser is sends me the "Hello" response. Additionally when I connect from React App I see a bunch of logs in Python Resembling the following:

Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62518)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62540)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62541)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62554)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62567)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62573)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62580)

To sum it up there are two primary things I am questioning here.

1-) I am not sure why it keeps making new connections

2-) I am also not sure why my information is not being transferred in between React Native and my Raspberry Pi Pico W server properly

Any help would be appreciated I am a socket-programming noob :/

I have tried using Socket IO and also I have tried using the react native socket library. I believe the socket io is much easier but I just cannot understand what is happening here

1 Answer 1

0

Here, you just accept connection, you are not doing anything with received message from your React application, you send response an close connection. Its not how websockets works

this explains both of your questions: why you get new connection each time (you close connection) and why it does not work properly (its not proper WS server implementation)

As far as I know it- micropython does not have proper WS implementation. Here is something about websockets (no experience, just decided to share)

https://github.com/marcidy/micropython-uasyncio-websockets

https://github.com/miguelgrinberg/microdot/tree/main/examples/websocket

You need to get down to very basics, study WS protocol and implement it if you want to have web sockets available in your pico.

Extremely simplified WS pseodocode:

  • accept connection
  • while new messages available
    • read message and send response
    • wait for messages
  • close connection when connection breaks
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.