<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Justin Mclean</title>
    <description>The latest articles on DEV Community by Justin Mclean (@justinmclean).</description>
    <link>https://dev.to/justinmclean</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4279458%2F840c0e24-6389-413c-a9bc-169231b311f2.jpg</url>
      <title>DEV Community: Justin Mclean</title>
      <link>https://dev.to/justinmclean</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/justinmclean"/>
    <language>en</language>
    <item>
      <title>What Wi-Fi gives away when you just listen</title>
      <dc:creator>Justin Mclean</dc:creator>
      <pubDate>Fri, 04 Dec 2026 05:14:02 +0000</pubDate>
      <link>https://dev.to/justinmclean/what-wi-fi-gives-away-when-you-just-listen-1ck8</link>
      <guid>https://dev.to/justinmclean/what-wi-fi-gives-away-when-you-just-listen-1ck8</guid>
      <description>&lt;p&gt;Wi-Fi encrypts what's inside a frame. It doesn't encrypt the header. Who sent the frame, who it's for, which way it's going, how long it is, how fast it was sent and whether it's a resend are all in the clear, for anyone with a radio to hear.&lt;/p&gt;

&lt;p&gt;I wrote five apps for the M5Stack CoreS3 that only listen. They join no network, send nothing and never read what's inside a frame. Between them they show how busy each channel is, check the health of your own network, map your coverage around the house, count the phones nearby and find Wi-Fi cameras, such as one hidden in a holiday rental. At the end there's a sixth that does the opposite: it runs its own network to take a live poll from people's phones.&lt;/p&gt;

&lt;p&gt;This is the second of two posts. The &lt;a href="https://dev.to/justinmclean/using-wi-fi-as-a-sensor-on-the-m5stack-cores3-37ba"&gt;first&lt;/a&gt; covered using Wi-Fi as a sensor: a motion radar, a breathing counter, a theremin and a tape measure. This one walks through the listening apps with the actual code, the numbers I measured, and the limits I hit.&lt;/p&gt;

&lt;p&gt;All of them are open source (MIT) and build with &lt;a href="https://platformio.org" rel="noopener noreferrer"&gt;PlatformIO&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-waterfall" rel="noopener noreferrer"&gt;esp32-wifi-waterfall&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-health" rel="noopener noreferrer"&gt;esp32-wifi-health&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-coverage-map" rel="noopener noreferrer"&gt;esp32-wifi-coverage-map&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-phone-counter" rel="noopener noreferrer"&gt;esp32-wifi-phone-counter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-camera-finder" rel="noopener noreferrer"&gt;esp32-wifi-camera-finder&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-vote" rel="noopener noreferrer"&gt;esp32-wifi-vote&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's in the clear
&lt;/h2&gt;

