Announce a Wi-Fi device
A device on the local network, such as an ESP32 stroker that takes TCode over UDP, announces itself without a library: it sends the beacon and registers a DNS-SD service. A hub with local network discovery turned on, such as RustyFunPlayer, then offers to play on it.
Hubs only listen on the local network when their user turned that on, and they always ask before they connect to a device there.
The C library does all of this for Arduino boards (ESP32, ESP8266, Pico W) and other C firmware, with fixed buffers and fuzzed parsers. The rest of this page shows what it sends, for firmware that cannot use it.
With the C library
Install Funlink-arduino-0.1.0.zip from the release (how), then:
#include <Funlink.h>
const uint16_t TCODE_PORT = 8000; // where the device takes TCode over UDP
Funlink funlink;
WiFiUDP tcode;
char id[32];
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin("your-network", "your-password"); // gitleaks:allow (placeholder)
while (WiFi.status() != WL_CONNECTED) delay(100);
WiFi.setSleep(false); // ESP32: modem sleep misses multicast queries
// A stable id from the MAC address; the session changes at every boot by itself.
uint8_t mac[6];
WiFi.macAddress(mac);
snprintf(id, sizeof id, "sr6-%02x%02x%02x", mac[3], mac[4], mac[5]);
tcode.begin(TCODE_PORT);
funlink.addService("tcode-udp", TCODE_PORT);
if (!funlink.begin(id, "SR6", "1.0.0")) {
Serial.println(funlink_strerror(funlink.lastError()));
}
}
void loop() {
funlink.loop();
int size = tcode.parsePacket();
if (size > 0) {
// ... read the TCode and drive the motors ...
}
}
The zip’s examples/TCodeUdpDevice sketch also works on ESP8266 and reports the connected hub.
#include "funlink_core.h"
static funlink_peer peer; /* about 3.3 KB: keep it static */
void funlink_setup(uint32_t now_ms) {
funlink_platform platform = {0};
platform.send = my_send; /* sends a datagram from the given socket */
platform.random = my_random32;
platform.local_address = my_ipv4; /* for the mDNS A record */
funlink_service service = {"tcode-udp", NULL, 8000, NULL, NULL};
funlink_record record = {0};
record.id = "sr6-a41c";
record.name = "SR6";
record.services = &service;
record.service_count = 1;
funlink_peer_init(&peer, &platform);
if (funlink_peer_configure(&peer, &record, now_ms) == FUNLINK_OK) funlink_peer_start(&peer, now_ms);
}
/* Hand every received datagram to funlink_peer_on_datagram(), call
funlink_peer_poll() every few milliseconds and funlink_peer_stop() on exit. */
Three sockets: port 37420 joined to 239.255.37.20, port 5353 joined to 224.0.0.251, and one on a random port that sends and receives hub answers. The repository’s c/test/posix_peer.c is a complete Linux host.
Without the library
What the device sends
- Its record, with
0.0.0.0as the host of every endpoint. The hub uses the address the packet came from instead, so the record does not need to know the device’s IP address. - No
icon; keep the record under 1200 bytes. - A stable
id, for example derived from the chip’s MAC address, and a new randomsessionat every boot.
ESP32 (Arduino)
The sketch below does both transports. Replace SR6, the port and the TCode handling with your device’s.
#include <WiFi.h>
#include <WiFiUdp.h>
#include <ESPmDNS.h>
const uint16_t TCODE_PORT = 8000; // where the device takes TCode over UDP
const uint16_t FUNLINK_PORT = 37420;
const IPAddress FUNLINK_GROUP(239, 255, 37, 20);
const IPAddress BROADCAST(255, 255, 255, 255);
WiFiUDP sender; // sends announces from a random port; hub status replies arrive here
WiFiUDP listener; // port 37420: hub queries
String id;
char session[17];
String announce;
uint32_t lastAnnounce = 0;
IPAddress lastAsker;
uint32_t lastAnswer = 0;
void sendAnnounce(IPAddress to, uint16_t port) {
sender.beginPacket(to, port);
sender.print(announce);
sender.endPacket();
}
void setup() {
WiFi.begin("ssid", "password");
while (WiFi.status() != WL_CONNECTED) delay(100);
WiFi.setSleep(false); // modem sleep misses multicast queries
id = "sr6-" + String((uint32_t)ESP.getEfuseMac(), HEX);
snprintf(session, sizeof session, "%08lx%08lx",
(unsigned long)esp_random(), (unsigned long)esp_random());
announce = String("{\"funlink\":1,\"type\":\"announce\",\"id\":\"") + id +
"\",\"session\":\"" + session +
"\",\"name\":\"SR6\",\"version\":\"1.0.0\",\"services\":[{\"protocol\":\"tcode-udp\","
"\"endpoint\":\"udp://0.0.0.0:" + TCODE_PORT + "\"}]}";
sender.begin(0);
listener.beginMulticast(FUNLINK_GROUP, FUNLINK_PORT);
// DNS-SD: _funlink._udp.local with the record in TXT.
MDNS.begin((String("funlink-") + session).c_str());
MDNS.setInstanceName(String("SR6 ") + String(session).substring(0, 8));
MDNS.addService("funlink", "udp", TCODE_PORT);
MDNS.addServiceTxt("funlink", "udp", "v", "1");
MDNS.addServiceTxt("funlink", "udp", "id", id);
MDNS.addServiceTxt("funlink", "udp", "s", session);
MDNS.addServiceTxt("funlink", "udp", "n", "SR6");
MDNS.addServiceTxt("funlink", "udp", "ver", "1.0.0");
MDNS.addServiceTxt("funlink", "udp", "p0",
String("tcode-udp udp://0.0.0.0:") + TCODE_PORT);
}
void loop() {
// Announce every 5 seconds to the group and as a broadcast.
if (millis() - lastAnnounce >= 5000 || lastAnnounce == 0) {
lastAnnounce = millis();
sendAnnounce(FUNLINK_GROUP, FUNLINK_PORT);
sendAnnounce(BROADCAST, FUNLINK_PORT);
}
// Answer a hub's query, only to the hub, at most once per second per hub.
if (listener.parsePacket() > 0) {
char buffer[256];
int length = listener.read(buffer, sizeof buffer - 1);
buffer[length > 0 ? length : 0] = 0;
IPAddress hub = listener.remoteIP();
uint16_t port = listener.remotePort();
bool query = strstr(buffer, "\"funlink\":1") && strstr(buffer, "\"type\":\"query\"");
if (query && (hub != lastAsker || millis() - lastAnswer >= 1000)) {
delay(random(20, 121));
sendAnnounce(hub, port);
lastAsker = hub;
lastAnswer = millis();
}
}
// Hubs answer each announce with a status; read it to show "connected".
if (sender.parsePacket() > 0) {
sender.flush();
}
// ... handle TCode on TCODE_PORT ...
}
ESPmDNS answers queries with its own TTLs, which is fine: hubs only treat a TTL of 0 as a goodbye and otherwise forget a device 15 seconds after they last heard of it.
Check it
On Linux, avahi-browse -rt _funlink._udp lists the device with its TXT record. To see the beacon, listen on the port:
socat -u UDP4-RECVFROM:37420,reuseaddr,ip-add-membership=239.255.37.20:0.0.0.0,fork -
In RustyFunPlayer, turn on Settings > Funlink > Find apps and devices on the local network. On NixOS, also set programs.rustyfunplayer.funlinkLan.openFirewall = true; so the firewall lets the packets in.
Security
Anyone on the network can send these packets, and anyone can connect to the device’s TCode port. Hubs therefore ask before every new device and remember answers per address. Keep such devices on a home network you trust, and do not forward their ports to the internet.