Post topics, source code that relate to the Arduino Platform

User avatar
By DhanOS
#93703 I'm writing a sketch to forward TCP connection data to the Serial port and also the other way around. Simple code worked flawlessly, but the speed I achieved is greatly below what I expected. So I thought I'll try to echo what I'm sending to see where the issue is:
Code: Select all#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:
Code: Select all#define WIFI_SSID ""
#define WIFI_PASSWORD ""

And Python code I'm running on my PC:
Code: Select allimport 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)
User avatar
By Inq720
#93851 I've recently uploaded my web server library into the Arduino library manager and finished an extensive amount of abusive tests and can give you some numbers that you should be able to expect.

  • Uploading files 44 files 1.8 MB worth to the ESP8266 using HTTP in 15 seconds =
    averaging 120KB/sec
  • Serving those 44 files in an abusive 100 hour test using 9 browsers delivered 180GB using HTTP = averaging 490KB/sec
  • Using small messages around 40-50 bytes over a 100 hour test via WebSockets (basically plain TCP) averages 32KB/sec using 5 ms intervals.
  • The same test pushed to 1 ms intervals starts buffering a great deal and typically runs for a couple of hours and a couple GBs worth before going belly-up. Averaging 132KB/sec