Created
January 13, 2019 07:19
-
-
Save JiriBilek/7bdde1863a3340ad58cbebbcc080583e to your computer and use it in GitHub Desktop.
16 concurrent clients on ESP8266. All connect to www.example.com, request GET of unique url and read the respose (that is the same for all urls)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Sketch for ESP8266 testing multiple tcp client connections | |
https://github.com/JiriBilek/WiFiSpi/issues/13 | |
*/ | |
#include <ESP8266WiFi.h> | |
const char* ssid = "**"; | |
const char* password = "**"; | |
#define maxClients 16 | |
WiFiClient clients[maxClients]; | |
void setup() { | |
Serial.begin(115200); | |
// Serial.setDebugOutput(true); | |
Serial.print("Connecting "); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
} | |
void loop() { | |
for (int i = 0; i < maxClients; ++i) { | |
yield(); | |
int ret = clients[i].connect("www.example.com", 80); | |
if (ret == 0) { | |
Serial.printf("Client #%d not connected\n", i); | |
break; | |
} | |
Serial.printf("Client #%d connected\n", i); | |
} | |
for (int i = 0; i < maxClients; ++i) { | |
yield(); | |
int ret = clients[i].printf("GET /dev%d.html HTTP/1.1" "\r\n" | |
"Host: www.example.com" "\r\n" | |
"Connection: close" "\r\n\r\n", i); | |
Serial.printf("Client #%d transferred %d bytes\n", i, ret); | |
} | |
for (int i = 0; i < maxClients; ++i) { | |
uint32_t m = millis(); | |
int av; | |
do { | |
yield(); | |
av = clients[i].available(); | |
} while (av == 0 && millis() - m < 5000); | |
if (av == 0) | |
Serial.printf("Client #%d not ready\n", i); | |
else { | |
Serial.printf("Client #%d available %d bytes\n", i, av); | |
uint8_t buf[100]; | |
int rd = 0; | |
while (true) { | |
yield(); | |
int r = clients[i].read(buf, sizeof(buf)); | |
rd += r; | |
if (r == 0 && ! clients[i].connected()) | |
break; | |
} | |
Serial.printf("Client #%d read %d bytes\n", i, rd); | |
} | |
} | |
for (int i = 0; i < maxClients; ++i) { | |
yield(); | |
if (clients[i].connected()) { | |
clients[i].stop(); | |
Serial.printf("Client #%d stopped\n", i); | |
} | |
} | |
Serial.printf("Waiting\n"); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment