Skip to content

udp.tc

UDP multicast — share float variables between devices

Source on GitHub

// UDP multicast — share float variables between devices
// Compatible with Tasmota Scripter protocol (239.255.255.250:1999)
//
// Scalar global floats auto-send via UDP on every assignment (STORE_GLOBAL_UDP).
// Incoming UDP packets auto-update global float variables.
// UdpCall() fires when any UDP variable is received.

global float temperature;
int counter = 0;

void EverySecond() {
    counter++;
    // Assignment to global float auto-sends via UDP multicast
    temperature = 20.0 + sin(counter) * 5.0;
}

void UdpCall() {
    // This callback fires when any UDP global variable is updated.
    // The variable value is already updated — just read it directly.
    char buf[64];
    sprintf(buf, "UDP rx: temperature = %.1f\n", temperature);
    printString(buf);
}

void WebCall() {
    char buf[64];
    sprintf(buf, "{s}Temperature{m}%.1f °C{e}", temperature);
    webSend(buf);
}

void JsonCall() {
    char buf[64];
    sprintf(buf, ",\"TinyC\":{\"Temp\":%.1f}", temperature);
    responseAppend(buf);
}

int main() {
    printStr("UDP multicast demo active\n");
    printStr("Sending temperature every second\n");
    return 0;
}