&lt;p&gt;Normally a Wi-Fi radio drops every frame that isn't addressed to it. In promiscuous mode it keeps them all: the ESP32-S3 hands every frame it hears to a callback, with two parts: the raw frame bytes, and a &lt;code&gt;rx_ctrl&lt;/code&gt; block of what the radio measured while receiving it. Between them you get:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Where&lt;/th&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;What it tells you&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Frame byte 0&lt;/td&gt;
&lt;td&gt;Type and subtype&lt;/td&gt;
&lt;td&gt;Beacon (0x80), probe request (0x40), data, deauthentication (0xC0), disassociation (0xA0)...&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frame byte 1, bits 0 and 1&lt;/td&gt;
&lt;td&gt;To DS / From DS&lt;/td&gt;
&lt;td&gt;Whether the frame is going into or out of the network behind the router (the "distribution system"). 1: device to router (upload). 2: router to device (download).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frame byte 1, bit 3&lt;/td&gt;
&lt;td&gt;Retry&lt;/td&gt;
&lt;td&gt;This frame is a resend&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bytes 4, 10, 16&lt;/td&gt;
&lt;td&gt;Addresses&lt;/td&gt;
&lt;td&gt;Receiver, transmitter, and the BSSID (the router's own address, which names the network it runs). Which is which depends on the direction bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rx_ctrl.rssi&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Signal strength&lt;/td&gt;
&lt;td&gt;In dBm (decibels relative to a milliwatt; -40 is very close, -90 is barely there). Roughly how far away the sender is&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rx_ctrl.sig_len&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Length&lt;/td&gt;
&lt;td&gt;Bytes on air, including the 4-byte checksum&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;rx_ctrl.rate&lt;/code&gt;, &lt;code&gt;mcs&lt;/code&gt;, &lt;code&gt;sig_mode&lt;/code&gt;, &lt;code&gt;cwb&lt;/code&gt;, &lt;code&gt;sgi&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Speed&lt;/td&gt;
&lt;td&gt;Which Wi-Fi generation sent it, its data rate, and two settings that change the rate (explained in the health checker section)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rx_ctrl.noise_floor&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Noise&lt;/td&gt;
&lt;td&gt;The radio's estimate of background noise&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Turning it on takes a few lines. This is from the health checker, which wants control frames too, because acknowledgements use airtime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;wifi_promiscuous_filter_t&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter_mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;WIFI_PROMIS_FILTER_MASK_ALL&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_promiscuous_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;wifi_promiscuous_filter_t&lt;/span&gt; &lt;span class="n"&gt;cf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter_mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;WIFI_PROMIS_CTRL_FILTER_MASK_ALL&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_promiscuous_ctrl_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// acknowledgements take airtime too&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_promiscuous_rx_cb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;onFrame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_promiscuous&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_channel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channelNow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;WIFI_SECOND_CHAN_NONE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The radio hears one channel at a time, so every app hops: set the channel, wait, move on. How long it waits on each channel is the main design choice, and each app picks differently.&lt;/p&gt;

&lt;p&gt;Two hardware limits shape everything below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;2.4 GHz only.&lt;/strong&gt; The ESP32-S3 has no 5 GHz radio.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One antenna.&lt;/strong&gt; Devices with two or more antennas can send two independent signals at once on the same channel (two spatial streams), doubling the speed. Pulling them apart needs as many antennas at the receiver, so the ESP32-S3 can only decode one-stream frames. It also can't decode Wi-Fi 6 (802.11ax) frames. Cheap cameras and gadgets send one stream. Most phones and laptops send two, so their frame contents are unreadable. The radio still reports their length and speed, though, so the health checker below can work out how much of the channel they use.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The waterfall: counting frames
&lt;/h2&gt;

&lt;p&gt;The simplest app hops channels 1 to 13, 40 ms on each (about two sweeps a second), and counts frames. The callback is one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wifi_promiscuous_pkt_type_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;heard&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// counting only&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bars at the top show each channel now, and the waterfall below scrolls the recent past. Frame rates range from a handful to thousands a second, so the colour is on a log scale, full at 2,000 frames a second:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;constrain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;log10f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fps&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;log10f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2001&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Colours run from deep navy for quiet, through blue and teal, to green and yellow for busy. The bottom line names the quietest of channels 1, 6 and 11 (the three that don't overlap) over the last half minute: where to put a hotspot.&lt;/p&gt;

&lt;p&gt;Counting frames is a crude measure. One long frame at a slow rate uses far more of the channel than a hundred short fast ones. That's what the health checker fixes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The health checker: airtime, not frames
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-health" rel="noopener noreferrer"&gt;esp32-wifi-health&lt;/a&gt; listens to each channel for 250 ms (a sweep every 3 or so seconds) and works out how much of the time each one is actually in use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcoj36rw1odft6ibszi3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcoj36rw1odft6ibszi3h.png" alt="Wi-Fi health channel view" width="240" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Airtime from length and speed
&lt;/h3&gt;

&lt;p&gt;Every frame starts with a preamble: a fixed training signal the receiver uses to lock on, sent before any data and taking the same time whatever the frame's length. So how long a frame was on air is its preamble plus its length in bits divided by its rate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="no"&gt;LEGACY_MBPS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;5.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;5.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="no"&gt;HT_MBPS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mf"&gt;6.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;19.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;39&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;52&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;58.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// How long a frame was on air, in microseconds, and its speed in Mbit/s.&lt;/span&gt;
&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;airtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;wifi_pkt_rx_ctrl_t&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mbps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sig_mode&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;                          &lt;span class="c1"&gt;// 802.11b and g&lt;/span&gt;
    &lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;LEGACY_MBPS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;192&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;                                        &lt;span class="c1"&gt;// 802.11n&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;streams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mcs&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;HT_MBPS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mcs&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;streams&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cwb&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mf"&gt;2.077f&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sgi&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mf"&gt;1.111f&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;streams&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aggregation&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ampdu_cnt&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ampdu_cnt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// one preamble per bundle&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mbps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sig_len&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The numbers come from the standard:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;802.11b&lt;/strong&gt; uses a 192 ¬µs long preamble (rate codes 0 to 3) or a 96 ¬µs short one (4 to 7).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;802.11g&lt;/strong&gt; uses OFDM (the data is spread across many narrow sub-carriers sent side by side) and has a 20 ¬µs preamble. The radio reports the rate as a 4-bit code taken from the frame's own signal field, and for 6 to 54 Mbit/s those codes aren't in speed order (code 8 is 48 Mbit/s, code 11 is 6). So the code is looked up in a table rather than calculated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;802.11n&lt;/strong&gt; (Wi-Fi 4) reports a modulation and coding scheme (MCS) number instead of a rate. MCS 0 to 7 are the one-stream rates, 6.5 to 65 Mbit/s; MCS 8 to 15 are the same eight with two streams, so twice as fast. The preamble is 36 ¬µs plus 4 ¬µs for each extra stream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Channel width&lt;/strong&gt; (&lt;code&gt;cwb&lt;/code&gt;). 802.11n can bond two 20 MHz channels into one 40 MHz channel. That carries 108 data sub-carriers instead of 52, so it's 108 / 52 = 2.077 times as fast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Guard interval&lt;/strong&gt; (&lt;code&gt;sgi&lt;/code&gt;). Between each OFDM symbol there's a short gap so echoes from the last one die away: normally 0.8 ¬µs, or 0.4 ¬µs with the short guard interval. A symbol plus its gap goes from 4.0 ¬µs to 3.6 ¬µs, so 4.0 / 3.6 = 1.111 times as fast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Aggregation.&lt;/strong&gt; 802.11n can bundle several frames behind one preamble (an A-MPDU). The radio reports each frame separately with how many were in the bundle, so the preamble is split between them rather than counted for each.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why the single antenna matters less here than you'd expect. A two-stream frame from a laptop can't be decoded, but the radio still reports its MCS and length, so its airtime still counts. Add up the airtime of every frame heard during one 250 ms listen on a channel (a dwell), divide by 250 ms, and you get the share of the channel in use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Choosing a channel
&lt;/h3&gt;

&lt;p&gt;The best channel is the one of 1, 6 and 11 with the lowest score, where busy is in per cent and noise in dBm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;others&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;overlapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;noise&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;others&lt;/code&gt; is airtime from other networks, not yours: your own traffic moves with you.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;overlapping&lt;/code&gt; counts strong neighbours on channels that overlap this one (a network on channel 3 overlaps both 1 and 6).&lt;/li&gt;
&lt;li&gt;Noise above -95 dBm adds 2 points per dB. The noise floor is the background radio energy on the channel that isn't Wi-Fi. Microwaves, baby monitors and analogue video senders raise it, and Wi-Fi can't avoid them the way it takes turns with other Wi-Fi.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  My network
&lt;/h3&gt;

&lt;p&gt;Pick your network and it reports on that alone: the signal where you are, how busy its channel is with other networks' traffic, strong overlapping neighbours, noise, the share of frames resent (the retry bit), the share of data airtime sent at slow rates, and frames knocking devices off it. Those are deauthentication and disassociation frames, the management frames a router (or anything pretending to be one) sends to end a device's connection. They're a single byte check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;knock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mh"&gt;0xC0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mh"&gt;0xA0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// deauthenticate, disassociate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those matter because a flood of them is how most Wi-Fi attacks start. They're also what a router sends to a device that talks to it without joining, which is how the checker caught a real problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  A real catch
&lt;/h3&gt;

&lt;p&gt;While testing the camera finder (below) I had a fake camera running: a Cardputer sending my router 50 frames a second at 1 Mbit/s, without joining. The health checker showed:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Channel 6 busy&lt;/th&gt;
&lt;th&gt;Knock-off frames a minute&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fake camera on&lt;/td&gt;
&lt;td&gt;50%&lt;/td&gt;
&lt;td&gt;63&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fake camera off&lt;/td&gt;
&lt;td&gt;13%&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Half the channel, from one small device sending junk at the slowest rate. Every one of those frames made the router send back a deauthentication: "you haven't joined me, go away". That's the kind of thing that makes a home network feel slow for no obvious reason.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limits
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Resends and slow data can only be read from one-antenna devices (gadgets, cameras, older phones). On a network of modern phones and laptops it says "not enough heard" rather than guessing.&lt;/li&gt;
&lt;li&gt;Frames leak into the channels either side, so a channel next to a busy one shows a little traffic of its own.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The coverage map: a heat map from passive scans
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-coverage-map" rel="noopener noreferrer"&gt;esp32-wifi-coverage-map&lt;/a&gt; maps your signal around the house. Walk somewhere, tap where you are on a rough floor plan on the screen, and it drops a dot. Between the dots it fills in a heat map.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqnt20vckhsogqnz42lzr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqnt20vckhsogqnz42lzr.png" alt="Coverage map" width="240" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The signal comes from passive scans: listen on each channel for a beacon rather than sending probe requests. Routers send a beacon every 102.4 ms, so the scan waits a little longer than that on each channel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="no"&gt;SCAN_MS_PER_CHANNEL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;110&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// a beacon is sent about every 102 ms&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nc"&gt;WiFi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scanNetworks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SCAN_MS_PER_CHANNEL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// async, passive&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A scan takes about 1.5 seconds. Each reading is the average of the last three, and with a mesh it takes whichever of your routers is strongest where you're standing. So: walk, wait about 5 seconds, tap.&lt;/p&gt;

&lt;p&gt;The CoreS3 has no idea where it is, which is why you tell it. The map is inverse distance weighting on a 6-pixel grid: each cell takes the signal of the dots near it, weighted by one over distance squared, and fades out further than 70 pixels from any dot so it doesn't pretend to know about rooms you haven't been in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nPts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;pts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cy&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;pts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;nearest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fminf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nearest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;wsum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;vsum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;pts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dbm&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nearest&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;uint16_t&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;colourOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vsum&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;wsum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;fade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;sqrtf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nearest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// dimmer away from the dots&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The top line says what the signal under you is good for:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Signal&lt;/th&gt;
&lt;th&gt;Means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;-60 dBm or better&lt;/td&gt;
&lt;td&gt;Strong: anything works&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-60 to -67&lt;/td&gt;
&lt;td&gt;Good: video calls are fine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-67 to -72&lt;/td&gt;
&lt;td&gt;Fair: browsing, but video may stutter&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-72 to -80&lt;/td&gt;
&lt;td&gt;Weak: drops out&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Below -80, or not heard&lt;/td&gt;
&lt;td&gt;Dead spot&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It only sees 2.4 GHz, which reaches further than 5 GHz. A spot that's fair on this map is probably weak on 5 GHz. The dots are kept in flash, and the CoreS3 SE has no battery, so you walk it round on a power bank.&lt;/p&gt;

&lt;h2&gt;
  
  
  The phone counter: probe requests and random addresses
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-phone-counter" rel="noopener noreferrer"&gt;esp32-wifi-phone-counter&lt;/a&gt; counts roughly how many phones are nearby.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgug8c2kx00gzjy8l342b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgug8c2kx00gzjy8l342b.png" alt="Phone counter screen" width="240" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A phone looking for Wi-Fi broadcasts probe requests ("is anyone there?", "is network X here?"), unencrypted, with its address in them. It does this even when it isn't connected to anything. The counter listens to channels 1, 6 and 11, a fifth of a second each, and keeps only probe requests (frame byte 0 is 0x40).&lt;/p&gt;

&lt;h3&gt;
  
  
  The random address problem
&lt;/h3&gt;

&lt;p&gt;Counting addresses doesn't work any more. Phones search with random addresses that change often, so one phone shows up under many. A real address is assigned by the maker, and its first three bytes are the maker's ID from the IEEE, called the OUI (organisationally unique identifier). A random one is marked "locally administered", meaning made up by the device itself, by bit 1 of its first byte. So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oui&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What stays the same when the address changes is the rest of the probe request: which rates, capabilities and extensions the phone says it supports. After the 24-byte header, a probe request is a list of information elements, each one an ID byte, a length byte and that many bytes of data. So the counter fingerprints that list. It hashes every element except the network name being asked for (element 0) and the channel (element 3), which change from one probe to the next:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2166136261u&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fnv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;221&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mh"&gt;0x00&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mh"&gt;0x50&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mh"&gt;0xF2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;readWps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The network name is skipped, never read. Element 221 is "vendor specific", and one with Microsoft's OUI (00:50:F2) and type 4 is WPS (Wi-Fi Protected Setup, the push-button pairing scheme). Its data includes fields where some devices say their maker and model, and those are read for the "other devices" list.&lt;/p&gt;

&lt;p&gt;A phone's probes don't all look alike either, because different kinds of scan include different elements. So groups link both ways: a new address with a known fingerprint joins that fingerprint's group, and a known address with a new fingerprint brings the new fingerprint into its group. Each group counts as one phone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Everything else
&lt;/h3&gt;

&lt;p&gt;Fixed addresses are laptops, speakers, cars, smart plugs and older phones, and their OUI says who made them. A table of 62 common makers, generated from the IEEE list by &lt;code&gt;tools/makers.py&lt;/code&gt;, is compiled in and searched with a binary search.&lt;/p&gt;

&lt;h3&gt;
  
  
  Near and in range
&lt;/h3&gt;

&lt;p&gt;It shows phones &lt;strong&gt;near&lt;/strong&gt; (stronger than -65 dBm, roughly the same room) and &lt;strong&gt;in range&lt;/strong&gt; (through walls too) over the last 1, 5 or 15 minutes. In a block of flats or on a busy street, "in range" includes the neighbours. "Near" is the number for the room.&lt;/p&gt;

&lt;h3&gt;
  
  
  How good it is
&lt;/h3&gt;

&lt;p&gt;It's an estimate, and it says so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It misses quiet phones.&lt;/strong&gt; A phone with its screen off, or already connected, may only probe every few minutes. The 15-minute window helps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Same-model phones can merge.&lt;/strong&gt; Two phones of the same model and software version send identical fingerprints.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It only hears 2.4 GHz.&lt;/strong&gt; Phones also probe on 5 GHz, but they scan 2.4 GHz often enough to be counted.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The camera finder: steady upload
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-camera-finder" rel="noopener noreferrer"&gt;esp32-wifi-camera-finder&lt;/a&gt; is the one I'd actually take on holiday. It finds Wi-Fi cameras nearby and then helps you walk to one.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Found&lt;/th&gt;
&lt;th&gt;Following it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm3yxnpnqx02paup15mpt.png" alt="Camera list" width="240" height="320"&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi7odkkptedfyn3yk9cpu.png" alt="Following a camera" width="240" height="320"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The idea: a camera streaming to the cloud uploads steadily, minute after minute, and downloads very little. Phones, laptops, speakers and TVs mostly download, or upload only for a while. The direction bits say which way every data frame is going, so this is visible without reading anything inside the frame.&lt;/p&gt;

&lt;h3&gt;
  
  
  Direction and device from the header
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                    &lt;span class="c1"&gt;// "null" frames carry no data&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                              &lt;span class="c1"&gt;// 1: to the router, 2: from it&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ds&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;ds&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// 0: not via a router, 3: router to router&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ds&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// the device: sender up, receiver down&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// broadcast&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Null data frames are data frames with nothing in them. A device sends one to tell the router it's going to sleep or waking up, so they say nothing about traffic. They're marked by bit 2 of the subtype (the top four bits of byte 0), hence &lt;code&gt;(f[0] &amp;gt;&amp;gt; 4) &amp;amp; 4&lt;/code&gt;. For uploads the device is the transmitter (address 2, byte 10); for downloads it's the receiver (address 1, byte 4). An address with bit 0 of its first byte set is a group address (broadcast or multicast), sent to everyone rather than to one device, so that's skipped too.&lt;/p&gt;

&lt;h3&gt;
  
  
  A device belongs to one channel
&lt;/h3&gt;

&lt;p&gt;It listens to channels 1 to 13, 150 ms each. Frames leak into the channels either side, and close up they're nearly as strong there, so counting every frame wherever it's heard would put a nearby device on three channels at once and count its traffic three times. So each device belongs to the channel it's heard on most, and only frames heard there count. A device moves channel only when another channel clearly wins:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;heardOn&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;heardOn&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The per-channel counts are halved every 5 seconds, so old history fades and a device that changes channel (say a phone moving to another router on a different channel) isn't stuck on the old one.&lt;/p&gt;

&lt;h3&gt;
  
  
  The verdict
&lt;/h3&gt;

&lt;p&gt;At the end of each listen on a channel, every device on it gets its upload and download rate for that listen, folded into a running average. For the first ten listens the weight &lt;code&gt;a&lt;/code&gt; is 1 / (n + 1), which makes it a plain average of everything so far; after that it's fixed at 0.1, so each new listen counts for 10% and old ones fade (an exponential moving average, roughly the last ten listens). &lt;code&gt;steady&lt;/code&gt; is the same kind of average of a yes or no: did it upload any real amount (over 300 bytes) in this listen? So it's the share of recent listens with upload:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;windows&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;windows&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.1f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// average of the last ~10&lt;/span&gt;
&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upRate&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upRate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;downRate&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;down&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;downRate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;steady&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upBytes&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;steady&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;judged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;windows&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="no"&gt;MIN_WINDOWS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;upMostly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upRate&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;downRate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;strong&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;judged&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;steady&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.6f&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upRate&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;upMostly&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;judged&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;steady&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.4f&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upRate&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;upMostly&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'p'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;verdict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strong&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="no"&gt;POSSIBLE&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="no"&gt;UPLOADING&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;QUIET&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// a phone can be a camera&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;verdict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="no"&gt;LIKELY&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;POSSIBLE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;verdict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strong&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="no"&gt;LIKELY&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="no"&gt;POSSIBLE&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;QUIET&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The kind comes from the address:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;'c', a camera maker.&lt;/strong&gt; Hikvision, Dahua, Wyze, Arlo, Ring and others, from a 58-maker table. A camera maker uploading at all is a likely camera; one that's quiet is still a possible one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;'p', a phone or computer.&lt;/strong&gt; A random address, or Apple, Samsung, Intel and the like. Uploading steadily is shown as &lt;strong&gt;uploading&lt;/strong&gt;, because that's usually a video call or a photo backup. But an old phone can be used as a camera, so one uploading as hard and as steadily as a camera (the same bar as below: at least 10 KB/s, in 60% of listens, more than twice what it downloads) is shown as a &lt;strong&gt;possible camera&lt;/strong&gt;, in the same 20 seconds. A video call can look like that too, which is why it's only "possible".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anything else&lt;/strong&gt; (unknown makers, Wi-Fi module makers like Espressif, which many cheap cameras use) has to earn it: at least 10 KB/s, up in 60% of listens, more than twice what it downloads.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ten listens on its channel is about 20 seconds before anything is judged.&lt;/p&gt;

&lt;h3&gt;
  
  
  Following it
&lt;/h3&gt;

&lt;p&gt;Tap a device and the CoreS3 stops hopping and stays on that device's channel, showing its signal strength with &lt;strong&gt;Warmer&lt;/strong&gt; and &lt;strong&gt;Colder&lt;/strong&gt;. Higher is closer. It's the oldest radio direction-finding trick there is, and it works well enough to walk to a device in a room.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing it with a fake camera
&lt;/h3&gt;

&lt;p&gt;To test it without planting a real camera, I built a fake one. &lt;code&gt;tools/fake-camera&lt;/code&gt; runs on an M5Stack Cardputer (or a second CoreS3). It sends the strongest network nearby, my own router, about 50 KB/s of junk data frames, without joining, so it looks like an ESP32 camera uploading. The Cardputer's G0 button turns the upload off and on, like a motion-triggered camera.&lt;/p&gt;

&lt;p&gt;The finder showed it as a likely camera &lt;strong&gt;within about 20 seconds&lt;/strong&gt;, measured at &lt;strong&gt;30 to 38 KB/s&lt;/strong&gt;. It reads low because it only hears the fake camera while it's on channel 6, and misses frames while it's on the other twelve. Real cameras upload about 30 to 500 KB/s, depending on resolution and how much the scene moves, so the 10 KB/s threshold leaves plenty of room.&lt;/p&gt;

&lt;p&gt;This is also the fake camera that the health checker caught knocking channel 6 to 50% busy.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it can't see
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Two-antenna devices.&lt;/strong&gt; It can only read frames sent with one spatial stream. Cheap cameras use one; most phones and laptops use two, so they rarely show up at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;5 GHz and Wi-Fi 6.&lt;/strong&gt; Most cheap cameras are 2.4 GHz Wi-Fi 4 (802.11n), but not all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cameras that don't use Wi-Fi.&lt;/strong&gt; One that records to a card or uses a cable sends nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Motion-only cameras.&lt;/strong&gt; Some only upload when they see movement. Wave at the spot you suspect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A phone used as a camera, sometimes.&lt;/strong&gt; One uploading as hard as a camera shows as a possible camera, but one streaming at low quality may only show as &lt;strong&gt;uploading&lt;/strong&gt;, and one with two antennas may not show at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Privacy
&lt;/h2&gt;

&lt;p&gt;Every listening app sees other people's devices, so I kept them deliberately forgetful. Addresses and fingerprints are never stored as they are. They're hashed with FNV-1a, a small fast hash (for each byte: XOR it in, then multiply by a fixed prime). The hash starts from a random number picked at each start (a salt) that is kept only in memory, so the same device gives a different ID after every restart and there's no way to get back from an ID to an address:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;esp_random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fnv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2166136261u&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart and every device gets a new, unrelated ID. No device data is written to flash (the health checker keeps only the name of your own network), and the phone counter forgets everything after 15 minutes. The network names phones ask for are never read, nor the device names some devices send, because those can be a person's name. Only counts, makers and models are shown or printed. Nothing inside any frame is read: only headers, sizes and directions.&lt;/p&gt;

&lt;h2&gt;
  
  
  One that talks back: a live poll
&lt;/h2&gt;

&lt;p&gt;The last app does the opposite of listening. &lt;a href="https://github.com/justinmclean/esp32-wifi-vote" rel="noopener noreferrer"&gt;esp32-wifi-vote&lt;/a&gt; runs its own open network, "CoreS3 vote". Join it and a voting page pops up by itself, with no app and no internet. Votes show as bars on the CoreS3 and on everyone's phone, refreshing every 2 seconds.&lt;/p&gt;

&lt;p&gt;The pop-up is the captive portal trick hotel Wi-Fi uses (a captive portal is the sign-in page a network makes you see before it lets you out). When a phone joins a network, it fetches a known address to check for internet. The CoreS3 runs a DNS server, the service that turns names into IP addresses, and answers every name with its own address:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;dns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;53&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;WiFi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;softAPIP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// every name points here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and its web server sends each phone's internet check to the poll:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"/generate_204"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/gen_204"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/hotspot-detect.html"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/library/test/success.html"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                         &lt;span class="s2"&gt;"/connecttest.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/ncsi.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/success.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/canonical.html"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;redirectToPage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onNotFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirectToPage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those are the checks Android, Apple and Windows use. The phone gets a 302 redirect (the HTTP reply for "it's moved, go here instead") instead of the answer it expects, decides it's behind a sign-in page, and shows the poll in its "sign in to network" sheet.&lt;/p&gt;

&lt;p&gt;One vote per phone (keyed by its IP address), which it can change. Polls live in &lt;code&gt;firmware/include/polls.h&lt;/code&gt;, or you can set a one-off poll from &lt;code&gt;192.168.4.1/admin&lt;/code&gt; with a code shown on the CoreS3's screen, new each start. The ESP32-S3 allows 15 connected phones at most. Some phones warn that the network has no internet: choose to stay connected. And &lt;code&gt;https://&lt;/code&gt; addresses can't be redirected: the CoreS3 can't prove it's the real site, so the browser refuses the connection before any redirect is seen. If the page doesn't pop up, browse to a plain &lt;code&gt;http://&lt;/code&gt; one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell someone starting out
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The header tells you a lot.&lt;/strong&gt; Direction, size and timing are enough to tell a camera from a phone without reading a byte of content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count airtime, not frames.&lt;/strong&gt; One slow frame can use more of a channel than a hundred fast ones, and the radio gives you enough to work it out even for frames it can't decode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frames leak into neighbouring channels.&lt;/strong&gt; Decide which channel a device belongs to, or close devices show up everywhere.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Random MAC addresses break naive counting.&lt;/strong&gt; Fingerprint what the device says it supports instead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build a fake target.&lt;/strong&gt; A Cardputer pretending to be a camera made the finder testable and turned up a real problem in the health checker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Design for forgetting.&lt;/strong&gt; Salted hashes, memory only, short windows. You get the counts without keeping anyone's data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Know your radio.&lt;/strong&gt; One antenna and 2.4 GHz only rule out a lot of devices. Say so on the screen rather than pretending.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try them
&lt;/h2&gt;

&lt;p&gt;Each app builds from its &lt;code&gt;firmware/&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pio run -t upload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To rebuild the maker tables in the phone counter and camera finder, download the IEEE list (3.9 MB) and run &lt;code&gt;tools/makers.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -L -o data/oui.csv https://standards-oui.ieee.org/oui/oui.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 tools/makers.py data/oui.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fake camera for testing is in the camera finder repo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; tools/fake-camera
pio run -t upload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;pio run -e m5stack-cores3 -t upload&lt;/code&gt; to run it on a CoreS3 instead of a Cardputer.&lt;/p&gt;

&lt;p&gt;Every app has the same serial interface at 115200: &lt;code&gt;log&lt;/code&gt; prints the numbers behind the screen, &lt;code&gt;shot&lt;/code&gt; sends a screenshot to &lt;code&gt;tools/screenshot.py&lt;/code&gt;, and &lt;code&gt;tap X Y&lt;/code&gt; touches the screen.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-waterfall" rel="noopener noreferrer"&gt;esp32-wifi-waterfall&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-health" rel="noopener noreferrer"&gt;esp32-wifi-health&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-coverage-map" rel="noopener noreferrer"&gt;esp32-wifi-coverage-map&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-phone-counter" rel="noopener noreferrer"&gt;esp32-wifi-phone-counter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-camera-finder" rel="noopener noreferrer"&gt;esp32-wifi-camera-finder&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-vote" rel="noopener noreferrer"&gt;esp32-wifi-vote&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>networking</category>
      <category>privacy</category>
      <category>security</category>
      <category>esp32</category>
    </item>
    <item>
      <title>Using Wi-Fi as a sensor on the M5Stack CoreS3</title>
      <dc:creator>Justin Mclean</dc:creator>
      <pubDate>Fri, 04 Dec 2026 04:54:32 +0000</pubDate>
      <link>https://dev.to/justinmclean/using-wi-fi-as-a-sensor-on-the-m5stack-cores3-37ba</link>
      <guid>https://dev.to/justinmclean/using-wi-fi-as-a-sensor-on-the-m5stack-cores3-37ba</guid>
      <description>&lt;p&gt;I wrote four small apps for the M5Stack CoreS3 that use its Wi-Fi radio as a sensor rather than as a network connection. One notices people moving in a room. One counts your breaths. One turns your hand into swirling colour. One measures the distance between two boards by timing radio waves. None of them use a camera, a microphone or a motion sensor (the CoreS3 SE has none of those anyway), and none of them join a network or need a password.&lt;/p&gt;

&lt;p&gt;This post walks through how each one works, with the actual code and the numbers I measured, including what didn't work. A second post covers the other half of my Wi-Fi experiments: what you can learn by only listening.&lt;/p&gt;

&lt;p&gt;All four are open source (MIT) and build with &lt;a href="https://platformio.org" rel="noopener noreferrer"&gt;PlatformIO&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-radar" rel="noopener noreferrer"&gt;esp32-wifi-radar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-breathing" rel="noopener noreferrer"&gt;esp32-wifi-breathing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-theremin" rel="noopener noreferrer"&gt;esp32-wifi-theremin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-tape-measure" rel="noopener noreferrer"&gt;esp32-wifi-tape-measure&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why a microcontroller can do this at all
&lt;/h2&gt;

&lt;p&gt;The CoreS3 is an ESP32-S3 at 240 MHz with 16 MB of flash, 8 MB of PSRAM (extra RAM on a separate chip, slower than the built-in 512 KB but much bigger) and a 320 x 240 touch screen. The interesting part is what Espressif's Wi-Fi driver exposes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Channel state information (CSI).&lt;/strong&gt; Since 802.11g, Wi-Fi uses OFDM: each 20 MHz channel is split into 64 narrow sub-carriers (sub-frequencies), and data is sent on many of them side by side. Every frame starts with a known training pattern, and the receiver measures how each sub-carrier arrived compared with how it was sent, so it can undo the distortion. The ESP32-S3 hands you those measurements: 64 complex numbers per frame, each one holding a sub-carrier's strength (amplitude) and timing shift (phase). Everything here uses the amplitude.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Raw transmit.&lt;/strong&gt; &lt;code&gt;esp_wifi_80211_tx&lt;/code&gt; sends a frame you built byte by byte.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fine timing measurement (FTM).&lt;/strong&gt; A round-trip timing protocol added to Wi-Fi in the 802.11mc revision, which turns radio timing into distance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CSI is the one that makes sensing possible. A Wi-Fi signal doesn't travel in a straight line: it bounces off walls, furniture and people, and the copies arrive at slightly different times. Each sub-carrier has a different wavelength, so each one adds up those copies differently. Move a body near the path and the pattern across the 64 sub-carriers changes. That's the whole idea behind all three CSI apps. The rest is getting a steady supply of it and deciding which changes matter.&lt;/p&gt;

&lt;p&gt;The limits, up front: 2.4 GHz only (the ESP32-S3 has no 5 GHz radio), one antenna, and 20 MHz channels for everything here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: get a steady stream of CSI without joining anything
&lt;/h2&gt;

&lt;p&gt;The obvious source is beacons, the "I'm here, this is my network" frames routers send about ten times a second. That looks like ten CSI readings a second for free.&lt;/p&gt;

&lt;p&gt;It isn't. Beacons go out in the old 802.11b format at 1 Mbit/s. 802.11b predates OFDM, so its frames have no training pattern across sub-carriers and carry no CSI. Listening to every network around me gave &lt;strong&gt;8 usable readings in 12 seconds&lt;/strong&gt;. Useless for anything.&lt;/p&gt;

&lt;p&gt;The fix was to make the router talk. A probe request is the "are you there?" frame a device sends before joining a network. If you address one to a specific router (rather than broadcasting it), the router acknowledges it straight away. That acknowledgement (ACK) is a tiny frame Wi-Fi sends for every frame addressed to one device, and it's sent in the same family of format as the frame it's acknowledging. So if the probe goes out at an OFDM rate, the ACK comes back at an OFDM rate, and OFDM frames carry CSI. No joining the network (association, in Wi-Fi terms), no password.&lt;/p&gt;

&lt;p&gt;This is the frame, built by hand in the radar's hop task (a FreeRTOS task, the ESP32's equivalent of a thread, that moves between channels):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// A probe request addressed to the router, offering only OFDM rates, so its replies&lt;/span&gt;
&lt;span class="c1"&gt;// use the modulation that carries CSI.&lt;/span&gt;
&lt;span class="nb"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x40&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                   &lt;span class="c1"&gt;// management frame, subtype probe request&lt;/span&gt;
&lt;span class="nb"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bssid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// to: the router&lt;/span&gt;
&lt;span class="nb"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// from: the CoreS3&lt;/span&gt;
&lt;span class="nb"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bssid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// BSSID: the router&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x8c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x98&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xb0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x48&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x6c&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;millis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;hopAt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;DWELL_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;esp_wifi_80211_tx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;WIFI_IF_STA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;vTaskDelay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pdMS_TO_TICKS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;POKE_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the 24-byte header, a management frame's body is a list of information elements: an ID byte, a length byte, then that many bytes of data. This body is two of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0, 0&lt;/code&gt;: an empty network name (element 0, length 0).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1, 8, ...&lt;/code&gt;: supported rates (element 1, length 8). Each byte is a rate in units of 500 kbit/s, with the top bit marking a basic rate (one every device on the network must support): 0x8c is 0x0c with the top bit set, 12 x 500 kbit/s = 6 Mbit/s; 0x12 is 9; 0x98 is 12; and so on up to 0x6c at 54. These are all OFDM rates. Leaving out 1, 2, 5.5 and 11 (the 802.11b rates) means the router has no reason to answer in 802.11b.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The transmit rate itself is fixed to 6 Mbit/s OFDM. The radio runs in station mode (&lt;code&gt;WIFI_STA&lt;/code&gt;, a client rather than an access point) without connecting to anything, and the rate has to be set while Wi-Fi is stopped:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="nc"&gt;WiFi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;WIFI_STA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;WiFi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_stop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                          &lt;span class="c1"&gt;// the raw frame rate is set while stopped&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_config_80211_tx_rate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;WIFI_IF_STA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;WIFI_PHY_RATE_6M&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then CSI is switched on. &lt;code&gt;lltf_en&lt;/code&gt; asks for CSI from the legacy long training field (LLTF), the part of the training pattern every OFDM frame has. &lt;code&gt;channel_filter_en = false&lt;/code&gt; turns off the driver's smoothing across neighbouring sub-carriers, so each one is reported as measured. Promiscuous mode, where the radio keeps every frame it hears rather than only those addressed to it, is turned on too. One setting matters more than the rest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;wifi_csi_config_t&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lltf_en&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;channel_filter_en&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dump_ack_en&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// acknowledgements carry CSI too&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_csi_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_csi_rx_cb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;onCsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_csi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;wifi_promiscuous_filter_t&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter_mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;WIFI_PROMIS_FILTER_MASK_ALL&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_promiscuous_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;esp_wifi_set_promiscuous&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without &lt;code&gt;dump_ack_en&lt;/code&gt; the ACKs never reach the CSI callback, and you're back to 8 readings in 12 seconds.&lt;/p&gt;

