ESP32 UDP broadcaster to send information to other devices
As I want to have the basic data from my weather station available on other devices as well, the data is sent periodically as UDP broadcasts on the local WiFi network.
As the status data of all my devices are “standardized” the broadcast message has to be in JSON object format like
1 |
{"de":"wei","te":26,"hu":75,"hi":27.47927,"dp":21.24109,"cr":70.94499,"pe":5} |
where
- “de” is the device name, “wei” stands for weather internal
- “te” is the temperature
- “hu” is the humidity
- “hi” is the heat index
- “dp” is the dew point
- “cr” is the comfort ratio of temperature and humidity
- “pe” is the human perception of temperature and humidity
The status data is prepared inside the routine that reads the measurements from the DHT sensor and stored in the global string tempMsg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** Buffer for outgoing JSON string */ DynamicJsonBuffer jsonOutBuffer; /** Json object for outgoing data */ JsonObject& jsonOut = jsonOutBuffer.createObject(); // add device ID jsonOut["de"] = "wei"; // add temperature value from sensor jsonOut["te"] = lastValues.temperature; // add humidity value from sensor jsonOut["hu"] = lastValues.humidity; // add calculated heat index jsonOut["hi"] = heatIndex; // add calculated dew point jsonOut["dp"] = dewPoint; // calculate and add comfort ration jsonOut["cr"] = dht.getComfortRatio(cf, lastValues.temperature, lastValues.humidity); // calculate and add human perception jsonOut["pe"] = dht.computePerception(lastValues.temperature, lastValues.humidity); tempMsg = ""; // Message will be broadcasted every 60 seconds by triggerSendTemp jsonOut.printTo(tempMsg); |
The string is then broadcasted by a timer every 1 minute from another task
1 2 3 |
// Broadcast weather status over UDP IPAddress multiIP (192, 168, 0, 255); udpSendMessage(multiIP, tempMsg, 9997); |
The function udpSendMessage starts an udpServer on the given port and sends a message to the given IP address. If the IP address is a broadcast address (indicated by the 255 of 192.168.0.255) the package is send as a broadcast.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/** * udpSendMessage * Send a message over UDP * * @param ipAddr * receiver IP address * @param udpMsg * payload to be send * @param udpPort * port to be used to send * @return <code>bool</bool> * true if payload could be sent * false if error occurs */ bool udpSendMessage(IPAddress ipAddr, String udpMsg, int udpPort) { /** WiFiUDP class for creating UDP communication */ WiFiUDP udpServer; // Start UDP client for sending packets int connOK = udpServer.begin(udpPort); if (connOK == 0) { Serial.println("UDP could not get socket"); return false; } int beginOK = udpServer.beginPacket(ipAddr, udpPort); if (beginOK == 0) { // Problem occured! udpServer.stop(); Serial.println("UDP connection failed"); return false; } int bytesSent = udpServer.print(udpMsg); if (bytesSent == udpMsg.length()) { udpServer.endPacket(); udpServer.stop(); return true; } else { Serial.println("Failed to send " + udpMsg + ", sent " + String(bytesSent) + " of " + String(udpMsg.length()) + " bytes"); udpServer.endPacket(); udpServer.stop(); return false; } } |






