{"id":23430150,"url":"https://github.com/softplus/esp8266-wifi-timing","last_synced_at":"2025-04-12T21:35:42.608Z","repository":{"id":155417257,"uuid":"558981186","full_name":"softplus/esp8266-wifi-timing","owner":"softplus","description":"ESP01 / ESP8266 timing app to check various wifi connection strategies","archived":false,"fork":false,"pushed_at":"2023-11-25T16:14:35.000Z","size":49,"stargazers_count":12,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T15:48:06.982Z","etag":null,"topics":["arduino","esp-01","esp8266","platformio","wifi"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/softplus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-10-28T18:35:09.000Z","updated_at":"2024-04-18T21:40:26.000Z","dependencies_parsed_at":"2023-11-25T17:27:46.672Z","dependency_job_id":"a2c21bc6-8120-4444-bb55-efb86adb69ff","html_url":"https://github.com/softplus/esp8266-wifi-timing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softplus%2Fesp8266-wifi-timing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softplus%2Fesp8266-wifi-timing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softplus%2Fesp8266-wifi-timing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softplus%2Fesp8266-wifi-timing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/softplus","download_url":"https://codeload.github.com/softplus/esp8266-wifi-timing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248637187,"owners_count":21137530,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","esp-01","esp8266","platformio","wifi"],"created_at":"2024-12-23T08:15:58.137Z","updated_at":"2025-04-12T21:35:42.582Z","avatar_url":"https://github.com/softplus.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ESP01 / ESP8266 wifi speed testing code\n\nCheck various ESP8266 / Arduino-framework wifi connection strategies and their timings.\nTiming includes publishing topics to the local MQTT server (which confirms a good wifi connection).\nWhat's the fastest way to connect? Where are weird things happening in the background?\nWe'll see.\n\nConfiguration used:\n\n* ESP8266 basic board ([ESP-01, 512MB](https://s.click.aliexpress.com/e/_DevU043))\n* USB/TTL programmer board ([like this](https://s.click.aliexpress.com/e/_DkvhxT9))\n* [Platformio](https://platformio.org/)\n* [ESP8266 Arduino framework](https://docs.platformio.org/en/stable/platforms/espressif8266.html) in Platformio\n* Wifi AP with WPA2, [MQTT server](https://mosquitto.org/) on local network using IPv4\n\n(c) 2022 John Mueller / [MIT license](LICENSE)\n\n## TLDR: Fastest ESP8266 wifi connection time can be less than 170ms\n\nThe fastest connection (that I found) is done with the following setup:\n\n1. Connect normally. Cache received BSSID \u0026 channel. O(4'000ms)\n2. Set wifi.persistent(true). Connect with BSSID \u0026 channel. O(1'500ms)\n3. Set wifi.persistent(true) to get persisted infos. Connect again with BSSID \u0026 channel. O(170ms) \n\nIn C++, steps 2 \u0026 3 look like this:\n\n```c++\nWiFi.persistent(true);\nWiFi.mode(WIFI_STA);\nWiFi.config(IPAddress(data-\u003eip_address),\n    IPAddress(data-\u003eip_gateway), IPAddress(data-\u003eip_mask), \n    IPAddress(data-\u003eip_dns1), IPAddress(data-\u003eip_dns2));\nWiFi.begin(data-\u003ewifi_ssid, data-\u003ewifi_auth, data-\u003ewifi_channel, data-\u003ewifi_bssid, true);\nuint32_t timeout = millis() + FAST_TIMEOUT;\nwhile ((WiFi.status() != WL_CONNECTED) \u0026\u0026 (millis()\u003ctimeout)) { delay(10); }\n// connected or timed out\n```\n\n## Arduino sketches\n\nThis repo includes Arduino sketches if you prefer those. They're at:\n\n* [hardcoded connection](arduino_sketches/test_wifi_speed_hardcoded/)\n* [normal connection](arduino_sketches/test_wifi_speed_flash/)\n\n### Arduino output \u0026 explanation\n\n```\n17:13:42.923 -\u003e Starting.\n17:13:46.905 -\u003e Timing connect(): 4001ms\n17:13:46.905 -\u003e Result=1\n17:13:47.934 -\u003e Rebooting.\n17:13:48.034 -\u003e ...\n17:13:49.595 -\u003e Starting.\n17:13:50.856 -\u003e Timing connect(): 1259ms\n17:13:50.856 -\u003e Result=1\n17:13:51.851 -\u003e Rebooting.\n17:13:51.951 -\u003e ...\n17:13:53.544 -\u003e Starting.\n17:13:53.709 -\u003e Timing connect(): 168ms\n17:13:53.709 -\u003e Result=1\n17:13:54.705 -\u003e Rebooting.\n17:13:54.804 -\u003e ...\n17:13:56.397 -\u003e Starting.\n17:13:56.397 -\u003e offset=2\n17:13:56.563 -\u003e Timing connect(): 167ms\n17:13:56.563 -\u003e Result=1\n17:13:57.558 -\u003e Rebooting.\n17:13:57.657 -\u003e ...\n```\n\n* First connection: does a normal connection with just SSID \u0026 Auth. Caches BSSID \u0026 channel. Timing is O(4'000ms)\n* Second connection: does a faster connection with SSID, Auth, BSSID, channel. Then persists the connection details. TIming is O(1'500ms)\n* Third (and subsequent) connnections: does a full-speed connection with SSID, Auth, BSSID, channel \u0026 persisted info. Timing is O(170ms)\n* If BSSID or channel change, the fallback will go to the full connection after a 5000ms timeout. Total time on fallback is O(10'000ms).\n\n## Variations tested\n\n* [Notes of variants](variations.txt)\n* [Google Sheet with times](https://docs.google.com/spreadsheets/d/12uTw4UXFPKMmaT33-qmTIPmPjjoEv63IqGaQU1Gozfs/edit?usp=sharing)\n\n### Variations \u0026 timings (overview)\n\nI included median and 90'th percentile (\"p90\") duration timing. \n90th percentile means that 90% of the runs were below this number.\nTimings were measured with the `micros()` function, and tracked over a number of iterations.\nThe timing data was output as `\u003ckey=value\u003e` to the serial port, aggregated with [/scripts/serial_monitor.sh] (a bash script that uses a Python-based serial port monitor, tracking the entries into a CSV file).\n\nThe total time includes:\n\n* Restoring settings from flash\n* Connecting to wifi\n* Connecting to the MQTT server\n* Publishing 1 MQTT topic (in last variant, 5 topics)\n\nSome of the variants tested (see [variations.txt](variations.txt) for more):\n\n* Normal connection (just SSID, auth): median 3898ms, p90 4267ms\n* Connect with BSSID, channel (Variant \"O\"): median 1086ms, p90 1146ms\n* With `persistent(true)`, no BSSID, channel specified (Variant \"K\"): median 2911ms, p90 3915ms\n* With `persistent(true)`, giving BSSID, channel, manually connecting using `wifi_station_connect()` (Variant \"J\"): median 940ms, p90 1552ms\n* With `persistent(true)`, giving BSSID, channel, manually connecting using `reconnect()` (variant \"I\"): median 587ms, p90 1749ms\n* With `persistent(true)`, giving BSSID, channel, requesting connection in `begin()` (Variant \"G\"): median 197ms, p90 1201ms\n* same, but doing 5x MQTT publishes instead of one (Variant \"P\"): median 188ms, p90 1227ms\n\nThe last variant (\"P\"; similar to the second-last one) is the one that's overall the fastest, with a median time from start to publishing 5 MQTT topics of 188ms (stddev: 334ms).\n\nThe MQTT portion of variant \"P\" (connecting to server, publishing topics) has a median time of 19ms (p90: 25ms, stddev: 290ms!). Without doing an IP/port pre-connection (variant \"Q\"), the total median time goes to 190ms (p90: 278ms, stddev: 257ms), so the IP pre-connection does not save much. Using the hostname instead of IP address (requiring a DNS lookup; variant \"R\"), the total time goes to median 202ms (p90 840ms, stddev 410ms), so caching the IP address is a good idea.\n\n## Anecdotes\n\nThe weird \u0026 wonderful:\n\n* `persistent()` is [disabled by default](https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-class.html#persistent) in the ESP SDK for wifi. I suspect because of flash wear? YOLO.\n* Platformio uses ancient ESP8266 \"Non-OS\" (Arduino) SDKs -- the [ESP8266 SDKs](https://github.com/esp8266/Arduino) are much newer. [Issue filed in 2018](https://github.com/platformio/platform-espressif8266/issues/85).\n* Connecting with BSSID and channel (without `persistent(true)`), will result in scanning for a channel, and may use a different channel. This adds ca 900ms to the connection time. This is probably a bug. This is the configuration that most external mentions for speed optimization suggest, which is better than nothing, but still slower than it needs to be.\n* The ESP SDK code in [ESP8266WiFiSTA.cpp](https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp#L123) shows how the connection is built, but you can't check the code for what actually happens. Howver, you can tell that using `persistent(true)` without specifying a BSSID will result in the persistent data not being used (despite having the BSSID too).\n* In the [code](https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp#L195), you also see that `wifi_station_connect()` is called before setting the channel number. Does this mean the persistent channel is not used? Weird.\n* Using `persistent(true)` without specifying BSSID / channel does not persist the received settings.\n* The debug output (see below) is the same for all connection types; it's useless to look at for speed optimizations.\n* The SDK versions provided by Platformio (v2.2.1 - 2.2.x, pre-3.0; 2018 to 2019) all have similar timings.\n* The unclear difference between `persistent(true)` + connect with BSSID \u0026 channel imo suggest that future SDK versions may be different, and that ESP32 may handle this differently. It's unclear what `persistent(true)` actually does. Magic.\n\n## ESP8266 Wifi debug output\n\nPlatformio has build flags to enable ESP wifi debug output.\nUnfortunately the main variations all show exactly the same debug output, so you can't tell what's happening.\n\nIn platformio.ini:\n\n```\nbuild_flags = -DDEBUG_ESP_WIFI -DDEBUG_ESP_PORT=Serial \n```\n\nOutput with normal connection (scan for BSSID, channel, connect:\n\n```\nfpm close 3 \nmode : sta(c4:5b:be:4a:69:34)\nadd if0\nwifi evt: 8\nwifi evt: 2\nscandone\nstate: 0 -\u003e 2 (b0)\nstate: 2 -\u003e 3 (0)\nstate: 3 -\u003e 5 (10)\nadd 0\naid 3\ncnt \n\nconnected with XXXXXX, channel 11\ndhcp client start...\nwifi evt: 0\nip:192.168.178.111,mask:255.255.255.0,gw:192.168.178.1\nwifi evt: 3\n\u003ctime=3791898\u003e\n```\n\nOutput without `persistent(true)` just with BSSID, Channel (variation \"O\"):\n\n```\nfpm close 3 \nmode : sta(c4:5b:be:4a:69:34)\nadd if0\nwifi evt: 8\nwifi evt: 2\nscandone\nstate: 0 -\u003e 2 (b0)\nstate: 2 -\u003e 3 (0)\nstate: 3 -\u003e 5 (10)\nadd 0\naid 3\ncnt \n\nconnected with XXXXXX, channel 11\nip:192.168.178.111,mask:255.255.255.0,gw:192.168.178.1\nip:192.168.178.111,mask:255.255.255.0,gw:192.168.178.1\nwifi evt: 0\nwifi evt: 3\n\u003ctime=1054361\u003e\n```\n\nOutput with `persistent(true)` + BSSID, channel (variation \"G\")\n\n```\nfpm close 3 \nmode : sta(c4:5b:be:4a:69:34)\nadd if0\nwifi evt: 8\nwifi evt: 2\nscandone\nstate: 0 -\u003e 2 (b0)\nstate: 2 -\u003e 3 (0)\nstate: 3 -\u003e 5 (10)\nadd 0\naid 3\ncnt \n\nconnected with XXXXXX, channel 11\nip:192.168.178.111,mask:255.255.255.0,gw:192.168.178.1\nip:192.168.178.111,mask:255.255.255.0,gw:192.168.178.1\nwifi evt: 0\nwifi evt: 3\n\u003ctime=168166\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftplus%2Fesp8266-wifi-timing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftplus%2Fesp8266-wifi-timing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftplus%2Fesp8266-wifi-timing/lists"}