&lt;p&gt;The callback turns each frame's 64 complex values into amplitudes and scales them so they always add up to the same total (normalising). The overall level moves around for reasons that have nothing to do with the room: the router's transmit power, and the receiver's automatic gain control, which turns its own amplification up and down to suit the signal. Normalising removes that and keeps only the shape across the sub-carriers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onCsi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wifi_csi_info_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;SUB&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;millis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;hopAt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;SETTLE_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// Acknowledgements carry no sender address; anything else must come from the router.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;memcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;memcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bssid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;rx_ctrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rssi&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// garbled frames&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;amp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;SUB&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;SUB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;amp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sqrtf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;SUB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;amp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="no"&gt;SUB&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter. An ACK has no sender address field, so the CSI callback sees all zeros for its MAC (hardware) address. That's why zero is accepted alongside the router's BSSID (the router's own address). And anything in the first 15 ms after a channel switch (&lt;code&gt;SETTLE_MS&lt;/code&gt;) is ignored, along with frames weaker than -80 dBm (signal strength in decibels relative to a milliwatt: -40 is very close, -90 is barely there).&lt;/p&gt;

&lt;p&gt;With a probe every 15 ms, each router gives about &lt;strong&gt;30 to 60 readings a second&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Only do this to routers you own.&lt;/strong&gt; The cost is small: about 20 to 35 tiny frames a second to each router, a few kilobytes a second and well under 1% of the channel's airtime (the share of time the channel is busy carrying frames). That's my estimate from frame sizes and rates, not a measurement. Being poked does keep a phone's Wi-Fi awake, so a hotspot's battery drains faster than when idle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: the radar
&lt;/h2&gt;

