Client Server with ESP32 #9714
-
I am trying to generate a signal on the ESP32 (the client) and to send it to a python script on a host PC (the server). On my home network, the host PC is on 192.168.1.22. The protocol is UDP, and the server listens on port 56000 for any data. The following code implements the server running on the PC: # This is the server in CPython
import socket
port = 56000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
print(f"waiting on port: {port}")
while 1:
data, addr = s.recvfrom(1024)
print(data) The client connects to the network on address 192.168.1.11, and sends data using port 56000. # this is the client in micropython
import usocket as socket
import esp_network
import make_tone
esp_network.do_connect()
# ======= AUDIO CONFIGURATION =======
TONE_FREQUENCY_IN_HZ = 5000
SAMPLE_SIZE_IN_BITS = 16
SAMPLE_RATE_IN_HZ = 48_000
# ======= AUDIO CONFIGURATION =======
ADDR = "192.168.1.11" # This is IP of the server
PORT = 56000
# Socket to exchange data with GR
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
samples = make_tone.make_tone(SAMPLE_RATE_IN_HZ,
SAMPLE_SIZE_IN_BITS, TONE_FREQUENCY_IN_HZ)
print(samples)
s.sendto(samples, (ADDR, PORT)) The esp32 successfully connects to the network on 192.168.1.11. Can anyone see my error, or make a suggestion of something to try? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 19 replies
-
Try printing the addr in the server while loop to confirm. I do something similar but send a message from the server, then at the client get the server's address. Then you can print it out to confirm that you have a working link. |
Beta Was this translation helpful? Give feedback.
-
Looks like I don't how to add links correctly! Copy/paste and see if that works. |
Beta Was this translation helpful? Give feedback.
-
Okay I think I have a path now, thanks to help from davefes. The server buffer size matters to make sure you capture all the data sent, but it doesn't appear to cause the crash. The crash is caused by not having a delay in the
There is no crash, and the server receives the data. I'm not sure I can do what I want to do, which is to send audio data in real time over the wifi. If the data is 16-bit, at 48khz sample rate, that's 768kpbs worst case. Can anyone comment on if this is too fast? |
Beta Was this translation helpful? Give feedback.
Okay I think I have a path now, thanks to help from davefes. The server buffer size matters to make sure you capture all the data sent, but it doesn't appear to cause the crash. The crash is caused by not having a delay in the
while
loop at client -- so I suppose if the data sent is more than the esp32 wifi can keep up with, there is a crash. If I add a delay as follows:There is no crash, and the server receives the data. I'm not sure I can do what I want to do, which is to send audio data in real …