#include <ESP8266WiFi.h>
#include "settings.h"
#define PORT 9001
WiFiServer server(PORT);
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(200);
}
server.begin();
}
void loop() {
client = server.available();
while(client.connected()) {
while(client.available()){
client.write(client.read());
}
}
}
Now, it connects to my WiFi and I'm connecting to it via Python using sockets. However, the way I'm connecting is not important.
So, within the test Python code, I'm sending 32 bytes and waiting for 32 back (as the sketch is echoing data). I'm achieving only about 22 packets per second. This means the latency from sending data to receiving it is about 50ms - way, way more than when I'm pinging the same IP (ping shows 1ms). What's slow here?
I'm kind of new to this.
If anyone wants to try to run it, this is settings.h for the sketch:
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
And Python code I'm running on my PC:
import time
from collections import deque
import struct
import socket
server_config = {
'host': '', # set your IP
'port': 9001,
'timeout': None,
'connect_timeout': 2,
}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(server_config['connect_timeout'])
print('connecting to ', server_config)
s.connect((server_config['host'], server_config['port']))
s.setblocking(True)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) # Send immediately if blocking
s.settimeout(None)
connection = s.makefile('rwb', 0)
q = deque([], 20)
while True:
t = time.time()
connection.write(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\r\n')
data = connection.readline()
t = time.time() - t
q.append(t)
print(f'{1/(sum(q)/len(q)):.3f}')
print(data)