&lt;p&gt;The radar uses up to four links at once: in my testing two routers and a phone hotspot. It hops between them, 200 ms on each one's channel, so each router gives a separate path across the room.&lt;/p&gt;

&lt;p&gt;The movement score for a path is how much each sub-carrier's amplitude has varied over the last 2 seconds, relative to its level (the coefficient of variation), averaged across the sub-carriers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;SUB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;mean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;mean&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;mean&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mean&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.2f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// empty and guard sub-carriers&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;tot&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nf"&gt;sqrtf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;used&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;tot&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;used&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not all 64 sub-carriers carry anything. The ones at each edge are left empty as guard bands, so the channel doesn't spill into its neighbours. The one in the middle (the DC sub-carrier, at the channel's centre frequency) is left empty because receivers can't measure it cleanly. Their amplitude sits near zero, so their "variation" is pure noise. Skipping anything with a mean under 0.2 (after normalisation, where the average is 1) drops them.&lt;/p&gt;

&lt;p&gt;Each path learns its own still level (its baseline) during a 3-second calibration, then keeps adapting slowly while nothing is happening: each quiet reading moves the baseline 0.5% of the way towards it, 10 times a second. The threshold is half as much again as the baseline, and at least 1.5 above it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;baseline&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fmaxf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.5f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.5f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;baseline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastOver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;millis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;baseline&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mf"&gt;0.005f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;baseline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The baseline only moves when the path is quiet, so someone standing in the room for a long time doesn't slowly become "normal". The screen says &lt;strong&gt;I see you!&lt;/strong&gt; for 1.5 seconds after any path goes over.&lt;/p&gt;

&lt;h3&gt;
  
  
  How well it works
&lt;/h3&gt;

&lt;p&gt;First tests at home on a CoreS3 SE, with two routers and a phone hotspot:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Movement score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Nothing moving&lt;/td&gt;
&lt;td&gt;about 3, steady&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hand waved between the CoreS3 and a phone half a metre away&lt;/td&gt;
&lt;td&gt;10 to 20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Standing still, hotspot path&lt;/td&gt;
&lt;td&gt;about 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Walking around the room, hotspot path&lt;/td&gt;
&lt;td&gt;peaks over 40&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The big lesson: &lt;strong&gt;where you move matters more than anything else.&lt;/strong&gt; Movement across the line between the CoreS3 and a router shows clearly. Off to the side it barely registers. One path covers a surprisingly thin slice of the room, which is why the radar uses several. Spread the routers and hotspot around the room and you get much better coverage.&lt;/p&gt;

&lt;p&gt;It also has no idea who is moving. In a busy room every person and phone disturbs the paths, so it sees movement most of the time. It's a "someone is here" sensor for an empty room, not a people counter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: breathing
&lt;/h2&gt;

&lt;p&gt;Breathing is the same signal with a very different problem. A chest rising and falling a centimetre or so is a much smaller change than a person walking past, and it's slow: 8 to 30 breaths a minute is 0.13 to 0.5 Hz.&lt;/p&gt;

&lt;p&gt;The breathing app pokes a single router every 20 ms, then averages whatever CSI arrives into 10 readings a second. It keeps 30 seconds of those (300 rows of 64 values, in PSRAM) and analyses them again once a second.&lt;/p&gt;

&lt;h3&gt;
  
  
  Keep only breathing speeds
&lt;/h3&gt;

&lt;p&gt;Each sub-carrier's 30 seconds is turned into change from its own average, then band-passed: everything slower or faster than breathing is removed. That's done with a fast Fourier transform (FFT) from Espressif's esp-dsp library. An FFT turns a signal over time into how much of each frequency it contains (its spectrum), split into frequency bins. So: transform, zero every bin outside 0.13 to 0.5 Hz, transform back. Along the way it adds up the power (the squared size of each bin) above 1 Hz and compares it with the power in the breathing band. Breathing is slow; fidgeting and moving are fast. So a lot of power above 1 Hz is the "you're moving" signal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;bandPass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;dsps_fft2r_fc32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;dsps_bit_rev_fc32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lroundf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;LOW_HZ&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="no"&gt;RATE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lroundf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;HIGH_HZ&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="no"&gt;RATE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lroundf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="no"&gt;RATE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;band&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// the matching positive frequency&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;band&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;quick&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// inverse = conjugate, forward, conjugate&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;dsps_fft2r_fc32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;dsps_bit_rev_fc32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fftBuf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="no"&gt;NFFT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;band&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;quick&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;band&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The FFT in esp-dsp (radix-2, meaning it works on lengths that are a power of two) only goes forward, so the inverse is done with the forward one. Flip the sign of the imaginary part of every bin (take the complex conjugate), run the forward FFT again, and divide by the length. The inverse and forward transforms differ only in the sign of the exponent, and conjugating before and after swaps that sign. Strictly the result should be conjugated again at the end, but that only flips the imaginary part, and only the real part is kept. That's the &lt;code&gt;fftBuf[2 * b + 1] = -fftBuf[2 * b + 1]&lt;/code&gt; line and the &lt;code&gt;/ NFFT&lt;/code&gt;. Because of the power-of-two rule, the 300 samples are followed by 212 zeros to make 512 (zero-padding). That doesn't add information, it just gives finer frequency bins: 10 / 512, about 0.02 Hz or 1.2 breaths a minute each.&lt;/p&gt;

&lt;h3&gt;
  
  
  Count breaths, don't look for a frequency
&lt;/h3&gt;

&lt;p&gt;The obvious approach is to take the biggest bin in the spectrum as the rate. That assumes one steady rhythm, and real breathing speeds up, slows down and has the odd sigh, so over 30 seconds the peak smears out.&lt;/p&gt;

&lt;p&gt;So instead it counts breaths in the filtered wave. A breath is a rise above +0.3 standard deviations (a measure of how far the wave typically swings from zero), then a fall below -0.3, with at least 1.7 seconds between breaths (the &lt;code&gt;17&lt;/code&gt;: 17 readings at 10 a second). That caps it at about 35 a minute, just above the band. The rate comes from the time between the first and last breath, not from how many fit in 30 seconds, so a breath cut off at either end of the window doesn't skew it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;breathRate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;sd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;sd&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;sd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sqrtf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;thr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.3f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;thr&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;thr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;60.0f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;RATE&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Needing a fall below the lower line before the next rise above the upper one counts (hysteresis) stops small wobbles around zero from counting as several breaths.&lt;/p&gt;

&lt;h3&gt;
  
  
  Only answer when the sub-carriers agree
&lt;/h3&gt;

&lt;p&gt;Every usable sub-carrier gives its own count. Some of them happen to sit where your chest makes a big difference and some don't. So the answer is the median across sub-carriers, and it's only shown when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;at least 8 sub-carriers found 3 or more breaths;&lt;/li&gt;
&lt;li&gt;half the spread of the middle half of their answers (half the interquartile range) is 3 breaths a minute or less;&lt;/li&gt;
&lt;li&gt;the power above 1 Hz is less than 1.5 times the power in the breathing band.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fail the first two and it shows "--". Fail the last and it shows "Too much movement". It says nothing rather than invent a number.&lt;/p&gt;

&lt;p&gt;The wave drawn on the screen is the filtered signal of the sub-carrier whose count was closest to the median, which makes a nice check by eye: you can see your own breaths go past.&lt;/p&gt;

&lt;h3&gt;
  
  
  How well it works
&lt;/h3&gt;

&lt;p&gt;In a counted test it read &lt;strong&gt;within one or two breaths a minute&lt;/strong&gt; of the real rate. It needs you sitting still, half a metre to a metre away, with your chest roughly between the CoreS3 and the router, and it takes 30 seconds to show its first number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: the theremin
&lt;/h2&gt;

&lt;p&gt;The theremin uses the same signal for something to play with rather than measure. The screen fills with swirling plasma, and moving your hand near the Wi-Fi link to the router changes how fast it swirls and which colours it shows. Like the instrument it's named after, you play it without touching anything.&lt;/p&gt;

&lt;p&gt;It pokes the router every 10 ms, smooths the 64 amplitudes into a profile, and compares that with a picture of the empty room taken at calibration. The profile is an average that moves 15% of the way towards each new reading, 25 times a second, so it smooths over about a quarter of a second. Two numbers come out of the comparison:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How much it differs.&lt;/strong&gt; For each sub-carrier, the relative change from the empty room (&lt;code&gt;profile / empty - 1&lt;/code&gt;); then the root mean square of those (square them, average, take the square root), a single size for the whole change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Which way.&lt;/strong&gt; Lay the changes out across the sub-carriers and fit one slow wave to them, one cycle from the first sub-carrier to the last (the first Fourier component). Where that wave peaks says which part of the band got stronger and which weaker. That position is an angle, and the angle becomes a place on the colour wheel:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;angle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;SUB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;cosf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;52&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;sn&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;sinf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;52&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;angle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;angle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atan2f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sqrtf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only the sub-carriers with real signal are used (the guard and DC ones are dropped at calibration), which leaves about 52, the number that carry data in a 20 MHz channel. So one full turn of the colour wheel spans the used band.&lt;/p&gt;

&lt;p&gt;Calibration takes 5 seconds: 3 seconds to learn the empty pattern, then 2 seconds measuring how much the empty room's own pattern wobbles against it. That wobble is kept as a margin: its average plus 3 standard deviations, and never less than 0.03. In testing a hand changed the pattern by about 0.06 to 0.4, against 0.01 to 0.05 for the empty room, so there's a clear gap. Anything below that margin counts as nothing. Above it, the difference becomes &lt;code&gt;size&lt;/code&gt;, from 0 to 1 (full at 9 times the margin), which sets how lively the plasma is: how fast it swirls (&lt;code&gt;0.6 + 9 * size&lt;/code&gt;) and how far round the colour wheel the palette spreads (&lt;code&gt;0.12 + 0.75 * size&lt;/code&gt;). The angle sets the colours, and each frame the colour moves only 15% of the way to the new angle (&lt;code&gt;hue += 0.15 * dh&lt;/code&gt;) so it doesn't flicker.&lt;/p&gt;

&lt;p&gt;Hold your hand still and the colours hold. Move it and they shift. You can learn to aim for a colour, but it takes practice, because each position's colour depends on how the signal happens to bounce around that particular room.&lt;/p&gt;

&lt;p&gt;The limit is drift. The empty picture is fixed on purpose, so a hand held still keeps its colours for as long as it's there. The cost is that the room's own pattern drifts slowly, and after a while the screen gets busy with nobody there. The fix is a &lt;strong&gt;Recalibrate&lt;/strong&gt; button that works like taring a kitchen scale: hands away, tap, wait 5 seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: a tape measure from radio timing
&lt;/h2&gt;

&lt;p&gt;The tape measure doesn't use CSI. It uses Wi-Fi fine timing measurement (FTM), which the ESP32-S3 supports at both ends. One side (the initiator) sends a burst of timing frames, the other (the responder) answers each one, and both note the time each frame left and arrived. Subtract the time the responder spent before answering, and what's left is the time there and back. Radio waves travel at the speed of light, about 30 cm a nanosecond, so half the round trip gives the distance.&lt;/p&gt;

&lt;p&gt;Most home routers don't answer FTM, so the far end is a second ESP32-S3 (another CoreS3, a Cardputer or anything with an S3). It runs a soft access point (a Wi-Fi network hosted by the ESP32 itself, rather than by a router) with the FTM responder turned on. Nobody joins it; timing works without joining, and the password is random each start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"abcdefghjkmnpqrstuvwxyz23456789"&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;esp_random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;WiFi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;WIFI_AP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;WiFi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;softAP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;CHANNEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// last argument: answer FTM&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CoreS3 finds "wifi-tape" on channel 11 and asks for bursts of 16 exchanges, with the burst period set to 2 (in units of 100 ms, so 200 ms):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="nc"&gt;WiFi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;onFtm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ARDUINO_EVENT_WIFI_FTM_REPORT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;WiFi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initiateFTM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;farChannel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;farMac&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a burst is done, the report event gives a distance estimate in centimetres. Single readings jump around, so it takes about 3 readings a second and shows the median (the middle value) of the last 9, which ignores the odd wild one.&lt;/p&gt;

&lt;p&gt;The first thing I noticed: with the two boards side by side it read about &lt;strong&gt;0.4 m&lt;/strong&gt;. That's the radios' own processing delay showing up as distance. So there's a &lt;strong&gt;Zero&lt;/strong&gt; button: put the two together, tap it, and that offset is stored in flash and taken off every reading. &lt;strong&gt;Reset&lt;/strong&gt; clears it.&lt;/p&gt;

&lt;p&gt;It's a rough tape measure, not a precise one. But it is measuring distance by timing radio waves, on two boards you can hold in one hand.&lt;/p&gt;

&lt;p&gt;One practical tip: power the far end from a battery or power bank, not a computer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing on the device without touching it
&lt;/h2&gt;

&lt;p&gt;Every one of these apps has the same small serial interface at 115200 baud:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;log&lt;/code&gt; prints the numbers behind the screen (off by default, so nothing piles up when no computer is reading);&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shot&lt;/code&gt; sends the screen as raw pixel rows (RGB565, the display's 16 bits a pixel), which &lt;code&gt;tools/screenshot.py&lt;/code&gt; turns into a PNG;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tap X Y&lt;/code&gt; touches the screen at that point.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The radar also has &lt;code&gt;test&lt;/code&gt;, which runs a cued test on the screen: get ready, then keep still and walk around in turn, 10 seconds each, while logging every 100 ms. It makes before and after comparisons repeatable. The breathing app has &lt;code&gt;stream&lt;/code&gt;, which prints every row of 64 values ten times a second for analysis on a laptop.&lt;/p&gt;

&lt;p&gt;I wrote these with an AI coding assistant, and this interface is what made that work well. It found that the ESP32-S3 exposes CSI, worked out the probe request trick, and could flash a build, tap through the screens, read the logs and look at screenshots without me pressing anything. The hardware loop got a lot shorter once the device could be driven from the command line.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell someone starting out
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check you're getting CSI at a usable rate before you write any analysis.&lt;/strong&gt; Count readings per second first. Mine was 0.7 a second until I made the router talk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Normalise each frame.&lt;/strong&gt; The overall level jumps around for reasons that have nothing to do with the room.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Throw away the guard and DC sub-carriers.&lt;/strong&gt; They're noise dressed up as signal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Position matters more than the algorithm.&lt;/strong&gt; Movement off the line between the two radios barely registers. Use more than one path if you can.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Let it say "I don't know".&lt;/strong&gt; The breathing app only shows a number when the sub-carriers agree, and says so when they don't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build a serial test interface on day one.&lt;/strong&gt; Cued tests and screenshots over USB save hours.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Only poke routers you own.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try them
&lt;/h2&gt;

&lt;p&gt;Each app builds from its &lt;code&gt;firmware/&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pio run -t upload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tape measure has two builds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pio run -e m5stack-cores3 -t upload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pio run -e far-end -t upload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;-e far-end-cardputer&lt;/code&gt; for a Cardputer as the far end.&lt;/p&gt;

&lt;p&gt;For the radar, breathing app and theremin, the first start lists the 2.4 GHz networks in range: tap your own router or hotspot. The choice is kept in flash.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-radar" rel="noopener noreferrer"&gt;esp32-wifi-radar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-breathing" rel="noopener noreferrer"&gt;esp32-wifi-breathing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-theremin" rel="noopener noreferrer"&gt;esp32-wifi-theremin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-wifi-tape-measure" rel="noopener noreferrer"&gt;esp32-wifi-tape-measure&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>esp32</category>
      <category>wifi</category>
      <category>iot</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Training a tiny model for the M5Stack CoreS3</title>
      <dc:creator>Justin Mclean</dc:creator>
      <pubDate>Thu, 03 Dec 2026 02:08:04 +0000</pubDate>
      <link>https://dev.to/justinmclean/training-a-tiny-model-for-the-m5stack-cores3-4849</link>
      <guid>https://dev.to/justinmclean/training-a-tiny-model-for-the-m5stack-cores3-4849</guid>
      <description>&lt;p&gt;Over the last few weeks I've built five small generative AI apps that run entirely on an M5Stack CoreS3: a recipe writer, a pixel-art sprite maker, a card game inventor, a cocktail maker and a Shakespeare scene writer that types into your computer as a USB keyboard. No cloud, no phone, no Wi-Fi. Each has its own model, from 82 KB to 4 MB, running on the ESP32-S3.&lt;/p&gt;

&lt;p&gt;This article walks through how to train one, using Recipe Maker as the worked example, then gives the exact commands to retrain it yourself and to train a model on a subject of your own. You pick a main ingredient and a kind of dish on the touchscreen, and about 25 seconds later you have a recipe nobody has written before. Everything here is in the repo, &lt;a href="https://github.com/justinmclean/esp32-recipe-maker" rel="noopener noreferrer"&gt;esp32-recipe-maker&lt;/a&gt;, and the model is on Hugging Face at &lt;a href="https://huggingface.co/jmclean/esp32-recipe-maker" rel="noopener noreferrer"&gt;jmclean/esp32-recipe-maker&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this fits on a microcontroller at all
&lt;/h2&gt;

&lt;p&gt;The CoreS3 has an ESP32-S3 at 240 MHz, 16 MB of flash (slow storage that keeps its contents with the power off) and 8 MB of PSRAM (faster working memory). That is not much for a language model.&lt;/p&gt;

&lt;p&gt;A model is a large set of numbers, its parameters (or weights), learnt in training. Writing text means doing arithmetic with all of them for every word piece it writes. &lt;a href="https://github.com/slvDev/esp32-ai" rel="noopener noreferrer"&gt;slvDev/esp32-ai&lt;/a&gt; has a clever layout for a small chip: most of the parameters are lookup tables, called embeddings, where the model only reads the few rows it needs for the current word piece. Those can stay in flash and be read in place. Only a small core, the transformer that does the real computing, needs the full arithmetic on every step, and that sits in PSRAM.&lt;/p&gt;

&lt;p&gt;The recipe model has 8.2 million parameters: 6.3 million in lookup tables for each of its layers (per-layer embeddings), 0.8 million in the table of word pieces (the token embedding), and 1.1 million in the core (6 layers, each 96 numbers wide). Each parameter is stored in 4 bits rather than the usual 32, so the whole model is 4.2 MB, and the core is expanded to 8 bits in PSRAM to run. It writes 6 to 7 tokens a second.&lt;/p&gt;

&lt;p&gt;A token is the unit the model reads and writes: a common word, part of a longer word, or a piece of punctuation. "pumpkin" is one token; a rarer word might be two or three.&lt;/p&gt;

&lt;p&gt;I didn't change the architecture or the inference code. All my work went into the data, the training options and the checking. That turned out to be the right split.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: decide what the model writes, and fix its layout
&lt;/h2&gt;

&lt;p&gt;A tiny model can't follow instructions, but it can learn a shape very reliably. So the first job is to design a fixed text layout that carries both the user's choices and the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Main: pumpkin
Dish: soup
Title: Roast Pumpkin and Ginger Soup
Ingredients:
- 1 kg pumpkin, peeled and cubed
- 1 onion, chopped
Method:
1. Roast the pumpkin at 200C for 25 minutes.
2. ...
&amp;lt;|endoftext|&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The device sends everything up to &lt;code&gt;Title:&lt;/code&gt; and the model writes the rest. The picks go first so the model learns to follow them. &lt;code&gt;&amp;lt;|endoftext|&amp;gt;&lt;/code&gt; tells the app when to stop.&lt;/p&gt;

&lt;p&gt;Design this around the screen, not the model. The touch picker means no keyboard. The model can only see 256 tokens at a time (its context), prompt included, so recipes have to be short, which suits a 320 x 240 display anyway.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: find the data, and read the licence
&lt;/h2&gt;

&lt;p&gt;The model only sees about 20 to 80 million tokens in training, so you don't need a huge dataset, but you do need enough examples for every combination of picks.&lt;/p&gt;

&lt;p&gt;There are two ways to get it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use an open dataset.&lt;/strong&gt; I used RecipeNLG, about 2.2 million recipes. Its terms allow non-commercial research and educational use only, so the trained model carries the same restriction (CC BY-NC 4.0). The official site was down, so I used the byte-identical copy on Hugging Face.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generate it with a large model.&lt;/strong&gt; You get exactly the layout and coverage you want, but check the provider's terms. Several, Anthropic's included, restrict using outputs to train other models.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's a third way, which Card Maker uses: write a program that makes the data. Its engine plays random rule sets against itself and keeps the good ones. No licence questions at all.&lt;/p&gt;

&lt;p&gt;Whatever you choose, convert or generate 200 first and read 30 by hand before doing the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: clean and convert (this is most of the work)
&lt;/h2&gt;

&lt;p&gt;RecipeNLG is American. I wanted Australian recipes: metric units and standard can sizes, Celsius, Australian names (capsicum, coriander, spring onions, thickened cream) and Australian spelling. The converter, &lt;code&gt;training/convert_recipenlg.py&lt;/code&gt;, uses only the standard library and does all of that, and also files each recipe under one of 14 mains and 12 kinds of dish by keyword matching.&lt;/p&gt;

&lt;p&gt;Most of the data didn't survive:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dropped because&lt;/th&gt;
&lt;th&gt;Share&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;From cookbooks.com or kraftrecipes.com&lt;/td&gt;
&lt;td&gt;42%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No main ingredient from the picker&lt;/td&gt;
&lt;td&gt;29%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Too long for the 256-token window&lt;/td&gt;
&lt;td&gt;9%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No proper method, or more than 10 steps&lt;/td&gt;
&lt;td&gt;6%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Built on US convenience products (cake mixes, cream soups in salads)&lt;/td&gt;
&lt;td&gt;5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Other checks (all caps, footnotes, adverts, huge batches)&lt;/td&gt;
&lt;td&gt;1%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Kept&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;9%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That still left 191,930 recipes, about 35 million training tokens.&lt;/p&gt;

&lt;p&gt;Three habits made this manageable:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A test case for every fix.&lt;/strong&gt; Each time I found a bad recipe in the output (a brand name, crock-pot instructions, a Fahrenheit temperature), I fixed the converter and added a case to &lt;code&gt;test_converter.py&lt;/code&gt;. The build script runs the tests first and stops if anything regresses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An audit, not just filters.&lt;/strong&gt; &lt;code&gt;audit_data.py&lt;/code&gt; lists anything that still looks American, for a person to read. It runs on the CPU while the GPU trains, so it never holds anything up.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A count of what should be gone.&lt;/strong&gt; &lt;code&gt;check_data.py&lt;/code&gt; counts anything the filters should have removed. It should be zero.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 4: check coverage for every pick
&lt;/h2&gt;

&lt;p&gt;A converter run also writes a table of how many recipes there are for every main and dish pair. Pairs with fewer than 50 are too thin to learn from: the model ignores the pick or copies one recipe. The app greys those out. Chocolate soup has no recipes, which is probably for the best.&lt;/p&gt;

&lt;p&gt;Generate the app's picker from this table (&lt;code&gt;tools/make_picks_h.py&lt;/code&gt;) so the device and the data can't drift apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: make the tokeniser and the training files
&lt;/h2&gt;

&lt;p&gt;The model works on numbers, not letters, so the text has to be cut into tokens and each token given a number. The tokeniser does that. It is learnt from the data itself: byte-level BPE (byte pair encoding) starts from single characters and keeps merging the pairs that appear together most often, so frequent words like "simmer" end up as a single token and rare ones are built from pieces.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;prepare_recipes.py&lt;/code&gt; learns the tokeniser, turns every recipe into token numbers, and splits them into two files: &lt;code&gt;train.bin&lt;/code&gt;, which the model learns from, and &lt;code&gt;val.bin&lt;/code&gt;, a small validation set of recipes it never trains on. Checking the model on recipes it has never seen tells you whether it has learnt how recipes go in general, or has just memorised the ones it was shown. A few things matter here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A small vocabulary.&lt;/strong&gt; The vocabulary is the list of tokens the tokeniser can use. Recipe Maker has 8,192, where the TinyStories model these tools were built for has 32,768. A narrow subject needs fewer, and the token tables, which have a row for every token, shrink to a quarter. Check that common words (&lt;code&gt;pumpkin&lt;/code&gt;, &lt;code&gt;garlic&lt;/code&gt;, &lt;code&gt;simmer&lt;/code&gt;) come out as one or two tokens. The cocktail model went further, with 2,048 tokens learnt from the drinks, so Chartreuse and Angostura are one token each.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shuffle first.&lt;/strong&gt; RecipeNLG is grouped by website. Take the last half a percent of it for validation without shuffling and every validation recipe comes from one site, so the check only tells you how well the model does on that site's style.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hold out validation before repeating anything.&lt;/strong&gt; I wanted more Australian style than the data had, so the 5,496 recipes from Australian websites are copied three times into training. Repeating good examples like this (oversampling) makes the model weight them more. The order matters: pick the validation recipes first, then make the copies, from what is left. Do it the other way round and some of those copies end up in validation, so the model is checked on recipes it has already trained on. It does well on those because it has seen them, and the validation score looks better than the model really is.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the tokeniser fixed once the firmware uses it.&lt;/strong&gt; The device turns token numbers back into text with a table built from the tokeniser (&lt;code&gt;vocab.h&lt;/code&gt;). If a data rebuild learnt a new tokeniser, the numbers would mean different words and the device would print nonsense. Later rebuilds reuse the published tokeniser so the numbers still match.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 6: train
&lt;/h2&gt;

&lt;p&gt;Training runs on a Mac with PyTorch's &lt;code&gt;mps&lt;/code&gt; backend. The final model took about 80 minutes on an M3 MacBook; this size of model needs well under 4 GB.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;ESP32AI_DATA&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;data/recipes &lt;span class="nv"&gt;PYTHONPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;src .venv/bin/python research/tinystories/train.py \
    --arm ple --vocab 8192 --d-model 96 --n-layers 6 --ple-dim 128 \
    --target-core 1100000 --batch-size 16 --seq-len 256 --steps 20000 \
    --eval-every 1000 --tag recipes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What the settings mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--arm ple&lt;/code&gt;: the per-layer-embedding layout described above.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--d-model 96 --n-layers 6 --ple-dim 128 --target-core 1100000&lt;/code&gt;: the model's size. Each layer is 96 numbers wide, there are 6 layers, each layer's lookup table is 128 wide, and the core is sized to about 1.1 million parameters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--vocab 8192&lt;/code&gt;: must match the tokeniser.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--batch-size 16 --seq-len 256&lt;/code&gt;: each training step looks at 16 pieces of text, 256 tokens each, about 4,000 tokens.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--steps 20000&lt;/code&gt;: how many training steps. 20,000 steps of 4,000 tokens is 82 million tokens, so the model sees the 35 million tokens of recipes a bit over twice.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--eval-every 1000&lt;/code&gt;: check the validation set every 1,000 steps.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each step, the model guesses every next token in the batch, the trainer measures how wrong the guesses were (the loss), and it nudges every parameter slightly in the direction that makes the guesses better. Training is that, 20,000 times.&lt;/p&gt;

&lt;p&gt;Start with a 500-step run to time it. Mine took 84 seconds, so 5,000 steps was about 12 minutes: short enough to try an idea and look at the result in the same sitting.&lt;/p&gt;

&lt;p&gt;Run times grew as the model did. All of these are on the same M3 MacBook:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Runs&lt;/th&gt;
&lt;th&gt;Steps&lt;/th&gt;
&lt;th&gt;Time each&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First timing run&lt;/td&gt;
&lt;td&gt;500&lt;/td&gt;
&lt;td&gt;84 seconds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Early runs, trying data changes&lt;/td&gt;
&lt;td&gt;5,000&lt;/td&gt;
&lt;td&gt;11 to 13 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;With the penalties, then the bigger core&lt;/td&gt;
&lt;td&gt;10,000&lt;/td&gt;
&lt;td&gt;23 to 39 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Final model&lt;/td&gt;
&lt;td&gt;20,000&lt;/td&gt;
&lt;td&gt;81 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The penalties added a few minutes a run, and doubling the core took a 10,000-step run to 39 minutes. Most of the iterating happened at 10,000 steps, where one idea took about half an hour to test.&lt;/p&gt;

&lt;p&gt;One practical warning: don't run two GPU training jobs at once. I tried two and it crashed the Mac. The queue scripts now wait for the GPU to be free.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: measure what you care about, not the loss
&lt;/h2&gt;

&lt;p&gt;The trainer reports a number called the loss: roughly, how surprised the model is by the real next token, averaged over the text. Lower is better. The validation loss is the same measure on the held-out validation recipes, so it shows how well the model does on recipes it hasn't seen.&lt;/p&gt;

&lt;p&gt;Validation loss is a poor guide once the data changes. My first full run had a validation loss of 1.81. The final model, which is much better, has 1.965. In between the tokeniser changed, the data got cleaner and harder to predict, and extra penalties were added, so the numbers across versions aren't comparable.&lt;/p&gt;

&lt;p&gt;Loss is still useful within one dataset. For Bard Lite it decided when to stop training (see below).&lt;/p&gt;

&lt;p&gt;What I needed to know was whether the recipes were right, so I wrote a test for that. &lt;code&gt;pick_test.py&lt;/code&gt; writes 20 recipes for each of 13 picks covering every kind of dish, at the device's temperature, and counts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Layout:&lt;/strong&gt; a title, an ingredient list and a numbered method, ending in time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Main in the list:&lt;/strong&gt; the main ingredient picked is in the ingredient list, not just the title.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method strays:&lt;/strong&gt; a method step uses an ingredient the list doesn't have ("add the leeks", with no leeks).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeats:&lt;/strong&gt; an ingredient listed twice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It runs at the device's temperature of 0.7, because temperature changes the results a lot (see What temperature does, in the section on training your own model).&lt;/p&gt;

&lt;p&gt;Each new model was tested against the one on the device, side by side. Every decision after this point came from that test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: iterate on what the test finds
&lt;/h2&gt;

&lt;p&gt;I trained 17 recipe models in under a day. The changes that helped, in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sort ingredient lists A to Z and drop repeated lines.&lt;/strong&gt; Real recipes list "salt" twice more often than you'd think, and the model copied it. A sorted list makes a repeat easy to avoid, because the model only has to check the line before.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unlikelihood penalties.&lt;/strong&gt; Normal training only teaches the model to copy what the data does. It can't tell the model that listing an ingredient twice is wrong; it just sees that it happens sometimes. An unlikelihood penalty adds a second lesson: at each point in a recipe, it also looks at how likely the model thinks a known-bad next token is, and pushes that down. I added three, in &lt;code&gt;esp32-ai.patch&lt;/code&gt;: one against an ingredient line using ingredient words already listed above it (units and numbers are allowed, as real lists repeat those), one against a method step naming an ingredient that isn't in the list, and one against repeating a word within a list inside a method step ("salt, pepper and salt"). Each has a weight, how hard it pushes compared with normal training; the final weights are 3, 3 and 1.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A bigger core.&lt;/strong&gt; Doubling the transformer core from 560,000 to 1.1 million parameters. The device still runs it at 6 to 7 tokens a second.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Train longer.&lt;/strong&gt; 20,000 steps instead of 5,000 to 10,000.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;An early 5,000-step model put the main ingredient in the list 140 times in 160. The final model:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Recipes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Layout right&lt;/td&gt;
&lt;td&gt;252 of 260&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Main ingredient in the list&lt;/td&gt;
&lt;td&gt;240 of 260 (92%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Method uses an ingredient the list lacks&lt;/td&gt;
&lt;td&gt;13 of 260 (5%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ingredient listed twice&lt;/td&gt;
&lt;td&gt;13 of 260 (5%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For comparison, 10.7% of real recipes have a method step naming something not in the list, by the same count. The weakest pick is mushroom salad, where the list often leaves out the mushrooms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: put it on the device
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Export&lt;/strong&gt; the trained model (the checkpoint, saved with full 32-bit numbers) to &lt;code&gt;model.bin&lt;/code&gt;, esp32-ai's device format. This rounds every parameter to 4 bits (quantisation), so the model is an eighth of the size. Rounding costs a little accuracy; testing the exported model, as below, shows whether that matters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regenerate &lt;code&gt;vocab.h&lt;/code&gt;&lt;/strong&gt;, the table the app uses to turn token numbers back into text, from the tokeniser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flash the model&lt;/strong&gt; into the &lt;code&gt;model&lt;/code&gt; partition, then the app. The flash is divided into partitions, fixed areas for the app, settings and data. The model goes in once (it's 4 MB, so it's slow to write); after that you can reflash the app as often as you like without touching it:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pio run -e m5stack-cores3 -t upload_model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pio run -e m5stack-cores3 -t upload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The apps share one partition layout, and each model starts at its own address (offset) in the &lt;code&gt;model&lt;/code&gt; partition: the recipe model at 0x190000, Bard Lite's at 0xC00000. Flashing one model doesn't overwrite another, so several can be on one CoreS3 at once.&lt;/p&gt;

&lt;p&gt;Before flashing anything, I test with &lt;code&gt;host/recipe.c&lt;/code&gt;. It loads the same &lt;code&gt;model.bin&lt;/code&gt; and runs the same 8-bit arithmetic and sampling as the device, and only needs a C compiler. That is also how the pick test's numbers can be trusted to match what the device writes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 10: publish it properly
&lt;/h2&gt;

&lt;p&gt;The repo has the device model, the full-precision weights for fine-tuning, the scripts to rebuild the data and retrain from scratch, and a model card. The model licence follows the data: RecipeNLG is non-commercial, so the model is CC BY-NC 4.0, while the code is MIT. The card also says plainly that the recipes are made up by a tiny model, so check quantities, cooking times and temperatures before you cook one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Follow along: retrain Recipe Maker
&lt;/h2&gt;

&lt;p&gt;Everything below was run on fresh clones on an M3 MacBook Pro (8 cores), and the times are from that run.&lt;/p&gt;

&lt;p&gt;You need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an Apple Silicon Mac, or Linux with a CUDA GPU (a CPU works, but slowly);&lt;/li&gt;
&lt;li&gt;git, a C compiler, &lt;a href="https://docs.astral.sh/uv/" rel="noopener noreferrer"&gt;uv&lt;/a&gt; and the Hugging Face CLI (&lt;code&gt;hf&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;about 3 GB of free disk for the dataset, the training files and PyTorch;&lt;/li&gt;
&lt;li&gt;for the device steps, &lt;a href="https://platformio.org" rel="noopener noreferrer"&gt;PlatformIO&lt;/a&gt; and an M5Stack CoreS3. Any ESP32-S3 board with 8 MB of flash and PSRAM can use the repo's &lt;code&gt;esp32s3-headless&lt;/code&gt; build instead, over serial.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Get the code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/slvDev/esp32-ai.git ~/esp32-ai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/justinmclean/esp32-recipe-maker.git ~/esp32-recipe-maker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/esp32-ai &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; git checkout 52ebcbb &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; git apply ~/esp32-recipe-maker/training/esp32-ai.patch &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; uv sync
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The patch adds the training options this needs: another data folder, the three penalties, starting from saved weights and CPU training. &lt;code&gt;git status&lt;/code&gt; should show only &lt;code&gt;research/tinystories/train.py&lt;/code&gt; changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Build the training data
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/esp32-recipe-maker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;hf download SandhyaKilari/RecipeNLG_dataset RecipeNLG_dataset.csv --repo-type dataset --local-dir data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;training/prepare_data.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The download is 2.2 GB. The build took 8.5 minutes, using every core. It runs the converter's tests first, converts and filters 2.2 million recipes, sorts the ingredient lists, tokenises everything with the published tokeniser and then counts anything that should have been removed. The end of the output should look like this, with every count at zero except the cream soups:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;train 34,903,013 tokens / val 165,909 tokens
191,924 recipes
  double numbered steps: 0
  brand marks: 0
  Fahrenheit: 0
  ...
  cream soups (allowed): 6637, in salads (should be 0): 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Check the pipeline with a short run
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;STEPS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;500 training/train.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This trains for 500 steps (2 minutes), exports the model to &lt;code&gt;firmware/model/model.bin&lt;/code&gt; and runs the pick test (2 more minutes). It overwrites the published model in your clone: &lt;code&gt;git checkout firmware/model&lt;/code&gt; puts it back.&lt;/p&gt;

&lt;p&gt;Don't expect a usable model from 500 steps. Mine put the main ingredient in the list 15 times in 260 and listed an ingredient twice in 178. Run it on the computer to see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cc -O2 -o recipe host/recipe.c -lm &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; ./recipe pumpkin soup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I asked for pumpkin soup and got "Pork Chops With Mushroom and Garlic And Pumpkin", with three cups of rice wine, half a cup of mayonnaise, water listed three times and a method that ends in the microwave. It has the layout and the right sort of words, and nothing more. That is what 500 steps is for: proving that every stage runs before you spend 80 minutes.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Train the real model
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;training/train.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;20,000 steps, about 80 minutes. The pick test at the end should come out close to the published model: about 250 of 260 with the right layout and 240 with the main ingredient in the list. Runs differ a little from one to the next.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Put it on the CoreS3
&lt;/h3&gt;

&lt;p&gt;This replaces whatever is on the board, partition table included.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/esp32-recipe-maker/firmware
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pio run -e m5stack-cores3 -t upload_model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pio run -e m5stack-cores3 -t upload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model took 60 seconds to write and the app 53 seconds to build and flash. Pick on the touchscreen, or open the serial monitor (&lt;code&gt;pio device monitor&lt;/code&gt;) and type &lt;code&gt;pumpkin soup&lt;/code&gt;. Mine wrote 130 tokens in 20.1 seconds, 6.5 tokens a second.&lt;/p&gt;

&lt;h2&gt;
  
  
  Train on your own subject
&lt;/h2&gt;

&lt;p&gt;Recipes need a big conversion job. The general route is much shorter, and you can try all of it in a few minutes. This example teaches a model to write made-up weather forecasts for Australian cities.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Write the examples
&lt;/h3&gt;

&lt;p&gt;Each example uses the same fixed layout and ends with &lt;code&gt;&amp;lt;|endoftext|&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;City: Hobart
Season: Winter
Day: Sunday
Forecast: Rain at times. Top 10, low -1. Calm.
&amp;lt;|endoftext|&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here a short program writes them, with simple rules: each city has a usual top for summer and winter, autumn and spring are halfway between, each city has its own chance of rain, the top varies by up to 4 degrees either way, and the low is 8 to 14 degrees under the top.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;

&lt;span class="c1"&gt;# City: (usual top in summer, usual top in winter, chance of rain)&lt;/span&gt;
&lt;span class="no"&gt;CITIES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"Darwin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Cairns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.45&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Brisbane"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Sydney"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Canberra"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Melbourne"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.35&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Hobart"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Adelaide"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Perth"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Alice Springs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;SEASONS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Summer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Autumn"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Winter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Spring"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;DAYS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Monday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Tuesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Wednesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Thursday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Friday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Saturday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Sunday"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;DRY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Sunny."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Mostly sunny."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Partly cloudy."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Clear skies."&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;WET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Showers."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"A shower or two."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Rain at times."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Possible storm."&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;WINDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Light winds."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Winds becoming gusty."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Fresh southerly change late."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Calm."&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;usual_top&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;season&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Summer and winter as listed; autumn and spring halfway between."""&lt;/span&gt;
    &lt;span class="n"&gt;summer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;winter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;CITIES&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"Summer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;summer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Winter"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;winter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;season&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;summer&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;winter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;rng&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"forecasts.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"w"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;CITIES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;season&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SEASONS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;usual_top&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;season&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;sky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;WET&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;CITIES&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="no"&gt;DRY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"City: {city}\nSeason: {season}\nDay: {rng.choice(DAYS)}\n"&lt;/span&gt;
                    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Forecast: {sky} Top {top}, low {low}. {rng.choice(WINDS)}\n&amp;lt;|endoftext|&amp;gt;\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it as &lt;code&gt;make_forecasts.py&lt;/code&gt; in a new &lt;code&gt;~/forecasts&lt;/code&gt; folder and run it there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mkdir -p ~/forecasts &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; ~/forecasts &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; python3 make_forecasts.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For your own subject, replace this with your own data in the same shape: one example per document, a fixed layout, &lt;code&gt;&amp;lt;|endoftext|&amp;gt;&lt;/code&gt; at the end of each.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Tokenise it
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/esp32-ai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;.venv/bin/python ~/esp32-recipe-maker/training/prepare_recipes.py ~/forecasts/forecasts.txt data/forecasts --vocab 1024
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;About a second. It says "recipes" and checks a few recipe words at the end; ignore those. A vocabulary of 1,024 is a ceiling: forecasts use so few words that the tokeniser stopped at 505 tokens, which is fine.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Train for 500 steps
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;ESP32AI_DATA&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;data/forecasts &lt;span class="nv"&gt;PYTHONPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;src .venv/bin/python research/tinystories/train.py --arm ple --vocab 1024 --d-model 96 --n-layers 6 --ple-dim 128 --target-core 560000 --batch-size 16 --seq-len 256 --steps 500 --eval-every 100 --tag forecasts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;About 70 seconds. Validation loss falls from 6.97 to 0.49. A layout this simple is learnt in about 200 steps.&lt;/p&gt;

&lt;p&gt;Don't want to use the GPU? Put ESP32AI_DEVICE=cpu at the start of the command. On the same Mac it took 77 seconds instead of 69, and reached the same validation loss.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Read some samples
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PYTHONPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;src .venv/bin/python -m research.tinystories.sample --run runs/ple-forecasts-s0.pt --tokenizer data/forecasts/vocab-1024/tokenizer.json --prompt &lt;span class="s2"&gt;"$(printf 'City: Darwin\nSeason: Winter\nDay: Monday\nForecast:')"&lt;/span&gt; --samples 3 --tokens 25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it as a module (&lt;code&gt;-m&lt;/code&gt;), as shown; running &lt;code&gt;sample.py&lt;/code&gt; as a script fails with an import error. It doesn't stop at &lt;code&gt;&amp;lt;|endoftext|&amp;gt;&lt;/code&gt;, so each sample runs on into the start of another forecast. Mine gave Darwin winter tops of 29 to 35; the same prompt for Hobart gave 12 to 16.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Export it and run it with the device's arithmetic
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PYTHONPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;src .venv/bin/python -m research.tinystories.export ple-forecasts-s0 --tokenizer data/forecasts/vocab-1024/tokenizer.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cp artifacts/tinystories/model.bin artifacts/tinystories/tokenizer.json ~/forecasts/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That gives a 0.75 MB &lt;code&gt;model.bin&lt;/code&gt;, which the &lt;code&gt;cp&lt;/code&gt; above copies to &lt;code&gt;~/forecasts&lt;/code&gt;. The exporter always writes to &lt;code&gt;artifacts/tinystories/&lt;/code&gt;, whatever model you export, so the next export (for example &lt;code&gt;training/train.sh&lt;/code&gt; for Recipe Maker) replaces these files. Keep your own copy somewhere else.&lt;/p&gt;

&lt;p&gt;To run it the way the device would, use &lt;code&gt;host/run.c&lt;/code&gt; from the Recipe Maker repo. It is &lt;code&gt;host/recipe.c&lt;/code&gt; cut down to take the prompt as token ids, so it works for any subject. It needs &lt;code&gt;vocab.h&lt;/code&gt; regenerated from your tokeniser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/esp32-recipe-maker &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; ~/esp32-ai/.venv/bin/python tools/make_vocab_h.py ~/forecasts/tokenizer.json &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; cc -O2 -o run host/run.c -lm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./run -m ~/forecasts/model.bin -n 3 &lt;span class="nv"&gt;$(&lt;/span&gt;~/esp32-ai/.venv/bin/python -c &lt;span class="s1"&gt;'import sys; from tokenizers import Tokenizer; print(*Tokenizer.from_file(sys.argv[1]).encode(sys.argv[2]).ids)'&lt;/span&gt; ~/forecasts/tokenizer.json &lt;span class="s2"&gt;"$(printf 'City: Hobart\nSeason: Winter\nDay: Sunday\nForecast:')"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This stops at &lt;code&gt;&amp;lt;|endoftext|&amp;gt;&lt;/code&gt;, as the device does. Run &lt;code&gt;git checkout firmware/src/vocab.h&lt;/code&gt; afterwards to put Recipe Maker's back.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Score it against the rules
&lt;/h3&gt;

&lt;p&gt;Reading samples only tells you so much. Because a program made this data, every forecast can be checked against the rules that made it. I generated 1,000 forecasts, 25 for each city and season, at the device's temperature of 0.7. A short script read each one and checked it:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Rule&lt;/th&gt;
&lt;th&gt;Forecasts that follow it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Layout right&lt;/td&gt;
&lt;td&gt;1,000 of 1,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Top within 4 degrees of that city's usual for the season&lt;/td&gt;
&lt;td&gt;908&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Low 8 to 14 degrees under the top&lt;/td&gt;
&lt;td&gt;884&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sky and wind from the generator's lists&lt;/td&gt;
&lt;td&gt;1,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Not bad for about a minute of training. Then I counted how often each city got rain. Darwin and Hobart came out right, but Adelaide, Perth and Alice Springs got rain about half as often as they should.&lt;/p&gt;

&lt;h3&gt;
  
  
  What temperature does
&lt;/h3&gt;

&lt;p&gt;That isn't the model getting rain wrong. It is the sampling temperature. The model gives a probability for every possible next token, and the temperature reshapes those probabilities before one is picked. At 1.0 they are used as the model learnt them. Below 1.0 the likelier choices get likelier still, and above 1.0 the unlikely ones get more of a chance.&lt;/p&gt;

&lt;p&gt;So when the model has learnt that Adelaide is dry 75% of the time, sampling at 0.7 pushes that higher still and rain mostly disappears. At 1.0 most cities came within a few points of their real rate. Perth overshot to 39%; with 100 forecasts a city, some of that is just chance:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;City&lt;/th&gt;
&lt;th&gt;Chance of rain in the data&lt;/th&gt;
&lt;th&gt;Model at 0.7&lt;/th&gt;
&lt;th&gt;Model at 1.0&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Darwin&lt;/td&gt;
&lt;td&gt;50%&lt;/td&gt;
&lt;td&gt;51%&lt;/td&gt;
&lt;td&gt;51%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sydney&lt;/td&gt;
&lt;td&gt;30%&lt;/td&gt;
&lt;td&gt;22%&lt;/td&gt;
&lt;td&gt;38%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Adelaide&lt;/td&gt;
&lt;td&gt;25%&lt;/td&gt;
&lt;td&gt;11%&lt;/td&gt;
&lt;td&gt;29%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Perth&lt;/td&gt;
&lt;td&gt;25%&lt;/td&gt;
&lt;td&gt;12%&lt;/td&gt;
&lt;td&gt;39%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alice Springs&lt;/td&gt;
&lt;td&gt;10%&lt;/td&gt;
&lt;td&gt;5%&lt;/td&gt;
&lt;td&gt;12%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The cost of 1.0 is more mistakes: tops within range fell from 908 to 791, and lows from 884 to 812.&lt;/p&gt;

&lt;h3&gt;
  
  
  Would more training fix it?
&lt;/h3&gt;

&lt;p&gt;Partly. I trained the same model for 2,000 steps instead of 500 (3 minutes 47 seconds) and scored it the same way:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;500 steps&lt;/th&gt;
&lt;th&gt;2,000 steps&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Top within 4 degrees, at 0.7&lt;/td&gt;
&lt;td&gt;908&lt;/td&gt;
&lt;td&gt;998&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Low 8 to 14 under the top, at 0.7&lt;/td&gt;
&lt;td&gt;884&lt;/td&gt;
&lt;td&gt;996&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Top within 4 degrees, at 1.0&lt;/td&gt;
&lt;td&gt;791&lt;/td&gt;
&lt;td&gt;981&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Adelaide rain at 0.7 (should be 25%)&lt;/td&gt;
&lt;td&gt;11%&lt;/td&gt;
&lt;td&gt;12%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Adelaide rain at 1.0&lt;/td&gt;
&lt;td&gt;29%&lt;/td&gt;
&lt;td&gt;24%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Perth rain at 1.0 (should be 25%)&lt;/td&gt;
&lt;td&gt;39%&lt;/td&gt;
&lt;td&gt;23%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The temperatures are now nearly perfect, and at 1.0 the rain rates are close to the data, with Perth's overshoot gone. But at 0.7 the dry cities are still too dry: Adelaide gets rain 12% of the time, Sydney 17% instead of 30%, Alice Springs 4% instead of 10%.&lt;/p&gt;

&lt;p&gt;That is the useful lesson. More training makes the model's probabilities more accurate. It can't undo what the sampling temperature does to them afterwards, because that happens in the app, not in the model. If the app needs the real odds, sample at 1.0 and train long enough that 1.0 doesn't bring mistakes with it.&lt;/p&gt;

&lt;p&gt;It is the same trade-off in every app. Recipe Maker samples at 0.7 because a recipe with an odd ingredient is worse than a predictable one, and that is also why recipes drift towards the commonest ingredients. Bard Lite samples at 0.8, where a bit of variety matters more. Whichever you choose, run your tests at the temperature the device uses, or the numbers won't match what people see.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. On the device
&lt;/h3&gt;

&lt;p&gt;Start from Recipe Maker's firmware. Copy in your &lt;code&gt;model.bin&lt;/code&gt; and &lt;code&gt;vocab.h&lt;/code&gt;, change the prompt the app builds (Recipe Maker's prompts are token ids in &lt;code&gt;firmware/src/picks.h&lt;/code&gt;, made by &lt;code&gt;tools/make_picks_h.py&lt;/code&gt;) and change the screens to suit. A small model like this one can sit at its own offset in the &lt;code&gt;model&lt;/code&gt; partition next to the others.&lt;/p&gt;

&lt;h2&gt;
  
  
  Same steps, different data
&lt;/h2&gt;

&lt;p&gt;The other apps follow the same ten steps. What changes is what a token is and where the data comes from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sprite Maker:&lt;/strong&gt; a sprite is 256 pixel tokens from a 32-colour palette, written a pixel at a time. It is trained in two stages on 1.4 million CC0 sprites: all of them so it learns outlines and shading, then a short fine-tune on character sprites so "skeleton" means skeleton. A small classifier filtered out sprites with wrong labels, which lifted the average "looks like its character" score from 54% to 67%.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Card Maker:&lt;/strong&gt; the model writes a rule set of up to 28 tokens. Decoding is constrained: at each step the app only lets the model pick from tokens that keep the rule set valid, so it can't write a broken one. The engine tests it by playing 100 games against itself before you see it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cocktail Maker:&lt;/strong&gt; RecipeNLG's drinks, converted to Australian bar measures, with its own 2,048-token tokeniser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bard Lite:&lt;/strong&gt; the speeches from Project Gutenberg's Complete Works of Shakespeare, only 1.5 million tokens. With that little data the problem flips: train for long and the model starts memorising lines instead of learning how they go. You can see it in the validation loss, here measured on the last 5% of the plays (The Winter's Tale and part of The Two Noble Kinsmen), held out from training. It was lowest at about 1,500 steps and then rose, because the model was getting better at the plays it had seen and worse at the ones it hadn't. So training stops at 1,600. The last 300 of those steps use the weights rounded to 4 bits, as the device has them, so the model learns to cope with the rounding (quantisation-aware training). Not one run of 8 words in 1,516 is copied from the plays.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd tell someone starting out
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Spend your time on the data. The model code didn't change; the data changed every round.&lt;/li&gt;
&lt;li&gt;Fix the layout first, around the screen and the picks.&lt;/li&gt;
&lt;li&gt;Write a test for the mistakes you actually see, and judge every model by it, not by the loss.&lt;/li&gt;
&lt;li&gt;Add a test case for every data fix, so fixes stay fixed.&lt;/li&gt;
&lt;li&gt;Keep a version that runs on a computer with the device's arithmetic. It saves a lot of flashing.&lt;/li&gt;
&lt;li&gt;Match the model's licence to the data's.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All five projects are open source, with versions that run on a computer and builds for other ESP32-S3 boards with PSRAM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-recipe-maker" rel="noopener noreferrer"&gt;esp32-recipe-maker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-sprite-maker" rel="noopener noreferrer"&gt;esp32-sprite-maker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-card-maker" rel="noopener noreferrer"&gt;esp32-card-maker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-cocktail-maker" rel="noopener noreferrer"&gt;esp32-cocktail-maker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/justinmclean/esp32-bard-lite" rel="noopener noreferrer"&gt;esp32-bard-lite&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>esp32</category>
      <category>ai</category>
      <category>machinelearning</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
