I’m trying to control the speed of a motor connected to a Raspberry Pi Pico W using our diy ESC based on the scroll wheel value of my mouse that is connected to computer. I have written two programs: one that runs on my laptop and captures the scroll wheel inputs, and another that runs on the Pico W and controls the motor speed.
Here’s the code running on my laptop (the sender):
from pynput import mouse
import socket
def on_scroll(x, y, dx, dy):
try:
message = str(dy)
client_socket.send(message.encode())
except Exception as e:
print(f"Failed to send data: {e}")
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client_socket.connect(('localip', 12345))
except Exception as e:
print(f"Failed to connect: {e}")
exit(1)
with mouse.Listener(on_scroll=on_scroll) as listener:
listener.join()
And here’s the code running on the Pico W (the receiver):
import network
import socket
from time import sleep
import machine
from machine import Pin, PWM
# Initialize GPIO 0 as a PWM pin
motor = PWM(Pin(0))
# Set the frequency of the PWM signal
motor.freq(50)
# Function to set motor speed
def set_speed(level):
if level < 0 or level > 100:
print("Invalid level! Please enter a value between 0 and 100.")
return
# Convert level to duty cycle (0-65535)
duty = int(level * 655.35)
# Set the duty cycle
motor.duty_u16(duty)
ssid = 'ssid'
password = 'pass'
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
print('Connected! Local IP:', wlan.ifconfig()[0])
# Create a socket server to receive scroll data
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((wlan.ifconfig()[0], 12345))
server_socket.listen()
while True:
client_socket, addr = server_socket.accept()
print('Client connected:', addr)
while True:
data = client_socket.recv(1024)
if not data:
break
scroll_value = int(data.decode())
set_speed(scroll_value)
client_socket.close()
try:
connect()
except KeyboardInterrupt:
machine.reset()
When I run these programs, the client on my laptop is supposed to connect to the server on the Pico W and send scroll wheel data over the connection. However, the programs don’t seem to connect to each other. The client program on my laptop just closes after a while, and the server program on the Pico W doesn’t print any message indicating that a client has connected.
I’ve checked the IP address and port number, and they match in both programs. I’ve also made sure that the server program is running before I start the client program. My firewall settings are not blocking the connection. btw there's nothing wrong with my hardware setup as they work if i use the manual code to controll the motor speed using predefined values -
from machine import Pin, PWM
# Initialize GPIO 0 as a PWM pin
motor = PWM(Pin(0))
# Set the frequency of the PWM signal
motor.freq(50)
# Function to set motor speed
def set_speed(level):
if level <0 or level > 100:
print("Invalid level! Please enter a value between 1 and 10.")
return
# Convert level to duty cycle (0-65535)
duty = int(level * 655.35)
# Set the duty cycle
motor.duty_u16(duty)
# Set the motor speed to 5
set_speed(0) #changing the value changes motor speed.
but now i need to do the same thing with internet and scroll wheel so i need the programms to work.
Does anyone know what could be causing this issue and how I can fix it?
I tried running the server program on my Pico W and the client program on my laptop simultaneously. I expected the client program to connect to the server program and send scroll wheel data over the connection, which would then be used to control the motor speed on the Pico W. However, the client program on my laptop just closes after a while, and the server program on the Pico W doesn’t print any message indicating that a client has connected. I’ve checked the IP address and port number, and they match in both programs. I’ve also made sure that the server program is running before I start the client program. My firewall settings are not blocking the connection.