Reputation: 21
I have a node.js server in which I am trying to send data in real time from my raspberry pi pico w over sockets.
My Simple Server is setup as follows:
const express = require("express")
const app = express();
const http = require("http");
const { Server } = require("socket.io");
const cors = require("cors")
const server = http.createServer(app)
const io = new Server(server, {
cors: {
origin: "*",
}
})
io.on("connection", (socket) => {
console.log('User Connected: ${socket.id}');
})
app.get('/', function(req, res, next) {
res.send("Hello world");
});
server.listen(80, () => {
console.log("Server is running")
})
The code I am running on the client side on my Raspberry Pi Pico W is as follows:
import time
import socket
import network
ssid = '<name>'
password = '<password>'
# Just making our internet connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print(wlan.ifconfig())
c = socket.socket()
c.connect(('http://127.0.0.1/',80))
print('Connected')
After ensuring the server is working just fine (i setup a react client and was able to transfer information), I am still unsure why my micropython-based client on my microcontroller cannot setup a socket. I keep getting the error:
Traceback (most recent call last):
File "<stdin>", line 32, in <module>
OSError: [Errno 103] ECONNABORTED
If someone could guide me on potential solutions that would be extremely helpful. Thanks!
Upvotes: 1
Views: 1094
Reputation: 74831
The python socket
library is for the lower level TCP connection to 127.0.0.1
on port 80
.
HTTP and WebSockets are a layer on top the raw socket and require a specific client or socket code that understands each protocol.
Socket.io is another layer on top of those providing a way to negotiate a connection via HTTP or WS and the subsequent message formatting.
A simple node socket server looks like:
const net = require('node:net')
const server = net.createServer(function(socket) {
socket.write('Echo server\r\n')
socket.pipe(socket)
})
server.listen(1111, '127.0.0.1')
Which you connect with:
c.connect(('127.0.0.1',1111))
HTTP is likely the easiest route. I think urequests
is bundled most of the time, or writing the HTTP request to the raw socket
is not overly hard to implement yourself.
Using HTTP will have some overhead compared to sockets/web sockets, if you are streaming large amounts of data.
On the python side you would need a client like micropython-uasyncio-websockets (not sure if this is useful, just searched). This would require a server like the ws
library provides on the node server side.
It might be hard to find an up to date socket.io library that works with micropython.
Upvotes: 0